Research breakthrough possible @S-Logix

Office Address

  • 2nd Floor, #7a, High School Road, Secretariat Colony Ambattur, Chennai-600053 (Landmark: SRM School) Tamil Nadu, India
  • +91- 81240 01111

Social List

How to split data set into train and test in simple linear regression using python?


To split data set into train and test in a simple linear regression model in python.


  Import regression model from scikit-learn library.

  Import train_test_split from scikit-learn library.

  Load the sample data set.

  Define independent(X) and dependent(Y) variable.

  Define the training data set from data sample.

Sample Code

#import needed library

import matplotlib.pyplot as plt

import pandas as pd

import numpy as np

#import linear model and train_test_split

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LinearRegression

#load the sample data







3000,1800,1400,850,250,750, 1000]}

#create the data frame


print(“Actual Data frame is:\n”,df)

#Define x(independent variable)

X = df.iloc[:, :1].values

print(“Value of X is\n”,X)

#Define y(dependent variable)

y = df.iloc[:,2].values

print(“Value of y is\n”,y)

#declare the train data set

X_train, X_test, y_train, y_test = train_test_split

(X, y, test_size=0.3, random_state=0)

#build linear model

regressor = LinearRegression()

#pass the X-train and y_train, y_train)

print(“X-Train data is”,X_train)

print(“Y-Train data is”,y_train)

print(“Regression intercept is”,regressor.intercept_)

print(“Regression coefficient is”,regressor.coef_)

split data set into train and test in simple linear regression using python
import pandas as pd
import needed library
Load the dataset