【发布时间】:2021-03-11 16:35:57
【问题描述】:
我正在尝试使用 sklearn 实现一个补充朴素贝叶斯分类器。我的数据有非常不平衡的类(0 类的 30k 个样本和 1 类的 6k 个样本),我正在尝试使用加权类来弥补这一点。
这是我的数据集的形状:
我尝试使用 compute compute_class_weight 函数计算权重,然后在训练模型时将其传递给 fit 函数:
import numpy as np
import seaborn as sn
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.utils import class_weight
from sklearn.naive_bayes import ComplementNB
#Import the csv data
data = pd.read_csv('output_pt900.csv')
#Create the header of the csv file
header = []
for x in range(0,2500):
header.append('pixel' + str(x))
header.append('status')
#Add the header to the csv data
data.columns = header
#Replace the b's and the f's in the status column by 0 and 1
data['status'] = data['status'].replace('b',0)
data['status'] = data['status'].replace('f',1)
print(data)
#Drop the NaN values
data = data.dropna()
#Separate the features variables and the status
y = data['status']
x = data.drop('status',axis=1)
#Split the original dataset into two other: train and test
x_train, x_test, y_train, y_test = train_test_split(x,y, test_size = 0.2)
all_together = y_train.to_numpy()
unique_classes = np.unique(all_together)
c_w = class_weight.compute_class_weight('balanced', unique_classes, all_together)
clf = ComplementNB()
clf.fit(x_train,y_train, c_w)
y_predict = clf.predict(x_test)
cm = confusion_matrix(y_test, y_predict)
svm = sn.heatmap(cm, cmap='Blues', annot=True, fmt='g')
figure=svm.get_figure()
figure.savefig('confusion_matrix_cnb.png', dpi=400)
plt.show()
但我得到了这些错误:
ValueError: sample_weight.shape == (2,), expected (29752,)!
有人知道如何在 sklearn 模型中使用加权类吗?
【问题讨论】:
标签: python machine-learning scikit-learn naivebayes