feature_names = np.array(X[0]) # Remove the first row because they are feature names return np.array(X[1:]).astype(np.float32), np.array(y[1:]).astype(np.float32), feature_names if __name__=='__main__': # Load the dataset from the input file X, y, feature_names = load_dataset(sys.argv[1]) X, y = shuffle(X, y, random_state=7) # Split the data 80/20 (80% for training, 20% for testing) num_training = int(0.9 * len(X)) X_train, y_train = X[:num_training], y[:num_training] X_test, y_test = X[num_training:], y[num_training:] # Fit Random Forest regression model rf_regressor = RandomForestRegressor(n_estimators=1000, max_depth=10, min_samples_split=1) rf_regressor.fit(X_train, y_train) # Evaluate performance of Random Forest regressor y_pred = rf_regressor.predict(X_test) mse = mean_squared_error(y_test, y_pred) evs = explained_variance_score(y_test, y_pred) print "\n#### Random Forest regressor performance ####" print "Mean squared error =", round(mse, 2) print "Explained variance score =", round(evs, 2) # Plot relative feature importances plot_feature_importances(rf_regressor.feature_importances_, 'Random Forest regressor', feature_names)
np.float32), feature_names if __name__ == '__main__': # Load the dataset from the input file X, y, feature_names = load_dataset(sys.argv[1]) X, y = shuffle(X, y, random_state=7) # Split the data 80/20 (80% for training, 20% for testing) num_training = int(0.9 * len(X)) X_train, y_train = X[:num_training], y[:num_training] X_test, y_test = X[num_training:], y[num_training:] # Fit Random Forest regression model rf_regressor = RandomForestRegressor(n_estimators=1000, max_depth=10, min_samples_split=1) rf_regressor.fit(X_train, y_train) # Evaluate performance of Random Forest regressor y_pred = rf_regressor.predict(X_test) mse = mean_squared_error(y_test, y_pred) evs = explained_variance_score(y_test, y_pred) print "\n#### Random Forest regressor performance ####" print "Mean squared error =", round(mse, 2) print "Explained variance score =", round(evs, 2) # Plot relative feature importances plot_feature_importances(rf_regressor.feature_importances_, 'Random Forest regressor', feature_names)
evs = explained_variance_score(y_test, y_pred_dt) print("\n#### Decision Tree performance ####") print("Mean squared error =", round(mse, 2)) print("Explained variance score =", round(evs, 2)) y_pred_ab = ab_regressor.predict(X_test) mse = mean_squared_error(y_test, y_pred_ab) evs = explained_variance_score(y_test, y_pred_ab) print("\n#### AdaBoost performance ####") print("Mean squared error =", round(mse, 2)) print("Explained variance score =", round(evs, 2)) #relative importance of features plot_feature_importances(dt_regressor.feature_importances_, 'Decision Tree regressor', housing_data.feature_names) plot_feature_importances(ab_regressor.feature_importances_, 'AdaBoost regressor', housing_data.feature_names) def plot_feature_importances(feature_importances, title, feature_names): # Normalize the importance values feature_importances = 100.0 * (feature_importances / max(feature_importances)) # Sort the index values and flip them so that they are arranged in decreasing order of importance index_sorted = np.flipud(np.argsort(feature_importances)) # Center the location of the labels on the X-axis (for display purposes only) pos = np.arange(index_sorted.shape[0]) + 0.5 # Plot the bar graph plt.figure() plt.bar(pos, feature_importances[index_sorted], align='center') plt.xticks(pos, feature_names[index_sorted]) plt.ylabel('Relative Importance') plt.title(title)