Created
October 6, 2018 12:36
-
-
Save animesh-agarwal/a4dbda91b48790c37942e62f6f9f1e09 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.preprocessing import PolynomialFeatures | |
def create_polynomial_regression_model(degree): | |
"Creates a polynomial regression model for the given degree" | |
poly_features = PolynomialFeatures(degree=degree) | |
# transforms the existing features to higher degree features. | |
X_train_poly = poly_features.fit_transform(X_train) | |
# fit the transformed features to Linear Regression | |
poly_model = LinearRegression() | |
poly_model.fit(X_train_poly, Y_train) | |
# predicting on training data-set | |
y_train_predicted = poly_model.predict(X_train_poly) | |
# predicting on test data-set | |
y_test_predict = poly_model.predict(poly_features.fit_transform(X_test)) | |
# evaluating the model on training dataset | |
rmse_train = np.sqrt(mean_squared_error(Y_train, y_train_predicted)) | |
r2_train = r2_score(Y_train, y_train_predicted) | |
# evaluating the model on test dataset | |
rmse_test = np.sqrt(mean_squared_error(Y_test, y_test_predict)) | |
r2_test = r2_score(Y_test, y_test_predict) | |
print("The model performance for the training set") | |
print("-------------------------------------------") | |
print("RMSE of training set is {}".format(rmse_train)) | |
print("R2 score of training set is {}".format(r2_train)) | |
print("\n") | |
print("The model performance for the test set") | |
print("-------------------------------------------") | |
print("RMSE of test set is {}".format(rmse_test)) | |
print("R2 score of test set is {}".format(r2_test)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment