Nima uchun numpy ning qatorini o'zgartira olmayman?

Mening qatorim quyidagicha o'xshash tasvirlardan iborat:

[[[ 66.17041352  32.64576397  20.96214396]                                                                                                                               
  [ 66.17041352  32.64576397  20.96214396]
  [ 65.96318838  32.36065031  16.13857633]
  ...,
  [ 69.04849876  28.06324166  26.57747623]
  [ 63.7269604   32.96378326  25.94336956]
  [ 53.96807994  39.33219382  23.9025511 ]]

 ...,
 [[ 18.55833403  34.4104455   -9.75497344]
  [ 18.55833403  34.4104455   -9.75497344]
  [ 21.45103128  32.77919479  -3.84284208]
  ...,
  [ 44.64859327  41.89617915  14.25196745]
  [ 43.40291913  43.25109885  17.43372679]
  [ 43.30009306  47.94315449  15.59464532]]

 [[ 18.64249436  31.63054472  -7.56023249]
  [ 18.64249436  31.63054472  -7.56023249]
  [ 23.23099091  32.284216    -3.86411699]
  ...,
  [ 44.98536772  45.0246078   17.92556564]
  [ 45.53417128  45.42120428  17.50264622]
  [ 46.7226915   45.42428651  19.21054283]]]

Jildni shu tarzda nolga o'tkazmoqchiman:

[[[ 0.  0.  0.]
  [ 0.  0.  0.]
  [ 0.  0.  0.]
  ...,
  [ 0.  0.  0.]
  [ 0.  0.  0.]
  [ 0.  0.  0.]]

 ...,

 [[ 0.  0.  0.]
  [ 0.  0.  0.]
  [ 0.  0.  0.]
  ...,

 [[ 0.  0.  0.]
  [ 0.  0.  0.]
  [ 0.  0.  0.]
  ...,
  [ 0.  0.  0.]
  [ 0.  0.  0.]
  [ 0.  0.  0.]]]

Buni qanday amalga oshirishni bilaman, lekin men kelib chiqish kodi nima uchun ishlashini bilmayman. Chop etish uchun hech narsa o'zgarmasligini ko'rsatadi.

for row in image_arr:
    for col in row:
        col = [0,0,0]

print image_arr
2

6 javoblar

Sizning loopingizda siz faqat col nomi [0,0,0] ro'yxatiga qayta va qayta-qayta topshirasiz. Siz istagan narsalarni emas! 3-qatorni barcha nollarga o'zgartirish uchun siz faqat buni bajarasiz:

arr[:, :, :,] = 0

Bing portlashi boom, ishingiz tugadi.

3
qo'shib qo'ydi
Yoki kamroq belgilarda arr [...] = 0 yoki hatto arr [()] = 0
qo'shib qo'ydi muallif Eric, manba
Bu col = [0,0,0] faqatgina nomini qaytaruvchi - bu juda muhim masala. Numpy bu erda nusxalarini qaytarib bermasligi kerak, lekin mutatsion bo'lishi mumkin bo'lgan asl ob'ektning ko'rinishi ( col [:] = 0 kabi) faqatgina numpy ; col = [0, 0, 0] hech qachon ishlamagani haqiqat Python haqida asosiy faktdir.
qo'shib qo'ydi muallif DSM, manba

col kodini yangi ro'yxatga kiritasiz, lekin col satr pastki ro'yxatiga havola emas. Buning o'rniga, col elements ni o'zgartirsangiz, siz kerakli natijani olasiz:

for row in image_arr:
    for col in row:
        for i in range(len(col))
            col[i] = 0
0
qo'shib qo'ydi

col is a 1d array, a view of arr. To change its values you need to use slicing notation. col=[0,0,0] reassigns the variable without mutating the iteration variable. Mutability is a key concept here (and applicable to lists and dictionaries as well).

In [254]: arr = np.ones((2,3,4))
In [255]: for row in arr:
     ...:     for col in row:
     ...:         col[:] = 0   # or = [1,2,3,5]
     ...:         
In [256]: arr
Out[256]: 
array([[[ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.]],

       [[ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.]]])

Barcha qiymatlarni o'zgartirmoqchi bo'lganingiz uchun col [:] = 0 ham col [:] = [0,0,0,0] mening shaklimda).

Ammo, biz bu erda bo'lsak, ulardan bittasi ham ishlaydi

In [257]: for row in arr:
     ...:     row[:,:] = 1
     ...:     
In [258]: arr
Out[258]: 
array([[[ 1.,  1.,  1.,  1.],
        [ 1.,  1.,  1.,  1.],
 ...]]])
In [259]: arr[:,:,:]=2     # one : per dimension
In [260]: arr[...] = 3     # ... shorthand for multiple :

Lekin nima uchun arr ni tiklash kerak? Nima uchun bir xil shakldagi yangi qatorni yaratib bo'lmaydi (va "original" ni tashlab ketasiz)?

arr1 = np.zeros_like(arr)
0
qo'shib qo'ydi

Buning sababi col nusxasi emas. Bu barcha piton uchun amal qiladi:

CODE Misol:

simple = [1,2,3,4,5,6]
print simple
for elem in simple:
    elem = 0
print simple

OUTPUT:

[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]

Buning o'rniga harakat qilib ko'ring:

rows,cols,lens = arr.shape
for r in range(rows):
    for c in range(cols):
        arr[r][c] = [0,0,0]
0
qo'shib qo'ydi
col hech narsa nusxasi emas. arr [r] bo'lsa, u holda arr [r] [c] = bir narsa ham arr .
qo'shib qo'ydi muallif DSM, manba

Loopda qiymat chizig'ini o'zgartirasiz, lekin u original o'zgaruvchiga image_arr bilan bog'liq emas. Indeksga kirish va to'g'ridan-to'g'ri image_arr o'zgaruvchini o'zgartirish uchun enumerate dan foydalanishingiz mumkin. Quyidagi misolda bo'lgani kabi:

import numpy as np
image_arr = np.arange(30).reshape(3, 5, 2)
print(image_arr)

for i,row in enumerate(image_arr):
    for j,col in enumerate(row):
        image_arr[i][j]=0

print(image_arr)
0
qo'shib qo'ydi

Is because col is a value not a reference , see Python objects confusion: a=b, modify b and a changes!

Buning o'rniga:

a=0
b=0

for row in image_arr:

    for col in row:
        image_arr[a][b] = [0,0,0]
        a=a+1
    b=b+1

print image_arr
0
qo'shib qo'ydi
Python
Python
372 ishtirokchilar

Bu guruh python dasturlash tilini muhokama qilish uchun. Iltimos, o'zingizni hurmat qiling va faqat dasturlash bo'yicha yozing. Botlar mavzusini @botlarhaqida guruhida muhokama qling! FAQ: @PyFAQ Offtopic: @python_uz_offtopic

Python offtopic group !
Python offtopic group !
150 ishtirokchilar

@python_uz gruppasining offtop gruppasi. offtop bo'lsa ham reklama mumkin emas ) Boshqa dasturlash tiliga oid gruppalar @languages_programming