import xlrd from numpy.linalg import inv import numpy as np data = xlrd.open_workbook(u‘C:\\Users\\xd/Desktop\\作业\\新建文件夹\\线性代数\\SpeedVideoDataforModeling.xlsx‘) table = data.sheet_by_index(0) x1 = np.array(table.col_values(17)[1:]) x2 = np.array(table.col_values(18)[1:]) x3 = np.array(table.col_values(19)[1:]) y1 = np.array(table.col_values(20)[1:]) y2 = np.array(table.col_values(21)[1:]) x0 = np.array([1 for i in x1]) X = np.vstack((x0,x1,x2,x3)).T ############最小二乘法多元线性回归####################### coef1 = np.dot(np.dot(inv(np.dot(X.T,X)),X.T),y1.T) coef2 = np.dot(np.dot(inv(np.dot(X.T,X)),X.T),y2.T) from sklearn.linear_model import LinearRegression model = LinearRegression() model.fit(X, y1) print model.coef_ model.fit(X, y2) print model.coef_ ##############小的实例======================== x = [[1,1,1], [1,1,2], [1,2,1]] y = [[6], [9], [8]] model = LinearRegression() model.fit(x, y)
时间: 2024-10-25 04:35:14