-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_regression_test.py
More file actions
64 lines (57 loc) · 1.61 KB
/
Copy pathlinear_regression_test.py
File metadata and controls
64 lines (57 loc) · 1.61 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#coding:UTF-8
import numpy as np
def load_data(file_path):
'''导入测试数据
input: file_path(string):训练数据
output: feature(mat):特征
'''
f = open(file_path)
feature = []
for line in f.readlines():
feature_tmp = []
lines = line.strip().split("\t")
feature_tmp.append(1) # x0
for i in xrange(len(lines)):
feature_tmp.append(float(lines[i]))
feature.append(feature_tmp)
f.close()
return np.mat(feature)
def load_model(model_file):
'''导入模型
input: model_file(string):线性回归模型
output: w(mat):权重值
'''
w = []
f = open(model_file)
for line in f.readlines():
w.append(float(line.strip()))
f.close()
return np.mat(w).T
def get_prediction(data, w):
'''得到预测值
input: data(mat):测试数据
w(mat):权重值
output: 最终的预测
'''
return data * w
def save_predict(file_name, predict):
'''保存最终的预测值
input: file_name(string):需要保存的文件名
predict(mat):对测试数据的预测值
'''
m = np.shape(predict)[0]
result = []
for i in xrange(m):
result.append(str(predict[i,0]))
f = open(file_name, "w")
f.write("\n".join(result))
f.close()
if __name__ == "__main__":
# 1、导入测试数据
testData = load_data("data_test.txt")
# 2、导入线性回归模型
w = load_model("weights")
# 3、得到预测结果
predict = get_prediction(testData, w)
# 4、保存最终的结果
save_predict("predict_result", predict)