1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
from sklearn.datasets import load_boston import numpy as np data = load_boston()
x = data['data'] y = data['target']
from sklearn.model_selection import train_test_split X_train,X_test,y_train,y_test = train_test_split(x,y,test_size = 0.2,random_state = 0)
from sklearn.tree import DecisionTreeRegressor
model = DecisionTreeRegressor(max_depth = 5)
model.fit(X_train,y_train)
y_train_pred = model.predict(X_train) y_test_pred = model.predict(X_test)
from sklearn.metrics import mean_squared_error mean_squared_error(y_train_pred,y_train) mean_squared_error(y_test_pred,y_test) from sklearn.metrics import r2_score r2_score(y_train_pred,y_train) r2_score(y_test_pred,y_test)
|