Created
October 6, 2020 18:14
-
-
Save rodrigols89/353156630f9d3be6f4d29a467b233046 to your computer and use it in GitHub Desktop.
This file contains 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
def ApplyesKFold(x_axis, y_axis): | |
# Linear Models. | |
from sklearn.linear_model import LinearRegression | |
from sklearn.linear_model import ElasticNet | |
from sklearn.linear_model import Ridge | |
from sklearn.linear_model import Lasso | |
# Cross-Validation models. | |
from sklearn.model_selection import cross_val_score | |
from sklearn.model_selection import KFold | |
# KFold settings. | |
kfold = KFold(n_splits=10, shuffle=True) # shuffle=True, Shuffle (embaralhar) the data. | |
# Axis | |
x = x_axis | |
y = y_axis | |
# Models instances. | |
linearRegression = LinearRegression() | |
elasticNet = ElasticNet() | |
ridge = Ridge() | |
lasso = Lasso() | |
# Applyes KFold to models. | |
linearRegression_result = cross_val_score(linearRegression, x, y, cv = kfold) | |
elasticNet_result = cross_val_score(elasticNet, x, y, cv = kfold) | |
ridge_result = cross_val_score(ridge, x, y, cv = kfold) | |
lasso_result = cross_val_score(lasso, x, y, cv = kfold) | |
# Creates a dictionary to store Linear Models. | |
dic_models = { | |
"LinearRegression": linearRegression_result.mean(), | |
"ElasticNet": elasticNet_result.mean(), | |
"Ridge": ridge_result.mean(), | |
"Lasso": lasso_result.mean() | |
} | |
# Select the best model. | |
bestModel = max(dic_models, key=dic_models.get) | |
print("Linear Regression Mean (R^2): {0}\nElastic Net Mean (R^2): {1}\nRidge Mean (R^2): {2}\nLasso Mean (R^2): {3}".format(linearRegression_result.mean(), elasticNet_result.mean(), ridge_result.mean(), lasso_result.mean())) | |
print("The best model is: {0} with value: {1}".format(bestModel, dic_models[bestModel])) | |
if __name__ =='__main__': | |
import pandas as pd | |
df = pd.read_csv("../datasets/Admission_Predict.csv") | |
df.drop('Serial No.', axis = 1, inplace = True) | |
x = df.drop('Chance of Admit ', axis = 1) | |
y = df['Chance of Admit '] | |
ApplyesKFold(x, y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment