机器学习之GBDT算法

第1关:机器学习之GBDT算法


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn import ensemble

# 数据的读入与处理
data_path ='./gbdt/housing.csv'
data_df = pd.read_csv(data_path)
######Begin######
# 移除的特征
features_to_remove = ['CRIM','ZN','CHAS','RAD','TAX','B-1000']
for feature in features_to_remove:
    data_df.drop([feature], axis=1, inplace=True)

# 移除的异常值
values_to_remove = [50]
length = len(data_df['MEDV'])
for value in values_to_remove:
    for i in range(length):
        if data_df.loc[i, 'MEDV'] == value:
            data_df.drop(index=i, axis=0, inplace=True)

# 得到特征和标签
labels = data_df['MEDV']  # 提取labels
features = data_df.drop(['MEDV'], axis=1)  # 提取特征

# 按4:1的比例划分训练和测试集
x_train, x_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, random_state=1)

# 实例化估计器对象
params = {'n_estimators': 500, 'max_depth': 4, 'min_samples_split': 2,
          'learning_rate': 0.01, 'loss': 'ls'}
gbr = ensemble.GradientBoostingRegressor(**params)
gbr.fit(x_train, y_train)
y_predict = gbr.predict(x_test)

#######End#######
# 输出测试集的预测值
print(y_predict.shape)
# 绘制预测值与真实值的对比图
plt.rcParams['font.family'] = "sans-serif"
plt.rcParams['font.sans-serif'] = "SimHei"
plt.title('对比图', fontsize=24)
xx = np.arange(0, 50)
yy = xx
plt.xlabel('* 真实值 *', fontsize=14)
plt.ylabel('* 预测值 *', fontsize=14)
plt.plot(xx, yy)
plt.scatter(y_test, y_predict, color='red')
plt.grid()
plt.savefig('./gbdt/out/result.png')
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值