In [ ]:
## Preprocessing
from sklearn.datasets import load_boston
from sklearn.cross_validation import train_test_split
boston = load_boston()
X = boston["data"]
y = boston["target"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state = 10)
In [ ]:
## Models, Predictions, and Metrics
from sklearn import linear_model
from sklearn.ensemble import RandomForestRegressor
def fit_and_score_model(model_type, hyperparameter, X_train, y_train, X_test, y_test):
if model_type == "Ridge":
ridge = linear_model.Ridge(alpha=hyperparameter)
ridge.fit(X_train, y_train)
return ridge.score(X_test, y_test)
if model_type == "RandomForestRegressor":
rf = RandomForestRegressor(n_estimators=hyperparameter)
rf.fit(X_train, y_train)
return rf.score(X_test, y_test)
for alpha in range(4):
ridge_score = fit_and_score_model("Ridge", alpha, X_train, y_train, X_test, y_test)
print("Ridge (alpha=" + str(alpha) + ") score: " + str(ridge_score))
for n_estimator in [1,5,7,10]:
rf_score = fit_and_score_model("RandomForestRegressor", n_estimator, X_train, y_train, X_test, y_test)
print("Random Forest (n_estimator=" + str(n_estimator) + ") score: " + str(rf_score))