how I convert 3D array to 2D without get error cannot reshape

Viewed 41

I need change 3D array to 2D

The SVM fit function that I want to use from Sklearn is telling me the data input into it (Lbp array) needs to be 2 dimensions and not 3.

Everytime we try to reshape our data, we get this error:

Traceback (most recent call last): File "/Users/name/PycharmProjects/pythonProject1/main.py", line 164, in output_array = np.reshape(lbp, (2, 3)) File "<array_function internals>", line 180, in reshape File "/Users/name/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 298, in reshape return _wrapfunc(a, 'reshape', newshape, order=order) File "/Users/name/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 57, in _wrapfunc return bound(*args, **kwds) ValueError: cannot reshape array of size 2621440 into shape (2,3)

error in this code

lbp.transpose()
output_array = np.reshape(lbp, (2, 3))#Error

the full code:

 import cv2
    import numpy as np
    import os
    import matplotlib.pyplot as plt
    import random
    from skimage import feature #-> python -m pip install -U scikit-image
    from PIL import  ImageOps
    from sklearn.model_selection import train_test_split
    from skimage.feature import greycomatrix
    from skimage import io
    from sklearn.metrics import confusion_matrix , f1_score
    from classification_utilities import display_cm, display_adj_cm
    from sklearn import svm
    import seaborn as sns
    
    
    
    #load and labeling the data
    #__________________________________________________________________________
  
    DIRECTORY ="/Users/name/Desktop/LSB"
    FILES = ['cover', 'stego']
    data = []
    for file in FILES:
        path = os.path.join(DIRECTORY, file)
        for img in os.listdir(path):
    
            #if img!='.DS_Store': # 2d error , reading null img
    
            img_path = os.path.join(path, img)
            #print("img path ", " ", img)
            label = FILES.index(file)
            img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
            if img is not None: # 2d error , reading null img
                data.append([img, label])
    
    print("data shape -> ",np.shape(data))    
    random.shuffle(data)

    X=[]
    y=[]
    
    for features, label in data:
            X.append(features)
            print('** ', np.shape(X))
            y.append(label)
    
    X=np.array(X)
    Y =np.array(y)
    
    
    print("type data->" , type(data))
    print("type x->" , type(X))
    print("type y->" , type(y))
    print(" x.shape->" , np.shape(X))
    print(" y.shape->" , np.shape(y))
    print(X)

   
    lbp =[]
    
    
    i=0
    
    for img in X :
    
        lbp.append(feature.local_binary_pattern(img, 8, 3, method="default"))
        i+=1
    
    
    
    lbp =np.array(lbp)
    
    lbp/=255
    
   
    print(" before : lbp.shape->" , np.shape(lbp))
    lbp.transpose()
    output_array = np.reshape(lbp, (2, 3))  X=[]
y=[]

for features, label in data:
        X.append(features)
        print('** ', np.shape(X))
        y.append(label)

X=np.array(X)
Y =np.array(y)


print("type data->" , type(data))
print("type x->" , type(X))
print("type y->" , type(y))
print(" x.shape->" , np.shape(X))
print(" y.shape->" , np.shape(y))
print(X)


lbp =[]


i=0

for img in X :

    lbp.append(feature.local_binary_pattern(img, 8, 3, method="default"))
    i+=1



lbp =np.array(lbp)

lbp/=255


print(" before : lbp.shape->" , np.shape(lbp))
lbp.transpose()
output_array = np.reshape(lbp, (2, 3))#Error

print(" after : lbp.shape->" , np.shape(lbp)) 
    print(" after : lbp.shape->" , np.shape(lbp))
0 Answers
Related