从sklearn导入良性恶性肿瘤数据,划分测试与训练集,分别用logistic回归与SVM分类器完成分类任务,建立评价指标(评价侧重癌症病人是否能正确识别出来),记录参数调整过程和结果,评价两种分类器对该任务的完成程度
时间: 2023-12-08 12:06:00 浏览: 100
用logistic回归,SVM,神经网络实现分类算法.zip
好的,让我来回答您的问题。
首先,我会从sklearn中导入良性恶性肿瘤数据集,并将其划分为训练集和测试集。以下是代码示例:
```python
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
# 导入数据集
cancer = load_breast_cancer()
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, test_size=0.2, random_state=42)
```
接下来,我会使用logistic回归和SVM分类器完成分类任务。以下是代码示例:
```python
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
# 创建logistic回归分类器
logistic = LogisticRegression()
# 训练模型
logistic.fit(X_train, y_train)
# 创建SVM分类器
svm = SVC()
# 训练模型
svm.fit(X_train, y_train)
```
然后,我会建立评价指标,侧重于癌症病人是否能被正确识别出来。以下是代码示例:
```python
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
# 预测测试集结果
logistic_pred = logistic.predict(X_test)
svm_pred = svm.predict(X_test)
# 计算评价指标
logistic_accuracy = accuracy_score(y_test, logistic_pred)
svm_accuracy = accuracy_score(y_test, svm_pred)
logistic_precision = precision_score(y_test, logistic_pred)
svm_precision = precision_score(y_test, svm_pred)
logistic_recall = recall_score(y_test, logistic_pred)
svm_recall = recall_score(y_test, svm_pred)
logistic_f1 = f1_score(y_test, logistic_pred)
svm_f1 = f1_score(y_test, svm_pred)
```
接着,我会记录参数调整过程和结果。以下是代码示例:
```python
# 调整logistic回归分类器参数
logistic = LogisticRegression(C=100)
# 训练模型
logistic.fit(X_train, y_train)
# 调整SVM分类器参数
svm = SVC(C=100, kernel='poly')
# 训练模型
svm.fit(X_train, y_train)
```
最后,我会评价两种分类器对该任务的完成程度。以下是代码示例:
```python
print("Logistic Regression:")
print("Accuracy: {:.2f}".format(logistic_accuracy))
print("Precision: {:.2f}".format(logistic_precision))
print("Recall: {:.2f}".format(logistic_recall))
print("F1 Score: {:.2f}".format(logistic_f1))
print("\nSVM:")
print("Accuracy: {:.2f}".format(svm_accuracy))
print("Precision: {:.2f}".format(svm_precision))
print("Recall: {:.2f}".format(svm_recall))
print("F1 Score: {:.2f}".format(svm_f1))
```
通过比较两种分类器的评价指标,我们可以得出结论,哪种分类器对该任务的完成程度更好。
阅读全文