基于python的鸢尾花分类代码
时间: 2023-11-09 17:06:58 浏览: 65
python机器学习入门,KNN鸢尾花分类
5星 · 资源好评率100%
以下是基于Python的鸢尾花分类代码示例:
```python
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# 导入数据集
from sklearn.datasets import load_iris
iris = load_iris()
# 特征矩阵
X = iris.data
# 目标向量
y = iris.target
# 数据集划分
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=1)
# 模型训练
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
# 模型评估
from sklearn.metrics import accuracy_score
y_pred = knn.predict(X_test)
accuracy_score(y_test, y_pred)
```
其中,使用了sklearn库中的KNeighborsClassifier算法进行建模,并使用train_test_split对数据集进行了划分,最后使用accuracy_score对模型结果进行评估。
阅读全文