Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
822 B
179
Indexable
Never
import pandas as pd
from pyod.models.knn import KNN
from sklearn.ensemble import IsolationForest
import numpy as np

RANDOM_STATE = 42

df = pd.read_csv('/datasets/sales.csv')
data = df[['Sales', 'Profit']]

model = KNN().fit(data)
estimation_knn = model.predict(data)
outliers_knn = sum(estimation_knn)
print("Количество аномалий (KNN): ", outliers_knn)

model = IsolationForest(random_state=RANDOM_STATE, n_estimators=100)
estimation_iforest = model.fit_predict(data)
estimation_iforest[estimation_iforest == 1] = 0
estimation_iforest[estimation_iforest == -1] = 1
outliers_iforest = sum(estimation_iforest)
print("Количество аномалий (изоляционный лес): ", outliers_iforest)

print("Совпало: ", (estimation_knn & estimation_iforest).sum())