2020-10-21

Numpy数据类型

        名称                                          描述
bool_布尔型数据类型(True 或者 False)
int_默认的整数类型(类似于 C 语言中的 long,int32 或 int64)
intc与 C 的 int 类型一样,一般是 int32 或 int 64
intp用于索引的整数类型(类似于 C 的 ssize_t,一般情况下仍然是 int32 或 int64)
int8字节(-128 to 127)
int16整数(-32768 to 32767)
int32整数(-2147483648 to 2147483647)
int64整数(-9223372036854775808 to 9223372036854775807)
uint8无符号整数(0 to 255)
uint16无符号整数(0 to 65535)
uint32无符号整数(0 to 4294967295)
uint64无符号整数(0 to 18446744073709551615)
float_float64 类型的简写
float16半精度浮点数,包括:1 个符号位,5 个指数位,10 个尾数位
float32单精度浮点数,包括:1 个符号位,8 个指数位,23 个尾数位
float64双精度浮点数,包括:1 个符号位,11 个指数位,52 个尾数位
complex_complex128 类型的简写,即 128 位复数
complex64复数,表示双 32 位浮点数(实数部分和虚数部分)
complex128复数,表示双 64 位浮点数(实数部分和虚数部分)

Numpy创建数组

 task02---索引、切片与迭代


1、副本与视图

     在 Numpy 中,尤其是在做数组运算或数组操作时,返回结果不是数组的 **副本** 就是 **视图**,所有赋值运算不会为数组和数组中的任何元素创建副本。

    `numpy.ndarray.copy()` 函数创建一个副本。 对副本数据进行修改,不会影响到原始数据,它们物理内存不在同一位置。

import numpy as np
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = x  # 创建视图
y[0] = -1
print(x)
# [-1  2  3  4  5  6  7  8]
print(y)
# [-1  2  3  4  5  6  7  8]

'''
此处改变视图y,原数组x也随之改变
可理解为x和y同指向同一缓冲区,对
任一个数组中的元素进行修改都会引
起缓冲区内容的改变。
'''

x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = x.copy()  # 创建副本
y[0] = -1
print(x)
# [1 2 3 4 5 6 7 8]
print(y)
# [-1  2  3  4  5  6  7  8]
'''
此处改变副本y,原数组x不变。承接之
前的思路:x和y指向不同的缓冲区,只
在创建副本时内容一致,在程序操作中
改变一者,另一者不受影响。
'''

   数组切片操作返回的对象只是原数组的视图。

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = x
y[::2, :3:2] = -1
print(x)
# [[-1 12 -1 14 15]
#  [16 17 18 19 20]
#  [-1 22 -1 24 25]
#  [26 27 28 29 30]
#  [-1 32 -1 34 35]]
print(y)
# [[-1 12 -1 14 15]
#  [16 17 18 19 20]
#  [-1 22 -1 24 25]
#  [26 27 28 29 30]
#  [-1 32 -1 34 35]]

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = x.copy()
y[::2, :3:2] = -1
print(x)
# [[11 12 13 14 15]
#  [16 17 18 19 20]
#  [21 22 23 24 25]
#  [26 27 28 29 30]
#  [31 32 33 34 35]]
print(y)
# [[-1 12 -1 14 15]
#  [16 17 18 19 20]
#  [-1 22 -1 24 25]
#  [26 27 28 29 30]
#  [-1 32 -1 34 35]]

2、索引与切片

      数组索引机制指的是用方括号( [ ] )加序号的形式引用单个数组元素,它的用处很多,比如抽取元素,选取数组的几个元素,甚至为其赋一个新值。类似字符串和列表中的索引操作。

     1)整数索引

               要获取数组的单个元素,指定元素的索引即可。

import numpy as np

x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
print(x[2])  # 3

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
print(x[2])  # [21 22 23 24 25]
print(x[2][1])  # 22
print(x[2, 1])  # 22

     2)切片索引

        切片操作是指抽取数组的一部分元素生成新数组。对 python 列表进行切片操作得到的数组是原数组的副本,而对Numpy数据进行切片操作得到的数组则是指向相同缓冲区的视图。

        如果想抽取(或查看)数组的一部分,必须使用切片语法,也就是,把几个用冒号( `start:stop:step` )隔开的数字置于方括号内。

        为了更好地理解切片语法,还应该了解不明确指明起始和结束位置的情况。如省去第一个数字,numpy 会认为第一个数字是0;如省去第二个数字,numpy 则会认为第二个数字是数组的最大索引值;如省去最后一个数字,它将会被理解为1,也就是抽取所有元素而不再考虑间隔。

        对一维数组的切片

import numpy as np

x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
print(x[0:2])  # [1 2]
#用下标1~5,以2为步长选取数组
print(x[1:5:2])  # [2 4]
print(x[2:])  # [3 4 5 6 7 8]
print(x[:2])  # [1 2]
print(x[-2:])  # [7 8]
print(x[:-2])  # [1 2 3 4 5 6]
print(x[:])  # [1 2 3 4 5 6 7 8]
#利用负数下标翻转数组
print(x[::-1])  # [8 7 6 5 4 3 2 1]

         对二维数组切片

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
print(x[0:2])
# [[11 12 13 14 15]
#  [16 17 18 19 20]]

print(x[1:5:2])
# [[16 17 18 19 20]
#  [26 27 28 29 30]]

print(x[2:])
# [[21 22 23 24 25]
#  [26 27 28 29 30]
#  [31 32 33 34 35]]

print(x[:2])
# [[11 12 13 14 15]
#  [16 17 18 19 20]]

print(x[-2:])
# [[26 27 28 29 30]
#  [31 32 33 34 35]]

print(x[:-2])
# [[11 12 13 14 15]
#  [16 17 18 19 20]
#  [21 22 23 24 25]]

print(x[:])
# [[11 12 13 14 15]
#  [16 17 18 19 20]
#  [21 22 23 24 25]
#  [26 27 28 29 30]
#  [31 32 33 34 35]]

print(x[2, :])  # [21 22 23 24 25]
print(x[:, 2])  # [13 18 23 28 33]
print(x[0, 1:4])  # [12 13 14]
print(x[1:4, 0])  # [16 21 26]
print(x[1:3, 2:4])
# [[18 19]
#  [23 24]]

print(x[:, :])
# [[11 12 13 14 15]
#  [16 17 18 19 20]
#  [21 22 23 24 25]
#  [26 27 28 29 30]
#  [31 32 33 34 35]]

print(x[::2, ::2])
# [[11 13 15]
#  [21 23 25]
#  [31 33 35]]

print(x[::-1, :])
# [[31 32 33 34 35]
#  [26 27 28 29 30]
#  [21 22 23 24 25]
#  [16 17 18 19 20]
#  [11 12 13 14 15]]

print(x[:, ::-1])
# [[15 14 13 12 11]
#  [20 19 18 17 16]
#  [25 24 23 22 21]
#  [30 29 28 27 26]
#  [35 34 33 32 31]]

x[0::2, 1::3] = 0
print(x)
# [[11  0 13 14  0]
#  [16 17 18 19 20]
#  [21  0 23 24  0]
#  [26 27 28 29 30]
#  [31  0 33 34  0]]

         通过对每个以逗号分隔的维度执行单独的切片,你可以对多维数组进行切片。因此,对于二维数组,我们的第一片定义了行的切片,第二片定义了列的切片。

 

task03---数组的操作

1)更改形状

  在对数组进行操作时,为了满足格式和计算的要求通常会改变其形状。

 `numpy.ndarray.shape`

  表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 `ndim` 属性(秩)。

import numpy as np

x = np.array([1, 2, 9, 4, 5, 6, 7, 8])
print(x.shape)  # (8,)
x.shape = [2, 4]
print(x)
# [[1 2 9 4]
#  [5 6 7 8]]

`numpy.ndarray.flat`

将数组转换为一维的迭代器,可以用for访问数组每一个元素。

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = x.flat
print(y)
# <numpy.flatiter object at 0x0000020F9BA10C60>
for i in y:
    print(i, end=' ')
# 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

y[3] = 0
print(end='\n')
print(x)
# [[11 12 13  0 15]
#  [16 17 18 19 20]
#  [21 22 23 24 25]
#  [26 27 28 29 30]
#  [31 32 33 34 35]]

        忽然发现在对x进行.flat函数处理后,y内保存的地址在经过for循环打印输出一次之后无法再次实现打印输出。缘由目前我还不是很清楚。需要向其他大佬请教一下,回头解释。

`numpy.ndarray.flatten([order='C'])`

