我正在尝试为多类分类问题找到一个混淆矩阵。 但是我总是这样: ValueError:分类指标无法处理multilabel-indicator和二进制目标的混合。 不知道该如何进行...
读取数据集
x1 = pd.read_csv('x daten encoder.csv')
y1 = pd.read_csv('y daten encodieren.csv')
df=pd.DataFrame(X1)
X = df
df1=pd.DataFrame(Y1)
编码
encoder = LabelEncoder()
encoder.fit(df1)
y2 = encoder.transform(df1)
Y = to_categorical(y2)
X,Y随机播放数据集
X, Y = shuffle(X, Y, random_state=1) #um meine x und y daten zu mischen
在培训中和考试日期
train_x, test_x, train_y, test_y = train_test_split(X, Y, test_size=0.20, random_state=101)
print(train_x.shape)
print(train_y.shape)
print(test_x.shape)
#model_path = "C: ...."
变节狂欢节
learning_rate = 0.3
training_epochs = 20
cost_history = np.empty(shape=[1], dtype=float)
n_dim = X.shape[1] # wie viele neuronen # also 1024
print("n_dim:", n_dim)
n_class = 6
ein NN定义: 隐藏层 n_hidden = 10
x = tf.placeholder(tf.float32, [None, n_dim])
W = tf.Variable(tf.zeros([n_dim, n_class]))
b = tf.Variable(tf.zeros([n_class]))
y_ = tf.placeholder(tf.float32, [None, n_class])
克雷耶尔的权重和偏见 重物 建立模型:
def multilayer_perceptron(x, weights, biases):
# hiddenlayer mit tanh aktivierungsfkt
layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.tanh(layer_1)
#output-layer mit linearer aktivierungsfkt
out_layer = tf.matmul(layer_1, weights['out'] + biases['out'])
return out_layer
定义每层的权重和偏差
weights = {
'h1' : tf.Variable(tf.truncated_normal([n_dim, n_hidden])),
'out' : tf.Variable(tf.truncated_normal([n_hidden, n_class]))
}
biases = {
'b1' : tf.Variable(tf.truncated_normal([n_hidden])),
'out' : tf.Variable(tf.truncated_normal([n_class]))
}
初始化所有变量
init = tf.global_variables_initializer()
saver = tf.train.Saver()
我的定义模型aufrufen,dort wird trainiert
y = multilayer_perceptron(x, weights, biases)
定义和优化程序
cost_function = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=y, labels=y_))
training_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_function)
饮食会议kreieren
sess = tf.Session()
sess.run(init)
berechne die kosten und genauigkeitfürjede epoche
mse_history = []
accuracy_history = []
for epoch in range(training_epochs):
sess.run(training_step, feed_dict = {x: train_x, y_: train_y})
cost = sess.run(cost_function, feed_dict={x: train_x, y_: train_y})
cost_history = np.append(cost_history, cost)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.arg_max(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print ("Accuracy: ", (sess.run(accuracy, feed_dict={x: test_x, y_: test_y})))
pred_y = sess.run(y, feed_dict={x: test_x})
mse = tf.reduce_mean(tf.square(pred_y - test_y))
mse_ = sess.run(mse)
mse_history.append(mse_)
accuracy = (sess.run(accuracy, feed_dict={x: train_x, y_: train_y}))
accuracy_history.append(accuracy)
print('epoch: ', epoch, '-', 'cost: ', cost, "-MSE: ", mse_, "Train-accuracy: ", accuracy)
绘制一个混淆矩阵
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(test_y, pred_y)