Skip to content

Instantly share code, notes, and snippets.

@ShrashtiSinghal
Created August 8, 2020 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShrashtiSinghal/c6b7b64f0290c93af97be9366c3ce175 to your computer and use it in GitHub Desktop.
Save ShrashtiSinghal/c6b7b64f0290c93af97be9366c3ce175 to your computer and use it in GitHub Desktop.
#Test Train Split
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
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,5925,7879,9000,2268,2280,2628,2645,3000],
"price":[124000,75500,86000,97000,85400,100000,106000,113000,122500,84500,151000,83000,106000,168500,178740,192500,215000,275000,39700,34900,35311,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)
y_test= y_test.values.reshape(-1,1)
regressor = LinearRegression()
regressor.fit(x_train,y_train)
y_pred = regressor.predict(x_test)
return(x_test,y_pred)
Object= predit()
size,price= Object.bestFitLine()
for s, p in zip(size, price):
print ("Price of {} sq feet house is: {}".format(s, p))
print('Mean Absolute Error:', metrics.mean_absolute_error(size, price))
print('Mean Squared Error:', metrics.mean_squared_error(size, price))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(size, price)))
print('R Square Error:', metrics.r2_score(size, price))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment