Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. # coding=utf-8
  2. import numpy as np
  3. import pandas as pd
  4. import math
  5. import matplotlib.pyplot as plt
  6. from sklearn import datasets, linear_model
  7. from sklearn.metrics import mean_squared_error, r2_score
  8. from sklearn import preprocessing
  9. from sklearn.pipeline import Pipeline
  10. from sklearn.preprocessing import PolynomialFeatures
  11.  
  12.  
  13. data = pd.read_csv('ticdata20001.txt', sep=" ", header=None, engine='python')
  14. data.columns = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"]
  15.  
  16. y = data.loc[:, ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"]]
  17. x = data.loc[:, ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"] ]
  18. all_rows = len(data.index)
  19. train_data = int(math.floor(all_rows * 0.5))
  20. test_data = int(math.floor(all_rows * 0.5))
  21.  
  22. x_train = x[:train_data]
  23. x_test = x[train_data:]
  24.  
  25. y_train = y.iloc[:train_data]
  26. y_test = y.iloc[train_data:]
  27.  
  28. x_train = preprocessing.scale(x_train)
  29. x_test = preprocessing.scale(x_test)
  30.  
  31. regr = linear_model.LinearRegression()
  32. # передаем тренировочную выборку в качестве параметра
  33. regr.fit(x_train, y_train)
  34. # предсказываем значение по модели
  35. y_pred = regr.predict(x_test)
  36.  
  37.  
  38. print('Coefficients: \n', regr.coef_)
  39. print("Mean squared error: %.100f" % mean_squared_error(y_test, y_pred))
  40. print('Variance score: %.2f' % r2_score(y_test, y_pred))
  41.  
  42. print('\nPOLINOM\n')
  43.  
  44. # степень полинома в массиве
  45. d = [1, 2, 3, 4, 5]
  46. varScr = []
  47. for i in d:
  48. polynomial_features = PolynomialFeatures(degree=i, include_bias=False)
  49. # построение полимиальной функции
  50. pipeline = Pipeline([("polynomial_features", polynomial_features), ("linear_regression", regr)])
  51. # расчет модели
  52. pipeline.fit(np.array(x_train), np.array(y_train))
  53. # получаем предсказанные значения на основе тестовой выборки
  54. y_pred = pipeline.predict(np.array(x_test))
  55. # сравнение тестовых и предсказанных значений
  56. r2 = r2_score(y_test, y_pred)
  57. varScr.append(r2)
  58. print("Mean squared error: %.100f" % mean_squared_error(y_test, y_pred))
  59. print('Variance score: %.2f' % r2)
  60.  
  61.  
  62. fig = plt.figure()
  63. plt.plot(d, varScr)
  64. plt.xlabel('Degree polinome', fontsize=15)
  65. plt.ylabel('Variance score', fontsize=15)
  66. plt.show()
  67.  
  68. print('\nLASSO\n')
  69. alphas = [0.0001, 0.001, 0.01, 0.1, 1]
  70. varScr = []
  71. for i in alphas:
  72. regr = linear_model.Lasso(alpha=i)
  73. regr.fit(x_train, y_train)
  74. y_pred = regr.predict(x_test)
  75.  
  76. r2 = r2_score(y_test, y_pred)
  77. varScr.append(r2)
  78. # The mean squared error
  79. print("Mean squared error: %.100f"
  80. % mean_squared_error(y_test, y_pred))
  81. # Explained variance score: 1 is perfect prediction
  82. print('Variance score: %.2f' % r2)
  83.  
  84. fig = plt.figure()
  85. plt.plot(alphas, varScr)
  86. plt.xlabel('Regularization coefficient (d)', fontsize=15)
  87. plt.ylabel('Variance score (varScr)', fontsize=15)
  88. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement