Python numpy.append()

Python numpy.append()

Python numpy.append()沿着上述axis在数组末尾追加值
语法 :

numpy.append(array, values, axis = None)

参数 :

array : [array_like]输入数组。
values : [array_like]要添加到arr中的值。值的形状应该是 arr[…,obj,…] = values。如果定义了轴,值可以是任何形状,因为它在使用前会被压扁。
axis :我们想沿着这个轴插入数值。默认情况下,数组是被压扁的。

返回 :

一个数组的副本,其值被附加在所述对象的最后。沿着一个给定的轴。

代码1:添加arrays

# Python Program illustrating
# numpy.append()
  
import numpy as geek
  
#Working on 1D
arr1 = geek.arange(5)
print("1D arr1 : ", arr1)
print("Shape : ", arr1.shape)
  
  
arr2 = geek.arange(8, 12)
print("\n1D arr2 : ", arr2)
print("Shape : ", arr2.shape)
  
  
# appending the arrays
arr3 = geek.append(arr1, arr2)
print("\nAppended arr3 : ", arr3)

输出 :

1D arr1 :  [0 1 2 3 4]
Shape :  (5,)

1D arr2 :  [ 8  9 10 11]
Shape :  (4,)

Appended arr3 :  [ 0  1  2  3  4  8  9 10 11]


代码2:

# Python Program illustrating
# numpy.append()
  
import numpy as geek
  
#Working on 1D
arr1 = geek.arange(8).reshape(2, 4)
print("2D arr1 : \n", arr1)
print("Shape : ", arr1.shape)
  
  
arr2 = geek.arange(8, 16).reshape(2, 4)
print("\n2D arr2 : \n", arr2)
print("Shape : ", arr2.shape)
  
  
# appending the arrays
arr3 = geek.append(arr1, arr2)
print("\nAppended arr3 by flattened : ", arr3)
  
# appending the arrays with axis = 0
arr3 = geek.append(arr1, arr2, axis = 0)
print("\nAppended arr3 with axis 0 : \n", arr3)
  
# appending the arrays with axis = 1
arr3 = geek.append(arr1, arr2, axis = 1)
print("\nAppended arr3 with axis 1 : \n", arr3)

输出 :

2D arr1 : 
 [[0 1 2 3]
 [4 5 6 7]]
Shape :  (2, 4)

2D arr2 : 
 [[ 8  9 10 11]
 [12 13 14 15]]
Shape :  (2, 4)

Appended arr3 by flattened :  [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15]

Appended arr3 with axis 0 : 
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

Appended arr3 with axis 1 : 
 [[ 0  1  2  3  8  9 10 11]
 [ 4  5  6  7 12 13 14 15]]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程