Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8347269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:18:41+00:00 2026-06-09T07:18:41+00:00

How do you calculate a best fit line in python, and then plot it

  • 0

How do you calculate a best fit line in python, and then plot it on a scatterplot in matplotlib?

I was I calculate the linear best-fit line using Ordinary Least Squares Regression as follows:

from sklearn import linear_model
clf = linear_model.LinearRegression()
x = [[t.x1,t.x2,t.x3,t.x4,t.x5] for t in self.trainingTexts]
y = [t.human_rating for t in self.trainingTexts]
clf.fit(x,y)
regress_coefs = clf.coef_
regress_intercept = clf.intercept_      

This is multivariate (there are many x-values for each case). So, X is a list of lists, and y is a single list.
For example:

x = [[1,2,3,4,5], [2,2,4,4,5], [2,2,4,4,1]] 
y = [1,2,3,4,5]

But how do I do this with higher order polynomial functions. For example, not just linear (x to the power of M=1), but binomial (x to the power of M=2), quadratics (x to the power of M=4), and so on. For example, how to I get the best fit curves from the following?

Extracted from Christopher Bishops’s “Pattern Recognition and Machine Learning”, p.7:

Extracted from Christopher Bishops's "Pattern Recognition and Machine Learning", p.7

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:18:43+00:00Added an answer on June 9, 2026 at 7:18 am

    The accepted answer to this question
    provides a small multi poly fit library which will do exactly what you need using numpy, and you can plug the result into the plotting as I’ve outlined below.

    You would just pass in your arrays of x and y points and the degree(order) of fit you require into multipolyfit. This returns the coefficients which you can then use for plotting using numpy’s polyval.

    Note: The code below has been amended to do multivariate fitting, but the plot image was part of the earlier, non-multivariate answer.

    import numpy
    import matplotlib.pyplot as plt
    import multipolyfit as mpf
    
    data = [[1,1],[4,3],[8,3],[11,4],[10,7],[15,11],[16,12]]
    x, y = zip(*data)
    plt.plot(x, y, 'kx')
    
    stacked_x = numpy.array([x,x+1,x-1])
    coeffs = mpf(stacked_x, y, deg) 
    x2 = numpy.arange(min(x)-1, max(x)+1, .01) #use more points for a smoother plot
    y2 = numpy.polyval(coeffs, x2) #Evaluates the polynomial for each x2 value
    plt.plot(x2, y2, label="deg=3")
    

    enter image description here


    Note: This was part of the answer earlier on, it is still relevant if you don’t have multivariate data. Instead of coeffs = mpf(..., use coeffs = numpy.polyfit(x,y,3)

    For non-multivariate data sets, the easiest way to do this is probably with numpy’s polyfit:

    numpy.polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False)

    Least squares polynomial fit.

    Fit a polynomial p(x) = p[0] * x**deg + ... + p[deg] of degree deg to points (x, y). Returns a vector of coefficients p that minimises the squared error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to calculate the number of lines of best fit for an ellipse;
What is the best way to get timestamps in iOS and then calculate time
I would like to calculate two different lines of best fit for 2 parts
I'm trying to figure out the best way to calculate which day is n
Given two dates, what is the best method to calculate the number of days
Would anyone have any ideas of how to best calculate the amount of days
I'm trying to figure out how to best pre-calculate some sin and cosine values,
What's the best way to calculate a time difference in C++? I'm timing the
What's the best way to calculate a geometric progression in Ruby? Are there any
What regex (or other technique) would help calculate or 'best-guess' the artist in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.