将数组的副本转换为一维数组,并返回。

  • ndarray.flatten(order='C')
  • Return a copy of the array collapsed into one dimension.

Parameters:  order : {‘C’, ‘F’, ‘A’, ‘K’}, optional

‘C’ means to flatten in row-major (C-style) order. ‘F’ means to flatten in column-major (Fortran- style) order. ‘A’ means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise. ‘K’ means to flatten a in the order the elements occur in memory. The default is ‘C’.

Returns: A copy of the input array, flattened to one dimension
.

x = np.random.randint(1,100,[5,1,5])
print(x)
y = x.flatten()
print(y)

'''
[[[86 89 92 24 55]]

 [[35 75 94 54 92]]

 [[51 52 62 92 50]]

 [[23 81 77 57 88]]

 [[16 69 74 25  9]]]
[86 89 92 24 55 35 75 94 54 92 51 52 62 92 50 23 81 77 57 88 16 69 74 25
  9]
'''



y[3] = 0
print(x)

'''
[[[86 89 92 24 55]]

 [[35 75 94 54 92]]

 [[51 52 62 92 50]]

 [[23 81 77 57 88]]

 [[16 69 74 25  9]]]
'''



y = x.flatten(order='F')
print(y)

'''
[86 35 51 23 16 89 75 52 81 69 92 94 62 77 74 24 54 92 57 25 55 92 50 88
  9]
'''

`numpy.ravel(a, order='C')`

  • Return a contiguous flattened array.
  • order='C'时返回的是视图;
  • order=’F‘ 时是拷贝。
y = x.ravel()
print(y)
y[3] = 0
print(x)

'''
[86 89 92 24 55 35 75 94 54 92 51 52 62 92 50 23 81 77 57 88 16 69 74 25
  9]
[[[86 89 92  0 55]]

 [[35 75 94 54 92]]

 [[51 52 62 92 50]]

 [[23 81 77 57 88]]

 [[16 69 74 25  9]]]
'''


y = x.ravel(order='F')
print(y)
y[1] = 0
print(x)

'''
[86 35 51 23 16 89 75 52 81 69 92 94 62 77 74  0 54 92 57 25 55 92 50 88
  9]
[[[86 89 92  0 55]]

 [[35 75 94 54 92]]

 [[51 52 62 92 50]]

 [[23 81 77 57 88]]

 [[16 69 74 25  9]]]
'''

`numpy.reshape(a, newshape[, order='C'])`

  •  在不更改数据的情况下为数组赋予新的形状。

 `reshape()`函数当参数`newshape = [rows,-1]`时,将根据行数自动确定列数。

x = np.arange(10)
y = np.reshape(x,[2,5])
print(y.dtype)
print(y)

#int32
#[[0 1 2 3 4]
# [5 6 7 8 9]]

y = np.reshape(x,[2,-1])
print(y)

#[[0 1 2 3 4]
# [5 6 7 8 9]]

y = np.reshape(x,[-1,2])
print(y)

#[[0 1]
# [2 3]
# [4 5]
# [6 7]
# [8 9]]

y[0,0] = 10
print(x)

#[10  1  2  3  4  5  6  7  8  9](改变x去reshape后y中的值,x对应元素也改变)

`reshape()`函数当参数`newshape = -1`时,表示将数组降为一维。

x = np.random.randint(1,100,[2,2,3])
print(x)
y = np.reshape(x,-1)
print(y)

#[[[47 76 25]
# [52 66 34]]
#
# [[29 76 56]
# [25 67 60]]]
#[47 76 25 52 66 34 29 76 56 25 67 60]

2)更改维度

`numpy.newaxis = None`

  •  `None`的别名,对索引数组很有用。

       很多工具包在进行计算时都会先判断输入数据的维度是否满足要求,如果输入数据达不到指定的维度时,可以使用`newaxis`参数来增加一个维度。

x = np.array([1,2,3,4,5,6,7,8,9])
print(x.shape)
print(x)

#(9,)
#[1 2 3 4 5 6 7 8 9]

y = x[np.newaxis,:]
print(y.shape)
print(y)

#(1, 9)
#[[1 2 3 4 5 6 7 8 9]]

y = x[:,np.newaxis]
print(y.shape)
print(y)

#(9, 1)
#[[1]
# [2]
# [3]
# [4]
# [5]
# [6]
# [7]
# [8]
# [9]]

