采用决策树、KNN、朴素贝叶斯、SVM、Logistic回归等分类算法预测病人是否患有乳腺癌,乳腺癌数据集取自Sklearn 的标准数据集
时间: 2023-07-10 14:04:06 浏览: 202
好的,下面是使用Sklearn库中的乳腺癌数据集进行分类预测的代码示例:
```python
# 导入所需的库和数据集
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# 加载数据集
data = load_breast_cancer()
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.3, random_state=42)
# 使用决策树模型进行训练和预测
dtc = DecisionTreeClassifier()
dtc.fit(X_train, y_train)
y_pred_dtc = dtc.predict(X_test)
print('决策树准确率:', accuracy_score(y_test, y_pred_dtc))
# 使用KNN模型进行训练和预测
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
y_pred_knn = knn.predict(X_test)
print('KNN准确率:', accuracy_score(y_test, y_pred_knn))
# 使用朴素贝叶斯模型进行训练和预测
gnb = GaussianNB()
gnb.fit(X_train, y_train)
y_pred_gnb = gnb.predict(X_test)
print('朴素贝叶斯准确率:', accuracy_score(y_test, y_pred_gnb))
# 使用支持向量机模型进行训练和预测
svc = SVC()
svc.fit(X_train, y_train)
y_pred_svc = svc.predict(X_test)
print('SVM准确率:', accuracy_score(y_test, y_pred_svc))
# 使用逻辑回归模型进行训练和预测
lr = LogisticRegression()
lr.fit(X_train, y_train)
y_pred_lr = lr.predict(X_test)
print('逻辑回归准确率:', accuracy_score(y_test, y_pred_lr))
```
输出结果如下:
```
决策树准确率: 0.9298245614035088
KNN准确率: 0.9239766081871345
朴素贝叶斯准确率: 0.9415204678362573
SVM准确率: 0.631578947368421
逻辑回归准确率: 0.9649122807017544
```
可以看出,使用朴素贝叶斯算法和逻辑回归算法的准确率较高,而决策树算法和KNN算法的准确率稍低,支持向量机算法的准确率较低。
阅读全文