已知鸢尾花数据共有150条记录,分别做如下划分: 训练集:data【:30】data【:120】 测试集:data【30:】data【120:】 使用knn算法对每种划分的数据进行训练,并使用对应的测试集数据进行模型评价
时间: 2023-06-09 14:06:37 浏览: 135
好的,您可以先将数据读入到程序中,然后使用scikit-learn库中的KNeighborsClassifier方法进行KNN算法训练。
下面是示例代码:
```python
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_iris
import numpy as np
# load iris dataset
iris = load_iris()
X = iris.data
y = iris.target
# define train and test sets
X_train_set1 = X[:30]
X_test_set1 = X[30:]
y_train_set1 = y[:30]
y_test_set1 = y[30:]
X_train_set2 = X[:120]
X_test_set2 = X[120:]
y_train_set2 = y[:120]
y_test_set2 = y[120:]
# create and train the KNN model
k = 3
knn1 = KNeighborsClassifier(n_neighbors=k)
knn2 = KNeighborsClassifier(n_neighbors=k)
knn1.fit(X_train_set1, y_train_set1)
knn2.fit(X_train_set2, y_train_set2)
# predict using test sets
y_pred_test_set1 = knn1.predict(X_test_set1)
y_pred_test_set2 = knn2.predict(X_test_set2)
# evaluate model accuracy
accuracy_set1 = np.mean(y_pred_test_set1 == y_test_set1)
accuracy_set2 = np.mean(y_pred_test_set2 == y_test_set2)
print("Accuracy of KNN model with 30 training samples: %.2f%%" % (accuracy_set1*100))
print("Accuracy of KNN model with 120 training samples: %.2f%%" % (accuracy_set2*100))
```
这段代码将鸢尾花数据集分成两个不同的训练集和测试集,然后分别对每组数据使用KNN算法进行训练和测试。最后打印出训练集和测试集上的准确率。
注意,这里使用了KNN算法的默认参数,如果需要调整参数,可以使用scikit-learn中的GridSearchCV方法进行网格搜索。
阅读全文