forked from lawlite19/MachineLearning_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearRegression_scikit-learn.py
executable file
·43 lines (32 loc) · 1.28 KB
/
LinearRegression_scikit-learn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import numpy as np
from sklearn import linear_model
from sklearn.preprocessing import StandardScaler #引入归一化的包
from IPython import embed
def linearRegression():
print(u"loading data.txt...\n")
data = loadtxtAndcsv_data("data.txt",",",np.float64) #读取数据
X = np.array(data[:,0:-1],dtype=np.float64) # X对应0到倒数第2列
y = np.array(data[:,-1],dtype=np.float64) # y对应最后一列
# 归一化操作
scaler = StandardScaler()
scaler.fit(X)
x_train = scaler.transform(X)
x_test = scaler.transform(np.array([[1650,3]]))
# 线性模型拟合
model = linear_model.LinearRegression()
model.fit(x_train, y)
#预测结果
result = model.predict(x_test)
print(model.coef_) # Coefficient of the features 决策函数中的特征系数
print(model.intercept_) # 又名bias偏置,若设置为False,则为0
print(result) # 预测结果
# 加载txt和csv文件
def loadtxtAndcsv_data(fileName,split,dataType):
return np.loadtxt(fileName,delimiter=split,dtype=dataType)
# 加载npy文件
def loadnpy_data(fileName):
return np.load(fileName)
if __name__ == "__main__":
linearRegression()