我有下面显示的代码,但是出现以下错误:
ValueError:无法将字符串转换为float:BRAF
提供的这是我的数据示例(|
只是我在此处添加的用于演示的分隔符,您可以想象每个值都在CSV文件的单独的单元格中):
c.401C> T |皮肤| 23:141905805-141905805 | 9947 | BRAF
可能是字符串问题吗?在这种情况下,如何读取和传递字符串?
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
答案 0 :(得分:0)
如果csv的最后一个值('BRAF')是分类的,则可以使用keras to_categorical 方法使用一键向量对它进行编码,这是神经网络的推荐编码。
from keras.utils import to_categorical
# one hot encode
encoded = to_categorical(data)
print(encoded)