Multiple_LinearRegression_Test2
1 import csv 2 import numpy as np 3 from sklearn import datasets,linear_model 4 5 with open("car_2.1.csv") as f: 6 car_data = list(csv.reader(f)) #转换为list 7 data_X = [row[:5] for row in car_data[:-1]] #变量x 8 data_Y = [row[-1] for row in car_data[:-1]] #值y 9 xPred = car_data[-1] #测试数据 10 f.close() 11 regression = linear_model.LinearRegression() #调用回归函数 12 regression.fit(data_X,data_Y) 13 xPred = np.array(xPred[:-1],dtype=float) #去掉最后的y值,并转换为数组类型 14 print(regression.coef_) #各个变量前的系数 15 print(regression.intercept_) #获取截距 16 17 #注意:reshape(1,-1)是为了让矩阵能够对齐 18 yPred = regression.predict(xPred.reshape(1, -1)) #测试数据 19 print("结果为:",yPred)
新手入门-解决csv文件中存在类型变量的问题代码
car_2.1.csv文件地址:链接:https://pan.baidu.com/s/1pMtZHCB 密码:wrce
谢谢观看!