Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.7 kB
1
Indexable
Never
import numpy as np
import pandas as pd
from scipy.spatial import distance

columns = ['комнаты', 'площадь', 'кухня', 'пл. жилая', 'этаж', 'всего этажей', 'кондиционер']

df_train = pd.DataFrame([
    [1, 38.5, 6.9, 18.9, 3, 5, 1],
    [1, 38.0, 8.5, 19.2, 9, 17, 0],
    [1, 34.7, 10.3, 19.8, 1, 9, 0],
    [1, 45.9, 11.1, 17.5, 11, 23, 1],
    [1, 42.4, 10.0, 19.9, 6, 14, 0],
    [1, 46.0, 10.2, 20.5, 3, 12, 1],
    [2, 77.7, 13.2, 39.3, 3, 17, 1],
    [2, 69.8, 11.1, 31.4, 12, 23, 0],
    [2, 78.2, 19.4, 33.2, 4, 9, 0],
    [2, 55.5, 7.8, 29.6, 1, 25, 1],
    [2, 74.3, 16.0, 34.2, 14, 17, 1],
    [2, 78.3, 12.3, 42.6, 23, 23, 0],
    [2, 74.0, 18.1, 49.0, 8, 9, 0],
    [2, 91.4, 20.1, 60.4, 2, 10, 0],
    [3, 85.0, 17.8, 56.1, 14, 14, 1],
    [3, 79.8, 9.8, 44.8, 9, 10, 0],
    [3, 72.0, 10.2, 37.3, 7, 9, 1],
    [3, 95.3, 11.0, 51.5, 15, 23, 1],
    [3, 69.3, 8.5, 39.3, 4, 9, 0],
    [3, 89.8, 11.2, 58.2, 24, 25, 0],
], columns=columns)


def nearest_neighbor_predict(train_features, train_target, new_features):
    distances = []
    for i in range(len(train_features)):
        distances.append(distance.euclidean(train_features.loc[i].values, new_features))

    best_index = np.array(distances).argsort()[1] 

    answer = train_target[best_index]
    return answer 
    

train_features = df_train.drop('кондиционер', axis=1)
train_target = df_train['кондиционер']
new_apartment = np.array([2, 51.0, 8.2, 35.9, 5, 5])
prediction = nearest_neighbor_predict(train_features, train_target, new_apartment)
print(prediction)