sklearn.metrics库
时间: 2023-12-06 17:38:02 浏览: 107
机器学习库spark-sklearn.pptx
sklearn.metrics库是一个用于评估模型性能的Python库,它包含了许多评价指标函数,例如准确率、召回率、F1值等。这些指标可以用于分类、回归和聚类等不同类型的机器学习问题。以下是一个使用sklearn.metrics库计算准确率的例子:
```python
from sklearn.metrics import accuracy_score
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
# 加载 iris 数据集
iris = load_iris()
X = iris.data
y = iris.target
# 将数据集拆分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, 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("Accuracy:", accuracy)
```
阅读全文