python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > numpy删除单行、删除单列、删除多列

numpy删除单行、删除单列、删除多列实现方式

作者:嗖100

这篇文章主要介绍了numpy删除单行、删除单列、删除多列实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

numpy删除单行、删除单列、删除多列

import numpy as np

删除元素

通过index删除单行、删除单列、删除多行

def delFun():
    """
    删除
    :return:
    """
    source = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])

    # 删除第三行
    del_arr_1 = source.copy()
    del_row = np.delete(del_arr_1, 2, axis=0)

    # 删除第二列
    del_arr_2 = source.copy()
    del_col = np.delete(del_arr_2, 1, axis=1)

    # 删除第二、三行
    del_arr_3 = source.copy()
    del_mult_row = np.delete(del_arr_3, (1, 2), axis=0)

    print(del_row)
    print(del_col)
    print(del_mult_row)

原始数据

	[
		[1 2 3]
 		[1 2 3]
 		[1 2 3]
 	]

del_row 删除第三行 返回结果

	[
		[1 2 3]
 		[1 2 3]
 	]

del_col 删除第二列 返回结果

	[
		[1 3]
		 [1 3]
		 [1 3]
	]

del_mult_row 删除第二、三行 返回结果

	[
		[1 2 3]
	]

Numpy增加和删除元素

1. delete

numpy.delete(arr, obj, axis=None):

>>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
>>> arr
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

# obj参数
>>> np.delete(arr,1,1)
array([[ 1,  3,  4],
       [ 5,  7,  8],
       [ 9, 11, 12]])

>>> np.delete(arr,[1,2],axis=1)
array([[ 1,  4],
       [ 5,  8],
       [ 9, 12]])

# axis参数
>>> np.delete(arr,1,0)
array([[ 1,  2,  3,  4],
       [ 9, 10, 11, 12]])
       
>>> np.delete(arr,1)
array([ 1,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])

2. insert

numpy.insert(arr, obj, values, axis=None):沿着给定的轴(axis),在给定的索引(obj)之前插入值

如果values的数据类型和arr的数据类型不同,values会被自动转换为arr的数据类型

values的形状应使 arr[…,obj,…] = values 合法

axis = None:arr会先被展平,类似numpy.delete函数

>>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
>>> arr
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

# obj参数
>>> np.insert(arr,1,0,axis=1)
array([[ 1,  0,  2,  3,  4],
       [ 5,  0,  6,  7,  8],
       [ 9,  0, 10, 11, 12]])

>>> np.insert(arr,1,[1,2,3],axis=1) # values的形状应使 arr[...,obj,...] = values 合法
array([[ 1,  1,  2,  3,  4],
       [ 5,  2,  6,  7,  8],
       [ 9,  3, 10, 11, 12]])

>>> np.insert(arr,[1,2],np.array([[1,2],[3,4],[5,6]]),axis=1) # values的形状应使 arr[...,obj,...] = values 合法
array([[ 1,  1,  2,  2,  3,  4],
       [ 5,  3,  6,  4,  7,  8],
       [ 9,  5, 10,  6, 11, 12]])

>>> np.insert(arr,[1],[1,2,3],axis=1)
array([[ 1,  1,  2,  3,  2,  3,  4],
       [ 5,  1,  2,  3,  6,  7,  8],
       [ 9,  1,  2,  3, 10, 11, 12]])

# values参数
>>> np.insert(arr,1,[1.5,2,True],axis=1)
array([[ 1,  1,  2,  3,  4],
       [ 5,  2,  6,  7,  8],
       [ 9,  1, 10, 11, 12]])

# axis参数
>>> np.insert(arr,1,0,axis=0)
array([[ 1,  2,  3,  4],
       [ 0,  0,  0,  0],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

>>> np.insert(arr,1,0)
array([ 1,  0,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])

注释:特别注意numpy.insert(obj = some_integer)与numpy.insert(obj = [some_integer])的区别。

3. append

numpy.append(arr, values, axis=None):添加值到数组的末尾

>>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
>>> arr
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

# values参数
>>> np.append(arr,[13,14,15,16],axis=0)
ValueError: all the input arrays must have same number of dimensions, but the array 
at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)

>>> np.append(arr,np.array([[13,14,15,16]]),axis=0)
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16]])

>>> np.append(arr,[5,9,13],axis=1)
ValueError: all the input arrays must have same number of dimensions, but the array 
at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)

>>> np.append(arr,np.array([[5],[9],[13]]),axis=1)
array([[ 1,  2,  3,  4,  5],
       [ 5,  6,  7,  8,  9],
       [ 9, 10, 11, 12, 13]])

# axis参数
>>> np.append(arr,[13,14,15,16])
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])

注释: 特别注意values参数的形状。

4. resize

numpy.resize(a, new_shape):返回具有指定形状的新数组

