pandas DataFrame和Keras [英] Pandas DataFrame and Keras

查看:215
本文介绍了 pandas DataFrame和Keras的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Keras在Python中执行情感分析.为此,我需要对文本进行单词嵌入.当我尝试将数据拟合到模型时出现问题:

I'm trying to perform a sentiment analysis in Python using Keras. To do so, I need to do a word embedding of my texts. The problem appears when I try to fit the data to my model:

model_1 = Sequential()
model_1.add(Embedding(1000,32, input_length = X_train.shape[0]))
model_1.add(Flatten())
model_1.add(Dense(250, activation='relu'))
model_1.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

我的火车数据的形状是

(4834,)

是Pandas系列对象.当我尝试拟合模型并用其他数据进行验证时,出现此错误:

And is a Pandas series object. When I try to fit my model and validate it with some other data I get this error:

model_1.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=2, batch_size=64, verbose=2)

ValueError:检查模型输入时出错:预期 embedding_1_input具有形状(无,4834),但具有形状的数组 (4834,1)

ValueError: Error when checking model input: expected embedding_1_input to have shape (None, 4834) but got array with shape (4834, 1)

如何重塑数据以使其适合Keras?我一直在尝试使用np.reshape,但无法将None元素放置在该函数中.

How can I reshape my data to make it suited for Keras? I've been trying with np.reshape but I cannot place None elements with that function.

预先感谢

推荐答案

None是接受训练的预期行数,因此您无法定义它. Keras还需要一个numpy数组作为输入,而不是pandas数据框.首先使用df.values将df转换为numpy数组,然后执行np.reshape((-1, 4834)).请注意,您应该使用np.float32.如果您在GPU上进行训练,这一点很重要.

None is the number of expected rows that goes into training therefore you can't define it. Also Keras needs a numpy array as input and not a pandas dataframe. First convert the df to a numpy array with df.values and then do np.reshape((-1, 4834)). Note that you should use np.float32. This is important if you train it on GPU.

这篇关于 pandas DataFrame和Keras的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