问题描述
我有下面显示的代码,但是出现以下错误:
I have the code shown below, but get the following error:
提供的这是我的数据示例( |
只是我在此处添加的分隔符,用于演示,您可以想象每个值都在CSV文件的单独的单元格中):
Provided that this is a sample of my data (|
is just a separator I added here for demonstration, you can imagine each value in a separate cell in a CSV file):
可能是字符串引起的问题吗?在这种情况下,我该如何读取和传递字符串?
Could the strings be the issue? How can I read and pass strings in this case?
from keras.models import Sequential
from keras.layers import Dense
from keras.models import Sequential, load_model
from keras.layers import Dense, Dropout, BatchNormalization, Activation
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.cross_validation import train_test_split
import pandas as pd
import numpy as np
df1 = pd.read_csv('mutation-train.csv')
y = df1[['Histology']]
X = df1[["CDS_Mutation","Primary_Tissue","Genomic","Gene_ID","Official_Symbol"]]
X = X.astype(np.str).values
y = y.astype(np.str).values
df2 = pd.read_csv('mutation-test.csv')
X_Test = df2[["CDS_Mutation","Primary_Tissue","Genomic","Gene_ID","Official_Symbol"]]
X_Test = X_Test.astype(np.str).values
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.2)
seed = 42
np.random.seed(seed)
model = Sequential()
#input layer
model.add(Dense(8, input_shape=(5,)))
model.add(BatchNormalization())
model.add(Activation("relu"))
model.add(Dropout(0.4))
model.add(Dense(8))
model.add(BatchNormalization())
model.add(Activation("sigmoid"))
model.add(Dropout(0.4))
model.add(Dense(4))
model.add(BatchNormalization())
model.add(Activation("sigmoid"))
model.add(Dropout(0.4))
model.add(Dense(2, activation="sigmoid"))
model.add(Dense(1, activation='linear'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, y, nb_epoch=300, batch_size=30)
谢谢.
编辑
这是回溯:
File "my_code.py", line 16, in <module>
df1 = pd.read_csv('mutation-train.csv',header=None,names=headers, dtype=dtypes)
File "/Users/abder/anaconda2/lib/python2.7/site-packages/pandas/io/parsers.py", line 678, in parser_f
return _read(filepath_or_buffer, kwds)
File "/Users/abder/anaconda2/lib/python2.7/site-packages/pandas/io/parsers.py", line 446, in _read
data = parser.read(nrows)
File "/Users/abder/anaconda2/lib/python2.7/site-packages/pandas/io/parsers.py", line 1036, in read
ret = self._engine.read(nrows)
File "/Users/abder/anaconda2/lib/python2.7/site-packages/pandas/io/parsers.py", line 1848, in read
data = self._reader.read(nrows)
File "pandas/_libs/parsers.pyx", line 876, in pandas._libs.parsers.TextReader.read
File "pandas/_libs/parsers.pyx", line 891, in pandas._libs.parsers.TextReader._read_low_memory
File "pandas/_libs/parsers.pyx", line 968, in pandas._libs.parsers.TextReader._read_rows
File "pandas/_libs/parsers.pyx", line 1094, in pandas._libs.parsers.TextReader._convert_column_data
File "pandas/_libs/parsers.pyx", line 1162, in pandas._libs.parsers.TextReader._convert_tokens
推荐答案
如果csv('BRAF')的最后一个值是分类的,则可以使用keras to_categorical 方法使用以下方法对它进行编码:热向量,这是神经网络的推荐编码.
If the last value of your csv ('BRAF') is categorical you could use keras to_categorical method to encode it using one-hot vectors, this is the recommended encoding for neural networks.
from keras.utils import to_categorical
# one hot encode
encoded = to_categorical(data)
print(encoded)
这篇关于Keras-ValueError:无法将字符串转换为浮点型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!