• numpy

import numpy as np

ndarray.shape

a = numpy.arange(12)
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

a.shape=(2,6)
[[ 0  1  2  3  4  5]
[ 6  7  8  9 10 11]]

a.shape=(3,2,2)
array([[[ 0,  1],
[ 2,  3]],

[[ 4,  5],
[ 6,  7]],

[[ 8,  9],
[10, 11]]])


ndarray.ndim

ndarray.size

ndarray.itemsize

ndarray.dtype
ndarray的类型

np.bool

'b'
np.int8

'i'
np.int16

'i2'
np.int32

'i4'
np.int64

'i8'
np.uint8

'u'
np.uint16

'u2'
np.uint32

'u4'
np.uint64

'u8'
np.float16

'f2'
np.float32

'f4'
np.float64

'f8'
np.complex64

'c8'
np.complex128

'c16'
np.object_
python对象
'O'
np.string_

'S'
np.unicode_
unicode类型
'U'


a = np.array([[1, 2, 3],[4, 5, 6]], dtype=np.float32)
a.dtype
dtype('float32')

arr = np.array(['python', 'tensorflow', 'scikit-learn', 'numpy'], dtype = np.string_)
arr
array([b'python', b'tensorflow', b'scikit-learn', b'numpy'], dtype='|S12')


a = numpy.arange(15)
b = a.reshape(3,5)

[[ 0  1  2  3  4]
[ 5  6  7  8  9]
[10 11 12 13 14]]

a.reshape((3,-1))

[[ 0  1  2  3  4]
[ 5  6  7  8  9]
[10 11 12 13 14]]

a.reshape((5,-1))

[[ 0  1  2]
[ 3  4  5]
[ 6  7  8]
[ 9 10 11]
[12 13 14]]

c = a.reshape((3,-1))

e = c.T

c
[[ 0  1  2  3  4]
[ 5  6  7  8  9]
[10 11 12 13 14]]

e
[[ 0  5 10]
[ 1  6 11]
[ 2  7 12]
[ 3  8 13]
[ 4  9 14]]


创建配置文件
jupyter notebook --generate-config
vi ~/.jupyter/jupyter_notebook_config.py

## (bytes/sec) Maximum rate at which messages can be sent on iopub before they
#  are limited.
c.NotebookApp.iopub_data_rate_limit = 10000000


ndarray运算

a = numpy.array([1,2,3,4])
print(a==4)

>>
[False False False  True]
numpy会用矩阵对象的每一个数据和值作比较，返回布尔值

b = numpy.array([[1,2,3,4],[2,3,4,5]])
print(b == 5)

>>
[[False False False False]
[False False False  True]]

b = numpy.array([[1,2,3,4],[2,3,4,5]])

c = (b==5)

print(b[c])

>>[5]


a = numpy.array([1,2,3,0])
print(a.min())    .>>> 0
print(a.max())    >>> 3


a = numpy.array([[1,5],[5,0]])
b = numpy.array([[2,8],[1,10]])

a
[[1 5]
[5 0]]

b
[[ 2  8]
[ 1 10]] 2

numpy.dot(a,b)

a.dot(b)

1*2 +5*1 =7
1*8 + 5*10 = 58
5*2 +0*1 =10
5*8 +0*10 = 40

[[ 7 58]
[10 40]]


a = numpy.array([1,2,3,4])
print(a,a.dtype)
>>>>
[1 2 3 4] int32

a = numpy.array([1,2,3,4.0])
print(a,a.dtype)
>>>
[1. 2. 3. 4.] float64

a = numpy.array([1,2,3,'4'])
print(a,a.dtype)
>>>
['1' '2' '3' '4'] <U11

a = numpy.array([1,2,3,4])
a
>>
[1 2 3 4]

b = numpy.array([[1,2,3,4],[2,3,4,5]])
b
>>
[[1 2 3 4]
[2 3 4 5]]

c = numpy.array([[[1,2,3,4],[2,3,4,5],[5,6,7,8]]])
c
>>
[[[1 2 3 4]
[2 3 4 5]
[5 6 7 8]]]


a = numpy.array([1,2,3,4])
print(a[1:3])
>>
[2 3]

a = numpy.array([[ 1  3  4  6  7]
[ 2  4  2  5 76]
[41 13 42 63 71]])

print(a[0:2])

>>
[[ 1  3  4  6  7]
[ 2  4  2  5 76]]

print(a[0:2,2])   可取列值
>>
[4 2]

b = a[:,0:2]   所有行第0列和第1列的数据
[[ 1  3]
[ 2  4]
[41 13]]



按照行
c = numpy.array(
[[
[10,20,30,40],
[20,30,40,50],
[50,60,70,80]
]])

1
print(c.sum(axis=1))

>>
[100 140 260]

