为什么我总是按照班级的预测获得“ 1”?

时间:2019-01-28 04:19:52

标签: python pandas csv numpy keras

我有this CSV file,我试图根据其他行中的数据预测Histology

我有下面显示的代码来做到这一点。但是,我得到的所有预测都是1。这是为什么?尽管训练模型后得到的准确性为86.81%

import numpy as np 
import pandas as pd 
from keras.layers import Dense, Dropout, BatchNormalization, Activation
import keras.models as md
import keras.layers.core as core
import keras.utils.np_utils as kutils
import keras.layers.convolutional as conv

from keras.layers import MaxPool2D

from subprocess import check_output
dataset = pd.read_csv('mutation-train.csv')

dataset = dataset[['CDS_Mutation',
                   'Primary_Tissue',
                    'Genomic',
                    'Gene_ID',
                    'Official_Symbol',
                    'Histology']]

X = dataset.iloc[:,0:5].values
y = dataset.iloc[:,5].values

# Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X_0 = LabelEncoder()
X[:, 0] = labelencoder_X_0.fit_transform(X[:, 0])
labelencoder_X_1 = LabelEncoder()
X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])
labelencoder_X_2= LabelEncoder()
X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2])
labelencoder_X_4= LabelEncoder()
X[:, 4] = labelencoder_X_4.fit_transform(X[:, 4])

X = X.astype(float)
labelencoder_y= LabelEncoder()
y = labelencoder_y.fit_transform(y)

onehotencoder0 = OneHotEncoder(categorical_features = [0])
X = onehotencoder0.fit_transform(X).toarray()
X = X[:,0:]
onehotencoder1 = OneHotEncoder(categorical_features = [1])
X = onehotencoder1.fit_transform(X).toarray()
X = X[:,0:]
onehotencoder2 = OneHotEncoder(categorical_features = [2])
X = onehotencoder2.fit_transform(X).toarray()
X = X[:,0:]
onehotencoder4 = OneHotEncoder(categorical_features = [4])
X = onehotencoder4.fit_transform(X).toarray()
X = X[:,0:]

# Splitting the dataset training and test sets
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2)

# Feature scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Evaluating the ANN
from sklearn.model_selection import cross_val_score
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout

model=Sequential()
model.add(Dense(32, activation = 'relu', input_shape=(X.shape[1],)))
model.add(Dense(16, activation = 'relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ["accuracy"])

# Compile model
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
# Fit the model
model.fit(X,y, epochs=3, batch_size=1)

# Evaluate the model
scores = model.evaluate(X,y)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

# Calculate predictions
predictions = model.predict(X)
prediction = pd.DataFrame(predictions,columns=['predictions']).to_csv('prediction.csv')

谢谢。

1 个答案:

答案 0 :(得分:3)

当所有值均为 1 时,您将获得86.81%的准确度,这似乎表明您的数据不平衡,这意味着在您的训练数据集中,其中一类优于另一类。 因此,即使您对所有测试数据都预测为1,您也将获得更高的准确性。

引用Accuracy paradox

例如。在您的数据集中,约有85%的数据样本属于 1 类,其余的样本属于 0 类。

如何处理

有很多方法可以解决它。

  1. 上采样:为类别 0 创建重复数据,因此类别 1 和类别 0 的比例相同。
  2. 下采样:只需从类 1 中删除一些示例即可获得相同的所有权。
  3. 更改绩效矩阵:与其将准确性用作绩效矩阵, F1得分,准确性或召回力
  4. 您可以在犯错误时将不同的罚款分配给不同的类别。在这种情况下,您可以为数据量少的班级赋予较高的权重。

还有更多处理方法。

有关更多详细信息,请参见this link