我试图将kNN算法测试结果的准确性与使用Matlab绘图的chi2传递的特征数量进行对比,但我不确定如何提取分数以使其在y轴上。事实上,我知道传递的特征数量应该在x轴上。
它还会打印出具有相应功能的最高分。
我的代码在
下面import numpy as np
import pandas as pd
from sklearn.datasets import make_classification
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
X, y = make_classification(n_samples=50,
n_features=6,
n_informative=3,
n_classes=2,
random_state=10,
shuffle=True)
# Creating a dataFrame
df = pd.DataFrame({'Feature 1':X[:,0],
'Feature 2':X[:,1],
'Feature 3':X[:,2],
'Feature 4':X[:,3],
'Feature 5':X[:,4],
'Feature 6':X[:,5],
'Class':y})
df = df.abs()
y = df['Class']
X = df.drop('Class', axis=1)
i = 0
for i in range(1,6):
i = i +1
X_new = SelectKBest(chi2, k=i).fit_transform(X, y)
X_train, X_test, y_train, y_test = train_test_split(X_new ,y,
stratify=y,
random_state=66,
test_size=0.3,
shuffle = True)
clf = KNeighborsClassifier()
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
#plt.xlabel("Number of features selected")
#plt.ylabel("Test Accuracy Score")
答案 0 :(得分:0)
您可以使用函数sklearn.metrics.accuracy_score
计算测试集的准确度。然后将此精度存储在每个循环迭代的列表中。
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, y_pred)