为什么我的两个分类器无法预测很多标签?

时间:2019-06-22 10:57:18

标签: python scikit-learn svm random-forest mlp

我尝试比较分类器RandomForest(RF),SupportVectorMachine(SVM)和多层感知器(MLP),查看它们的classification_report。它是分类数据的多重分类。 相同的数据(378443条目,7列) 同样y_tain, 相同的y_test, 我用以下方法检查了y_train和y_test:

 from collections import Counter
    Counter(y_train)
    Counter(y_test)

我看到两者都有相同的31个班级。

OUTPUT Counter(y_train):             OUTPUT Counter(y_test)
Counter({'Class 1': 201096,        Counter({'Class 1': 133917,
         'Class 2': 24109,                  'Class 11': 5,
         'Class 3': 731,                    'Class 2': 16167,
         'Class 4': 851,                    'Class 3': 475,
         'Class 5': 60,                     'Class 4': 628,
         'Class 6': 7,                      'Class 8': 7,
         'Class 7': 19,                     'Class 12': 19,
         'Class 8': 3,                      'Class 21': 3,
         'Class 9': 12,                     'Class 25': 10,
         'Class 10': 7,                     'Class 18': 6,
         'Class 11': 5,                     'Class 9': 12,
         'Class 12': 28,                    'Class 5': 41,
         'Class 13': 5,                     'Class 16': 4,
         'Class 14': 8,                     'Class 7': 14,
         'Class 15': 9,                     'Class 17': 3,
         'Class 16': 6,                     'Class 30': 3,
         'Class 17': 7,                     'Class 26': 4,
         'Class 18': 4,                     'Class 27': 4,
         'Class 19': 6,                     'Class 14': 2,
         'Class 20': 5,                     'Class 28': 5,
         'Class 21': 7,                     'Class 13': 5,
         'Class 22': 6,                     'Class 24': 9,
         'Class 23': 7,                     'Class 15': 7,
         'Class 24': 15,                    'Class 31': 5,
         'Class 25': 10,                    'Class 10': 3,
         'Class 26': 10,                    'Class 23': 3,
         'Class 27': 6,                     'Class 29': 1,
         'Class 28': 5,                     'Class 22': 4,
         'Class 29': 9,                     'Class 20': 5,
         'Class 30': 7,                     'Class 6': 3,
         'Class 31': 5})                    'Class 19': 4})

但是在打印category_report(y_train,y_pred)时收到此警告:

  

UndefinedMetricWarning:精度和F分数定义不正确,   在没有预测样本的标签中将其设置为0.0。 '精确',   “预测”,平均,警告)

这意味着并非所有标签都包含在y_pred中,即y_test中有一些分类器无法预测的标签。

使用RF,一切工作都很好(只有一个类在category_report中获得0.00,以提高准确性和查全率)。

SVM和MLP的分类报告在类的一半中包含0.00s。

MLP可以预测 13个类 (在“精确度/调用率”中大于0.00):class 1,2,3,4, 6, 8,9, 12, 14, 20,21, 23, 25

我所有的代码:

 #data is imported

Y = data['class']
data=data.drop['class']
labEn = {}
#LabelEncoding for cols
for x in range(len(data.columns)):
    #creating the LabelEncoder for col x 
    labEn[x] = LabelEncoder()
    dfPre[data.columns[x]] = labEn[x].fit_transform(data[data.columns[x]])
    #for unknown labels
    labEn[x].classes_ = np.append(labEn[x].classes_, '-unknown-')

X = data
X.shape #Output:(378443, 7)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0)
    ###### amount of train and test data####################
X_train.shape, y_train.shape
    #Output: (227065, 7)
(227065,)
X_test.shape, y_test.shape
    #Output: (151378, 7)
(151378,)

from collections import Counter
print(Counter(y_train))
print(Counter(y_test))

##RF
rfclf = RandomForestClassifier(class_weight = 'balanced')
rfclf.fit(X_train,  y_train)

y_train_pred = cross_val_predict(rfclf, X_train, y_train, cv=3)
y_test_pred=cross_val_predict(rfclf, X_test, y_test, cv=3)

print(classification_report(y_train, y_train_pred))

print(classification_report(y_test, y_test_pred))

##for SVM and MLP: Scaling data
start_time_standardscaler = time.time()
scaler = StandardScaler()
scaler.fit(X_train) 

X_train_scaled=scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test) 

#for svm: One Hot Encoder - I also tried it without!
enc = OneHotEncoder(handle_unknown='ignore')
enc.fit(X_train_scaled)
X_train_X_test_ohencoded=enc.transform(X_train_scaled)
X_test_ohencoded=enc.transform(X_test_scaled)

##SVM
svmclf=svm.SVC(kernel='rbf', gamma='scale')
svmclf.fit(X_train_scaled,y_train)#also tried X_train_ohencoded,y_train)

y_train_pred_scaled = cross_val_predict(svmclf, X_train_scaled, y_train, cv=10)
#y_train_pred_ohencoded = cross_val_predict(svmclf, X_train_ohencoded, y_train, cv=10)


print(classification_report(y_train, y_train_pred_scaled))
#print(classification_report(y_train, y_train_pred_ohencoded))

print(classification_report(y_test, y_test_pred))
#print(classification_report(y_test, y_test_pred_ohencoded))

##MLP
mlpclf = MLPClassifier(solver='adam', alpha=1e-5, hidden_layer_sizes=(50, 100), random_state=1)
mlpclf.fit(X_train_scaled,y_train)

y_train_pred = cross_val_predict(mlpclf, X_train_scaled, y_train, cv=10)
y_test_pred=cross_val_predict(rfclf, X_test_scaled, y_test, cv=3)

print(classification_report(y_train, y_train_pred))
print(classification_report(y_test, y_test_pred))



##Prediction
#works well since the 5 classes all classifiers could train has to be predicted here (how lucky)

#newdata is imported
#Scaler from above is used 
newdata_scaled=scaler.transform(newdata)
#Encoder from above is used
newdata_enc=enc.transform(newdata_scaled)

rfclf.predict(newdata)
svmclf.predict(newdata_enc)
mlpclf.predict(newdata_scaled)

1 个答案:

答案 0 :(得分:0)

您有严重的失衡数据集,其中31%的类别中只有2个类别的数据集占99%。除了数据集的大小外,分布变化(每个类的百分比)也很重要。您的模型倾向于过度拟合以较高的百分比进行分类,因为在那里它将获得较高的准确性。

解决该问题的一种方法是为少数群体生成合成样本。 可以通过适用于python的imblearn软件包将 SMOTE (综合少数族裔过采样技术)应用于您的数据。

您可以转到herehere以获得更多详细信息