函数np.append(arr, values, axis=None)
作用:
为原始array添加一些values
参数:
arr:需要被添加values的数组
values:添加到数组arr中的值(array_like,类数组)
axis:可选参数,如果axis没有给出,那么arr,values都将先展平成一维数组。注:如果axis被指定了,那么arr和values需要同为一维数组或者有相同的shape,否则报错:ValueError: arrays must have same number of dimensions
补充对axis的理解
axis的最大值为数组arr的维数-1,如arr维数等于1,axis最大值为0;arr维数等于2,axis最大值为1,以此类推。
当arr的维数为2(理解为单通道图),axis=0表示沿着行增长方向添加values;axis=1表示沿着列增长方向添加values
当arr的维数为3(理解为多通道图),axis=0,axis=1时同上;axis=2表示沿着图像深度增长方向添加values
返回:
添加了values的新数组
e.g.
1. 不考虑axis
arr,values都将先展平成一维数组,然后沿着axis=0的方向在arr后添加values
import numpy as np
a = [1, 2, 3]
b = [4, 5]
c = [[6, 7], [8, 9]]
print(np.append(a, b))
print(np.append(a, c))
输出结果如下:
[1 2 3 4 5]
[1 2 3 6 7 8 9]
2. 考虑axis,arr,values同为一维数组或两者shape相同
import numpy as np
a = [1, 2, 3]
b = [4, 5]
c = [[6, 7], [8, 9]]
d = [[10, 11], [12, 13]]
print('在一维数组a后添加values,结果如下:\n{}'.format(np.append(a, b, axis=0)))
print('沿二维数组c的行增长方向添加values结果如下:\n{}'.format(np.append(c, d, axis=0)))
print('沿二维数组c的列增长方向添加values结果如下:\n{}'.format(np.append(c, d, axis=1)))
结果如下:
在一维数组a后添加values,结果如下:
[1 2 3 4 5]
沿二维数组c的行增长方向添加values结果如下:
[[ 6 7]
[ 8 9]
[10 11]
[12 13]]
沿二维数组c的列增长方向添加values结果如下:
[[ 6 7 10 11]
[ 8 9 12 13]]
3. 考虑axis,如果arr和values不同为一维数组且shape不同,则报错:
import numpy as np
a = [1, 2, 3]
c = [[6, 7], [8, 9]]
print(np.append(a, c, axis=0))
输出结果如下:
Traceback (most recent call last):
File "F:\eclipse-workspace\test\t1.py", line 4, in
print(np.append(a,c,axis=0))
File "E:\anaconda\anzhuang\lib\site-packages\numpy\lib\function_base.py", line 4694, in append
return concatenate((arr, values), axis=axis)
ValueError: all the input arrays must have same number of dimensions
np.empty()
依据给定形状和类型(shape,[dtype, order])返回一个新的空数组
官方解释:
np.empty()返回一个随机元素的矩阵,所以使用的时候要小心。需要手工把每一个值重新定义,否则该值是接近零的随机数。
gauss = np.empty((5, 64, 64), dtype=np.float64)
print(gauss)
结果:
[[[7.18634390e-312 7.19311879e-312 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
...
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]]
[[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
...
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]]
[[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
...
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]]
[[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
...
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]]
[[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
...
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 0.00000000e+000 ... 0.00000000e+000
0.00000000e+000 0.00000000e+000]]]
numpy.array()使用说明:object是必须输入的参数,其余为可选参数。
import numpy as np
np.array(object,dtype,copy=True,order,ndmin,subok=Fasle)
# object: 一个数组序列,例如[1,2,3,4]
# dtype: 更改数组内的数据类型
# copy: 数据源是ndarray时数组能否被复制,default=True
# order: 选择数组的内存布局,C(行序列)|F(列序列)|A(默认)
# ndmin: 数组维度
# subok: bool类型,True,使用object的内部数据类型;False,使用object的数组的数据类型,default=Fasle
创建存储元素类型不同的数组:
#int型
import numpy as np
a=np.array([1,2,3,4],dtype=int) #"dtype="可省略
print(a)
a.dtype
'''
输出:
[1 2 3 4]
dtype('int32')
'''
#float型
b=np.array([1,2,3,4],dtype=float)
print(b)
b.dtype
'''
输出:
[1. 2. 3. 4.]
dtype('float64')
'''
创建生成器:
a=np.array([i*10 for i in range(10)])
print(a)
b=np.array([i+2 for i in range(10)])
print(b)
'''
输出:
[ 0 10 20 30 40 50 60 70 80 90]
[ 2 3 4 5 6 7 8 9 10 11]
'''
当输入的object元素有不同类型时,将保留存储空间最大的类型:
x1=np.array([1,2,3,4,5.1])
print(x1)
x2=np.array([1,2,3,'a'])
print(x2)
x3=np.array([1,2.1,'a'])
print(x3)
'''
输出:
[1. 2. 3. 4. 5.1]
['1' '2' '3' 'a']
['1' '2.1' 'a']
'''
当多维数组元素个数不一致时:
x=np.array([[1,2,3],[1,2,3,4],[1,2,3,4,5]],dtype=object) #存储长度不一致序列时,应有“dtype=object”,否则会报错
print(x)
print(x.shape)
print(x.ndim) #输出数组的维度,2Darray强制转换成1Darray
'''
输出:
[list([1, 2, 3]) list([1, 2, 3, 4]) list([1, 2, 3, 4, 5])]
(3,)
1
'''
float强制转化int(向下取整):
a=np.array([1,2,3.1],int)
b=np.array([1,2,3.7],int)
print(a)
print(b)
'''
输出:
[1 2 3]
[1 2 3]
'''
用copy参数定义是否创建副本:
#默认copy=True情况下,复制创建x1的副本为x2
x1=np.array([1,2,3])
x2=np.array(x1)
print('x1 ',id(x1),'x2',id(x2))
#更改x2的值,x1的值不会发生改变,反之,修改x1的值,x2也不会发生改变,因为二者地址不同
x2[2]=100
print('x1',x1)
print('x2',x2)
'''
输出:
x1 2055556179312 x2 2055300844976 x1 x2地址不同
x1 [1 2 3]
x2 [ 1 2 100]
'''
#copy=Fasle情况下,复制创建x1的副本为x2
x1=np.array([1,2,3])
x2=np.array(x1,copy=False)
print('x1 ',id(x1),'x2',id(x2))
#更改x2的值,x1的值会发生改变,且x1永远等于x2,因为二者地址相同
x2[2]=100
print('x1',x1)
print('x2',x2)
'''
输出:
x1 2055300125584 x2 2055300125584 x1 x2地址相同
x1 [ 1 2 100]
x2 [ 1 2 100]
'''
#另一种创建副本方法:copy() 这种方法更常用
x1=np.array([1,2,3])
x2=x1.copy()
print('x1 ',id(x1),'x2',id(x2))
#更改x2的值,x1的值不会发生改变
x2[2]=100
print('x1',x1)
print('x2',x2)
'''
输出:
x1 2055556233040 x2 2055556062160
x1 [1 2 3]
x2 [ 1 2 100]
'''
ps:如果直接用 x2=x1 的形式复制array,此时x1 x2 共用同一个地址
用ndmin改变数组维度(升维有效,降维无效):
a=np.array([1,2,3,4],ndmin=2)
print('a ',a)
b=np.array([[1,2],[1,2]],ndmin=3)
print('b ',b)
#2D降维成1D,但输出结果仍为2D
c=np.array([[1,2],[1,2]],ndmin=1)
print('c ',c)
'''
输出:
a [[1 2 3 4]]
b [[[1 2]
[1 2]]]
c [[1 2]
[1 2]]
'''
用subok参数(bool值)确定数据类型:
x1=np.mat([1,2,3])
a1=np.array(x1) #存储为原类型
b1=np.array(x1,subok=True) #存储为数组类型
print('x1 ',type(x1),'a1 ',type(a1),'b1 ',type(b1))
#原始格式为list,无论subok为何值都转换成数组类型
x2=[[1,2],[1,2],[1]]
a2=np.array(x2,dtype=object) #存储为原类型
b2=np.array(x2,dtype=object,subok=True) #存储为数组类型
print('x2 ',type(x2),'a2 ',type(a2),'b2 ',type(b2))
'''
输出:
x1 a1 b1
x2 a2 b2
'''