1、NumPy包含的内容

  1、ndarrray,高效的多维数组,提供了基于数组的便捷算术操作以及灵活的广播功能;

  2、对所有数组对象进行快速的矩阵计算,而无需编写循环;

  3、提供对硬盘中的数据的读写工具,并对内存映射文件进行操作;

  4、可实现线性变换、随机数生成以及傅里叶变换功能;

  特点:NumPy在内部将数据存储在连续的内存块上,这与其他的Python内建序列是不同的,这使得NumPy数组对象的计算速度比其他同内容同操作的对象快(快10—100倍)。

 

2、每个ndarray对象具备的属性

  1、shape属性用来描述数组的维数;

  2、dtype属性描述数组的数据类型,它包含了ndarray需要为某一种类型数据所申明的内存块信息,是NumPy能够与其他系统数据灵活交互的原因;

1 import numpy as np
2 data = np.random.randn(2,3)
3 data.shape
4 Out[14]: 
5 》(2, 3)
6 data.dtype
7 Out[15]: 
8 》dtype('float64')

 

3、生成ndarray

  1、array函数,array函数接受任意的序列型对象(包括数组),生成一个包含传递数据的NumPy数组;接受嵌套序列时,自动转换成多维数组。

 1 data1 = [1,2,7.9,0,1]
 2 arr1 = np.array(data1)
 3 arr1
 4 Out[18]: 
 5 array([1. , 2. , 7.9, 0. , 1. ])
 6 data2 = [[1,2,3],[1,2,3]]
 7 np.array(data2)                 # 接受嵌套序列,自动转成二维数组
 8 Out[20]: 
 9 》array([[1, 2, 3],
10        [1, 2, 3]])

  2、zeros()、ones()、empty()、full()等函数,注意zeros()、ones()、empty()等函数接受的参数只有一个,如果要生成多维数组,则需要为shape传递一个元组指定维数;

  3、ones_like()根据所给的数据数组生成一个形状一摸一样的全1数组;zeros_like(),empty_like()、full_like()也类似;

  4、arange()生成python内建函数range的数组版,返回一个数组。

 

4、ndarray数据类型

  1、python数据数据类型众多,可以使用astype方法显示地转换数组地数据类型,注意:浮点型转整型时,小数点后部分将被消除。使用astype时,返回一个新的数组,而原数组不变。

1 arr = np.array([1, 2, 3, 4, 5])
2 print(arr.dtype)
3 float_arr = arr.astype(np.float64)
4 print(float_arr.dtype)
5 》》int32
6 》》float64

  2、如果一个数组里面地元素都是表达数字含义地字符串,也可以将字符串转换成数字;

1 numeric_strings = np.array(['1.25', '-9.6', '42'], dtype=np.string_)
2 numeric_strings
3 Out[30]: 
4 >>array([b'1.25', b'-9.6', b'42'], dtype='|S4')
5 numeric_strings.astype(np.float)
6 Out[31]: 
7 >>array([ 1.25, -9.6 , 42.  ])

 

5、NumPy数组算术

  数组之间可以直接进行批量操作而无需任何for循环;

  任何在两个等尺寸数组之间的算术操作都运用了逐元素操作的方式;

  带有标量的算术操作,会把计算参数传递给每一个元素;

  同尺寸数组之间的比较,会产生一个布尔类型的数组;

 

6、数组索引与切片

  1、数组的索引与列表的索引类似,但不相同。区别于python数组的内建列表,数组的切片是原数组的视图,即数组并不是被复制了,任何对于视图的修改都会反应到原数组上。

 1 arr = np.arange(10)
 2 arr[5:8] = 12
 3 arr_slice = arr[5:8]
 4 print(arr_slice)
 5 arr_slice[1] = 12345    # 对视图的修改后,原数组也会被改变
 6 arr
 7 [12 12 12]
 8 Out[38]: 
 9 array([    0,     1,     2,     3,     4,    12, 12345,    12,     8,
10            9])

  2、如果相对数组进行复制而不是得到一份视图,应该使用copy方法,eg:arr[5:8].copy()

  3、多维数组的索引方法,以二维数组为例,数组名[index][index]或者数组名[index,index]的方式。

 

7、布尔索引

  1、在索引数组时,可以传入布尔值数组,返回的是True对应的内容。

names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])
data = np.random.randn(7, 4)
names == 'Bob'
Out[41]: 
array([ True, False, False,  True, False, False, False])
data[names == 'Bob']
Out[42]: 
array([[-0.71491699,  0.40102409, -0.42140722, -1.50136196],
       [ 0.21920979, -0.13960939,  1.60586575,  0.0712131 ]])

  2、基于常识来设置布尔数组的值也是可行的。

 1 data = np.random.randn(7, 4)
 2 data
 3 Out[44]: 
 4 >>array([[-0.83434737, -0.67205305,  0.17626815, -0.60448911],
 5        [ 0.30011278, -0.98530314, -0.58816207,  2.40943742],
 6        [-0.94236761,  1.12991724, -0.4361433 ,  0.75806253],
 7        [-0.7228912 ,  1.1955933 , -0.75127874, -0.73905711],
 8        [-0.4128283 , -0.15726642,  0.86381129, -1.2467569 ],
 9        [-0.3290692 , -1.03838623,  0.68320058, -0.58237692],
10        [ 1.40461917,  0.55720836,  0.39822819,  0.64182056]])
11 data[data < 0] = 0
12 data
13 Out[46]: 
14 >>array([[0.        , 0.        , 0.17626815, 0.        ],
15        [0.30011278, 0.        , 0.        , 2.40943742],
16        [0.        , 1.12991724, 0.        , 0.75806253],
17        [0.        , 1.1955933 , 0.        , 0.        ],
18        [0.        , 0.        , 0.86381129, 0.        ],
19        [0.        , 0.        , 0.68320058, 0.        ],
20        [1.40461917, 0.55720836, 0.39822819, 0.64182056]])

8、reshape函数,格式 reshape(a, newshape, order='C')

Parameters
----------
a : array_like
Array to be reshaped.
newshape : int or tuple of ints
The new shape should be compatible with the original shape. If
an integer, then the result will be a 1-D array of that length.
One shape dimension can be -1. In this case, the value is
inferred from the length of the array and remaining dimensions.
order : {'C', 'F', 'A'}, optional
1 a = np.arange(6) 2 a 3 Out[49]: 4 array([0, 1, 2, 3, 4, 5]) 5 a.reshape(3,2) 6 Out[50]: 7 array([[0, 1], 8 [2, 3], 9 [4, 5]])

 

9、数组转置和换轴

  1、转置是一种特殊的数据重组形式,也可以返回底层数据的视图而不需要复制任何内容,数组拥有transpose方法,也拥有T属性。当数组是一维或者二维时,数组名.T的形式直接返回转置后的数组(原来的数组不会修改),

 1 arr = np.arange(15).reshape((3, 5))
 2 arr
 3 Out[54]: 
 4 array([[ 0,  1,  2,  3,  4],
 5        [ 5,  6,  7,  8,  9],
 6        [10, 11, 12, 13, 14]])
 7 arr.T
 8 Out[55]: 
 9 array([[ 0,  5, 10],
10        [ 1,  6, 11],
11        [ 2,  7, 12],
12        [ 3,  8, 13],
13        [ 4,  9, 14]])

  2、对于更高维的数组,转置使用transpose和swapxes方法。