匯入資料分析工具
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
匯入一組資料
x=np.
array([
1,2,
3,4,
5])y=np.
array([
1,3,
2,3,5])
畫出散點圖
求平均值
x_mean=np.
mean
(x)y_mean=np.
mean
(y)
求函式
num=
0#分子
d=0#分母
for x_i,y_i in
zip(x,y)
: num+=
(x_i-x_mean)
*(y_i-y_mean)
d+=(x_i-x_mean)**2
a=num/d
b=y_mean-a*x_mean
y_hat=a*x+b
畫出線性回歸影象
復現函式
class
******linearregression1
: def _init_
(self)
: self.a_ = none
self.b_ = none
def fit
(self,x_train,y_train)
: assert x_train.ndim ==1,\
"****** linear regressor can only solve single feature training data."
assert len
(x_train)
==len
(y_train)
,\ "the size of x_train must be equal to the size of y_train"
x_mean=np.
mean
(x_train)
y_mean=np.
mean
(y_train)
num=
0#分子
d=0#分母
for x,y in
zip(x_train,y_train)
: num+=
(x-x_mean)
*(y-y_mean)
d+=(x-x_mean)**2
self.a_ =num/d
self.b_ =y_mean-self.a_*x_mean
return self
def predict
(self, x_predict)
: assert x_predict.ndim ==1,\
"****** linear regressor can only solve single feature training data."
assert self.a_ is not none and self.b_ is not none,\
"must fit before predict!"
return np.
array
([self.
_predict
(x)for x in x_predict]
)
def _predict
(self,x_single)
:return self.a_*x_single+self.b_
def _repr_
(self)
:return
"******linearregression1()"
呼叫函式
reg1=
******linearregression1()
reg1.
fit(x,y)
plt.
scatter
(x,y)
plt.
plot
(x,y_hat1,color=
'red'
)plt.
axis([
0,6,
0,6]
)plt.
show
()
畫出線性回歸影象
簡單線性回歸
真實值 y theta x varepsilon 值 hat theta x varepsilon 為誤差 項,服從 均值為0 方差為 為誤差項,服從均值為0,方差為 為誤差項,服 從均值為 0,方差 為 sigma 的高斯分布。已知若干樣本,可以得到若干 varepsilon 值,根 據極大似 然...
簡單線性回歸
資料預處理 data student data 1 刪除缺失值 lm data na.omit data 散點圖 plot height,weight,data data,main scatter plot col 1,pch col為顏色,pch為形狀 箱線圖 boxplot height wei...
2 1 簡單線性回歸
使用一種基於自變數 x 來 因變數 y 的方法,假設這兩個變數是線性相關的,因此我們嘗試尋找一種根據特徵或自變數 x 的線性函式來精確 響應值 y import pandas as pd import numpy as np import matplotlib.pyplot as plt datas...