c = numpy.array(
[[
[10,20,30,40],
[20,30,40,50],
[50,60,70,80]
]])

2
print(c.sum(axis=2))

>>
[[100 140 260]]

c = numpy.array(
[[
[10,20,30,40],
[20,30,40,50],
[50,60,70,80]
]])

1
print(c.sum(axis=1))

>>
[100 140 260]

c = numpy.array(
[[
[10,20,30,40],
[20,30,40,50],
[50,60,70,80]
]])

2
print(c.sum(axis=2))

>>
[[100 140 260]]


a = numpy.arange(15)
print(a)
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]

a = numpy.arange(2,30,2)
[ 2  4  6  8 10 12 14 16 18 20 22 24 26 28]

numpy.linspace(0,100,11)
[  0.  10.  20.  30.  40.  50.  60.  70.  80.  90. 100.]

a = numpy.zeros((4,))
print(a)

[0. 0. 0. 0.]

a = numpy.zeros((4,6))
print(a)

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

numpy.ones((元组))
a = numpy.ones((2,3,4))
print(a)

[[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]

[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]]



numpy.random.random((2,3))

[
[0.07261529 0.37003586 0.00799021]
[0.24945076 0.92461768 0.80258728]
]


e的次幂计算
a = numpy.arange(3)

numpy.exp(a))

e的0次
e的1次
e的2次

e=2.7182
[1.         2.71828183  7.3890561 ]

a = numpy.sqrt(81)

a
9.0

type(a)
<class 'numpy.float64'>

a.dtype
float64

a = numpy.arange(3)
numpy.sqrt(a)
[0.         1.         1.41421356]



numpy.floor(矩阵)
a = numpy.array([1.3,2.4,5.6,6.1,6.9])
[1.3 2.4 5.6 6.1 6.9]

b = numpy.floor(a)
[1. 2. 5. 6. 6.]

a = numpy.floor(10*numpy.random.random((2,3)))
b = numpy.floor(10*numpy.random.random((2,3)))

a
[[9. 1. 6.]
[1. 3. 8.]]

b
[[7. 7. 0.]
[1. 9. 8.]]

numpy.hstack((a,b))

[[9. 1. 6. 7. 7. 0.]
[1. 3. 8. 1. 9. 8.]]

[[1. 0. 6.]
[0. 8. 8.]]

[[4. 8. 2.]
[9. 7. 4.]]

vstack

[[1. 0. 6.]
[0. 8. 8.]
[4. 8. 2.]
[9. 7. 4.]]

[[3. 4. 4. 2. 9. 3.]
[7. 9. 1. 1. 8. 7.]
[8. 8. 6. 2. 2. 7.]
[5. 5. 8. 7. 8. 1.]]

numpy.vsplit(a,2)

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

numpy.hsplit(a,(3,4))
a=
numpy.floor(10 * numpy.random.random((2, 12)))
[[3. 8. 3. 5. 1. 9. 7. 5. 9. 1. 1. 0.]
[0. 4. 3. 9. 9. 0. 0. 2. 8. 1. 0. 7.]]

numpy.hsplit(a,(3,5))
[
array([[3., 8., 3.],
[0., 4., 3.]]),
array([[5., 1.],
[9., 9.]]),
array([[9., 7., 5., 9., 1., 1., 0.],
[0., 0., 2., 8., 1., 0., 7.]])
]

view    浅复制
a = numpy.arange(12)

b = a.view()

b is a
false   id 不一样

b.shape=2,6

[[   0    1    2    3 1234    5]
[   6    7    8    9   10   11]]

a
[   0    1    2    3  4    5    6    7    8    9   10   11]

b[0,4] =1234  改变b中一个值
b
[[   0    1    2    3 1234    5]
[   6    7    8    9   10   11]]

a
[   0    1    2    3 1234    5    6    7    8    9   10   11]

copy    深复制
a = numpy.arange(12)
b = a.copy()

[ 0  1  2  3  4  5  6  7  8  9 10 11]
[ 0  1  2  3  4  5  6  7  8  9 10 11]

b.shape=3,4
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

b[0,1] = 32
[[ 0 32  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

a
[ 0  1  2  3  4  5  6  7  8  9 10 11]

a.argmax(axis=0)
a = numpy.array([[1,3,61,5],[4,2,55,6]])
[[ 1  3 61  5]
[ 4  2 55  6]]

index = a.argmax(axis=0)
[1 0 0 1]

。。。。。

max = a[index,range(a.shape[1])]

[ 4  3 61  6]

a.argmax(axis=1)
index1 = a.argmax(axis=1)
[2 2]


a = numpy.array([[1,3,61,5],[4,2,55,6]])

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

b = numpy.sort(a）
[[ 1  3  5 61]
[ 2  4  6 55]]