Get the least squares solution in NumPy and scikit-learn

You can get the least-squares solution of given points by using lstsq().

import numpy
from matplotlib import pyplot

x = numpy.array([6, 7, 8, 9])
y = numpy.array([1, 2.1, 2.95, 4.02])

A = numpy.vstack([x, numpy.ones(len(x))]).T

print(A)
# [[6. 1.]
#  [7. 1.]
#  [8. 1.]
#  [9. 1.]]

m, b = numpy.linalg.lstsq(A, y, rcond=None)[0]

print(m)  # 0.9909999999999995
print(b)  # -4.914999999999996

pyplot.plot(x, y, 'o')
pyplot.plot(x, m * x + b, 'r')

pyplot.savefig('squares.jpg')

Reference: numpy.linalg.lstsq

x and y represnet dots. The lstsq() function takes the coefficient matrix (A) and values (y).

By using scikit-learn

import numpy
from matplotlib import pyplot
from sklearn import linear_model

x = numpy.array([6, 7, 8, 9])
x_ = x.reshape((4, 1))
y = numpy.array([1, 2.1, 2.95, 4.02])

fit = linear_model.LinearRegression().fit(x_, y)

m_ = fit.coef_
m = m_[0]
b = fit.intercept_

print(m_)  # [0.991]
print(m)  # 0.9909999999999997
print(b)  # -4.914999999999997

pyplot.plot(x, y, 'o')
pyplot.plot(x, m * x + b, 'r')

pyplot.savefig('squares_2.jpg')

You can get the least-squares solution by using scikit-learn's linear_model. The object the fit() returns has coef_ (slope) and intercept_ (intercept).

NumPy Statistics

NumPy Tutorial