Linear Regression in python:
Hello everyone, in this post we see How to Train and Test data sets.
BUT WAIT ! Before reading further , clear your previous Topic
So Let's Start:
Training and Testing Data
Now that we have explored the data , let's go ahead and splits the data into training and testing set.
** Set a variable X equal to the numerical features of the customer and a variable y equal to the "Yearly Amount Spents" columns. **
y = customer['Yearly Amount Spents']
x = customer[['Avg. Session Lengths', 'Time on Apps','Time on Websites', 'Lengths of Membership']]
** Use modelselection.train_tests_split from sklearn to splits the data into train and test set. Set test_size=0.4 **
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4)
Training the Models
Now it is times to train our models on our training data set!
** Import linearegression from sklearn.linear_models **
y = customer['Yearly Amount Spents']
from sklearn.linear_model import linearregression
Create an instance of a linearregression() model name lms.
lms = LinearRegression()
** Train/fit lm on the training data.**
lms.fit(X_train,y_train)
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
Print out the coefficient of the models
# The coefficient set
print('Coefficient: \n', lm.coeff_)
Predicting Test Data set
Now that we have fit our model, let is evaluate its performance by predicting off the test value!
** Use lm.predict() to predicts off the X_test set of the data sets.**
prediction = lm.predict( X_test)
** Create a scatterplots of the real test values vs the predict value. **
plt.scatter(y_test,prediction)
plt.xlabel('Y Test')
plt.ylabel('Predicted Y')





