XGBoost预测乳腺癌数据python代码
时间: 2024-05-06 09:12:46 浏览: 239
XGBoost是一种强大的机器学习算法,常用于分类和回归问题。下面是使用XGBoost进行乳腺癌数据预测的Python代码示例:
首先,你需要安装XGBoost库。可以使用以下命令进行安装:
```
pip install xgboost
```
接下来,导入所需的库和模块:
```python
import xgboost as xgb
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
```
加载乳腺癌数据集:
```python
data = load_breast_cancer()
X = data.data
y = data.target
```
将数据集划分为训练集和测试集:
```python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
定义XGBoost分类器并进行训练:
```python
model = xgb.XGBClassifier()
model.fit(X_train, y_train)
```
使用训练好的模型进行预测:
```python
y_pred = model.predict(X_test)
```
计算预测准确率:
```python
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
```
以上是使用XGBoost进行乳腺癌数据预测的基本代码示例。
阅读全文