Guest User

Untitled

a guest
Jul 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. tupleS = numpy.zeros( (N , 2) )
  2. tupleS[:,0] = S
  3. tupleS[:,1] = S
  4. product = A * tupleS
  5.  
  6. >> A = numpy.array(range(10)).reshape(5, 2)
  7. >>> B = numpy.array(range(5))
  8. >>> B
  9. array([0, 1, 2, 3, 4])
  10. >>> A * B
  11. Traceback (most recent call last):
  12. File "<stdin>", line 1, in <module>
  13. ValueError: shape mismatch: objects cannot be broadcast to a single shape
  14. >>> B = B.reshape(5, 1)
  15. >>> B
  16. array([[0],
  17. [1],
  18. [2],
  19. [3],
  20. [4]])
  21. >>> A * B
  22. array([[ 0, 0],
  23. [ 2, 3],
  24. [ 8, 10],
  25. [18, 21],
  26. [32, 36]])
  27.  
  28. product = A * S
  29.  
  30. >>> S = np.arange(5)
  31. >>> S
  32. array([0, 1, 2, 3, 4])
  33. >>> A = np.arange(10).reshape((5,2))
  34. >>> A
  35. array([[0, 1],
  36. [2, 3],
  37. [4, 5],
  38. [6, 7],
  39. [8, 9]])
  40. >>> S[:,None]
  41. array([[0],
  42. [1],
  43. [2],
  44. [3],
  45. [4]])
  46. >>> A * S[:,None]
  47. array([[ 0, 0],
  48. [ 2, 3],
  49. [ 8, 10],
  50. [18, 21],
  51. [32, 36]])
  52.  
  53. In []: N= 5
  54. In []: A= rand(N, 2)
  55. In []: A.shape
  56. Out[]: (5, 2)
  57.  
  58. In []: S= rand(N)
  59. In []: S.shape
  60. Out[]: (5,)
  61.  
  62. In []: A* S
  63. ------------------------------------------------------------
  64. Traceback (most recent call last):
  65. File "<ipython console>", line 1, in <module>
  66. ValueError: operands could not be broadcast together with shapes (5,2) (5)
  67.  
  68. In []: A* S[:, None]
  69. Out[]:
  70. array([[ 0.54216549, 0.04964989],
  71. [ 0.41850647, 0.4197221 ],
  72. [ 0.03790031, 0.76744563],
  73. [ 0.29381325, 0.53480765],
  74. [ 0.0646535 , 0.07367852]])
  75.  
  76. In []: expand_dims(S, 1).shape
  77. Out[]: (5, 1)
  78.  
  79. In []: S= rand(N, 1)
  80. In []: S.shape
  81. Out[]: (5, 1)
  82.  
  83. In []: A* S
  84. Out[]:
  85. array([[ 0.40421854, 0.03701712],
  86. [ 0.63891595, 0.64077179],
  87. [ 0.03117081, 0.63117954],
  88. [ 0.24695035, 0.44950641],
  89. [ 0.14191946, 0.16173008]])
  90.  
  91. product = A * numpy.tile(S, (2,1)).T
  92.  
  93. product = [d * S for d in A.T]
  94.  
  95. product = numpy.array([d * S for d in A.T]).T
Add Comment
Please, Sign In to add comment