`numpy.squeeze(a, axis=None)` 

  •   `a`表示输入的数组;
  •   `axis`用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错
import numpy as np
x = np.arange(10)
print(x.shape)

#(10,)

x = x[np.newaxis,:]
print(x.shape)

#(1, 10)

x = np.squeeze(x)
print(x.shape)

#(10,)

x = np.random.randint(0,100,(1,3,5,1,7))
print(x.shape)

#(1, 3, 5, 1, 7)

y = np.squeeze(x)
print(y.shape)

#(3, 5, 7)

y = np.squeeze(x,0)
print(y.shape)

#(3, 5, 1, 7)

y = np.squeeze(x,1)

#ValueError: cannot select an axis to squeeze out which has size not equal to one

        在机器学习和深度学习中,通常算法的结果是可以表示向量的数组(即包含两对或以上的方括号形式[[]]),如果直接利用这个数组进行画图可能显示界面为空(见后面的示例)。我们可以利用`squeeze()`函数将表示向量的数组转换为秩为1的数组,这样利用 matplotlib 库函数画图时,就可以正常的显示结果了。

import matplotlib.pyplot as plt
x = np.array([[1,1,2,3,5,8,13,21]])
print(x.shape)
plt.plot(x)
plt.show()

(1, 8)

x = np.squeeze(x)
print(x.shape)
plt.plot(x)
plt.show()

(8,)

3)数组组合

`numpy.concatenate((a1, a2, ...), axis=0, out=None)`

  • Join a sequence of arrays along an existing axis.
x = np.array([1,2,3,4])
y = np.array([2,3,4,5])
z = np.concatenate([x,y])
print(z)

#[1 2 3 4 2 3 4 5]

# x,y是二维的,拼接后的结果也是二维的。

x = x.reshape(1,4)
y = y.reshape(1,4)
z = np.concatenate([x,y])
print(z)

#[[1 2 3 4]
# [2 3 4 5]]

z = np.concatenate([x,y],axis=0)
print(z)

#[[1 2 3 4]
# [2 3 4 5]]

z = np.concatenate([x,y],axis=1)
print(z)

#[[1 2 3 4 2 3 4 5]]

`numpy.stack(arrays, axis=0, out=None)`

  • Join a sequence of arrays along a new axis.
x = np.array([1,2,3,4])
y = np.array([2,3,4,5])
z = np.stack([x,y])
print(z.shape)
print(z)

#(2, 4)
#[[1 2 3 4]
# [2 3 4 5]]

z = np.stack([x,y],axis=0)
print(z.shape)
print(z)

#(2, 4)
#[[1 2 3 4]
# [2 3 4 5]]

z = np.stack([x,y],axis=1)
print(z.shape)
print(z)

#(4, 2)
#[[1 2]
# [2 3]
# [3 4]
# [4 5]]

x = x.reshape(1,4)
y = y.reshape(1,4)
z = np.stack([x,y])
print(z.shape)
print(z)

#(2, 1, 4)
#[[[1 2 3 4]]
#
# [[2 3 4 5]]]

z = np.stack([x,y],axis=0)
print(z.shape)
print(z)

#(2, 1, 4)
#[[[1 2 3 4]]
#
# [[2 3 4 5]]]

z = np.stack([x,y],axis=1)
print(z.shape)
print(z)

#(1, 2, 4)
#[[[1 2 3 4]
#  [2 3 4 5]]]

`numpy.hstack(tup)`

  • Stack arrays in sequence horizontally (column wise). 

操作数.ndim > 1时等价于np.concatenate((a1, a2, ...), axis=1), 操作数.ndim = 1时则可视作np.concatenate((a1, a2, ...), axis=0)

 

注:操作数为一维数组时.hstack并不完全等价于.concatenate

a = np.hstack([np.array([1, 2, 3, 4]), 5])
print(a)  

# [1 2 3 4 5]

a = np.concatenate([np.array([1, 2, 3, 4]), 5])
print(a)

# all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 0 dimension(s)

`numpy.vstack(tup)`

  • Stack arrays in sequence vertically (row wise).

操作数.ndim > 1时等价于np.concatenate((a1, a2, ...), axis=1),操作数.ndim =1时等价于np.stack(arrays, axis=0, out=None)

x = np.array([1,2,3])
y = np.array([4,5,6])

z = np.vstack([x,y])
print(z)

#[[1 2 3]
# [4 5 6]]

z = np.stack([x,y])
print(z)

#[[1 2 3]
# [4 5 6]]

x = np.random.randint(0,10,(3,3))
y = np.random.randint(0,10,(3,3))
print(x)
print(y)

#[[8 8 0]
# [4 2 6]
# [3 0 7]]

#[[0 0 1]
# [4 0 0]
# [0 3 1]]

z = np.vstack([x,y])
print(z)

#[[8 8 0]
# [4 2 6]
# [3 0 7]
# [0 0 1]
# [4 0 0]
# [0 3 1]]

z = np.concatenate([x,y])
print(z)

#[[8 8 0]
# [4 2 6]
# [3 0 7]
# [0 0 1]
# [4 0 0]
# [0 3 1]]

4)数组拆分

`numpy.split(ary, indices_or_sections, axis=0)` 

  • Split an array into multiple sub-arrays as views into ary.
x = np.arange(12,24).reshape(3,-1)
print(x)

#[[12 13 14 15]
#  [16 17 18 19]
#  [20 21 22 23]]

#indices_or_sections=a 为实数时,数组沿着axis=0轴均分成a段,不能均分时报错
y = np.split(x,3)
print(y)

#[array([[12, 13, 14, 15]]), array([[16, 17, 18, 19]]), array([[20, 21, 22, 23]])]

y = np.split(x,2)

#ValueError: array split does not result in an equal division

#indices_or_sections=a[a1,a2,...] 为一维数组时,数组沿着axis=0轴按照 [:a1],[a1,a2],[a2:]切割为片段,若数组中实数超过axis=0轴上的最大索引,生产空数组片段。
y = np.split(x,[1,3])
print(y)

#[array([[12, 13, 14, 15]]), array([[16, 17, 18, 19],
#       [20, 21, 22, 23]]), array([], shape=(0, 4), dtype=int32)]

y = np.split(x,[1,3],axis=1)
print(y)

#[array([[12],
#       [16],
#       [20]]), array([[13, 14],
#       [17, 18],
#       [21, 22]]), array([[15],
#       [19],
#       [23]])]

`numpy.vsplit(ary, indices_or_sections)` 

  • Split an array into multiple sub-arrays vertically (row-wise).
#.vsplit等价于参数axis=0的.split函数
x = np.arange(12,24).reshape(3,-1)
print(x)

#[[12 13 14 15]
#  [16 17 18 19]
#  [20 21 22 23]]

z = np.vsplit(x,3)
print(z)

#[array([[12, 13, 14, 15]]), array([[16, 17, 18, 19]]), array([[20, 21, 22, 23]])]

z = np.split(x,3)
print(z)

#[array([[12, 13, 14, 15]]), array([[16, 17, 18, 19]]), array([[20, 21, 22, 23]])]

z = np.vsplit(x,[1,3])
print(z)

#[array([[12, 13, 14, 15]]), array([[16, 17, 18, 19],
#       [20, 21, 22, 23]]), array([], shape=(0, 4), dtype=int32)]

= np.split(x,[1,3])
print(z)

#[array([[12, 13, 14, 15]]), array([[16, 17, 18, 19],
#       [20, 21, 22, 23]]), array([], shape=(0, 4), dtype=int32)]

`numpy.hsplit(ary, indices_or_sections)`

  • Split an array into multiple sub-arrays horizontally (column-wise).
#.hsplit等价于参数axis=1的.split函数
x = np.arange(12,24).reshape(3,-1)
print(x)

#[[12 13 14 15]
#  [16 17 18 19]
#  [20 21 22 23]]

z = np.hsplit(x,3)
print(z)

#[array([[12, 13],
#       [16, 17],
#       [20, 21]]), array([[14, 15],
#       [18, 19],
#       [22, 23]])]

z = np.hsplit(x,[1,3])
print(z)

#[array([[12],
#       [16],
#       [20]]), array([[13, 14],
#       [17, 18],
#       [21, 22]]), array([[15],
#       [19],
#       [23]])]

z = np.split(x,[1,3],axis=1)
print(z)
#[array([[12],
#       [16],
#       [20]]), array([[13, 14],
#       [17, 18],
#       [21, 22]]), array([[15],
#       [19],
#       [23]])]

5)数组展平

