1. 程式人生 > >Python資料分析與機器學習-使用sklearn構造決策樹模型

Python資料分析與機器學習-使用sklearn構造決策樹模型


# datasets包括內建的資料集 california_housing房價的資料集
from sklearn.datasets.california_housing import fetch_california_housing
import pandas as pd

housing = fetch_california_housing()
# print(housing.DESCR)
# print(housing.data)
# print(housing.data.shape) #(20640, 8)
# print(housing.target)
# print(housing.feature_names)
# #['MedInc', 'HouseAge', 'AveRooms', 'AveBedrms', 'Population', 'AveOccup', 'Latitude', 'Longitude']

from sklearn import tree

dtr = tree.DecisionTreeRegressor(max_depth=2)  # DecisionTreeRegressor 決策樹 max_depth 樹的最大深度
dtr.fit(housing.data[:, [6, 7]], housing.target)  # latitude longitude  緯度經度 傳入:X y
# print(dtr)
'''
DecisionTreeRegressor(criterion='mse', max_depth=2, max_features=None,
           max_leaf_nodes=None, min_samples_leaf=1, min_samples_split=2,
           min_weight_fraction_leaf=0.0, presort=False, random_state=None,
           splitter='best')
'''

'''決策樹模型視覺化'''

dot_data = \
    tree.export_graphviz(
        dtr,  # 構造矩陣名字
        out_file="tree.dot",
        feature_names=housing.feature_names[6:8],  # 特徵名字
        filled=True,
        impurity=False,
        rounded=True
    )

import pydotplus
from IPython.display import Image

graph = pydotplus.graph_from_dot_file("tree.dot")
graph.get_nodes()[7].set_fillcolor("#FFF2DD")
Image(graph.create_png())
graph.write_png("dtr_white_background.png")  # 儲存為本地圖片

from sklearn.cross_validation import train_test_split

X_train, X_test, y_train, y_test = train_test_split(housing.data[0:1000], housing.target[0:1000], test_size=0.1,
                                                    random_state=42)  # random_state = 42 值隨意,保證每次隨機完結果一樣
dtr = tree.DecisionTreeRegressor(random_state=42)
dtr.fit(X_train, y_train)
print(dtr.score(X_test, y_test))

'''隨機森林'''
from sklearn.grid_search import GridSearchCV  # GridSearchCV 自動設定引數組合
from sklearn.ensemble import RandomForestRegressor

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)  # cv交叉驗證(切分的是測試集)
grid.fit(X_train, y_train)
print(grid.grid_scores_)
print(grid.best_params_)
print(grid.best_score_)

rfr = RandomForestRegressor(min_samples_split=3, n_estimators=100, random_state=42)
rfr.fit(X_train, y_train)
rfr.score(X_test, y_test)

pd.Series(rfr.feature_importances_, index=housing.feature_names).sort_values(ascending=False)