2016-04-08 7 views
2

boyutunda "gradient stili" boyutsal degrade tarzı dizi gradient descent using python and numpy 'u kontrol ettim ancak sorunumu çözmedi.Python'daki

image-processing ile aşina olmaya çalışıyorum ve Python'da etrafta dolaşmak için birkaç test dizisi oluşturmak istiyorum.

İç girdilerin bir tür degrade oluşturduğu bir m x n dizisi oluşturmak için bir yöntem (np.arange gibi) var mı?

İstenen çıktıyı üretmek için saf bir yöntem örneği yaptım.

Degrade terimiyle ilgili genelleme, renkte düzgün geçiş olarak basit anlamında kullanıyorum.

#!/usr/bin/python 
import numpy as np 
import matplotlib.pyplot as plt 

#Set up parameters 
m = 15 
n = 10 
A_placeholder = np.zeros((m,n)) 
V_m = np.arange(0,m).astype(np.float32) 
V_n = np.arange(0,n).astype(np.float32) 

#Iterate through combinations 
for i in range(m): 
    m_i = V_m[i] 
    for j in range(n): 
     n_j = V_n[j] 
     A_placeholder[i,j] = m_i * n_j #Some combination 

#Relabel 
A_gradient = A_placeholder 
A_placeholder = None 

#Print data 
print A_gradient 
#[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] 
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.] 
[ 0. 2. 4. 6. 8. 10. 12. 14. 16. 18.] 
[ 0. 3. 6. 9. 12. 15. 18. 21. 24. 27.] 
[ 0. 4. 8. 12. 16. 20. 24. 28. 32. 36.] 
[ 0. 5. 10. 15. 20. 25. 30. 35. 40. 45.] 
[ 0. 6. 12. 18. 24. 30. 36. 42. 48. 54.] 
[ 0. 7. 14. 21. 28. 35. 42. 49. 56. 63.] 
[ 0. 8. 16. 24. 32. 40. 48. 56. 64. 72.] 
[ 0. 9. 18. 27. 36. 45. 54. 63. 72. 81.] 
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90.] 
[ 0. 11. 22. 33. 44. 55. 66. 77. 88. 99.] 
[ 0. 12. 24. 36. 48. 60. 72. 84. 96. 108.] 
[ 0. 13. 26. 39. 52. 65. 78. 91. 104. 117.] 
[ 0. 14. 28. 42. 56. 70. 84. 98. 112. 126.]] 

#Show Image 
plt.imshow(A_gradient) 
plt.show() 

enter image description here

Ben np.gradient denedim ama bana istenen çıktı vermedi.

#print np.gradient(np.array([V_m,V_n])) 
#Traceback (most recent call last): 
# File "Untitled.py", line 19, in <module> 
#  print np.gradient(np.array([V_m,V_n])) 
# File "/Users/Mu/anaconda/lib/python2.7/site-packages/numpy/lib/function_base.py", line 1458, in gradient 
#  out[slice1] = (y[slice2] - y[slice3]) 
#ValueError: operands could not be broadcast together with shapes (10,) (15,) 

cevap

2
A_placeholder[i,j] = m_i * n_j 

gibi herhangi bir işlem kullanılarak Numpy ifade edilebilir broadcasting

A = np.arange(m)[:, None] * np.arange(n)[None, :]