免責聲明

Disclaimer (免責聲明)
繼續閱覽代表您接受以上的免責聲明.
To continue reading means you accept the above disclaimer.

2014年12月18日 星期四

try ipytho#5 , numpy, array,matrix, vector

//=== numpy support matrix operation by numpy.array
import numpy as np
arr= np.random.rand(3,5)

arr.shape             ### give the dimension of the matrix
Out[80]: (3L, 5L)

print arr
[[ 0.67248573  0.48092984  0.69184439  0.71945406  0.57882278]
 [ 0.79634702  0.02227532  0.77490943  0.36783077  0.98777438]
 [ 0.36116686  0.74907581  0.23774053  0.82991951  0.29351733]]

b=arr.T          ### perform matrix transpose

b
Out[83]:
array([[ 0.67248573,  0.79634702,  0.36116686],
       [ 0.48092984,  0.02227532,  0.74907581],
       [ 0.69184439,  0.77490943,  0.23774053],
       [ 0.71945406,  0.36783077,  0.82991951],
       [ 0.57882278,  0.98777438,  0.29351733]])

b.shape
Out[84]: (5L, 3L)


//=== indexer, i:j , i:j:s
i : j == i : j : 1  --> from i to j with increment step 1 ,  j is not included
i : j : s  --> from i to j with increment step s,  j is not included


 arr[ : , 0]  : get the column 0
 arr[..., :4] == arr[..., 0:4] : get column 0 -- 3 
 arr[..., :5] : get column 0 -- 4 

arr[:, 3:1:-1]
Out[95]: 
array([[ 0.71945406,  0.69184439],
       [ 0.36783077,  0.77490943],
       [ 0.82991951,  0.23774053]])



//=== bracket affect [ ]
arr= np.array([np.random.rand(3,5)])
arr.shape
Out[76]: (1L, 3L, 5L)

arr= np.array(np.random.rand(3,5))
arr.shape
Out[78]: (3L, 5L)

arr= np.random.rand(3,5)
arr.shape
Out[80]: (3L, 5L)




//=== double bracket affect
import numpy as np
arr1= np.array([1,2,3])

type(arr1)
Out[45]: numpy.ndarray

arr1.shape
Out[47]: (3L,)

arr2= np.array([[1,2,3]])
arr2.shape
Out[49]: (1L, 3L)

arr3= np.array([[1],[2],[3]])
arr3.shape
Out[51]: (3L, 1L)

arr4=arr3.T
arr4.shape
Out[53]: (1L, 3L)

arr5= arr1.T
arr5.shape
Out[55]: (3L,)


*** although type(arr1) shows numpy.ndarray ,
arr1 is actually a vector( === column vector === row vector ?)
the transpose of arr1 is still a vector containing 3 elements --> 3-tuple vector
i.e. transpose has no effect on vector

*** arr2 is a row 'vector' (single-row matrix)
*** arr3 is a column 'vector'(single-column matrix)



//=== the built-in list of Python not support transpose
L1=[1,2,3]

type(L1)
Out[61]: list

L1.T
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-62-167a2304fdf0> in <module>()
----> 1 L1.T

AttributeError: 'list' object has no attribute 'T'




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

type(LL1)
Out[64]: list

LL1.T
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-65-49e25d799db3> in <module>()
----> 1 LL1.T

AttributeError: 'list' object has no attribute 'T'

沒有留言:

張貼留言