0

I have a 3 dimensional numpy array

   ([[[0.30706802]],

   [[0.19451728]],

   [[0.19380492]],

   [[0.23329106]],

   [[0.23849282]],

   [[0.27154338]],

   [[0.2616704 ]], ... ])

with shape (844,1,1) resulting from RNN model.predict()

y_prob = loaded_model.predict(X)

, my problem is how to convert it to a pandas dataframe. I have used Keras

my objective is to have this:

0      0.30706802
7      0.19451728
21     0.19380492
35     0.23329106
42       ...
         ...   
815      ...
822      ...
829      ...
836      ...
843      ...
Name: feature, Length: 78, dtype: float32

3 Answers 3

1

idea is to first flatten the nested list to list than convert it in df using from_records method of pandas dataframe

import numpy as np
import pandas as pd

data = np.array([[[0.30706802]],[[0.19451728]],[[0.19380492]],[[0.23329106]],[[0.23849282]],[[0.27154338]],[[0.2616704 ]]])

import itertools
data  = list(itertools.chain(*data))
df = pd.DataFrame.from_records(data)

Without itertools

data = [i for j in data for i in j]
df = pd.DataFrame.from_records(data)

Or you can use flatten() method as mentioned in one of the answer, but you can directly use it like this

pd.DataFrame(data.flatten(),columns = ['col1']) 
Sign up to request clarification or add additional context in comments.

1 Comment

okey, your solution works great, didn't get nan value as result , thank you
0

Here you go!

import pandas as pd
y = ([[[[11]],[[13]],[[14]],[[15]]]])
a = []
for i in y[0]:
    a.append(i[0])
df = pd.DataFrame(a)
print(df)

Output:

    0
0  11
1  13
2  14
3  15

Feel free to set your custom index values both for axis=0 and axis=1.

Comments

0

You could try:

s = pd.Series(your_array.flatten(), name='feature')

https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flatten.html

You can then convert the series to a dataframe using s.to_frame()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.