python鸢尾花分类
时间: 2024-09-23 09:12:23 浏览: 78
Python中的鸢尾花分类通常指的是使用机器学习算法对Iris数据集进行预测,这是一个经典的多变量分类问题,非常适合初学者入门机器学习。Iris数据集包含了三种不同种类的鸢尾花(Setosa、Versicolour和Virginica)的花萼长度、花瓣长度、花萼宽度和花瓣宽度等四个特征。
在Python中,我们可以使用scikit-learn库中的`load_iris()`函数加载数据,并通过诸如决策树(DecisionTreeClassifier)、K近邻(KNeighborsClassifier)或支持向量机(SVC)等分类器来进行模型训练和预测。常见的步骤包括数据预处理、划分训练集和测试集、选择并训练模型,以及评估模型性能。
下面是一个简单的示例,使用KNN分类器:
```python
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
# 加载鸢尾花数据
iris = datasets.load_iris()
X = iris.data
y = iris.target
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建KNN分类器
knn = KNeighborsClassifier(n_neighbors=3)
# 训练模型
knn.fit(X_train, y_train)
# 预测测试集
y_pred = knn.predict(X_test)
# 评估准确性
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")
```
阅读全文
相关推荐



















