Numpy


In [2]:
import numpy as np

data type


In [54]:
a = np.arange(10)
a


Out[54]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [33]:
type(a)


Out[33]:
numpy.ndarray

In [80]:
a.dtype


Out[80]:
dtype('int32')

In [81]:
a.shape


Out[81]:
(10,)

In [38]:
a = np.arange(10.0)
a


Out[38]:
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.])

In [111]:
a.dtype


Out[111]:
dtype('int32')

In [115]:
a = array(["1","2","3"])
a


Out[115]:
array(['1', '2', '3'], 
      dtype='|S1')

In [116]:
a = array(["1","2","3",1,2,3])
a


Out[116]:
array(['1', '2', '3', '1', '2', '3'], 
      dtype='|S1')

In [44]:
a = np.arange(1,10)
a


Out[44]:
array([1, 2, 3, 4, 5, 6, 7, 8, 9])

In [48]:
a = np.arange(1,10,2)
a


Out[48]:
array([1, 3, 5, 7, 9])

In [50]:
a = np.arange(10,1,-1)
a


Out[50]:
array([10,  9,  8,  7,  6,  5,  4,  3,  2])

In [53]:
print('a[0] = %s, a[1] = %s') % (a[0],a[1])


a[0] = 10, a[1] = 9

In [11]:
b = array([1,2,3,4,5])

In [13]:
type(b)


Out[13]:
numpy.ndarray

In [15]:
b ** 2


Out[15]:
array([ 1,  4,  9, 16, 25])

In [16]:
b + 1


Out[16]:
array([2, 3, 4, 5, 6])

In [18]:
a + b


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-f96fb8f649b6> in <module>()
----> 1 a + b

ValueError: operands could not be broadcast together with shapes (10,) (5,) 

In [117]:
a1 = array([1,2,3,4,5])
a2 = array([5,4,3,2,1])
a1 + a2


Out[117]:
array([6, 6, 6, 6, 6])

In [128]:
a1_a2 = concatenate((a1,a2))


Out[128]:
(10,)

多維


In [103]:
m = array([arange(1,4),arange(4,7)])
m


Out[103]:
array([[1, 2, 3],
       [4, 5, 6]])

In [105]:
m.shape


Out[105]:
(2, 3)

In [107]:
print(m[0,0])
print(m[0,1])
print(m[1,0])
print(m[1,1])


1
2
4
5

In [171]:
m = array([arange(1,5),arange(4,7)])
m


Out[171]:
array([array([1, 2, 3, 4]), array([4, 5, 6])], dtype=object)

In [172]:
m.shape


Out[172]:
(2,)

In [173]:
m[0]


Out[173]:
array([1, 2, 3, 4])

In [131]:


In [145]:
a1 = array([[1,2],[3,4]])
a2 = array([5,6])
print(a1)
print(a2)


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

In [147]:
concatenate((a1,a2),axis=0)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-147-7abe06269518> in <module>()
----> 1 concatenate((a1,a2),axis=0)

ValueError: all the input arrays must have same number of dimensions

In [152]:
print(a1.shape)
print(a2.shape)


(2, 2)
(2,)

In [161]:
a1 = array([[1,2],[3,4]])
a2 = array([[5,6]])
print(a1)
print(a2)


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

In [164]:
print(a1.shape)
print(a2.shape)


(2, 2)
(1, 2)

In [166]:
concatenate((a1,a2),axis=0)


Out[166]:
array([[1, 2],
       [3, 4],
       [5, 6]])

In [1]:
concatenate((a1,a2.T),axis = 1)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-ab3a56bc0583> in <module>()
----> 1 concatenate((a1,a2.T),axis = 1)

NameError: name 'concatenate' is not defined

操作


In [4]:
a = np.arange(24)

In [5]:
a


Out[5]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23])

In [7]:
b = a.reshape(2,3,4)

In [9]:
b


Out[9]:
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

In [11]:
b[0,0,0]


Out[11]:
0

In [13]:
b[:,0,0]


Out[13]:
array([ 0, 12])

In [15]:
b[0,:,:]


Out[15]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [16]:
b[0,...]


Out[16]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [22]:
b[0,1,::2]


Out[22]:
array([4, 6])

In [24]:
b[0]+b[1]


Out[24]:
array([[12, 14, 16, 18],
       [20, 22, 24, 26],
       [28, 30, 32, 34]])

In [42]:
b[0] * b[1]


Out[42]:
array([[  0,  13,  28,  45],
       [ 64,  85, 108, 133],
       [160, 189, 220, 253]])

In [53]:
np.arange(3*2).reshape(3,2).dot(np.arange(3*2).reshape(3,2).T)


Out[53]:
array([[ 1,  3,  5],
       [ 3, 13, 23],
       [ 5, 23, 41]])

In [29]:
#計算一二維矩陣每個欄的總和, 平均數, 標準差

In [33]:
a = np.arange(10).reshape(2,5)

In [35]:
a


Out[35]:
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

In [41]:
a[:,1].sum()


Out[41]:
7