DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Revolutionizing Stock Trading With AI and ML: Opportunities and Challenges
  • Gradient Descent vs Normal Equation for Regression Problems
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Data Privacy and Security: A Developer's Guide to Handling Sensitive Data With DuckDB

Trending

  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  1. DZone
  2. Data Engineering
  3. Data
  4. Linear Regression Using Python scikit-learn

Linear Regression Using Python scikit-learn

Let's say you have some people's height and weight data. Can you use it to predict other people's weight? Find out using Python scikit-learn.

By 
Vinay Kumar user avatar
Vinay Kumar
·
Nov. 14, 17 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
24.4K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, I am going to explain how to use scikit-learn/sk-learn, a machine learning package in Python, to do linear regression for a set of data points.

Below is a video tutorial on this:

 I am not going to explain training data, testing data, and model evaluation concepts here, but they are important.

We know that the equation of a line is given by y=mx+b, where m is the slope and b is the intercept.

Our goal is to find the best values of slope (m) and intercept (b) to fit our data.

Linear regression uses the ordinary least squares method to fit our data points.

import statement:

from sklearn import linear_model

I have the height and weight data of some people. Let's use this data to do linear regression and try to predict the weight of other people.

height=[[4.0],[4.5],[5.0],[5.2],[5.4],[5.8],[6.1],[6.2],[6.4],[6.8]]
weight=[  42 ,  44 , 49, 55  , 53  , 58   , 60  , 64  ,  66 ,  69]

print("height weight")
for row in zip(height, weight):
    print(row[0][0],"->",row[1])

Output:

height weight
4.0 -> 42
4.5 -> 44
5.0 -> 49
5.2 -> 55
5.4 -> 53
5.8 -> 58
6.1 -> 60
6.2 -> 64
6.4 -> 66
6.8 -> 69

import statement to plot graph using matplotlib:

import matplotlib.pyplot as plt

Plotting the height and weight data:

plt.scatter(height,weight,color='black')
plt.xlabel("height")
plt.ylabel("weight")

Output:

Image title

Declaring the linear regression function and call the fit method to learn from data:

reg=linear_model.LinearRegression()
reg.fit(height,weight)

Slope and intercept:

m=reg.coef_[0]
b=reg.intercept_
print("slope=",m, "intercept=",b)

Output:

slope= 10.1936218679 intercept= -0.4726651480
Using the values of slope and intercept to construct the line to fit our data points:
plt.scatter(height,weight,color='black')
predicted_values = [reg.coef_ * i + reg.intercept_ for i in height]
plt.plot(height, predicted_values, 'b')
plt.xlabel("height")
plt.ylabel("weight")
Output:
Image title

And that's it!

Linear regression in python scikit learn | Quick KT

Linear regression Scikit-learn Python (language) Data (computing)

Published at DZone with permission of Vinay Kumar. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Revolutionizing Stock Trading With AI and ML: Opportunities and Challenges
  • Gradient Descent vs Normal Equation for Regression Problems
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Data Privacy and Security: A Developer's Guide to Handling Sensitive Data With DuckDB

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: