Commit 49eafd76 by Paktalin

Initial commit

parents
import numpy as np
import matplotlib.pyplot as plt
from knn_7 import KNN
def get_data():
width = 8
height = 8
N = width * height
X = np.zeros((N, 2))
Y = np.zeros(N)
n = 0
start_t = 0
for i in range(width):
t = start_t
for j in range(height):
X[n] = [i, j]
Y[n] = t
n += 1
t = (t + 1) % 2
start_t = (start_t + 1) % 2
return X, Y
if __name__ == "__main__":
X, Y = get_data()
plt.scatter(X[:,0], X[:,1], s=100, c=Y, alpha=0.5)
plt.show()
model = KNN(3)
model.fit(X, Y)
print("Train accuracy:", model.score(X, Y))
\ No newline at end of file
import numpy as np
from sortedcontainers import SortedList
from util import get_data
from datetime import datetime
class KNN(object):
def __init__(self, k):
self.k = k
def fit(self, x, y):
self.x = x
self.y = y
def predict(self, X):
y = np.zeros(len(X))
for i, x in enumerate(X):
sl = SortedList()
for j, xt in enumerate(self.x):
diff = x - xt
d = diff.dot(diff)
if len(sl) < self.k:
sl.add((d, self.y[j]))
else:
if d < sl[-1][0]:
del sl[-1]
sl.add((d, self.y[j]))
votes = {}
for _, v in sl:
votes[v] = votes.get(v, 0) + 1
max_votes = 0
max_votes_class = -1
for v, count in votes.items():
if count > max_votes:
max_votes = count
max_votes_class = v
y[i] = max_votes_class
return y
def score(self, X, Y):
P = self.predict(X)
return np.mean(P == Y)
if __name__ == "__main__":
X, Y = get_data(2000)
Ntrain = 1000
Xtrain, Ytrain = X[:Ntrain], Y[:Ntrain]
Xtest, Ytest = X[Ntrain:], Y[Ntrain:]
for k in (1,2,3,4,5):
knn = KNN(k)
print("k =", k)
t0 = datetime.now()
knn.fit(Xtrain, Ytrain)
print("Training time:", (datetime.now() - t0))
t0 = datetime.now()
print("Train accuracy:", (knn.score(Xtrain, Ytrain)))
print("Time to compute train accuracy:", (datetime.now() - t0))
t0 = datetime.now()
print("Test accuracy:", (knn.score(Xtest, Ytest)))
print("Time to compute test accuracy:", (datetime.now() - t0))
\ No newline at end of file
from util import get_donut
from knn_7 import KNN
import matplotlib.pyplot as plt
if __name__ == "__main__":
X, Y = get_donut()
plt.scatter(X[:,0], X[:,1], c=Y)
plt.show()
model = KNN(3)
model.fit(X, Y)
print("Accuracy:", model.score(X, Y))
from knn_7 import KNN
from util import get_xor
import matplotlib.pyplot as plt
if __name__ == "__main__":
X, Y = get_xor()
plt.scatter(X[:,0], X[:,1], c=Y)
plt.show()
model = KNN(3)
model.fit(X, Y)
print("Accuracy:", model.score(X, Y))
\ No newline at end of file
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("../files/mnist_train.csv", header=0, nrows=2000)
Y = df["label"].values
X = df.loc[:, df.columns != 'label'].values
Xmeans = X.mean(axis=0)
print(np.arange(Xmeans))
# plt.scatter(np.linspace(Xmeans), c=Y)
# plt.show()
\ No newline at end of file
import pandas as pd
import numpy as np
def get_data(limit=None):
print("Reading in and transforming data...")
df = pd.read_csv('../files/mnist_train.csv')
data = df.values
np.random.shuffle(data)
X = data[:, 1:] / 255.0 # data is from 0..255
Y = data[:, 0]
if limit is not None:
X, Y = X[:limit], Y[:limit]
return X, Y
def get_xor():
X = np.zeros((200, 2))
X[:50] = np.random.random((50, 2)) / 2 + 0.5
X[50:100] = np.random.random((50, 2)) / 2
X[100:150] = np.random.random((50, 2)) / 2 + np.array([[0, 0.5]])
X[150:] = np.random.random((50, 2)) / 2 + np.array([[0.5, 0]])
Y = np.array([0]*100 + [1]*100)
return X, Y
def get_donut():
N = 200
R_inner = 5
R_outer = 10
R1 = np.random.randn(N//2) + R_inner
theta = 2*np.pi*np.random.random(N//2)
X_inner = np.concatenate([[R1 * np.cos(theta)], [R1 * np.sin(theta)]]).T
R2 = np.random.randn(N//2) + R_outer
theta = 2*np.pi*np.random.random(N//2)
X_outer = np.concatenate([[R2 * np.cos(theta)], [R2 * np.sin(theta)]]).T
X = np.concatenate([X_inner, X_outer])
Y = np.array([0]*(N//2) + [1]*(N//2))
return X, Y
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment