Reshaping a dataframe in python into 3D

▼魔方 西西 提交于 2021-02-05 08:20:07

问题


I am trying to reshape a handwritten character dataset into 3D form so that it can be concatenated with digit recognition dataset. I tried multiple times, but I couldnt figure out how it can be done.

The actual digit recognition dataset has the shape (60000, 28, 28) The character recognition dataset has the shape (372450, 785) and the first column is target variable. Since excluding first column 28*28=784 there is a possibility that it can be converted to 3D same as digit dataset. Please advice on how this can be done?

I need a shape like (372450,28,28) for the entire dataframe

Thank you in advance


回答1:


An array of shape (372450, 785) cannot be made into (372450,28,28) because 28*28 is 784 not 785. But if you meant making a (372450, 784) into (372450,28,28), you could do

arr = df.column_name.values 

will give a numpy array of values from the column_name field of the data frame df.

Now you can use reshape() like

arr = arr.reshape(-1,28,28)

Now arr will be of shape (372450,28,28).




回答2:


If you have a handwritten character dataset with gray scaled character, you may use following to convert your dataset into 3D,

data = data.values.reshape(-1,28,28,1)

assuming you have data as pandas dataframe.

Last argument is for channels, which is 1 for gray scaled data. If you have RGB images, there are 3 channels and thus we would have converted data into 28x28x3 matrices.



来源:https://stackoverflow.com/questions/50711786/reshaping-a-dataframe-in-python-into-3d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!