加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
knnClassifier.py 3.65 KB
一键复制 编辑 原始数据 按行查看 历史
Piyush Bhardwaj 提交于 2019-01-10 20:52 . Add files via upload
import numpy
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
def knn_classifier():
file_x = 'data/features_sampled.dat'
file_y = 'data/label_class_0.dat'
X = numpy.genfromtxt(file_x, delimiter=' ')
y = numpy.genfromtxt(file_y, delimiter=' ')
# Split the data into training/testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# KNN
clf = KNeighborsClassifier(n_neighbors=1)
clf.fit(X_train, y_train)
y_predict = clf.predict(X_test)
cm = confusion_matrix(y_test, y_predict)
print(cm)
print("Accuracy score of Valence ")
print(accuracy_score(y_test, y_predict)*100)
###############################################################################
file_x = 'data/features_sampled.dat'
file_y = 'data/label_class_1.dat'
X = numpy.genfromtxt(file_x, delimiter=' ')
y = numpy.genfromtxt(file_y, delimiter=' ')
# Split the data into training/testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# KNN
clf = KNeighborsClassifier(n_neighbors=1)
clf.fit(X_train, y_train)
y_predict = clf.predict(X_test)
cm = confusion_matrix(y_test, y_predict)
print(cm)
print("Accuracy score of Arousal ")
print(accuracy_score(y_test, y_predict)*100)
###############################################################################
file_x = 'data/features_sampled.dat'
file_y = 'data/label_class_2.dat'
X = numpy.genfromtxt(file_x, delimiter=' ')
y = numpy.genfromtxt(file_y, delimiter=' ')
# Split the data into training/testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# KNN
clf = KNeighborsClassifier(n_neighbors=1)
clf.fit(X_train, y_train)
y_predict = clf.predict(X_test)
cm = confusion_matrix(y_test, y_predict)
print(cm)
print("Accuracy score of Dominance ")
print(accuracy_score(y_test, y_predict)*100)
###############################################################################
file_x = 'data/features_sampled.dat'
file_y = 'data/label_class_3.dat'
X = numpy.genfromtxt(file_x, delimiter=' ')
y = numpy.genfromtxt(file_y, delimiter=' ')
# Split the data into training/testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# KNN
clf = KNeighborsClassifier(n_neighbors=1)
clf.fit(X_train, y_train)
y_predict = clf.predict(X_test)
cm = confusion_matrix(y_test, y_predict)
print(cm)
print("Accuracy score of Liking ")
print(accuracy_score(y_test, y_predict)*100)
if __name__ == '__main__':
knn_classifier()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化