gpt4 book ai didi

python - 虹膜模型不同K值的KNN模型的统计指标?

转载 作者:行者123 更新时间:2023-11-30 09:30:25 25 4
gpt4 key购买 nike

我编写了一些Python代码来将著名的鸢尾花数据集与KNN模型相匹配,我尝试使用不同的k值,如k=2、k=3、k=5,根据我对这些不同k值的理解,混淆矩阵,准确率得分和分类报告值应该不同,但是,无论我给出什么 k 值,输出的统计指标都是相同的,而且“精度”、“召回率”和“f1-score”都是 1.00,如下所示在快照中codes and output 。我在这里错过了什么吗?谢谢!

from sklearn.model_selection import train_test_split

# first split the dataset into its attributes and labels
X = data.iloc[:, :-1].values
y = data.iloc[:, 4].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30,
random_state=42)

from sklearn.neighbors import KNeighborsClassifier

# Instantiate learning model (k = 5)
clf = KNeighborsClassifier(n_neighbors=5)
# Fitting the model
clf.fit(X_train, y_train)
# Predicting the Test set results
y_pred = clf.predict(X_test)
print(y_pred)

from sklearn.metrics import classification_report, confusion_matrix, accuracy_score

print(confusion_matrix(y_test, y_pred))
print(accuracy_score(y_test, y_pred))
print("classification report:---------------------------\n")
print(classification_report(y_test, y_pred, labels=iris.target))

最佳答案

很可能您在加载数据集并将其拆分为 Xy 时犯了错误。检查这个修正。它给出了正确的结果。然而,iris 数据集非常简单,没有太多内容 multicollinearityheteroscedasticity 。这意味着 knn 在对它们进行完美分类时不会遇到太多麻烦,并且通过更改 knn 参数您不会看到输出指标有太大差异。为了观察剧烈的变化,你应该选择难度较高的数据。

from sklearn.model_selection import train_test_split
from sklearn import datasets

iris = datasets.load_iris()

# import some data to play with
X = iris.data[:, :2] # we only take the first two features.
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30,
random_state=42)

from sklearn.neighbors import KNeighborsClassifier

# Instantiate learning model (k = 5)
clf = KNeighborsClassifier(n_neighbors=10)
# Fitting the model
clf.fit(X_train, y_train)
# Predicting the Test set results
y_pred = clf.predict(X_test)
print(y_pred)

from sklearn.metrics import classification_report, confusion_matrix, accuracy_score

print(confusion_matrix(y_test, y_pred))
print(accuracy_score(y_test, y_pred))
print("classification report:---------------------------\n")
print(classification_report(y_test, y_pred, labels=iris.target))

这表明

[1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 2 1 1 1 2 0 1 0 2 2 1 1 2 0 0 0 0 2 0 0 1 2
0 0 0 1 2 2 0 0]
[[19 0 0]
[ 0 8 5]
[ 0 6 7]]
0.7555555555555555
classification report:---------------------------
...

当您调整 knn 参数时,精度确实会发生变化。

关于python - 虹膜模型不同K值的KNN模型的统计指标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60133508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com