train.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. '''
  4. 最简单的mse
  5. '''
  6. import sys
  7. import os
  8. sys.path.append(os.path.abspath('..'))
  9. import numpy as np
  10. from sklearn.linear_model import LinearRegression
  11. from sklearn import metrics
  12. from draw import draw_util
  13. def curce_data(x,y,y_pred):
  14. x=x.tolist()
  15. y=y.tolist()
  16. y_pred=y_pred.tolist()
  17. results=zip(x,y,y_pred)
  18. results=["{},{},{}".format(s[0],s[1][0],s[2][0]) for s in results ]
  19. return results
  20. def read_data(path):
  21. with open(path) as f :
  22. lines=f.readlines()
  23. lines=[eval(line.strip()) for line in lines]
  24. X,y=zip(*lines)
  25. X=np.array(X)
  26. y=np.array(y)
  27. return X,y
  28. def demo():
  29. X_train,y_train=read_data("../bbztx/train_data")
  30. X_test,y_test=read_data("../bbztx/test_data")
  31. #一个对象,它代表的线性回归模型,它的成员变量,就已经有了w,b. 刚生成w和b的时候 是随机的
  32. model = LinearRegression()
  33. #一调用这个函数,就会不停地找合适的w和b 直到误差最小
  34. model.fit(X_train, y_train)
  35. #打印W
  36. print(model.coef_)
  37. #打印b
  38. print(model.intercept_)
  39. #模型已经训练完毕,用模型看下在训练集的表现
  40. y_pred_train = model.predict(X_train)
  41. #sklearn 求解训练集的mse
  42. # y_train 在训练集上 真实的y值
  43. # y_pred_train 通过模型预测出来的y值
  44. #计算 (y_train-y_pred_train)^2/n
  45. train_mse = metrics.mean_squared_error(y_train, y_pred_train)
  46. print("训练集MSE:", train_mse)
  47. #看下在测试集上的效果
  48. y_pred_test = model.predict(X_test)
  49. test_mse = metrics.mean_squared_error(y_test, y_pred_test)
  50. print("测试集MSE:",test_mse)
  51. # train_curve = curce_data(X_train,y_train,y_pred_train)
  52. test_curve = curce_data(X_test,y_test,y_pred_test)
  53. print("推广mse差", test_mse-train_mse)
  54. '''
  55. with open("train_curve.csv","w") as f :
  56. f.writelines("\n".join(train_curve))
  57. 保存数据
  58. with open("test_curve.csv","w") as f :
  59. f.writelines("\n".join(test_curve))
  60. '''
  61. for x in test_curve:
  62. print(x)
  63. return X_train,y_train, model.coef_, model.intercept_
  64. def draw_line():
  65. x_train, y_train = read_data("../bbztx/train_data")
  66. print(x_train.tolist())
  67. print(y_train.tolist())
  68. draw_util.drawScatter(x_train.tolist(), y_train.tolist())
  69. if __name__ == '__main__':
  70. # draw_line()
  71. p, q, w,b = demo()
  72. p = [i[0] for i in p.tolist()]
  73. q = [i[0] for i in q.tolist()]
  74. w = w[0]
  75. b = b[0]
  76. # draw_util.drawScatterAndLine(p, q, w, b)