#读取数据
data = pd.read_csv("C:/Users/lumou/Desktop/传感器/0二稿/data/KNN分类预测疲劳.csv",header=0)
data["pilao"] = data ["pilao"].map({"pre":0,"dimian":1})
class KNN:
def __init__(self, k):
self.k = k
def fit(self, X, y):
self.X = np.asarray(X)
self.y = np.asarray(y)
def predict(self, X):
X = np.asarray(X)
result = []
for x in X:
dis = np.sqrt(np.sum((x - self.X) ** 2, axis=1))
index = dis.argsort()
index = index[:self.k]
count = np.bincount(self.y[index],weight=1/dis[index])
result.append(count.argmax())
return np.asarray(result)
t0 = data[data["pilao"] == 0]
t1 = data[data["pilao"] == 1]
t0 = t0.sample(len(t0), random_state=0)
t1 = t1.sample(len(t1))
train_X = pd.concat([t0.iloc[:18,:-1], t1.iloc[:18,:-1]],axis=0)
train_y = pd.concat([t0.iloc[:18,-1], t1.iloc[:18,-1]],axis=0)
test_X = pd.concat([t0.iloc[18:,:-1], t1.iloc[18:,:-1]],axis=0)
test_y = pd.concat([t0.iloc[18:,-1], t1.iloc[18:,-1]],axis=0)
knn = KNN (k=4)
knn.fit(train_X, train_y)
result = knn.predict(test_X)
print (np.sum(result == test_y))
print (np.sum(result == test_y)/ len(result))