Machine Learning–Linear Regression

Time:2019-8-13

linear regression

Linear RegressionIt is a statistical analysis method to determine the interdependent quantitative relationship between two or more variables by using regression analysis in mathematical statistics.

Core Ideas

Machine Learning--Linear Regression

Sklearn Document Overview

Machine Learning--Linear Regression

Simple Linear Regression (Univariate Linear Regression)

Find a straight line, the minimum sum of the distance from the line to each point ==> least square method (junior high school)
Machine Learning--Linear Regression

import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
### Distance km
x = [3.4, 1.8, 4.6, 2.3, 3.1, 5.5, 0.7, 3.0, 2.6, 4.3, 2.1, 1.1, 6.1, 4.8, 3.8]
### Price
y = [26.2, 17.8, 31.3, 23.1, 27.5, 36.0, 14.1, 22.3, 19.6, 31.3, 24.0, 17.3, 43.2, 36.4, 26.1]
x_in = np.array(x).reshape(-1,1)
y_in = np.array(y).reshape(-1,1)
lreg = LinearRegression()
result = lreg.fit(x_in, y_in)
b1 = result.coef_[0][0]
b0 = result.intercept_[0]
print(b1)
print(b0)
y_prd = lreg.predict(x_in)
# scatter plot
plt.scatter(x, y)
plt.plot(x_in,y_prd,'m')
plt.xlabel("km")
plt.ylabel("rmb")
plt.title("price")
plt.show()

multiple linear regression

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from sklearn.linear_model import LinearRegression

x = [[100,4],[50,3],[100,4],[100,2],[50,2],[80,2],[75,3],[65,4],[90,3],[90,2]]
y = [9.3,4.8,8.9,6.5,4.2,6.2,7.4,6.0,7.6,6.1]

x = np.array(x)
y = np.array(y)
lreg = LinearRegression()
result = lreg.fit(x, y)
# Weight value
b1 = result.coef_[0][0]
# Bias value
b0 = result.intercept_[0]
print(b1)
print(b0)

Recommended Today

Implementation of PHP Facades

Example <?php class RealRoute{ public function get(){ Echo’Get me’; } } class Facade{ public static $resolvedInstance; public static $app; public static function __callStatic($method,$args){ $instance = static::getFacadeRoot(); if(!$instance){ throw new RuntimeException(‘A facade root has not been set.’); } return $instance->$method(…$args); } // Get the Facade root object public static function getFacadeRoot() { return static::resolveFacadeInstance(static::getFacadeAccessor()); } protected […]