Я делаю модель KNN, и у меня очень несбалансированные данные, такие как:
Buy 515
Don't move 1473
Sell 466
Теперь я хочу перевыбор метки покупки и продажи, чтобы она равнялась количеству не двигаться в моей модели KNN.
Это моя модель KNN, но я не знаю, как сделать SMOTE.
knn_neighbours = [2,3,4,5,10]
knn_weights = ['uniform','distance']
knn_leaf = [10,20,30,40,80]
knn_grid_search = list(product(knn_neighbours,knn_weights,knn_leaf))
knn_grid_results = {}
for option in knn_grid_search:
knn_neighbours = option[0]
knn_weights = option[1]
knn_leaf = option[2]
X_data = result
y_data = y
knn_macro_list = []
knn_micro_list = []
knn_auc_list = []
acc_knn_score1 = []
from sklearn.model_selection import StratifiedKFold
skf = StratifiedKFold(n_splits=10, shuffle=True)
for train_index, test_index in skf.split(result, y):
train_news, test_news = result.iloc[train_index], result.iloc[test_index]
train_labels, test_labels = y[train_index], y[test_index]
le = preprocessing.LabelEncoder()
le.fit(train_labels)
classes_name=le.classes_
le.transform(train_labels)
# Binarize the output
train_labels = le.transform(train_labels)
test_labels = le.transform(test_labels)
train_labels = label_binarize(train_labels, classes=[0,1,2,3,4])
test_labels = label_binarize(test_labels, classes=[0,1,2,3,4])
n_classes = train_labels.shape[1]
#K-Nearest Neighbour
knn = KNeighborsClassifier(n_neighbors=knn_neighbours,weights=knn_weights,leaf_size=knn_leaf)
acc_knn_score1.append(knn.fit(train_news,train_labels).score(test_news, test_labels))
Научиться прогнозировать каждый класс против другого
knn_classifier = OneVsRestClassifier(knn)
knn_score = knn_classifier.fit(train_news, train_labels).predict_proba(test_news)
acc_knn_score = knn_classifier.score(test_news, test_labels)