文章目录
np.array
就是创建一个数组。比如:
import numpy as np
a = np.array([[1, 2], [3, 4]])
输出结果就是:
[
[
1
2
]
[
3
4
]
]
[[1\ 2]\\ \ [3\ 4]]
[[1 2] [3 4]]
常见的还有两个参数
参数 | 意义 |
---|---|
d t y p e dtype dtype | 数据类型,可选参数,默认 n p . f l o a t 64 np.float64 np.float64 |
n d m i n ndmin ndmin | 最小维度。比如一维的也给你整成二维的。 |
里面 n d m i n ndmin ndmin不大好理解,举个例子。
import numpy as np
a = np.array([1, 2, 3, 4], ndmin=2)
print(a)
输出结果为: [ [ 1 2 3 4 ] ] [[1\ 2\ 3\ 4]] [[1 2 3 4]]
NumPy中的各种数据类型
名称 | 描述 |
---|---|
b o o l _ bool\_ bool_ | 布尔型数据类型( T r u e True True 或者 F a l s e False False) |
i n t _ int\_ int_ | 默认的整数类型(类似于 C C C语言中的 l o n g long long, i n t 32 int32 int32 或 i n t 64 int64 int64) |
i n t c intc intc | 与 C C C 的 i n t int int 类型一样,一般是 i n t 32 int32 int32 或 i n t 64 int 64 int64 |
i n t p intp intp | 用于索引的整数类型(类似于 C C C 的 s s i z e _ t ssize\_t ssize_t,一般情况下仍然是 i n t 32 int32 int32 或 i n t 64 int64 int64) |
i n t 8 int8 int8 | 字节( − 128 -128 −128 t o to to 127 127 127) |
i n t 16 int16 int16 | 整数( − 32768 -32768 −32768 t o to to 32767 32767 32767) |
i n t 32 int32 int32 | 整数( − 2147483648 -2147483648 −2147483648 t o to to 2147483647 2147483647 2147483647) |
i n t 64 int64 int64 | 整数( − 9223372036854775808 -9223372036854775808 −9223372036854775808 t o to to 9223372036854775807 9223372036854775807 9223372036854775807) |
u i n t 8 uint8 uint8 | 无符号整数( 0 0 0 t o to to 255 255 255) |
u i n t 16 uint16 uint16 | 无符号整数( 0 0 0 t o to to 65535 65535 65535) |
u i n t 32 uint32 uint32 | 无符号整数( 0 0 0 t o to to 4294967295 4294967295 4294967295) |
u i n t 64 uint64 uint64 | 无符号整数( 0 0 0 t o to to 18446744073709551615 18446744073709551615 18446744073709551615) |
f l o a t _ float\_ float_ | f l o a t 64 float64 float64 类型的简写 |
f l o a t 16 float16 float16 | 半精度浮点数,包括: 1 1 1 个符号位, 5 5 5 个指数位, 10 10 10 个尾数位 |
f l o a t 32 float32 float32 | 单精度浮点数,包括: 1 1 1 个符号位, 8 8 8 个指数位, 23 23 23 个尾数位 |
f l o a t 64 float64 float64 | 双精度浮点数,包括: 1 1 1 个符号位, 11 11 11 个指数位, 52 52 52 个尾数位 |
c o m p l e x _ complex\_ complex_ | c o m p l e x 128 complex128 complex128 类型的简写,即 128 128 128 位复数 |
c o m p l e x 64 complex64 complex64 | 复数,表示双 32 32 32 位浮点数(实数部分和虚数部分) |
c o m p l e x 128 complex128 complex128 | 复数,表示双 64 64 64 位浮点数(实数部分和虚数部分) |
np.zeros
就是产生一个全为 0 0 0的数组。一般的格式如下:
zeros(shape, dtype=float, order=‘C’)
参数 | 意义 |
---|---|
s h a p e shape shape | 形状 |
d t y p e dtype dtype | 数据类型,可选参数,默认 n p . f l o a t 64 np.float64 np.float64 |
o r d e r order order | 可选参数, c c c代表与 c c c语言类似,行优先; F F F代表列优先 |
举例来说:
import numpy as np
np.zeros((2,5), dtype = np.int))
输出结果为:
[
[
0
0
0
0
0
]
[
0
0
0
0
0
]
]
[[0\ \ 0\ \ 0\ \ 0\ \ 0] \\ \ [0\ \ 0\ \ 0\ \ 0\ \ 0]]
[[0 0 0 0 0] [0 0 0 0 0]]
np.unique
这个函数的意思就是去除数组里的重复元素,然后排序后输出。举例来说就是:
import numpy as np
a = np.array([1, 1, 3, 3, 2, 2])
b = np.unique(a)
print(b)
输出结果就是: [ 1 2 3 ] [1\ \ 2\ \ 3] [1 2 3]
值得注意的是:即使是多维数组,在执行这条之后也会变成一维数组。
np.arange
产生一个数列。函数格式如下:
import numpy as np
np.arange(start, stop, step, dtype)
参数 | 描述 |
---|---|
s t a r t start start | 起始值,默认为 0 0 0 |
s t o p stop stop | 终止值(不包含) |
s t e p step step | 步长,默认为 1 1 1 |
d t y p e dtype dtype | 返回 n d a r r a y ndarray ndarray的数据类型,如果没有提供,则会使用输入数据的类型。 |
例如:
x = np.arange(5)
print (x)
输出为: [ 0 1 2 3 4 ] [0 \ \ 1\ \ 2\ \ 3\ \ 4] [0 1 2 3 4]
x = np.arange(10, 20, 2)
print (x)
输出为: [ 10 12 14 16 18 ] [10\ \ 12\ \ 14\ \ 16\ \ 18] [10 12 14 16 18]
np.linspace
这是一个产生等差数列的函数。一般的格式如下:
array = np.linspace(start, end, num = num_points)
也就是确定了等差数列的起点、终点以及产生多少个数据。如果我们这么输入:
import numpy as np
array = np.linspace(0, 10, num = 5)
print(array)
那么所产生的数组就会是: [ 0 , 2.5 , 5 , 7.5 , 1 ] [0,\ 2.5,\ 5,\ 7.5,\ 1] [0, 2.5, 5, 7.5, 1]
当然,还有一些其他的常用的可选参数。
endpoint
这个参数的取值是 b o o l bool bool类型的,默认为 T r u e True True。如果设为 F a l s e False False,那么所生成的等差数列就是不会包括 s t o p stop stop的。比如:
import numpy as np
array = np.linspace(0, 10, num = 5, endpoint=False)
print(array)
那么所产生的数组就会是: [ 0 , 2 , 4 , 6 , 8 ] [0,\ 2,\ 4,\ 6,\ 8] [0, 2, 4, 6, 8]
简单理解一下,这个的意思就是让 n u m = 6 num=6 num=6,然后把最后一个数字去掉。
retstep
这个参数同样也是 b o o l bool bool类型的,默认为 F a l s e False False。如果设为 T r u e True True,那么就会在输出结果后面带上步长。比如:
import numpy as np
array = np.linspace(0, 10, num = 5, retstep=True)
print(array)
那么所产生的结果就会是: ( a r r a y ( [ 0. , 2.5 , 5. , 7.5 , 10. ] ) , 2.5 ) (array([ 0. ,\ 2.5,\ 5. ,\ 7.5,\ 10. ]), 2.5) (array([0., 2.5, 5., 7.5, 10.]),2.5)
dtype
没啥好说的,就是很简单的设置数组的类型。
np.logspace
这是一个产生等比数列的函数。一般的格式如下:
np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)
b a s e base base 参数意思是取对数的时候 l o g log log 的下标。
参数 | 描述 |
---|---|
s t a r t start start | 序列的起始值为: b a s e s t a r t base^{start} basestart |
s t o p stop stop | 序列的终止值为: b a s e s t o p base^{stop} basestop。如果 e n d p o i n t endpoint endpoint为 t r u e true true,该值包含于数列中 |
n u m num num | 要生成的等步长的样本数量,默认为 50 50 50 |
e n d p o i n t endpoint endpoint | 该值为 t r u e true true 时,数列中中包含 s t o p stop stop值,反之不包含,默认是 T r u e True True。 |
b a s e base base | 对数 l o g log log 的底数。 |
d t y p e dtype dtype | n d a r r a y ndarray ndarray 的数据类型 |
np.where
这就是一个条件语句。一般来说有两种形式。
形式一:判断
判断是否符合条件要求,会返回索引。
import numpy as np
x = np.arange(9.).reshape(3, 3)
print ('我们的数组是:')
print (x)
print ( '大于 3 的元素的索引:')
y = np.where(x > 3)
print (y)
print ('使用这些索引来获取满足条件的元素:')
print (x[y])
输出结果为:
我们的数组是:
[[0. 1. 2.]
[3. 4. 5.]
[6. 7. 8.]]
大于 3 的元素的索引:
(array([1, 1, 2, 2, 2]), array([1, 2, 0, 1, 2]))
使用这些索引来获取满足条件的元素:
[4. 5. 6. 7. 8.]
两个数组就表示坐标,一个表示第几行,一个表示第几列。
形式二:修改
import numpy as np
a = np.array([[1, 2, 3], [4, 5, -1], [-2, 1, -3]])
b = np.where(a > 0, a, 0)
print(b)
输出结果为:
[
[
1
,
2
,
3
]
,
[
4
,
5
,
0
]
,
[
0
,
1
,
0
]
]
[[1,\ 2,\ 3],\\ \ [4,\ 5,\ 0], \\ \ [0,\ 1,\ 0]]
[[1, 2, 3], [4, 5, 0], [0, 1, 0]]
np.c_ && np.r_
这两个的主要作用就是合并,而且这俩对于一维和二维数组的作用是不一样的。
np.c_
在对象是一维数组的时候,它会把横着的数组竖过来然后合并起来。具体来说就是像这样的:
数组A:
[
1
,
2
,
3
,
4
,
5
]
[1,\ 2,\ 3,\ 4,\ 5]
[1, 2, 3, 4, 5]
数组B:
[
6
,
7
,
8
,
9
,
10
]
[6,\ 7,\ 8,\ 9,\ 10]
[6, 7, 8, 9, 10]
在两个数组合并后所得到的数组就是:
[
[
1
,
6
]
,
[
2
,
7
]
,
[
3
,
8
]
,
[
4
,
9
]
,
[
5
,
10
]
]
[[1,\ 6], \\ \ [2,\ 7], \\ \ [3,\ 8], \\ \ [4,\ 9], \\ \ [5,\ 10]]
[[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]]
而当对象是二维数组时,例如:
数组A:
[
[
1
,
2
,
3
]
,
[
4
,
5
,
6
]
]
[[1,\ 2,\ 3], \\\ [4,\ 5,\ 6]]
[[1, 2, 3], [4, 5, 6]]
数组B:
[
[
4
,
5
,
6
]
,
[
7
,
8
,
9
]
]
[[4,\ 5,\ 6], \\\ [7,\ 8,\ 9]]
[[4, 5, 6], [7, 8, 9]]
那么两个数组合并之后就会得到:
[
[
1
,
2
,
3
,
4
,
5
,
6
]
,
[
4
,
5
,
6
,
7
,
8
,
9
]
]
[[1,\ 2,\ 3,\ 4,\ 5,\ 6], \\\ [4,\ 5,\ 6,\ 7,\ 8,\ 9]]
[[1, 2, 3, 4, 5, 6], [4, 5, 6, 7, 8, 9]]
np.r_
在对象是一维数组的时候,就是会连成一个贼长的一维数组。比如:
数组A:
[
1
,
2
,
3
,
4
,
5
]
[1,\ 2,\ 3,\ 4,\ 5]
[1, 2, 3, 4, 5]
数组B:
[
6
,
7
,
8
,
9
,
10
]
[6,\ 7,\ 8,\ 9,\ 10]
[6, 7, 8, 9, 10]
在两个数组合并后所得到的数组就是:
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
]
[1,\ 2,\ 3,\ 4,\ 5,\ 6,\ 7,\ 8,\ 9,\ 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
而对象是二维数组的时候,就会是:
数组A:
[
[
1
,
2
,
3
]
,
[
4
,
5
,
6
]
]
[[1,\ 2,\ 3], \\\ [4,\ 5,\ 6]]
[[1, 2, 3], [4, 5, 6]]
数组B:
[
[
4
,
5
,
6
]
,
[
7
,
8
,
9
]
]
[[4,\ 5,\ 6], \\\ [7,\ 8,\ 9]]
[[4, 5, 6], [7, 8, 9]]
那么两个数组合并之后就会得到:
[
[
1
,
2
,
3
]
,
[
4
,
5
,
6
]
,
[
4
,
5
,
6
]
,
[
7
,
8
,
9
]
]
[[1,\ 2,\ 3], \\\ [4,\ 5,\ 6], \\\ [4,\ 5,\ 6],\\\ [7,\ 8,\ 9]]
[[1, 2, 3], [4, 5, 6], [4, 5, 6], [7, 8, 9]]
冒号的使用
以二维数组为例。
a
r
r
a
y
[
a
:
b
,
c
:
d
]
array[a:b,c:d]
array[a:b,c:d]
就是选中
a
r
r
a
y
array
array数组的
a
a
a到
b
−
1
b-1
b−1行以及
c
c
c到
d
−
1
d-1
d−1列。
np.mean
是用来求平均值的。
import numpy as np
ids = np.array([1, 2, 3, 4])
print(np.mean(ids))
输出的结果就是 2.5 2.5 2.5
如果是矩阵的话,可以加 a x i s axis axis参数:
a
x
i
s
=
0
axis=0
axis=0时,计算每一列的平均值
a
x
i
s
=
1
axis=1
axis=1时,计算每一行的平均值
np.sum
求和。和上一个 n p . m e a n np.mean np.mean一模一样,参数都一样,实在是懒得写了。
就这样吧。