The difference between the array [i] [:] and the array [i ,:]

I am new to python, so I used array[i][j]instead array[i,j]. Today the script that I created after the tutorial didn't work until it found out what I used

numpy.dot(P[0][:], Q[:][0])

instead

numpy.dot(P[0,:], Q[:,0])

For some reason, the second one works, and the first gives me a form error. Matrix sizes: MxK and KxN.

I tried to print both P[0][:], and P[0,:], run id(), type()and P[0][:].shape, but could not find the reason. Why are these things different?

I run it on Jupyter Notebook 4.3.0 and Python 2.7.13.

+4
source share
2 answers

[i, j] [i][j] numpy. , .

, :

>>> import numpy as np
>>> arr = np.arange(16).reshape(4, 4)
>>> arr 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

[:], , [1, :] [:, 1], , (). , : , , , ::

>>> arr[:]
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

>>> arr[:, 1]  #  get the second column
array([ 1,  5,  9, 13])
>>> arr[:][1]  # get a new view of the array, then get the second row
array([4, 5, 6, 7])

, [1] [1, ...] (... - Ellipsis), 2D [1, :].

, ( ):

>>> arr[1, :]  # get the second row
array([4, 5, 6, 7])
>>> arr[1][:]  # get the second row, then get a new view of that row
array([4, 5, 6, 7])
+4

x[:] , , x . view - , . , numpy .

2d , A[0,:] A[:, 1:5], : , , . : Python slice(None,None,None), start:stop:step slice(start, stop, step).

A[0,:], A[0], "" A "". , .

A[:,0] 0- .

A[0][:] A[0,:][:] [:] A[0,:], , 1- ( 1- ).

A[:][0] A[:,0]; , A[0,:]. A[:] - , A[:,:] 2d.

, A.__getitem__(...). [] .

A[:] = ... [:] , .


2 :

numpy.dot(P[0][:], Q[:][0])
numpy.dot(P[0,:], Q[0,:])
+3

Source: https://habr.com/ru/post/1677310/


All Articles