回归写完了,但分析还没写完,只计算方差,其它的乱西八糟的检验以后用到了再写,想补充的小伙伴可以评论区留言,我把你的加上去
import matplotlib.pyplot as plt
# plt.style.use('ggplot')
from matplotlib.font_manager import FontProperties # 解决中文字符显示不全
import numpy as np
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=12)
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]
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 = np.array(x)
y = np.array(y)
class linearRegression:
def __init__(self):
self.X = None
self.y = None
self.wb = None # 权重 偏置
self.w = None # 权重
self.b = None # 偏置
def fit(self, X=None, y=None):
self.X = np.array(X)
self.y = np.array(y)
if len(self.X.shape) == 1: # 若为单自变量
self.X.resize((len(self.X),1)) # 转为一列
self.y.resize((len(self.y),1)) # 转为一列
self.X = np.concatenate( [self.X, np.ones((len(self.X), 1))],axis=1 ) # 加一列常数项1
# print(self.X )
# self.X.transpose() # 自转置
matmul_XX = np.matmul(self.X.T, self.X)
matmul_Xy = np.matmul(self.X.T, self.y)
self.wb = np.matmul(np.linalg.inv(matmul_XX), matmul_Xy)
# print(self.wb)
self.w = self.wb.T[0,0:-1]
self.b = self.wb[-1,0]
def predict(self,X=None):
temp_X = np.array(X)
if len(temp_X.shape) == 1:
temp_X.resize((1,len(temp_X))) # 加一维[temp_X]
y_pred = temp_X * self.w + self.b
# print(y_pred)
return y_pred.flat
def getR_2(X, y, y_pred): # 获取R^2
Sr = sum((y_pred - np.mean(y))**2)
Se = sum((y - y_pred)**2)
St = Sr + Se
R_square = Sr / St # 相关性系数R^2
print(R_square)
return R_square
regression = linearRegression()
regression.fit(x,y)
y_pred = regression.predict(x)
R_square = getR_2(x,y,y_pred)
plt.title('回归方差:{}'.format(R_square),fontproperties = font)
plt.scatter(x, y)
plt.plot(x, y_pred)
plt.show()
Python_一元线性回归及回归显著性
数理统计知识整理——回归分析与方差分析