python使用GBDT输出f1指标
时间: 2023-06-18 12:04:55 浏览: 110
Adaboost算法介绍及应用.docx
要输出GBDT模型的f1指标,需要进行以下步骤:
1. 导入所需的库和数据
```python
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import f1_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.3, random_state=42)
```
2. 创建并训练GBDT模型
```python
gbdt = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42)
gbdt.fit(X_train, y_train)
```
3. 对测试集进行预测并计算f1指标
```python
y_pred = gbdt.predict(X_test)
f1 = f1_score(y_test, y_pred)
print("f1 score: {:.2f}".format(f1))
```
完整代码如下:
```python
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import f1_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.3, random_state=42)
gbdt = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42)
gbdt.fit(X_train, y_train)
y_pred = gbdt.predict(X_test)
f1 = f1_score(y_test, y_pred)
print("f1 score: {:.2f}".format(f1))
```
注意:在实际应用中,为了更准确地评估模型性能,应该使用交叉验证等更加严格的方法进行评估。
阅读全文