Advertisement
den4ik2003

Untitled

Feb 25th, 2023
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. def knn(features, train_features, train_target, k=1):
  2.     '''
  3.    Предсказание целевого признака для данных features -- pandas-таблица данных.
  4.    на основе обучающего набора данных train_features, train_target.
  5.    Возвращает pandas.Series с теми же индексами, что и у features.
  6.    Число k -- количество соседей.
  7.    '''
  8.    
  9.     predicted_list = []
  10.    
  11.     features['Is female'] = (features['Sex'] == 'female') * 1
  12.     train_features['Is female'] = (features['Sex'] == 'female') * 1
  13.    
  14.     columns = ['Age', 'Is female', 'Pclass', 'SibSp', 'Parch', 'Fare']
  15.    
  16.     A, B = train_features[columns].to_numpy(), features[columns].to_numpy()
  17.    
  18.     dist = ((B.reshape(B.shape[0], B.shape[1], 1) - A.T.reshape(1, A.shape[1], A.shape[0])) ** 2).sum(axis=1)
  19.     nearest_people_indices = (dist.argsort(axis=1))[:, :k]
  20.    
  21.     for i in range(B.shape[0]):
  22.         neighbours_surv = train_target.iloc[nearest_people_indices[i]].sum()
  23.         if neighbours_surv >= k / 2:
  24.             predicted_list.append(1)
  25.         else:
  26.             predicted_list.append(0)
  27.        
  28.     predicted = pd.Series(predicted_list)
  29.        
  30.     return predicted
  31.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement