Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Simple Linear Regression explained so simply that even a 5yo can understand
Shubhra Agarwal
Shubhra Agarwal

Posted on

     

Simple Linear Regression explained so simply that even a 5yo can understand

Linear regression is an algorithm used to predict or visualise a relationship between two different features/variables. In linear regression tasks, there are two kinds of variables being examined: the dependent variable and the independent variable.

Let us build our first Simple Linear Regression Model and learn along the way by building.

This particular model is called as simple because it has only one independent variable.

Let's start by importing the modules

importmatplotlib.pyplotaspltimportpandasaspdfromsklearn.model_selectionimporttrain_test_splitfromsklearn.linear_modelimportLinearRegression
Enter fullscreen modeExit fullscreen mode
  • matplotlib: used to plot the data in a graphical manner
  • pandas: used for working with the dataset
  • sklearn: used to split the dataset and then apply the linear regression class onto the data.

Importing the dataset

dataset=pd.read_csv("Salary_Data.csv")X=dataset.iloc[:,:-1].valuesy=dataset.iloc[:,-1].values# X is taking all the values except the last # column whereas y is taking the last value
Enter fullscreen modeExit fullscreen mode

Here we are using the data containing people's salary and working experience to predict someone's salary based on their experience.

This is what the dataset looks like
image

Splitting the dataset into training and test set

We need to split the dataset into two models i.e. the test set and the training set.

X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=0)# X_train contains the independent varibale# y_train contains the dependent variable
Enter fullscreen modeExit fullscreen mode

Here we have used thetrain_test_split function that we imported fromsklearn.model_selection.
x and y are the variables, test_size tells the function about the size of the test set.

So, if there exists 100 lines of data, It will be split into following segments,

  • Training: 80 lines
  • Testing: 20 lines

Training the model

After we are done with splitting the model, now is the time to actually train the model with the training set.

regressor=LinearRegression()regressor.fit(X_train,y_train)
Enter fullscreen modeExit fullscreen mode

We simply initialise the LinearRegression class and then pass our training sets into thefit() method of the LinearRegression Class

Visualising the training set results

plt.scatter(X_train,y_train,color="red")# Plotting the dataplt.plot(X_train,regressor.predict(X_train),color="blue")# Add title to the plotplt.title("Salary vs Experience(train)")# Labels on x and y axisplt.xlabel("Years of Experience")plt.ylabel("Salary")#Finally, display the plotplt.show()
Enter fullscreen modeExit fullscreen mode

The output of the following code block will be

image

Visualising the test set results

plt.scatter(X_test,y_test,color="red")# Here we are not replacing X_train with X_test because this line tells us about the data predicted and how close our results are to the training setplt.plot(X_train,regressor.predict(X_train),color="blue")# Add title and labelsplt.title("Salary vs Experience (test)")plt.xlabel("Years of Experience")plt.ylabel("Salary")# Finally, display the plotplt.show()
Enter fullscreen modeExit fullscreen mode

The output of the following code block will be

image

This was it!! We have successfully built our fully functional simple linear regression model.

If you liked this, don't forget to give a like on the post and share it with your friends.

Do give me a follow for more such blog posts.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Started as tech enthusiast, later developed keen interest in programming. I have always been interested in learning new technical things, solving problems.
  • Location
    Bengaluru, Karnataka
  • Work
    Frontend Developer at Swivl
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp