kaggle Partial_Dependence_Plots

# Partial dependence plots
# 改变单变量对最终预测结果的影响
# 先fit出一种模型,然后取一行,不断改变某一特征,看它对最终结果的印象。
# 但是,只使用一行不具有典型性
# 所以对所有行执行上述操作,求均值

 

import pandas as pd
from sklearn.ensemble import GradientBoostingRegressor, GradientBoostingClassifier
from sklearn.ensemble.partial_dependence import partial_dependence, plot_partial_dependence
from sklearn.preprocessing import Imputer
import matplotlib.pyplot as plt 

train_path = r"C:\Users\cbattle\Desktop\train.csv"
test_path = r"C:\Users\cbattle\Desktop\test.csv"
out_path = r"C:\Users\cbattle\Desktop\out.csv"

def get_some_data():
    data = pd.read_csv(train_path)
    y = data.SalePrice
    cols_to_use = ['YearBuilt', 'GrLivArea', 'TotRmsAbvGrd']
    X = data[cols_to_use]
    my_imputer = Imputer()
    imputed_X = my_imputer.fit_transform(X)
    return imputed_X, y
    
X, y = get_some_data()
my_model = GradientBoostingRegressor()
my_model.fit(X, y)
my_plots = plot_partial_dependence(my_model, 
                                   features=[0,2], 
                                   X=X, 
                                   feature_names=cols_to_use, 
                                   grid_resolution=10)
plt.show()
# print('ok')

# There is a function called partial_dependence to get the raw data making up this plot, rather than making the visual plot itself.
# This is useful if you want to control how it is visualized using a plotting package like Seaborn. With moderate effort, you could
# make much nicer looking plots.

 

转载于:https://www.cnblogs.com/cbattle/p/8830774.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值