Python Forum
ValueError: setting an array element with a sequence
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: setting an array element with a sequence
#1
I am new to this python coding, I tried to modified other author's code for object classification. However, I encounter this error, ValueError: setting an array element with a sequence (error at immatrix). when I tried to convert RGB images to create matrix. It can convert 4 species to matrix,is it this coding is not suitable to flatten large amount of images? Thank you.
path1 = 'C:/Users/Z/Documents/Python Scripts/Input'      
path2 = 'C:/Users/Z/Documents/Python Scripts/Input_resized'   

listing = os.listdir(path1) 
#num_samples=size(listing)

for file in listing:
    im = Image.open(path1 + '\\' + file)
    img_rows, img_cols = 224, 224  
    img = im.resize((img_rows,img_cols),3)  
#    gray = img.convert('L')  
    img.save(path2 +'\\' +  file, "JPEG")

imlist = os.listdir(path2)

im1 = array(Image.open(path2 + '/'+ imlist[0])) 
m,n = im1.shape[0:2] 
imnbr = len(imlist) 
num_samples = len(imlist)
print (imnbr)


immatrix = array([array(Image.open(path2+ '/' + im2)).flatten()
              for im2 in imlist],'f')
label=np.ones((num_samples,),dtype = int)
label[0:1315]=0
label[1315:1977]=1
label[1977:2689]=2
label[2689:3861]=3
label[3861:5427]=4
label[5427:6446]=5
label[6446:6966]=6
label[6966:7985]=7
label[7985:8997]=8
label[8997:9311]=9
label[9311:10622]=10
label[10622:11477]=11
label[11477:12990]=12
label[12990:14405]=13
label[14405:15660]=14
label[15660:15948]=15
label[15948:17042]=16
label[17042:18060]=17
label[18060:19193]=18
label[19193:20360]=19
label[20360:21970]=20


data,Label = shuffle(immatrix,label, random_state=2)
train_data = [data,Label]
Reply
#2
Please, post the full traceback you get, in error tags
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Error:
runfile('C:/Users/Z/gray scale.py', wdir='C:/Users/Z') Using TensorFlow backend. C:\Users\Z\Anaconda3\envs\deeplearning\lib\site-packages\sklearn\cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20. "This module will be removed in 0.20.", DeprecationWarning) 21970 Traceback (most recent call last): File "<ipython-input-1-2c7840cbbd80>", line 1, in <module> runfile('C:/Users/Z/gray scale.py', wdir='C:/Users/Z') File "C:\Users\Z\Anaconda3\envs\deeplearning\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile execfile(filename, namespace) File "C:\Users\Z\Anaconda3\envs\deeplearning\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/Z/gray scale.py", line 46, in <module> for im2 in imlist],'f') ValueError: setting an array element with a sequence.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Array NaN values? ValueError: continuous format is not supported metalray 3 9,584 Jan-05-2018, 08:56 AM
Last Post: metalray
  reshaping numpy array ValueError: total size of new array must be unchanged metalray 0 5,893 Jul-15-2017, 05:41 PM
Last Post: metalray
  ValueError: shape mismatch: value array of shape... ulrich48155 2 23,669 Jul-10-2017, 02:17 PM
Last Post: ulrich48155
  2D array element Calculation poonck1 1 3,352 Mar-02-2017, 03:54 PM
Last Post: wavic
  How to increase speed of access element in 2 dimension array? Diver 6 6,029 Nov-03-2016, 03:42 AM
Last Post: Diver

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020