<jupyter_start><jupyter_text>ICA on sound tracks<NAME> (<EMAIL>) 26th April 2018Source: CS229: PS4Q4Starting code: http://cs229.stanford.edu/ps/ps4/q4/<jupyter_code>import sounddevice as sd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
Fs = 11025
def normalize(dat):
    return 0.99 * dat / np.max(np.abs(dat))

def load_data():
    mix = np.loadtxt('cs229_Data/mix.dat')
    return mix

def play(vec):
    sd.play(vec, Fs, blocking=True)
def unmixer(X):
    M, N = X.shape
#     W = np.random.rand(N,N)
    W = np.eye(N)

    anneal = [0.1, 0.1, 0.1, 0.05, 0.05, 0.05, 0.02, 0.02, 0.01, 0.01,
              0.005, 0.005, 0.002, 0.002, 0.001, 0.001]
    print('Separating tracks ...')
    sigmoid = lambda x: 1 / (1 + np.exp(-x))
    tol = []
    ######## Your code here ##########
    for alpha in anneal:
        print('working on alpha = {0}'.format(alpha))
        for xi in X:
            p1 = np.outer(1 - 2 * sigmoid(np.dot(W, xi.T)), xi)
            p2 = np.linalg.inv(W.T)
            W += alpha * (p1 + p2)
        # Vectorisation not working :(
#         gradient = (1 - 2*sigmoid(np.dot(W,X.T))).dot(X)/M + np.linalg.inv(W.T)
#         W = W + alpha*gradient
#         tol.append(np.linalg.norm(W_old - W))
#     plt.plot(tol)
    ###################################
    return W
def unmix(X, W):
    S = np.zeros(X.shape)

    ######### Your code here ##########
    S = np.dot(W, X.T)
    ##################################
    return S.T
X = normalize(load_data())

for i in range(X.shape[1]):
    print('Playing mixed track %d' % i)
    play(X[:, i])
W = unmixer(X)
W
S = normalize(unmix(X, W))

for i in range(S.shape[1]):
    print('Playing separated track %d' % i)
    play(S[:, i])
# p1 = np.outer(1 - 2 * sigmoid(np.dot(W, xi.T)), xi)
sigmoid = lambda x: 1 / (1 + np.exp(-x))
m = 1-2*sigmoid(np.dot(W, xi.T))
m2 = np.dot(m.reshape([-1,1]), xi.reshape([1,-1]))
np.shape(xi)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2280431432973806, 'fraction_numerical': 0.05290190035952748, 'mean_word_length': 2.6685499058380415, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '135522', 'license': 'MIT', 'n_tokens_mistral': 883, 'n_tokens_neox': 802, 'n_words': 183, 'path': 'CS229_PS/PS4_Q4_ICA.ipynb', 'repo_name': 'meichen91/MachineLearning-Snippets', 'size': '1965'}