import numpy as np
下面验证一下 np.concatenate 的用法
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
x = []
x.append(a)
x.append(b)
x
[array([[1, 2],
[3, 4]]), array([[5, 6],
[7, 8]])]
发现不管是列表还是元组都是可以的
x1 = np.concatenate([a,b])
x1
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
x2 = np.concatenate((a,b))
x2
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
def my_sum(a, order=0):
sum = 0
for i in a:
sum += i
return sum
my_sum([1,2,3], 0)
6
my_sum((1,2,3), 0)
6
因为在 for 循环中不仅列表可以循环,元组,字符只要是可迭代的都可以循环
而在下面的my_sum1 函数的定义中,我们是以可变参数来定义的
def my_sum1(*a, order=0):
sum = 0;
for i in a:
sum += i
return sum
my_sum1(1,2,3)
6
my_sum1([1,2,3])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-20-0a7a90b048ef> in <module>()
----> 1 my_sum1([1,2,3])
<ipython-input-18-1de405cf845d> in my_sum1(order, *a)
2 sum = 0;
3 for i in a:
----> 4 sum += i
5 return sum
TypeError: unsupported operand type(s) for +=: 'int' and 'list'
my_sum1(*[1,2,3])
6
如果用 np.concatenate(a1,a2…)会报错
x3 = np.concatenate(a,b)
x3
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-22-9c75f38c761f> in <module>()
----> 1 x3 = np.concatenate(a,b)
2 x3
TypeError: only integer scalar arrays can be converted to a scalar index
综上 np.concatenate 的调用只能 np.concatenate((a1,a2,…)) 或者 np.concatenate([a1,a2,…])
比较异同
a = np.array([1,2,3]) # 可以把它当成列向量
b = np.array([4,5,6,7])
np.concatenate((a,b))
array([1, 2, 3, 4, 5, 6, 7])
np.concatenate((a,b), axis=0)
array([1, 2, 3, 4, 5, 6, 7])
np.concatenate((a,b), axis=None)
array([1, 2, 3, 4, 5, 6, 7])
np.concatenate((a,b), axis=1)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-79-2a8f7c46ccd2> in <module>()
----> 1 np.concatenate((a,b), axis=1)
ValueError: all the input array dimensions except for the concatenation axis must match exactly
无法对一维数组进行 axis = 1 上的操作,因为一维数组只有一个纵向的维度
a = np.array([1,2,3])
b = np.array([4,5,6])
np.concatenate((a,b), axis=1)
---------------------------------------------------------------------------
AxisError Traceback (most recent call last)
<ipython-input-80-2528feefaf7f> in <module>()
1 a = np.array([1,2,3])
2 b = np.array([4,5,6])
----> 3 np.concatenate((a,b), axis=1)
AxisError: axis 1 is out of bounds for array of dimension 1
a = np.array([[1,2,3]])
b = np.array([[4,5,6,7]])
np.concatenate((a,b))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-71-4b1407c41995> in <module>()
1 a = np.array([[1,2,3]])
2 b = np.array([[4,5,6,7]])
----> 3 np.concatenate((a,b))
ValueError: all the input array dimensions except for the concatenation axis must match exactly
a = np.array([[1],[2],[3]])
b = np.array([[4],[5],[7]])
np.concatenate((a,b),axis=1)
array([[1, 4],
[2, 5],
[3, 7]])
np.concatenate((a,b), axis=1)
array([[1, 2, 3, 4, 5, 6, 7]])
a = np.array([[1,2],[5,4]])
b = np.array([1,2])
a + b
array([[2, 4],
[6, 6]])
a = np.array([[1,2],[5,4]])
b = np.array([[1,2]])
a + b
array([[2, 4],
[6, 6]])
np.flatnonzero
np.flatnonzero(a) 返回非0数组的下标的平铺形式
比较:
a.ravel() 返回数组的平铺形式
np.flatnonzero(a)
array([0, 1, 2, 3])
a.ravel()
array([1, 2, 3, 4])
a = np.array([[1,2,3]])
b = np.array([[3,4,5]])
np.sum((a-b)**2)
12
a-b
array([[-2, -2, -2]])
np.argsort
返回数组从小到大的序号
a = np.array([[3,1,2],[4,7,1]])
np.argsort(a)
array([[1, 2, 0],
[2, 0, 1]])
np 中找到出现最多的元素
a = np.array([1,2,3,3,4,4,4,5])
x = np.bincount(a)
x
array([0, 1, 1, 2, 3, 1])
np.argmax(x)
4
np.sum
a = np.array([[1,2,3],[4,5,6]])
np.sum(a,axis=0)
array([5, 7, 9])
np.sum(a,axis=1)
array([ 6, 15])
np.sum(a)
21
会发现不管axis 是0还是1得出的都是(n,)这样的数组
切片
a = np.array([1,2,3])
a.shape
(3,)
[1] + [2]
[1, 2]
[1,2,3][0:1]
[1]
[1,2,3][1:2]
[2]
[1,2,3][0:0]
[]
x1 = []
x2 = []
x1.append(a)
x2.append(a)
比较异同
x1[0:]
[array([1, 2, 3])]
x1[0]
array([1, 2, 3])
x1[0:] + x2[0:]
[array([1, 2, 3]), array([1, 2, 3])]