`numpy.tile(A, reps)`

  • Construct an array by repeating A the number of times given by reps.
  • 'tile'是平铺或并列显示的意思
#将原矩阵横向、纵向地复制
x = np.array([[1,2],[3,4]])
print(x)

#[[1 2]
# [3 4]]

y = np.tile(x,(1,3))
print(y)

#[[1 2 1 2 1 2]
# [3 4 3 4 3 4]]

y = np.tile(x,(3,1))
print(y)

#[[1 2]
# [3 4]
# [1 2]
# [3 4]
# [1 2]
# [3 4]]

y = np.tile(x,(3,3))
print(y)

#[[1 2 1 2 1 2]
# [3 4 3 4 3 4]
# [1 2 1 2 1 2]
# [3 4 3 4 3 4]
# [1 2 1 2 1 2]
# [3 4 3 4 3 4]]

`numpy.repeat(a, repeats, axis=None)` 

  • Repeat elements of an array.
  • `axis=0`,沿着y轴复制,实际上增加了行数。
  • `axis=1`,沿着x轴复制,实际上增加了列数。
  • `repeats`,可以为一个数,也可以为一个矩阵。
  • `axis=None`时就会flatten当前矩阵,实际上就是变成了一个行向量。
x = np.repeat(3,4)
print(x)

#[3 3 3 3]

x = np.array([[1,2],[3,4]])
print(x)

#[[1 2]
# [3 4]]

y = np.repeat(x,2)
print(y)

#[1 1 2 2 3 3 4 4]

y = np.repeat(x,2,axis=0)
print(y)

#[[1 2]
# [1 2]
# [3 4]
# [3 4]]

y = np.repeat(x,2,axis=1)
print(y)

#[[1 1 2 2]
# [3 3 4 4]]

y = np.repeat(x,[2,3],axis=0)
print(y)

#[[1 2]
# [1 2]
# [3 4]
# [3 4]
# [3 4]]

y = np.repeat(x,[2,3],axis=1)
print(y)

#[[1 1 2 2 2]
# [3 3 4 4 4]]

6)添加和删除元素

numpy.insert(arr, obj, values, axis=None)

#参数axis为默认值时会将arr展平后进行insert操作

x = np.array([[1,2],[3,4]])
print(x)

#[[1 2]
# [3 4]]

y = np.insert(x,2,5)
print(y)

#[1 2 5 3 4]

#参数values与arr中的元素数据类型不同时,values会自动转换为arr中元素的类型

y = np.insert(x,2,5.0)
print(y)

#[1 2 5 3 4]

#multiple insertionns

y = np.insert(x,[2,3],5)
print(y)

#[1 2 5 3 5 4]

y = np.insert(x,[1,2],[5,6],axis=0)
print(y)

#[[1 2]
# [5 6]
# [3 4]
# [5 6]]

y = np.insert(x,[1,2],[5,6],axis=1)
print(y)

#[[1 5 2 6]
# [3 5 4 6]]

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

#参数axis为默认值时会将arr展平后进行delete操作

x = np.array([[1,2],[3,4]])
print(x)

#[[1 2]
# [3 4]]

y = np.delete(x,1)
print(y)

#[1 3 4]

y = np.delete(x,(1,2))
print(y)

#[1 4]

y = np.delete(x,1,axis=0)
print(y)

#[[1 2]]

y = np.delete(x,1,axis=1)
print(y)

#[[1]
# [3]]

7)查找元素

`numpy.unique(ar, return_index=False, return_inverse=False,return_counts=False, axis=None)` 

  • Find the unique elements of an array.
  • return_index:the indices of the input array that give the unique values
  • return_inverse:the indices of the unique array that reconstruct the input array
  • return_counts:the number of times each unique value comes up in the input array
a=np.array([1,1,2,3,3,4,4])
b=np.unique(a)
print(b)

#[1 2 3 4]

b = np.unique(a,return_index=True)
print(b)

#(array([1, 2, 3, 4]), array([0, 2, 3, 5], dtype=int64))

b = np.unique(a,return_inverse=True)
print(b)

#(array([1, 2, 3, 4]), array([0, 0, 1, 2, 2, 3, 3], dtype=int64))

b = np.unique(a,return_counts=True)
print(b)

#(array([1, 2, 3, 4]), array([2, 1, 2, 2], dtype=int64))

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值