NumPy numpy.concatenate
在NumPy中,numpy.concatenate
函数用于沿指定轴连接数组序列。它是数组连接的一种方法,可以将多个数组沿指定轴连接在一起,生成一个新的数组。
语法
numpy.concatenate((a1, a2, ...), axis=0, out=None)
参数说明:
– a1, a2, ...
:要连接的数组序列
– axis
:指定连接的轴,默认为0
– out
:指定输出数组
示例代码
示例1:连接一维数组
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = np.concatenate((a, b))
print(result)
Output:
示例2:连接二维数组
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
result = np.concatenate((a, b))
print(result)
Output:
示例3:指定连接轴
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
result = np.concatenate((a, b), axis=1)
print(result)
Output:
示例4:连接多个数组
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.array([7, 8, 9])
result = np.concatenate((a, b, c))
print(result)
Output:
示例5:连接多维数组
import numpy as np
a = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
b = np.array([[[9, 10], [11, 12]], [[13, 14], [15, 16]]])
result = np.concatenate((a, b))
print(result)
Output:
示例6:指定输出数组
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
out = np.zeros(6)
result = np.concatenate((a, b), out=out)
print(result)
Output:
示例7:连接空数组
import numpy as np
a = np.array([])
b = np.array([1, 2, 3])
result = np.concatenate((a, b))
print(result)
Output:
示例8:连接不同维度的数组
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([5, 6])
result = np.concatenate((a, b))
print(result)
示例9:连接字符串数组
import numpy as np
a = np.array(['geek', 'docs'])
b = np.array(['com'])
result = np.concatenate((a, b))
print(result)
Output:
示例10:连接布尔数组
import numpy as np
a = np.array([True, False])
b = np.array([False, True])
result = np.concatenate((a, b))
print(result)
Output:
示例11:连接浮点数数组
import numpy as np
a = np.array([1.1, 2.2])
b = np.array([3.3, 4.4])
result = np.concatenate((a, b))
print(result)
Output:
示例12:连接复数数组
import numpy as np
a = np.array([1+2j, 3+4j])
b = np.array([5+6j, 7+8j])
result = np.concatenate((a, b))
print(result)
Output:
示例13:连接空数组序列
import numpy as np
a = np.array([])
b = np.array([])
result = np.concatenate((a, b))
print(result)
Output:
示例14:连接多个空数组
import numpy as np
a = np.array([])
b = np.array([])
c = np.array([])
result = np.concatenate((a, b, c))
print(result)
Output:
示例15:连接不同形状的数组
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([5, 6, 7])
result = np.concatenate((a, b))
print(result)
示例16:连接不同类型的数组
import numpy as np
a = np.array([1, 2])
b = np.array(['geek', 'docs'])
result = np.concatenate((a, b))
print(result)
Output:
示例17:连接不同dtype的数组
import numpy as np
a = np.array([1, 2], dtype=np.int32)
b = np.array([3, 4], dtype=np.float64)
result = np.concatenate((a, b))
print(result)
Output:
示例18:连接不同形状和dtype的数组
import numpy as np
a = np.array([[1, 2], [3, 4]], dtype=np.int32)
b = np.array([5, 6, 7], dtype=np.float64)
result = np.concatenate((a, b))
print(result)
示例19:连接不同维度和dtype的数组
import numpy as np
a = np.array([[1, 2], [3, 4]], dtype=np.int32)
b = np.array([5, 6], dtype=np.float64)
result = np.concatenate((a, b))
print(result)
示例20:连接不同维度和dtype的数组
import numpy as np
a = np.array([1, 2], dtype=np.int32)
b = np.array([[3, 4], [5, 6]], dtype=np.float64)
result = np.concatenate((a, b))
print(result)
总结
通过numpy.concatenate
函数,我们可以方便地连接多个数组,实现数组的拼接操作。在使用时需要注意数组的维度、形状和dtype,确保能够正确地连接数组序列。