forked from lawlite19/MachineLearning_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#-*- coding: utf-8 -*- | ||
import numpy as np | ||
from sklearn import linear_model | ||
from sklearn.preprocessing import StandardScaler #引入归一化的包 | ||
|
||
def linearRegression(): | ||
print u"加载数据...\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() |