逻辑回归乳腺癌python
时间: 2023-10-13 12:23:12 浏览: 231
逻辑回归python代码
逻辑回归可以用于乳腺癌的分类问题。这里提供一个使用Python进行乳腺癌分类的示例代码:
首先,我们需要导入需要用到的库:
```python
import pandas as pd
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
```
然后,我们可以使用sklearn自带的乳腺癌数据集:
```python
breast_cancer = datasets.load_breast_cancer()
```
接着,我们将数据集分为训练集和测试集:
```python
X_train, X_test, y_train, y_test = train_test_split(breast_cancer.data, breast_cancer.target, test_size=0.2, random_state=0)
```
然后,我们可以使用逻辑回归进行分类:
```python
logistic_reg = LogisticRegression()
logistic_reg.fit(X_train, y_train)
```
最后,我们可以使用测试集评估模型的性能:
```python
y_pred = logistic_reg.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy: {:.2f}%".format(accuracy*100))
```
完整代码如下:
```python
import pandas as pd
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
breast_cancer = datasets.load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(breast_cancer.data, breast_cancer.target, test_size=0.2, random_state=0)
logistic_reg = LogisticRegression()
logistic_reg.fit(X_train, y_train)
y_pred = logistic_reg.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy: {:.2f}%".format(accuracy*100))
```
输出结果为:
```
Accuracy: 95.61%
```
这意味着我们的模型可以正确地对95.61%的测试样本进行分类。
阅读全文