Excersize

From Ivan Kirilenkov, 11 Months ago, written in Python, viewed 432 times.
URL http://codebin.org/view/129078dd Embed
Download Paste or View Raw
  1. import numpy as np
  2. import pandas as pd
  3. from sklearn.metrics import r2_score
  4.  
  5.  
  6. data_train = pd.read_csv('/datasets/train_data_n.csv')
  7. features_train = data_train.drop(['target'], axis=1)
  8. target_train = data_train['target']
  9.  
  10. data_test = pd.read_csv('/datasets/test_data_n.csv')
  11. features_test = data_test.drop(['target'], axis=1)
  12. target_test = data_test['target']
  13.  
  14.  
  15. class SGDLinearRegression:
  16.     def __init__(self, step_size, epochs, batch_size, reg_weight):
  17.         self.step_size = step_size
  18.         self.epochs = epochs
  19.         self.batch_size = batch_size
  20.         self.reg_weight = reg_weight
  21.    
  22.     def fit(self, train_features, train_target):
  23.         X = np.concatenate((np.ones((train_features.shape[0], 1)), train_features), axis=1)        
  24.         y = train_target
  25.         w = np.zeros(X.shape[1])
  26.        
  27.         for _ in range(self.epochs):
  28.             batches_count = X.shape[0] // self.batch_size
  29.             for i in range(batches_count):
  30.                 begin = i * self.batch_size
  31.                 end = (i + 1) * self.batch_size
  32.                 X_batch = X[begin:end, :]
  33.                 y_batch = y[begin:end]
  34.                
  35.                 gradient = 2 * X_batch.T.dot(X_batch.dot(w) - y_batch) / X_batch.shape[0]
  36.                                                                 # копируем вектор w, чтобы его не менять
  37.                 reg = 2 * w.copy()
  38.                 # < напишите код здесь >
  39.                 gradient += self.reg_weight * reg
  40.                
  41.                 w -= self.step_size * gradient
  42.  
  43.         self.w = w[1:]
  44.         self.w0 = w[0]
  45.  
  46.     def predict(self, test_features):
  47.         return test_features.dot(self.w) + self.w0
  48.  
  49. # Чтобы сравнить гребневую регрессию с линейной, начнём с
  50. # веса регуляризации, равного 0. Затем добавим
  51. # обучение с его различными значениями.
  52. print("Регуляризация:", 0.0)
  53. model = SGDLinearRegression(0.01, 10, 100, 0.0)
  54. model.fit(features_train, target_train)
  55. pred_train = model.predict(features_train)
  56. pred_test = model.predict(features_test)
  57. print(r2_score(target_train, pred_train).round(5))
  58. print(r2_score(target_test, pred_test).round(5))
  59.  
  60. print("Регуляризация:", 0.1)
  61. model = SGDLinearRegression(0.01, 10, 100, 0.1)
  62. model.fit(features_train, target_train)
  63. pred_train = model.predict(features_train)
  64. pred_test = model.predict(features_test)
  65. print(r2_score(target_train, pred_train).round(5))
  66. print(r2_score(target_test, pred_test).round(5))
  67.  
  68. print("Регуляризация:", 1.0)
  69. model = SGDLinearRegression(0.01, 10, 100, 1.0)
  70. model.fit(features_train, target_train)
  71. pred_train = model.predict(features_train)
  72. pred_test = model.predict(features_test)
  73. print(r2_score(target_train, pred_train).round(5))
  74. print(r2_score(target_test, pred_test).round(5))
  75.  
  76. print("Регуляризация:", 10.0)
  77. model = SGDLinearRegression(0.01, 10, 100, 10.0)
  78. model.fit(features_train, target_train)
  79. pred_train = model.predict(features_train)
  80. pred_test = model.predict(features_test)
  81. print(r2_score(target_train, pred_train).round(5))
  82. print(r2_score(target_test, pred_test).round(5))

Replies to Excersize rss

Title Name Language When
Re: Excersize Chocolate Capybara python 9 Months ago.

Reply to "Excersize"

Here you can reply to the paste above