Created
March 13, 2019 03:22
-
-
Save sword-jin/c6c7ddda80927e4f898c1a46d663b946 to your computer and use it in GitHub Desktop.
多元回归
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=utf-8 | |
# @Author: yangenneng | |
# @Time: 2018-01-17 15:42 | |
# @Abstract:多元线性回归(Multiple Regression)算法 | |
from numpy import genfromtxt | |
import numpy as np | |
from sklearn import linear_model | |
datapath=r"D:\Python\PyCharm-WorkSpace\MachineLearningDemo\MultipleRegression\data\data.csv" | |
#从文本文件中提取数据并转为numpy Array格式 | |
deliveryData = genfromtxt(datapath,delimiter=',') | |
print "data" | |
# print deliveryData | |
# 读取自变量X1(运送英里数),X2(运送次数) | |
x= deliveryData[1:,1:-1] | |
# 读取因变量(运送时间) | |
y = deliveryData[1:,-1] | |
print "x:",x | |
print "y:",y | |
# 调用线性回归模型 | |
lr = linear_model.LinearRegression() | |
# 装配数据 | |
lr.fit(x, y) | |
print lr | |
print("coefficients:") | |
print lr.coef_ | |
print("intercept:") | |
print lr.intercept_ | |
#预测 | |
xPredict = [102,6] | |
yPredict = lr.predict(xPredict) | |
print("predict:") | |
print yPredict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment