dushine00 2020-06-09
import matplotlib.pyplot as plt import pandas as pd from sklearn.datasets import fetch_california_housing housing = fetch_california_housing() # print(housing.DESCR) print(housing.data.shape) print(housing.data[0]) from sklearn import tree dtr = tree.DecisionTreeRegressor(max_depth=2) dtr.fit(housing.data[:, [6, 7]], housing.target) # 树模型参数 # 1.criterion gini or entroy # 2.splitter best or random 前者是在所有特征最终找到最好的切分点,后者实在部分特征中 # 3.max_features None, log2, sqrt, N特征小于50一般使用所有的 # 4.mini_depth 数据少或者特征少的情况下,可以不管这个值,如果模型数据样本量多或者特征多的情况下,可以尝试限制 # 5.mini_samples_split 如果节点数量少,不用管,如果样本数非常大,则推荐增加这个值 # 6.mini_samples_leaf 限制了叶子节点的样本权重和最小值,如果小于这个值,样本量不大,不需要管这个值,大些如果10w,可以设置5 from sklearn.model_selection import train_test_split data_train, data_test, target_train, target_test = train_test_split(housing.data, housing.target, test_size=0.1, random_state=42) # dtr = tree.DecisionTreeRegressor(random_state= 42) # dtr.fit(data_train, target_train) # # print(dtr.score(data_test, target_test)) # # from sklearn.ensemble import RandomForestRegressor # # rfr = RandomForestRegressor(random_state=42) # rfr.fit(data_train, target_train) # print(rfr.score(data_test, target_test)) from sklearn.model_selection import GridSearchCV tree_param_grid = {‘min_samples_split‘:list((3, 6, 9)), ‘n_estimators‘:list((10, 50, 100))} grid = GridSearchCV(RandomForestRegressor(), param_grid=tree_param_grid, cv=5) grid.fit(data_train, target_train) print(grid.error_score, grid.best_params_, grid.best_score_)