My LinearRegression v2

From SeleznevAlex, 2 Months ago, written in Python, viewed 168 times.
URL http://codebin.org/view/7799cf2a 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. columns = ['комнаты', 'площадь', 'кухня', 'пл. жилая', 'этаж', 'всего этажей', 'цена']
  6.  
  7. data = pd.DataFrame([
  8.     [1, 38.5, 6.9, 18.9, 3, 5, 4200000],
  9.     [1, 38.0, 8.5, 19.2, 9, 17, 3500000],
  10.     [1, 34.7, 10.3, 19.8, 1, 9, 5100000],
  11.     [1, 45.9, 11.1, 17.5, 11, 23, 6300000],
  12.     [1, 42.4, 10.0, 19.9, 6, 14, 5900000],
  13.     [1, 46.0, 10.2, 20.5, 3, 12, 8100000],
  14.     [2, 77.7, 13.2, 39.3, 3, 17, 7400000],
  15.     [2, 69.8, 11.1, 31.4, 12, 23, 7200000],
  16.     [2, 78.2, 19.4, 33.2, 4, 9, 6800000],
  17.     [2, 55.5, 7.8, 29.6, 1, 25, 9300000],
  18.     [2, 74.3, 16.0, 34.2, 14, 17, 10600000],
  19.     [2, 78.3, 12.3, 42.6, 23, 23, 8500000],
  20.     [2, 74.0, 18.1, 49.0, 8, 9, 6000000],
  21.     [2, 91.4, 20.1, 60.4, 2, 10, 7200000],
  22.     [3, 85.0, 17.8, 56.1, 14, 14, 12500000],
  23.     [3, 79.8, 9.8, 44.8, 9, 10, 13200000],
  24.     [3, 72.0, 10.2, 37.3, 7, 9, 15100000],
  25.     [3, 95.3, 11.0, 51.5, 15, 23, 9800000],
  26.     [3, 69.3, 8.5, 39.3, 4, 9, 11400000],
  27.     [3, 89.8, 11.2, 58.2, 24, 25, 16300000],
  28. ], columns=columns)
  29.  
  30. features = data.drop('цена', axis=1)
  31. target = data['цена']
  32.  
  33. class LinearRegression:
  34.     def fit(self, train_features, train_target):
  35.         self.w = np.zeros(train_features.shape[0])
  36.         self.w0 = train_target.mean()
  37.        
  38.     def predict(self, test_features):
  39.         return test_features.dot(self.w) + self.w0
  40.    
  41. model = import numpy as np
  42. import pandas as pd
  43. from sklearn.metrics import r2_score
  44.  
  45. columns = ['комнаты', 'площадь', 'кухня', 'пл. жилая', 'этаж', 'всего этажей', 'цена']
  46.  
  47. data = pd.DataFrame([
  48.     [1, 38.5, 6.9, 18.9, 3, 5, 4200000],
  49.     [1, 38.0, 8.5, 19.2, 9, 17, 3500000],
  50.     [1, 34.7, 10.3, 19.8, 1, 9, 5100000],
  51.     [1, 45.9, 11.1, 17.5, 11, 23, 6300000],
  52.     [1, 42.4, 10.0, 19.9, 6, 14, 5900000],
  53.     [1, 46.0, 10.2, 20.5, 3, 12, 8100000],
  54.     [2, 77.7, 13.2, 39.3, 3, 17, 7400000],
  55.     [2, 69.8, 11.1, 31.4, 12, 23, 7200000],
  56.     [2, 78.2, 19.4, 33.2, 4, 9, 6800000],
  57.     [2, 55.5, 7.8, 29.6, 1, 25, 9300000],
  58.     [2, 74.3, 16.0, 34.2, 14, 17, 10600000],
  59.     [2, 78.3, 12.3, 42.6, 23, 23, 8500000],
  60.     [2, 74.0, 18.1, 49.0, 8, 9, 6000000],
  61.     [2, 91.4, 20.1, 60.4, 2, 10, 7200000],
  62.     [3, 85.0, 17.8, 56.1, 14, 14, 12500000],
  63.     [3, 79.8, 9.8, 44.8, 9, 10, 13200000],
  64.     [3, 72.0, 10.2, 37.3, 7, 9, 15100000],
  65.     [3, 95.3, 11.0, 51.5, 15, 23, 9800000],
  66.     [3, 69.3, 8.5, 39.3, 4, 9, 11400000],
  67.     [3, 89.8, 11.2, 58.2, 24, 25, 16300000],
  68. ], columns=columns)
  69.  
  70. features = data.drop('цена', axis=1)
  71. target = data['цена']
  72.  
  73. class LinearRegression:
  74.     def fit(self, train_features, train_target):
  75.         self.w = np.zeros(train_features.shape[0])
  76.         self.w0 = train_target.mean()
  77.        
  78.     def predict(self, test_features):
  79.         return test_features.dot(self.w) + self.w0
  80.    
  81. model = LinearRegression()
  82. model.fit(features, target)
  83. predictions = model.predict(features)
  84. print(r2_score(target, predictions))()
  85. model.fit(features, target)
  86. predictions = model.predict(features)
  87. print(r2_score(target, predictions))

Reply to "My LinearRegression v2"

Here you can reply to the paste above