numpy

  • numpy

import numpy as np
为什么numpy运算比纯Python要块

属性
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
数组维数,一维是1,二维是2...
ndarray.size
数组中的元素 数量,总的数据量 二维矩阵5行8列数量为40
ndarray.itemsize
一个数组元素的 空间大小(字节)
ndarray.dtype
ndarray的类型

np.bool
用一个字节存储的布尔类型(True或False)
'b'
np.int8
一个字节大小,-128 至 127
'i'
np.int16
整数,-32768 至 32767
'i2'
np.int32
整数,-2 31 至 2 32 -1
'i4'
np.int64
整数,-2 63 至 2 63 - 1
'i8'
np.uint8
无符号整数,0 至 255
'u'
np.uint16
无符号整数,0 至 65535
'u2'
np.uint32
无符号整数,0 至 2 ** 32 - 1
'u4'
np.uint64
无符号整数,0 至 2 ** 64 - 1
'u8'
np.float16
半精度浮点数:16位,正负号1位,指数5位,精度10位
'f2'
np.float32
单精度浮点数:32位,正负号1位,指数8位,精度23位
'f4'
np.float64
双精度浮点数:64位,正负号1位,指数11位,精度52位
'f8'
np.complex64
复数,分别用两个32位浮点数表示实部和虚部
'c8'
np.complex128
复数,分别用两个64位浮点数表示实部和虚部
'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')

若不指定,整数默认int64,小数默认float64
字符串 np.string_

创建

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]

不包括30

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.]]]

三维 3行 4列

随机
numpy.random.random((2,3))
生成2行3列的矩阵 介于0-1之间

[
[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.]])
]
在3-5构成第二个array

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

b = a.view()

b is a
false   id 不一样


b.shape=2,6

改变b的结构
[[   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]
按照列, 
第一列  索引1 的4大
第二列 索引0 的3大
。。。。。
得出  1001

按照索引取出相应值
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]]

二次学习





原文地址:https://www.cnblogs.com/cizao/p/11484346.html