Last active
August 8, 2020 16:35
-
-
Save ShrashtiSinghal/bd0fc40296d1a518a18386b3d9e497c0 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
#Regularization | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
from sklearn.linear_model import LinearRegression, Lasso,ElasticNet, Ridge, MultiTaskLasso, LassoLars, OrthogonalMatchingPursuit | |
from sklearn.model_selection import train_test_split | |
from sklearn import metrics | |
class predit: | |
def bestFitLine(self): | |
datadict={"size":[1300,1491,1526,1533,1680,1680,1869,1890,1920,1936,1950,1953,2016,2117,3072,3182,3196,3842,2268,2280,2628,2645,3000], | |
"price":[124000,75500,86000,97000,85400,100000,106000,113000,122500,84500,151000,83000,106000,168500,178740,192500,215000,275000,173000, 179400,175500,172500,173733]} | |
df=pd.DataFrame.from_dict(datadict) | |
x_train, x_test, y_train, y_test = train_test_split(df["size"], df["price"], test_size= 0.2, random_state=0) | |
x_train= x_train.values.reshape(-1, 1) | |
y_train= y_train.values.reshape(-1, 1) | |
x_test = x_test.values.reshape(-1, 1) | |
models = [] | |
models.append(('LR', LinearRegression())) | |
models.append(('LASSO', Lasso())) | |
models.append(('EN', ElasticNet())) | |
models.append(('Ridge', Ridge())) | |
models.append(('MultiTaskLasso', MultiTaskLasso())) | |
models.append(('LarsLasso', LassoLars())) | |
models.append(('OMP', OrthogonalMatchingPursuit())) | |
results = [] | |
names = [] | |
for name, model in models: | |
regressor=model.fit(x_train,y_train) | |
cv_results=regressor.score(x_test,y_test) | |
results.append(cv_results) | |
names.append(name) | |
msg = "%s: %f" % (name, cv_results) | |
print(msg) | |
return(True) | |
Object= predit() | |
size= Object.bestFitLine() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment