Skip to content

Instantly share code, notes, and snippets.

@Manikant92
Last active September 4, 2018 09:47
Show Gist options
  • Save Manikant92/56891d577e3bc9835f97563646ef61af to your computer and use it in GitHub Desktop.
Save Manikant92/56891d577e3bc9835f97563646ef61af to your computer and use it in GitHub Desktop.
#importing train_test_split class from scikit-learn module/library
from sklearn.model_selection import train_test_split
#importing Linear regression class from scikit-learn module/library
from sklearn.linear_model import LinearRegression
#Radomly shuffling the X and y columns data into training and testing data and with test size of 30 percent and training data is of 70 percent
#random_state= some number is to keep same data for train and test to evaluate our algorithm when called with same number(in our case '15')
X_train, X_test, y_train, y_test = train_test_split(df[['X']],df.y,test_size=0.3, random_state=15)
print('Training input data: ',X_train)
print('Testing input data: ',X_test)
print('Testing output data: ',y_train)
print('Testing output data: ',y_test)
#data output of train and test stored in respective variables as above
'''
Training input data: X
6 7
11 12
3 4
10 11
0 1
7 8
12 13
5 6
8 9
Testing input data: X
13 14
2 3
9 10
4 5
1 2
Training output data: 6 77
11 132
3 44
10 121
0 11
7 88
12 143
5 66
8 99
Name: y, dtype: int64
Testing output data: 13 154
2 33
9 110
4 55
1 22
'''
#load linearregression object into a variable. This is the algorithm we are training our data with.
lr = LinearRegression()
#fit our training data of both X and y. by calling fit method on lr object.
lr.fit(X_train,y_train)
#we are done with training our data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment