11

I have a data-frame of size (140000,22) dimensions.

i have to create 2d array of equal dimensions to pass it into convolution neural network .

Can you please guide how to transform on this dataframe

martineau
  • 119,623
  • 25
  • 170
  • 301
KURUVILLA ABRAHAM
  • 111
  • 1
  • 2
  • 4
  • Possible duplicate of [Convert pandas dataframe to NumPy array](https://stackoverflow.com/questions/13187778/convert-pandas-dataframe-to-numpy-array) – FChm Feb 06 '19 at 08:29
  • 1
    please read https://stackoverflow.com/help/mcve – Jon Scott Feb 06 '19 at 08:31

2 Answers2

15

Edit: If by 'equal' dimensions you mean it should be a square array, this is not possible, since 140000*22 is not a square number. However, in general you don't need a square array to feed data into a CNN, but this of course depends on your specific CNN.

Old answer: You just need to call .values on the DataFrame.

If for example your dataframe is called df, then you can pass df.values to your convolutional neural network.

markuscosinus
  • 2,248
  • 1
  • 8
  • 19
4

Use arr = df.to_numpy()

See: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_numpy.html

It will create a 2d array where each row is the inner array.

[[row1],
 [row2],
 [row3]...
 [row n]]

To turn it into a 2d array where each column is the inner array, use transpose:

arr = np.transpose(arr)

[[all_data_in_col_1],
 [all_data_in_col_2],...
 [all_data_in_col_n]]
Mike718
  • 41
  • 2