使用Sklearn搭建KNN模型,数据采用鸢尾花数据或者波士顿数据 上传格式为:.jpynb
时间: 2024-05-15 13:16:16 浏览: 89
As an AI language model, I don't have the capability to upload files. However, here is an example code for building a KNN model using Sklearn with the Iris dataset:
```python
from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Load the iris dataset
iris = load_iris()
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3)
# Create a KNN classifier with k=3
knn = KNeighborsClassifier(n_neighbors=3)
# Train the model
knn.fit(X_train, y_train)
# Make predictions on the testing set
y_pred = knn.predict(X_test)
# Evaluate the accuracy of the model
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
```
You can modify this code to work with the Boston dataset as well.
阅读全文