<jupyter_start><jupyter_text>http://chrisstrelioff.ws/sandbox/2015/06/25/decision_trees_in_python_again_cross_validation.html  보스턴 주택 가격 정보 예측하기 - Regression<jupyter_code>import numpy as np
import pylab as pl
import matplotlib.pyplot as plt
import sklearn
from sklearn.model_selection import ShuffleSplit
from sklearn.model_selection import train_test_split
from sklearn import datasets
from sklearn.tree import DecisionTreeRegressor

from sklearn.metrics import mean_squared_error,median_absolute_error,r2_score,mean_absolute_error
from sklearn import grid_search<jupyter_output><empty_output><jupyter_text>Training Set 로딩<jupyter_code>boston = datasets.load_boston()
X_train, X_test, Y_train, Y_test = train_test_split(boston.data, boston.target, test_size = 0.33, random_state = 5)
print(X_train.shape)
print(X_test.shape)
print(Y_train.shape)
print(Y_test.shape)<jupyter_output>(339, 13)
(167, 13)
(339,)
(167,)<jupyter_text>모델 학습하기<jupyter_code># Setup a Decision Tree Regressor
regressor = DecisionTreeRegressor()

parameters = {
    'max_depth':(1,2,3,4,5,6,7,8,9,10),
    'min_samples_split': [2, 10, 20],
    'min_samples_leaf': [1, 5, 10],
    'max_leaf_nodes': [5, 10, 20],
}
# Create cross-validation sets from the training data
cv_sets = ShuffleSplit(n_splits = 10, test_size = 0.20, random_state = 0)

regressors = grid_search.GridSearchCV(regressor, parameters, scoring='neg_mean_squared_error') #, cv=cv_sets)
regressors.fit(boston.data, boston.target)

# pick the best
reg = regressors.best_estimator_

# Fit the learner to the training data
reg.fit(X_train, Y_train)<jupyter_output><empty_output><jupyter_text>Model Validation<jupyter_code>import matplotlib.pyplot as plt
Y_pred = reg.predict(X_test)
plt.scatter(Y_test, Y_pred)
plt.xlabel("Prices: $Y_i$")
plt.ylabel("Predicted prices: $\hat{Y}_i$")
plt.title("Prices vs Predicted prices: $Y_i$ vs $\hat{Y}_i$")
plt.show()
mse = sklearn.metrics.mean_squared_error(Y_test, Y_pred)
print(mse)
print(Y_test[:5])
print(Y_pred[:5])<jupyter_output>[ 37.6  27.9  22.6  13.8  35.2]
[ 41.54444444  30.3         24.56         8.04615385  41.54444444]<jupyter_text>Decision Tree Visualization<jupyter_code>from sklearn import tree
dotfile = open("boston_housing_dt.dot", 'w')
tree.export_graphviz(reg, out_file = dotfile, feature_names = boston.feature_names)
dotfile.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11807024968260686, 'fraction_numerical': 0.047820567075751164, 'mean_word_length': 6.906354515050167, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '174776', 'license': 'Apache-2.0', 'n_tokens_mistral': 1017, 'n_tokens_neox': 941, 'n_words': 174, 'path': 'boston_housing_prices_decision_tree.ipynb', 'repo_name': 'ferdinand33/TestingRepo', 'size': '2363'}