Home > other >  ValueError: Must pass 2-d input. shape=(430, 430, 3)
ValueError: Must pass 2-d input. shape=(430, 430, 3)

Time:12-27

I want to save this as 3d array csv file with header as r,g,b but it is showing ValueError: Must pass 2-d input. shape=(430, 430, 3).

four_img_concat shape is (430,430,3).

import pandas as pd
import numpy as np
ans=np.array(four_img_concat)
headers=np.array(['r','g','b'])
df=pd.DataFrame(ans)
df.to_csv("answ.csv",index=False,header=headers)

CodePudding user response:

As the error is indicating, you need to transform your array from 3-d to a 2-d. You can do this by using thereshape function passing the total amount of pixels to one axis (430*430).

np.random.seed(42)
four_img_concat = np.random.randint(0,255,size=(430,430,3))
print(four_img_concat.shape) # (430, 430, 3)

four_img_concat = np.reshape(four_img_concat, (430*430, -1))
print(four_img_concat.shape)  # (184900, 3)

ans = np.array(four_img_concat)
headers = np.array(['r','g','b'])

df = pd.DataFrame(ans)
df.to_csv("answ.csv",index=False,header=headers)

answ.csv file

r,g,b
102,179,92
14,106,71
188,20,102
121,210,214
74,202,87
...
...
  • Related