使用LogisticRegression对乳腺癌诊断数据分类python代码
时间: 2024-05-07 17:22:37 浏览: 122
16.逻辑回归LogisticRegression分析鸢尾花数据1
以下是使用LogisticRegression对乳腺癌诊断数据进行二分类的Python代码示例:
```
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# 加载乳腺癌诊断数据
data = load_breast_cancer()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = pd.Series(data.target)
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 训练LogisticRegression模型
model = LogisticRegression()
model.fit(X_train, y_train)
# 在测试集上进行预测,并计算准确率
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
```
运行结果示例:
```
Accuracy: 0.9649122807017544
```
注意,以上示例代码只是一个简单的演示,实际应用中还需要进行更多的数据预处理、特征工程、模型调优等步骤。
阅读全文