将inverse_scale NN输出和model.predict_classes输出附加到csv中

时间:2018-03-29 23:41:45

标签: python pandas tensorflow neural-network keras

我已经训练了一个神经网络,我想将预测值附加到inverse_scaled测试数据,这样我就可以检查预测值与原始特征值。但是,当我运行代码时,以下行:

Xtest["prediciton"] = pred

引发以下错误:

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

我相信这是因为在以下行之后,Xtest变成了np.array:

Xtest = scaler.inverse_transform(Xtest)

这是完整的代码:

import keras
import numpy
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD
import pandas as pd
import numpy as np
import matplotlib
from matplotlib import style
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from IPython.core.display import display
from sklearn.preprocessing import MinMaxScaler

matplotlib.style.use('ggplot')

data_num = pd.read_csv('mult_test.csv')
print(data_num.head(n=10))

scaler = MinMaxScaler(feature_range=(0, 1))
features = data_num.drop(['Label1'], axis=1, errors='ignore')
features = pd.DataFrame(scaler.fit_transform(features))
scale_num_data = pd.concat([data_num['Label1'], features], axis=1)


dtrain, dtest = train_test_split(scale_num_data, test_size=0.25, random_state=570)
X = dtrain.drop(['Label1'], axis=1, errors='ignore')
y = dtrain['Label1']
Xtest = dtest.drop(['Label1'], axis=1, errors='ignore')
Xtest.to_csv('X_test_1.csv')
ytest = dtest['Label1']


model = Sequential([
    Dense(10, input_shape=(4, ), activation='relu'),
    Dense(32, activation='relu'),
    Dense(10, activation='softmax')
])

model.summary()
model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(X, y, epochs=10, batch_size=10, shuffle=True)


scores = model.evaluate(Xtest, ytest, batch_size=5)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))


pred = model.predict_classes(Xtest)
Xtest = scaler.inverse_transform(Xtest)
Xtest["prediciton"] = pred
Xtest.to_csv("Xtest_predict.csv")

谢谢你的帮助,伙计们!

1 个答案:

答案 0 :(得分:0)

scikit的transformers不保留熊猫数据帧。因此,当您通过transforminverse_transform传递np.ndarray时,Xtest即将出现。

您曾尝试访问'prediction'np.ndarray,但这不是DataFrame的有效排名项。

要解决此问题,只需创建新的scaler.inverse_transform或不要将Xtest直接转储到results = pd.DataFrame({ "data": scaler.inverse_transform(Xtest), "predictions": model.predict_classes(Xtest) }) results.to_csv("Xtest_predict.csv")

class V1::LpDeveloperReferralsController < V1::BaseController
 def create
   add(a, b).delay
 end

 def add(a, b)
 end
end