Re: Untitled

From Sloppy Pintail, 1 Year ago, written in Python, viewed 347 times. This paste will check out in 1 Second. This paste is a reply to Untitled from Beefy Monkey - view diff
URL http://codebin.org/view/92b2340f Embed
Download Paste or View Raw
  1. import numpy as np
  2. import pandas as pd
  3. from scipy.spatial import distance
  4.  
  5. columns = ['комнаты', 'площадь', 'кухня', 'пл. жилая', 'этаж', 'всего этажей', 'кондиционер']
  6.  
  7. df_train = pd.DataFrame([
  8.     [1, 38.5, 6.9, 18.9, 3, 5, 1],
  9.     [1, 38.0, 8.5, 19.2, 9, 17, 0],
  10.     [1, 34.7, 10.3, 19.8, 1, 9, 0],
  11.     [1, 45.9, 11.1, 17.5, 11, 23, 1],
  12.     [1, 42.4, 10.0, 19.9, 6, 14, 0],
  13.     [1, 46.0, 10.2, 20.5, 3, 12, 1],
  14.     [2, 77.7, 13.2, 39.3, 3, 17, 1],
  15.     [2, 69.8, 11.1, 31.4, 12, 23, 0],
  16.     [2, 78.2, 19.4, 33.2, 4, 9, 0],
  17.     [2, 55.5, 7.8, 29.6, 1, 25, 1],
  18.     [2, 74.3, 16.0, 34.2, 14, 17, 1],
  19.     [2, 78.3, 12.3, 42.6, 23, 23, 0],
  20.     [2, 74.0, 18.1, 49.0, 8, 9, 0],
  21.     [2, 91.4, 20.1, 60.4, 2, 10, 0],
  22.     [3, 85.0, 17.8, 56.1, 14, 14, 1],
  23.     [3, 79.8, 9.8, 44.8, 9, 10, 0],
  24.     [3, 72.0, 10.2, 37.3, 7, 9, 1],
  25.     [3, 95.3, 11.0, 51.5, 15, 23, 1],
  26.     [3, 69.3, 8.5, 39.3, 4, 9, 0],
  27.     [3, 89.8, 11.2, 58.2, 24, 25, 0],
  28. ], columns=columns)
  29.  
  30.  
  31. def nearest_neighbor_predict(train_features, train_target, new_features):
  32.     distances = []
  33.     for i in range(train_features.shape[0]):
  34.         distances.append(distance.euclidean(train_features.loc[i], new_apartment))
  35.        
  36.     nearest_idx = distances.index(min(distances))
  37.     return train_target.loc[nearest_idx]
  38.    
  39.  
  40. train_features = df_train.drop('кондиционер', axis=1)
  41. train_target = df_train['кондиционер']
  42.  
  43. df_test = pd.DataFrame([
  44.     [1, 36.5, 5.9, 17.9, 2, 7, 0],
  45.     [2, 71.7, 12.2, 34.3, 5, 21, 1],
  46.     [3, 88.0, 18.1, 58.2, 17, 17, 1],
  47. ], columns=columns)
  48.  
  49. test_features = df_test.drop('кондиционер', axis=1)
  50.  
  51. def nearest_neighbor_predict(train_features, train_target, new_features):
  52.     distances = []
  53.     for i in range(train_features.shape[0]):
  54.         vector = train_features.loc[i].values
  55.         distances.append(distance.euclidean(new_features, vector))
  56.     best_index = np.array(distances).argmin()
  57.     return train_target.loc[best_index]
  58.  
  59. class NearestNeighborClassificator:
  60.     def fit(self, features_train, target_train):
  61.         self.features_train = features_train
  62.         self.target_train = target_train
  63.        
  64.     def predict(self, new_features):
  65.         result = []
  66.         for i in range(new_features.shape[0]):
  67.             index = nearest_neighbor_predict(self.features_train, self.target_train, new_features.loc[i])
  68.             result.append(self.target_train.loc[index])
  69.         answer = pd.Series(result, index=new_features.index)
  70.         return answer
  71.            
  72. model = NearestNeighborClassificator()
  73. model.fit(train_features, train_target)
  74. new_predictions = model.predict(test_features)
  75. print(new_predictions)

Replies to Re: Untitled rss

Title Name Language When
Re: Re: Untitled Bitty Tortoise python 1 Year ago.

Reply to "Re: Untitled"

Here you can reply to the paste above