多元线性回归python代码实现:
from numpy import genfromtxt
import numpy as np
from sklearn import datasets, linear_model
dataPath = r"D:\eclipse\mars\project\DeepLearningBasicsMachineLearning\Datasets\Delivery.txt"
deliveryData = genfromtxt(dataPath,delimiter='\t')
print ("data")
print (deliveryData)
X = deliveryData[:, :-1]
Y = deliveryData[:, -1]
print ("X:")
print (X)
print ("Y: ")
print (Y)
regr = linear_model.LinearRegression()
regr.fit(X, Y)
print ("coefficients")
print (regr.coef_)
print ("intercept: ")
print (regr.intercept_)
xPred = [102, 6]
yPred = regr.predict(xPred)
print ("predicted y: ")
print (yPred)
运行结果:
data:
[[ 100. 4. 9.3]
[ 50. 3. 4.8]
[ 100. 4. 8.9]
[ 100. 2. 6.5]
[ 50. 2. 4.2]
[ 80. 2. 6.2]
[ 75. 3. 7.4]
[ 65. 4. 6. ]
[ 90. 3. 7.6]
[ 90. 2. 6.1]]
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. 7.6 6.1]
coefficients:
[ 0.0611346 0.92342537]
intercept:
-0.868701466782
predicted y:
C:\Program Files\Anaconda3\lib\site-packages\sklearn\utils\validation.py:386: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
DeprecationWarning)
[ 10.90757981]