The Code Monster

Create.Share.Learn

Follow publication

Linear Regressions and Split Datasets Using Sklearn

Himanshu Verma
The Code Monster
Published in
4 min readDec 9, 2019
Photo by Drew Beamer on Unsplash

Step #1

import pandas as pd
from sklearn.model_selection import train_test_split
from matplotlib import pyplot as plt

Step #2

df = pd.read_csv('carprices.csv')
df.head()
Top five rows of our dataset

Step #3


plt.scatter(df['Mileage'],df['Sell Price($)'])
X = df[['Mileage','Age(yrs)']]
Y = df['Sell Price($)']
X.head(10)
x_train, x_test,y_train,y_test = train_test_split(X,Y,test_size =0.2)
# print the data
x_train

Step #4

from sklearn.linear_model import LinearRegression
clf = LinearRegression()
clf.fit(x_train,y_train)
clf.predict(x_test)
clf.score(x_test,y_test)

Conclusion

Know your author

Himanshu Verma
Himanshu Verma

Written by Himanshu Verma

Visit my website http://thehimanshuverma.com/. Android & IOS Developer | Researcher | ML & Data Science Enthusiastic | Blogger | FA

Responses (2)

Write a response