import numpy as np
from sklearn.linear_model import LinearRegression
from scipy.io.wavfile import read

# Load training and test data
X_train = read('../X_train.wav')[1].reshape(-1, 784)
y_train = (read('../y_train.wav')[1] * 9).astype(int)
X_test = read('../X_test.wav')[1].reshape(-1, 784)
y_test = (read('../y_test.wav')[1] * 9).astype(int)
X20 = X_test[:1000]
yt20 = y_test[:1000]

f = LinearRegression(fit_intercept=False)
g = LinearRegression(fit_intercept=False)
h = LinearRegression(fit_intercept=False)

# non linear KMeans via index search


def index(x,min_,max_):
    range_ = np.linspace(min_,max_,100)
    return np.stack([np.isclose(x.flatten(),range_[i],atol=0.1) for i in range(100)]).sum(0)
        
def transform(x,min_,max_):
    return range_[index(x, min_, max_)]

def predict(x,c):
    f.coef_ = c
    return f.predict(x)

def score(x,yt,c):
    f.coef_ = c
    return np.mean(f.predict(x).argmax(1)==yt)

# err
C = []
i = 0
while True:
    idx = np.random.randint(0,60000,1000)
    X = X_train[idx]
    yt = y_train[idx]
    f.fit(X,np.eye(10)[yt])
    C.append(f.coef_)
    if i==100:break    
    i+=1

c = np.stack(C)
print(score(X,yt,c.mean(0)))
# bug fix linear algebra

"""while True:
    idx = np.random.randint(0,60000,100)
    X = X_train[idx]
    yt = y_train[idx]
    f.fit(X,np.eye(10)[yt])
    id0 = index(f.coef_,f.coef_.min(),f.coef_.max()).reshape(f.coef_.shape)
    idx = np.random.randint(0,60000,100)
    X = X_train[idx]
    yt = y_train[idx]
    f.fit(X,np.eye(10)[yt])
    id1 = index(f.coef_,f.coef_.min(),f.coef_.max()).reshape(f.coef_.shape)
    break
"""

"""
Learn theory in (.txt)

Ex. 

f = LinearRegression()

C = []
i = 0
while True:
    idx = np.random.randint(0,60000,100)
    X = X_train[idx]
    yt = y_train[idx]
    f.fit(X,np.eye(10)[yt])
    C.append(f.coef_)
    if i==1000:break    
    i+=1

c = np.stack(C)
print(score(X,yt,c.mean(0)))

This mimics the first approach of generalization where the bugs generated between 
the batches in f.coef_ domain is overwritten by the mean at some below 80% accuracy

My idea is that there exist special bug-fixing-linear-algebra that is better than mean()


"""
