Advertisement
Guest User

All input arrays must have the same shape ValueError.?

a guest
Oct 12th, 2018
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. #===================================================== Snippet 1 =======================================
  2.  
  3. %matplotlib inline
  4.  
  5. import os
  6. import random
  7.  
  8. import imageio
  9. import pandas as pd
  10. from scipy.misc import imread
  11.  
  12. import numpy as np
  13. import matplotlib.pyplot as plt
  14. import matplotlib.image as mpimg
  15.  
  16. #===================================================== Snippet 2 =======================================
  17.  
  18. # Then I loaded the csv files, so that it would be easier to locate the files
  19.  
  20. root_dir = os.path.abspath('.')
  21. data_dir = '/Users/renedena/Documents/Machine_Learning_Project_II/all'
  22.  
  23. #print(root_dir)
  24.  
  25.  
  26. train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
  27. test = pd.read_csv(os.path.join(data_dir, 'test.csv'))
  28.  
  29.  
  30. #===================================================== Snippet 3(issue found here) ======================
  31.  
  32. # I first load all the images and "TRY" to resize them into a single numpy array
  33.  
  34. #from scipy.misc import imresize
  35. import skimage.transform
  36.  
  37. # Code to resizing all Train images first
  38. temp = []
  39. for img_name in train.Id:
  40.     img_path = os.path.join(data_dir, 'train', img_name)
  41.     img = imageio.imread(img_path)
  42.     img = skimage.transform.resize(img, (32, 32), mode='constant') # mode='reflect'
  43.     img = img.astype('float32') # this will help us in later stage
  44.     temp.append(img)
  45. train_x = np.stack(temp)
  46.  
  47. #____________________________________________________________________________________________________________
  48. #++++++++++++++++++++^^^^^^^Like i mentioned, this is where im getting my error ^^^^^^+++++++++++++++++++++++
  49. ##++++++++++++++++++++++++++++ The output im getting is as follows: +++++++++++++++++++++++++++++++++++++++++
  50. #____________________________________________________________________________________________________________
  51.  
  52. #Output below:
  53.  
  54. ---------------------------------------------------------------------------
  55. ValueError                                Traceback (most recent call last)
  56. <ipython-input-7-60df81a21aae> in <module>()
  57.      21     temp.append(img)
  58.      22 print(len(temp))
  59. ---> 23 train_x = np.stack(temp)
  60.      24 print(len(train_x))
  61.  
  62. ~/anaconda3/lib/python3.6/site-packages/numpy/core/shape_base.py in stack(arrays, axis, out)
  63.     351     shapes = set(arr.shape for arr in arrays)
  64.     352     if len(shapes) != 1:
  65. --> 353         raise ValueError('all input arrays must have the same shape')
  66.     354
  67.     355     result_ndim = arrays[0].ndim + 1
  68.  
  69. ValueError: all input arrays must have the same shape
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement