一、不包含分类型变量
from numpy import genfromtxt
import numpy as np
from sklearn import datasets,linear_model
path=r‘D:\daacheng\Python\PythonCode\machineLearning\Delivery.csv‘
data=genfromtxt(path,delimiter=‘,‘)
print(data)
x=data[:,:-1]
y=data[:,-1]
regr=linear_model.LinearRegression()#创建模型
regr.fit(x,y)
#y=b0+b1*x1+b2*x2
print(regr.coef_)#b1,b2
print(regr.intercept_)#b0
Xpred=[[102,6]]
Ypred=regr.predict(Xpred)#预测
print(Ypred)
二、包含分类型变量
转换后:
import numpy as np
from sklearn import datasets,linear_model
from numpy import genfromtxt
path=r‘D:\daacheng\Python\PythonCode\machineLearning\Delivery_Dummy.csv‘
data=genfromtxt(path,delimiter=‘,‘)
data=data[1:]
x=data[:,:-1]
y=data[:,-1]
print(x)
print(y)
regr=linear_model.LinearRegression()
regr.fit(x,y)
print(regr.coef_)#b1,b2,b3,b4,b5
print(regr.intercept_)#b0
原文地址:https://www.cnblogs.com/daacheng/p/8111597.html