如果新数组比原始数组大,那么新数组会用重复的原始数组来填充,这时会按照C语言的顺序重复遍历数组

>>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
>>> arr
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

>>> np.resize(arr,(4,3))
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]])

>>> np.resize(arr,(4,4))
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [ 1,  2,  3,  4]])

>>> np.resize(arr,(3,5))
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10],
       [11, 12,  1,  2,  3]])

>>> np.resize(arr,(3,3))
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

注释:

numpy.resize没有单独考虑各个轴,因此其不适用于插值/外推。所以,numpy.resize不适用于调整图像或数据的大小,其中每个轴代表一个单独的不同实体。

numpy.resize和ndarray.resize的区别:

>>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
>>> arr
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
       
>>> np.resize(arr,[3,5])
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10],
       [11, 12,  1,  2,  3]])
>>> arr
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

>>> arr.resize(3,5,refcheck=False)
>>> arr
array([[ 1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10],
       [11, 12,  0,  0,  0]])```

numpy.resize和numpy.reshape的区别

>>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
>>> arr
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
>>> id(arr)
2040077636592
>>> arr.ctypes.data
2039348516176

>>> arr_reshape = np.reshape(arr,(4,3))
>>> arr_reshape
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]])
>>> id(arr_reshape)
2040077636832
>>> arr_reshape.ctypes.data
2039348516176

>>> arr_resize = np.resize(arr,(4,3))
>>> arr_resize
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]])
>>> id(arr_resize)
2040077636752
>>> arr_resize.ctypes.data
2039348513616

>>> arr[0][0] = 0
>>> arr
array([[ 0,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
>>> arr_reshape
array([[ 0,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]])
>>> arr_resize
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]])

5. trim_zeros

numpy.trim_zeros(filt, trim=‘fb’):修剪一维数组或序列开头和/或尾部的0

filt:一维数组或序列

trim:字符串,可选参数

>>> arr = np.array([0, 0, 0, 1, 2, 3, 0, 2, 1, 0, 0])
>>> arr
array([0, 0, 0, 1, 2, 3, 0, 2, 1, 0, 0])

>>> np.trim_zeros(arr)
array([1, 2, 3, 0, 2, 1])
>>> np.trim_zeros(arr,trim='f')
array([1, 2, 3, 0, 2, 1, 0, 0])
>>> np.trim_zeros(arr,trim='b')
array([0, 0, 0, 1, 2, 3, 0, 2, 1])

>>> list1 = [0, 0, 0, 1, 2, 3, 0, 2, 1, 0, 0]
>>> np.trim_zeros(list1)
[1, 2, 3, 0, 2, 1]
>>> np.trim_zeros(list1,trim='f')
[1, 2, 3, 0, 2, 1, 0, 0]
>>> np.trim_zeros(list1,trim='b')
[0, 0, 0, 1, 2, 3, 0, 2, 1]

6. unique

numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None):查找数组唯一的元素,返回数组排序后的唯一的元素

参数:

返回:

>>> arr = np.array([[1,2,3,4], [5,6,7,8], [5,6,7,8], [9,10,11,12]])
>>> arr
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

>>> arr_unique, arr_index, arr_inverse, arr_counts = np.unique(arr, return_index=True, return_inverse=True, return_counts=True)
>>> arr_unique
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])
>>> arr_index
array([ 0,  1,  2,  3,  4,  5,  6,  7, 12, 13, 14, 15], dtype=int64)
>>> arr_inverse
array([ 0,  1,  2,  3,  4,  5,  6,  7,  4,  5,  6,  7,  8,  9, 10, 11],
      dtype=int64)
>>> arr_counts
array([1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1], dtype=int64)

# unique = arr.flatten()[index]
>>> arr.flatten()[arr_index]
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])

# arr = unique[inverse].reshape(arr.shape)
>>> arr_unique[arr_inverse].reshape(arr.shape)
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

>>> arr_unique_0, arr_index_0, arr_inverse_0, arr_counts_0 = np.unique(arr, return_index=True, return_inverse=True, return_counts=True,axis=0)
>>> arr_unique_0
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
>>> arr_index_0
array([0, 1, 3], dtype=int64)
>>> arr_inverse_0
array([0, 1, 1, 2], dtype=int64)
>>> arr_counts_0
array([1, 2, 1], dtype=int64)

>>> arr_unique_1, arr_index_1, arr_inverse_1, arr_counts_1 = np.unique(arr, return_index=True, return_inverse=True, return_counts=True,axis=1)
>>> arr_unique_1
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
>>> arr_index_1
array([0, 1, 2, 3], dtype=int64)
>>> arr_inverse_1
array([0, 1, 2, 3], dtype=int64)
>>> arr_counts_1
array([1, 1, 1, 1], dtype=int64)

注释: 本篇中所有函数都会先对待操作数组进行拷贝,再进行操作。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文