'Passing numpy ndarray as keras input
I have a dataset that consists of numpy array and I need to pass this data as input for keras.
| Idx | Target | RF | DL |
|---|---|---|---|
| 0 | P219109 | [0.05555555555555555, 0.0, 0.0, 0.0, 0.0, 0.0,... | [1.1074159, -5.242936, -6.9121795, 0.931392, -... |
| 1 | P219109 | [0.5833333333333334, 0.0, 0.0, 0.0, 0.0, 0.0, ... | [-9.173296, -4.847732, -2.5727227, 8.794523, 7... |
| 2 | P219109 | [0.05555555555555555, 0.0, 0.0, 0.0, 0.0, 0.0,... | [2.5204952, 1.3955389, -4.755222, -1.7222288, ... |
| 3 | P219109 | [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... | [1.4951401, 1.2499368, -3.08991, -2.0176327, -... |
| 4 | P219109 | [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... | [-3.4984744, 7.1746902, -0.36313212, -3.760725... |
And here my code:
X = df[['RF', 'DL']]
y = df['Target']
_, y_ = np.unique(y, return_inverse=True)
y = y_
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)
train_labels = to_categorical(y_train)
test_labels = to_categorical(y_test)
model = models.Sequential()
model.add(layers.Dense(100, activation='relu', input_shape=(2,)))
model.add(layers.Dense(50, activation='relu'))
model.add(layers.Dense(40, activation='softmax'))
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(X_train, train_labels, epochs=10, batch_size=36)
And I got the following error:
Traceback (most recent call last):
File "d:/FINAL/main.py", line 81, in <module>
model.fit(X_train, train_labels, epochs=20, batch_size=40)
File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1050, in fit
data_handler = data_adapter.DataHandler(
File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1100, in __init__
self._adapter = adapter_cls(
File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 263, in __init__
x, y, sample_weights = _process_tensorlike((x, y, sample_weights))
File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1016, in _process_tensorlike
inputs = nest.map_structure(_convert_numpy_and_scipy, inputs)
File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\util\nest.py", line 659, in map_structure
structure[0], [func(*x) for x in entries],
File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\util\nest.py", line 659, in <listcomp>
structure[0], [func(*x) for x in entries],
File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1011, in _convert_numpy_and_scipy
return ops.convert_to_tensor_v2_with_dispatch(x, dtype=dtype)
File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\util\dispatch.py", line 201, in wrapper
return target(*args, **kwargs)
File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\ops.py", line 1404,
in convert_to_tensor_v2_with_dispatch
return convert_to_tensor_v2(
File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\ops.py", line 1410,
in convert_to_tensor_v2
return convert_to_tensor(
File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\profiler\trace.py", line 163,
in wrapped
return func(*args, **kwargs)
File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\ops.py", line 1540,
in convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\tensor_conversion_registry.py", line 52, in _default_conversion_function
return constant_op.constant(value, dtype, name=name)
File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\constant_op.py", line 264, in constant
return _constant_impl(value, dtype, shape, name, verify_shape=False,
File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\constant_op.py", line 276, in _constant_impl
return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\constant_op.py", line 301, in _constant_eager_impl
t = convert_to_eager_tensor(value, ctx, dtype)
File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\constant_op.py", line 98, in convert_to_eager_tensor
return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).
Can someone help me to fix this?
Keras: 2.4.0 Tensorflow: 2.4.0 Python: 3.8
EDIT: How to pass a data frame object as a NumPy array? The RF column and DL column are the results of predict_proba from two different models. So, I joined it into a pandas data frame using the following code:
rf = [item for item in rf1]
dl = [item for item in out]
y = y_test
df = pd.Series(rf)
df = df.to_frame()
df.rename(columns = {0:'RF'}, inplace = True)
df['Target'] = y
df['DL'] = dl
cols = ['Target', 'RF', 'DL']
df = df[cols]
I also tried this:
X = np.asarray(X).astype(np.float32)
but it doesn't work as I get the following error:
ValueError: setting an array element with a sequence.
Solution 1:[1]
As discussed in the comments, it would be best if you created your arrays directly instead of having a DataFrame in the middle.
The problem is that even if X is a numpy array, it contains other arrays because Pandas returns an array for each row and each cell. An example:
import pandas as pd
import numpy as np
df = pd.DataFrame({'RF':[np.array([1, 2, 3, 4, 5]), np.array([6, 2, 3, 4, 5])], 'DL': [np.array([1, 2, 3, 4, 5]), np.array([7, 2, 3, 4, 5])]})
print(df)
RF DL
0 [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]
1 [6, 2, 3, 4, 5] [7, 2, 3, 4, 5]
X = df[['RF', 'DL']].to_numpy()
print(X)
[[array([1, 2, 3, 4, 5]) array([1, 2, 3, 4, 5])]
[array([6, 2, 3, 4, 5]) array([7, 2, 3, 4, 5])]]
You can reshape to try to fix the problem. This should work:
# You have to reshape each cell in each row.
X = np.array([np.reshape(x, (1, len(X[0][0])))
for i in range(len(X))
for x in X[i]]).reshape(-1, 2*len(X[0][0])).astype(np.float32)
print(X)
print(X.shape)
[[1. 2. 3. 4. 5. 1. 2. 3. 4. 5.]
[6. 2. 3. 4. 5. 7. 2. 3. 4. 5.]]
(2, 10)
Obviously, this assumes the shapes of the arrays in both the 'RF' and 'DL' columns are the same, which I believe is true because they are the output of predict_proba.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
