By akrama81


2017-07-31 06:05:26 8 Comments

I'm trying to reshape a numpy array as:

data3 = data3.reshape((data3.shape[0], 28, 28))

where data3 is:

[[54 68 66 ..., 83 72 58]
 [63 63 63 ..., 51 51 51]
 [41 45 80 ..., 44 46 81]
 ..., 
 [58 60 61 ..., 75 75 81]
 [56 58 59 ..., 72 75 80]
 [ 4  4  4 ...,  8  8  8]]

data3.shape is (52, 2352 )

But I keep getting the following error:

ValueError: cannot reshape array of size 122304 into shape (52,28,28)
Exception TypeError: TypeError("'NoneType' object is not callable",) in <function _remove at 0x10b6477d0> ignored

What is happening and how to fix this error?

UPDATE:

I'm doing this to obtain data3 that is being used above:

def image_to_feature_vector(image, size=(28, 28)):

    return cv2.resize(image, size).flatten()

data3 = np.array([image_to_feature_vector(cv2.imread(imagePath)) for imagePath in imagePaths])  

imagePaths contains paths to all the images in my dataset. I actually want to convert the data3 to a flat list of 784-dim vectors, however the

image_to_feature_vector 

function converts it to a 3072-dim vector!!

2 comments

@akilat90 2017-07-31 11:07:08

First, your input image's number of elements should match the number of elements in the desired feature vector.

Assuming the above is satisfied, the below should work:

# Reading all the images to a one numpy array. Paths of the images are in the imagePaths
data = np.array([np.array(cv2.imread(imagePaths[i])) for i in range(len(imagePaths))])

# This will contain the an array of feature vectors of the images
features = data.flatten().reshape(1, 784)

@akrama81 2017-07-31 17:26:36

Getting error: ValueError: cannot reshape array of size 52 into shape (1,784)

@akilat90 2017-07-31 18:13:14

@akrama81 Do your images satisfy the mentioned requirement? (Total elements == 784)

@akrama81 2017-07-31 22:11:35

If you mean feature vectors, then it's 2352. How to convert to 784?

@akilat90 2017-08-01 10:48:54

@akrama81 I didn't get this: If you mean feature vectors, then it's 2352. How to convert to 784? What I said was that your input image should have a total number of 784(exactly) pixels if you want to convert it to a [1,784] vector.

@Kaushik Nayak 2017-07-31 06:27:56

You can reshape the numpy matrix arrays such that before(a x b x c..n) = after(a x b x c..n). i.e the total elements in the matrix should be same as before, In your case, you can transform it such that transformed data3 has shape (156, 28, 28) or simply :-

import numpy as np

data3 = np.arange(122304).reshape(52, 2352 )

data3 = data3.reshape((data3.shape[0]*3, 28, 28))

print(data3.shape)

Output is of the form

[[[     0      1      2 ...,     25     26     27]
  [    28     29     30 ...,     53     54     55]
  [    56     57     58 ...,     81     82     83]
  ..., 
  [   700    701    702 ...,    725    726    727]
  [   728    729    730 ...,    753    754    755]
  [   756    757    758 ...,    781    782    783]]
  ...,
[122248 122249 122250 ..., 122273 122274 122275]
  [122276 122277 122278 ..., 122301 122302 122303]]]

@akrama81 2017-07-31 07:10:40

What is 156 in (156, 28, 28)??

@akrama81 2017-07-31 07:12:27

I tried doing what you suggested but then when I do (trainData, testData, trainLabels, testLabels) = train_test_split( data3 / 255.0, labels, test_size=0.33), I get this error: ValueError: Found input variables with inconsistent numbers of samples: [156, 52] on that line.

Related Questions

Sponsored Content

8 Answered Questions

[SOLVED] Is arr.__len__() the preferred way to get the length of an array in Python?

  • 2009-02-05 21:26:47
  • Joan Venge
  • 1590117 View
  • 673 Score
  • 8 Answer
  • Tags:   python arrays methods

1 Answered Questions

Isomap Cant Reshape reshape array of size 72 into shape (8,8)

0 Answered Questions

Processing very large images as numpy arrays

1 Answered Questions

2 Answered Questions

0 Answered Questions

Cropping an image in python (numpy), array reshape

  • 2018-03-07 17:04:39
  • Caims
  • 155 View
  • 0 Score
  • 0 Answer
  • Tags:   python numpy

1 Answered Questions

[SOLVED] error reading back a numpy array from csv

  • 2017-10-24 01:45:39
  • Michael Niebauer
  • 46 View
  • 0 Score
  • 1 Answer
  • Tags:   python numpy

1 Answered Questions

[SOLVED] Fail to convert List of ndarrays to numpy array

  • 2017-05-07 08:25:53
  • Ali Kohan
  • 312 View
  • 2 Score
  • 1 Answer
  • Tags:   python numpy

0 Answered Questions

1 Answered Questions

[SOLVED] Seeking a more efficient python numpy ravel+reshape

Sponsored Content