@

目录

  1. 下面的代码是使pyhton3.6写的,有些可能是使用python2.7写的
  2. 1维数组叫矢量,2维数组叫矩阵,3维及大于3维的数组就叫多维数组了

help ,帮助

  1. # 打印 numpy.genfromtxt 帮助文档
  2. print (help(numpy.genfromtxt))
  3. # 打印range 帮助文档
  4. print(help(range))

numpy.genfromtxt,导入文件

示例

  1. import numpy
  2. # 读入文本文件
  3. # "world_alcohol.txt" == > 读入的文件
  4. # delimiter="," == > 文件内容之间的分割符
  5. # dtype=str == > 读入的文件的数据类型
  6. # skip_header=1 == > 跳过第一行,即第一行数据不读取
  7. world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",",dtype=str, skip_header=1)
  8. # 打印类型
  9. print(type(world_alcohol))
  10. # 打印读入的内容
  11. print (world_alcohol)

array,创建数组(1,2维数组)


  1. import numpy
  2. #The numpy.array() function can take a list or list of lists as input. When we input a list, we get a one-dimensional array as a result:
  3. vector = numpy.array([5, 10, 15, 20])
  4. #When we input a list of lists, we get a matrix as a result:
  5. matrix = numpy.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])
  6. # 一维数组
  7. print(vector)
  8. # 二维数组
  9. print(matrix)

结果

array,创建行列向量

Numpy中的行向量和列向量 - Suprecat的博客 - CSDN博客

https://blog.csdn.net/wintersshi/article/details/80489258

numpy.shape,看numpy数据的行列信息

  1. #We can use the ndarray.shape property to figure out how many elements are in the array
  2. vector = numpy.array([1, 2, 3, 4])
  3. print(vector.shape)
  4. #For matrices, the shape property contains a tuple with 2 elements.
  5. matrix = numpy.array([[5, 10, 15], [20, 25, 30]])
  6. print(matrix.shape)

结果

  1. (4,)
  2. (2, 3)

numpy.dtype,查看numpy数据的类型

  1. import numpy
  2. #Each value in a NumPy array has to have the same data type
  3. #NumPy will automatically figure out an appropriate data type when reading in data or converting lists to arrays.
  4. #You can check the data type of a NumPy array using the dtype property.
  5. numbers = numpy.array([1, 2, 3, 4])
  6. print (numbers)
  7. numbers.dtype

结果

  1. [1 2 3 4]
  2. dtype('int32')

切片读取

示例一

  • 说明

    [0:3], 范围包括0,但是不包括3 ,共3
  1. import numpy
  2. vector = numpy.array([5, 10, 15, 20])
  3. print(vector[0:3])

结果

  1. [ 5 10 15]

示例二

  1. import numpy
  2. matrix = numpy.array([
  3. [5, 10, 15],
  4. [20, 25, 30],
  5. [35, 40, 45]
  6. ])
  7. # 读取所有的行,第1列(共0,1,2列)的数
  8. print(matrix[:,1])

结果

  1. [10 25 40]

示例三

  1. import numpy
  2. matrix = numpy.array([
  3. [5, 10, 15],
  4. [20, 25, 30],
  5. [35, 40, 45]
  6. ])
  7. # 读取所有的行,第0,1(共0,1,2列)列的数据
  8. print(matrix[:,0:2])

结果

  1. [[ 5 10]
  2. [20 25]
  3. [35 40]]
  • 示例四
  1. matrix = numpy.array([
  2. [5, 10, 15],
  3. [20, 25, 30],
  4. [35, 40, 45]
  5. ])
  6. # 矩阵的行1,2,列 0 ,1(共0,1,2列)
  7. print(matrix[1:3,0:2])

结果

  1. [[20 25]
  2. [35 40]]

== , 矩阵元素等号判断

  • 示例一
  1. import numpy
  2. #it will compare the second value to each element in the vector
  3. # If the values are equal, the Python interpreter returns True; otherwise, it returns False
  4. vector = numpy.array([5, 10, 15, 20])
  5. vector == 10

结果

  1. array([False, True, False, False], dtype=bool)
  • 示例二
  1. import numpy
  2. matrix = numpy.array([
  3. [5, 10, 15],
  4. [20, 25, 30],
  5. [35, 40, 45]
  6. ])
  7. matrix == 25

结果

  1. array([[False, False, False],
  2. [False, True, False],
  3. [False, False, False]], dtype=bool)
  • 示例三
  1. import numpy
  2. # Compares vector to the value 10, which generates a new Boolean vector [False, True, False, False]. It assigns this result to equal_to_ten
  3. # 生成矩阵向量
  4. vector = numpy.array([5, 10, 15, 20])
  5. # 与 10 相等 进行布尔运算
  6. equal_to_ten = (vector == 10)
  7. print equal_to_ten
  8. # 得到原来的数据
  9. print(vector[equal_to_ten])

结果

  1. [False True False False]
  2. [10]
  • 示例四

  1. import numpy
  2. matrix = numpy.array([
  3. [5, 10, 15],
  4. [20, 25, 30],
  5. [35, 40, 45]
  6. ])
  7. # 对所有行的第1列与 25 进行相等布尔运算
  8. second_column_25 = (matrix[:,1] == 25)
  9. # 布尔运算的结果
  10. print(second_column_25)
  11. # 还原 原来 的列
  12. print(matrix[:, second_column_25])
  13. # 将布尔结果对 行 进行求数
  14. print(matrix[second_column_25, :])

结果

  1. [False True False]
  2. [[10]
  3. [25]
  4. [40]]
  5. [[20 25 30]]
  • 示例五,对二维矩阵中的数一个指定的数据进行修改
  1. import numpy
  2. matrix = numpy.array([
  3. [5, 10, 15],
  4. [20, 25, 30],
  5. [35, 40, 45]
  6. ])
  7. # 所有行的第1列(共0,1,2列)的数据与25进行布尔运算
  8. second_column_25 = matrix[:, 1] == 25
  9. print(second_column_25)
  10. # 数据第1行第一列(共0,1,2列),25 ===> 10
  11. matrix[second_column_25, 1] = 10
  12. print(matrix)

结果

  1. [False True False]
  2. [[ 5 10 15]
  3. [20 10 30]
  4. [35 40 45]]

&,与操作

  1. import numpy
  2. #We can also perform comparisons with multiple conditions
  3. vector = numpy.array([5, 10, 15, 20])
  4. # 与10进行布尔运算
  5. print(vector == 10)
  6. # 与 5进行布尔运算
  7. print(vector == 5)
  8. # 与操作
  9. equal_to_ten_and_five = (vector == 10) & (vector == 5)
  10. print(equal_to_ten_and_five)

结果

  1. [False True False False]
  2. [ True False False False]
  3. [False False False False]

|,或操作

  • 示列一
  1. import numpy
  2. vector = numpy.array([5, 10, 15, 20])
  3. # 与10进行布尔运算
  4. print(vector == 10)
  5. # 与 5进行布尔运算
  6. print(vector == 5)
  7. # 或操作
  8. equal_to_ten_and_five = (vector == 10) | (vector == 5)
  9. print(equal_to_ten_and_five)

结果

  1. [False True False False]
  2. [ True False False False]
  3. [ True True False False]
  • 示例二

  1. import numpy
  2. vector = numpy.array([5, 10, 15, 20])
  3. # 与10进行布尔运算
  4. print(vector == 10)
  5. # 与 5进行布尔运算
  6. print(vector == 5)
  7. # 或操作
  8. equal_to_ten_or_five = (vector == 10) | (vector == 5)
  9. # 或操作后的数据进行修改
  10. # 5, 10 ==> 50
  11. vector[equal_to_ten_or_five] = 50
  12. print(vector)

结果

  1. [False True False False]
  2. [ True False False False]
  3. [50 50 15 20]

字符串 ==> 数据类型

  1. import numpy
  2. # We can convert the data type of an array with the ndarray.astype() method.
  3. vector = numpy.array(["1", "2", "3"])
  4. # 原来的类型
  5. print(vector.dtype)
  6. print(vector)
  7. # 字符串 ==> 数据类型
  8. vector = vector.astype(float)
  9. # 现在的类型
  10. print(vector.dtype)
  11. print(vector)

结果

  1. <U1
  2. ['1' '2' '3']
  3. float64
  4. [1. 2. 3.]

min,最小值

  1. import numpy
  2. vector = numpy.array([5, 10, 15, 20])
  3. vector.min()
  4. print (vector.min())

结果

  1. 5

sum

  • 说明

    axis=1,每一行求和

    axis=0,每一列求和

  • 示例一

  1. import numpy
  2. # The axis dictates which dimension we perform the operation on
  3. #1 means that we want to perform the operation on each row, and 0 means on each column
  4. matrix = numpy.array([
  5. [5, 10, 15],
  6. [20, 25, 30],
  7. [35, 40, 45]
  8. ])
  9. print(matrix.sum(axis=1))

结果

  1. [ 30 75 120]
  • 示例二
  1. import numpy
  2. matrix = numpy.array([
  3. [5, 10, 15],
  4. [20, 25, 30],
  5. [35, 40, 45]
  6. ])
  7. print(matrix.sum(axis=0))

结果

  1. [60 75 90]

shape,arange,reshape

  • shape, 查看维度信息

  • arange,生成一组向量

  • reshape,对一组向量修改成新一矩阵分布

  • 示例一,shape

  1. import numpy as np
  2. a = np.array([
  3. [6., 9., 1., 5.],
  4. [3., 6., 4., 0.],
  5. [3., 9., 5., 7.]
  6. ])
  7. print("a\n", a)
  8. print("a.shape\n", a.shape)
  9. a.shape = (2,6)
  10. print("a.shape = (2,6)\n", a)
  11. a.shape = (1,12)
  12. print("a.shape = (1,12)\n", a)

结果

  1. a
  2. [[6. 9. 1. 5.]
  3. [3. 6. 4. 0.]
  4. [3. 9. 5. 7.]]
  5. a.shape
  6. (3, 4)
  7. a.shape = (2,6)
  8. [[6. 9. 1. 5. 3. 6.]
  9. [4. 0. 3. 9. 5. 7.]]
  10. a.shape = (1,12)
  11. [[6. 9. 1. 5. 3. 6. 4. 0. 3. 9. 5. 7.]]
  • 示例二,arange

  1. import numpy as np
  2. # 生成 10 到 30 之间的数据,不包括30,间距为5的数据
  3. print(np.arange( 10, 30, 5 ))
  4. # 生成 10 到 11 之间的数据,不包括11,间距为0.2的数据
  5. print(np.arange( 10, 11, 0.2))

结果

  1. [10 15 20 25]
  2. [10. 10.2 10.4 10.6 10.8]
  • 示例三,shape,reshape
  1. import numpy as np
  2. # 生成一组向量
  3. # To create sequences of numbers
  4. print(np.arange(15))
  5. # 查看矩阵的信息
  6. print(np.arange(15).shape)
  7. # 修改成新3行5列的矩阵数据
  8. a = np.arange(15).reshape(3, 5)
  9. print(a)
  10. # 查看矩阵的信息
  11. print(a.shape)

结果

  1. [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
  2. (15,)
  3. [[ 0 1 2 3 4]
  4. [ 5 6 7 8 9]
  5. [10 11 12 13 14]]
  6. (3, 5)

ndim

  • 说明: 用来查看矩阵的维度信息,1维,2维等
  1. import numpy as np
  2. # 生成一组向量
  3. print(np.arange(15))
  4. # 查看矩阵的维度信息
  5. print(np.arange(15).ndim)
  6. # 修改成新3行5列的矩阵数据
  7. a = np.arange(15).reshape(3, 5)
  8. print(a)
  9. # 查看矩阵的维度信息
  10. print(a.ndim)

结果

  1. [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
  2. 1
  3. [[ 0 1 2 3 4]
  4. [ 5 6 7 8 9]
  5. [10 11 12 13 14]]
  6. 2

size,查看矩阵的元素多少

  1. import numpy as np
  2. # 生成一组向量
  3. print(np.arange(15))
  4. # 查看矩阵的信息
  5. print(np.arange(15).dtype)
  6. print(np.arange(15).dtype.name)

结果

  1. [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
  2. int32
  3. int32

  1. import numpy as np
  2. # 生成一组向量
  3. print(np.arange(15))
  4. # 查看矩阵总共有多少元素
  5. print(np.arange(15).size)
  6. # 修改成新3行5列的矩阵数据
  7. a = np.arange(15).reshape(3, 5)
  8. print(a)
  9. # 查看矩阵总共有多少元素
  10. print(a.size)

结果

  1. [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
  2. 15
  3. [[ 0 1 2 3 4]
  4. [ 5 6 7 8 9]
  5. [10 11 12 13 14]]
  6. 15

zeros,生成0矩阵

  • 说明:生成的0矩阵是一个float64类型的,注意到0后面有一个小数点
  1. import numpy as np
  2. # 3行4列
  3. print(np.zeros ((3,4)) )
  4. print(np.zeros ((3,4)).dtype )

结果

  1. [[0. 0. 0. 0.]
  2. [0. 0. 0. 0.]
  3. [0. 0. 0. 0.]]
  4. float64

ones,生成单位矩阵

  • 说明
  1. numpy.ones(shape, dtype=None, order='C')
  2. Parameters:
  3. # 形状
  4. shape : int or sequence of ints
  5. Shape of the new array, e.g., (2, 3) or 2.
  6. # 数据类型
  7. dtype : data-type, optional
  8. The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64.
  9. # 存储順序
  10. order : {‘C’, F’}, optional, default: C
  11. Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.
  12. Returns:
  13. out : ndarray
  14. Array of ones with the given shape, dtype, and order.

示例一

  1. import numpy as np
  2. # 生成float64类型
  3. print(np.ones(5))

结果

  1. [1. 1. 1. 1. 1.]

示例二

  1. import numpy as np
  2. # 生成 int 类型
  3. print(np.ones((5,), dtype=int))

结果

  1. [1, 1, 1, 1, 1]

示例三

  1. import numpy as np
  2. # 生成2行3列的float64类型数据
  3. s = (2,3)
  4. print( np.ones(s))

结果

  1. [[1. 1. 1.]
  2. [1. 1. 1.]]

示例四

  • 说明

    np.ones(2,3,4)含义:2个3X4矩阵,所有元素为1

    又如,np.ones(4,5,3)含义:4个5X3矩阵,所有元素为1

    ==> np.ones(a,b,c):a个b*c矩阵
  1. import numpy as np
  2. print(np.ones( (2,3,4), dtype=np.int32 ))

结果

  1. [[[1 1 1 1]
  2. [1 1 1 1]
  3. [1 1 1 1]]
  4. [[1 1 1 1]
  5. [1 1 1 1]
  6. [1 1 1 1]]]
  • 扩展阅读

numpy 中ones,zeros函数 - mmr598146920的专栏 - CSDN博客

https://blog.csdn.net/mmr598146920/article/details/80539733

random

  1. import numpy as np
  2. # 2行3列随机数
  3. print(np.random.random((2, 3)))

结果

  1. [[0.69509627 0.04046586 0.26786661]
  2. [0.44120144 0.05091389 0.44784084]]

linspace, 在指定范围内等间距生成指定的数据个数

  1. import numpy as np
  2. from numpy import pi
  3. # 在 0 到时 2*pi 区间(不包括右端点 2*pi)内,等间距生成 5 个数据
  4. print(np.linspace( 0, 2*pi, 5 ))

结果

  1. [0. 1.57079633 3.14159265 4.71238898 6.28318531]

sin, 对矩阵中的数据求三角函数sin值

  1. import numpy as np
  2. from numpy import pi
  3. # 求三角函数sin值
  4. print(np.sin(np.linspace( 0, 2*pi, 5 )))

结果

  1. [ 0.0000000e+00 1.0000000e+00 1.2246468e-16 -1.0000000e+00
  2. -2.4492936e-16]

矩阵的减-,平方**,范围判断<

  1. import numpy as np
  2. # the product operator * operates elementwise in NumPy arrays
  3. a = np.array([20, 30, 40, 50])
  4. b = np.arange(4)
  5. print("a:", a)
  6. print("b", b)
  7. # 矩阵相减
  8. c = a - b
  9. print("c = a - b", c)
  10. # 矩阵减1
  11. c = c - 1
  12. print("c = c - 1", c)
  13. # 矩阵中的每一个元素的平方
  14. print("b ** 2", b ** 2)
  15. # 范围判断
  16. print("a < 35", a < 35)

结果

  1. a: [20 30 40 50]
  2. b [0 1 2 3]
  3. c = a - b [20 29 38 47]
  4. c = c - 1 [19 28 37 46]
  5. b ** 2 [0 1 4 9]
  6. a < 35 [ True True False False]

A * B,矩阵之间对应元素相乘

  1. import numpy as np
  2. A = np.array([[1, 1],
  3. [0, 1]])
  4. B = np.array([[2, 0],
  5. [3, 4]])
  6. print('---A----')
  7. print(A)
  8. print('---B----')
  9. print(B)
  10. # 矩阵之间对应元素相乘
  11. print('---A * B----')
  12. print(A * B)

结果

  1. ---A----
  2. [[1 1]
  3. [0 1]]
  4. ---B----
  5. [[2 0]
  6. [3 4]]
  7. ---A * B----
  8. [[2 0]
  9. [0 4]]

A.dot(B),np.dot(A, B)),矩阵相乘

  1. import numpy as np
  2. # The matrix product can be performed using the dot function or method
  3. A = np.array([[1, 1],
  4. [0, 1]])
  5. B = np.array([[2, 0],
  6. [3, 4]])
  7. print('---A----')
  8. print(A)
  9. print('---B----')
  10. print(B)
  11. print('---A * B----')
  12. print(A * B)
  13. # 矩阵相乘
  14. print('---A.dot(B)----')
  15. print(A.dot(B))
  16. # 矩阵相乘
  17. print('---np.dot(A, B)----')
  18. print(np.dot(A, B))

结果

  1. ---A----
  2. [[1 1]
  3. [0 1]]
  4. ---B----
  5. [[2 0]
  6. [3 4]]
  7. ---A.dot(B)----
  8. [[5 4]
  9. [3 4]]
  10. ---np.dot(A, B)----
  11. [[5 4]
  12. [3 4]]

exp,自然指数e

  1. import numpy as np
  2. B = np.arange(3)
  3. print(B)
  4. # 求自然指数e的次幂,B中的元素为e每一幂次方
  5. print(np.exp(B))

结果

  1. [0 1 2]
  2. [1. 2.71828183 7.3890561 ]

sqrt,开方运算

  1. import numpy as np
  2. B = np.arange(3)
  3. print(B)
  4. # 对B中的每一个元素求根号,即1/2次幂
  5. print(np.sqrt(B))

结果

  1. [0 1 2]
  2. [0. 1. 1.41421356]

floor,向下取整

示例一

  1. import numpy as np
  2. a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0])
  3. np.floor(a)

结果

  1. [-2., -2., -1., 0., 1., 1., 2.]

示例二

  1. import numpy as np
  2. # Return the floor of the input
  3. a = 10 * np.random.random((3, 4))
  4. print(a)
  5. a = np.floor(a)
  6. print(a)

结果

  1. [[6.6143481 9.9613796 1.30947854 5.6078685 ]
  2. [3.10948678 6.83076618 4.92651686 0.15127964]
  3. [3.3036663 9.44427669 5.25021126 7.66229507]]
  4. [[6. 9. 1. 5.]
  5. [3. 6. 4. 0.]
  6. [3. 9. 5. 7.]]

ravel,水平铺开矩阵

  • 说明

    2维矩阵 ==> 1维向量,水平方向进行转化
  1. import numpy as np
  2. # Return the floor of the input
  3. a = 10 * np.random.random((3, 4))
  4. print(a)
  5. a = np.floor(a)
  6. print(a)
  7. print('--------')
  8. # 2维矩阵 ==> 1维向量,水平方向进行转化
  9. # flatten the array
  10. print(a.ravel() )

结果

  1. [[6.6143481 9.9613796 1.30947854 5.6078685 ]
  2. [3.10948678 6.83076618 4.92651686 0.15127964]
  3. [3.3036663 9.44427669 5.25021126 7.66229507]]
  4. [[6. 9. 1. 5.]
  5. [3. 6. 4. 0.]
  6. [3. 9. 5. 7.]]
  7. --------
  8. [6. 9. 1. 5. 3. 6. 4. 0. 3. 9. 5. 7.]

T,矩阵的转置

  • 说明

    在这种方式下,行向量无法直接转化为列向量

    具体的转化方式见:

[numpy] np.newaxis 如何将行向量转换成列向量 - doufuxixi的博客 - CSDN博客

https://blog.csdn.net/doufuxixi/article/details/80356946

  1. import numpy as np
  2. # Return the floor of the input
  3. a = 10 * np.random.random((3, 4))
  4. print(a)
  5. # 向下取整
  6. a = np.floor(a)
  7. print("a = np.floor(a)")
  8. print(a)
  9. # a 的转置
  10. print("--------a.T")
  11. print(a.T)
  12. print('--------b = a.ravel()')
  13. # a 水平方向铺平
  14. b = a.ravel()
  15. print(b)
  16. # b 的转置
  17. print('--------b.T')
  18. print(b.T)

结果

  1. [[6.8977933 6.5823566 2.70240107 4.48524208]
  2. [0.96507135 4.58781425 6.2868975 7.39792458]
  3. [6.18095442 4.62072618 5.73384294 8.45966937]]
  4. a = np.floor(a)
  5. [[6. 6. 2. 4.]
  6. [0. 4. 6. 7.]
  7. [6. 4. 5. 8.]]
  8. --------a.T
  9. [[6. 0. 6.]
  10. [6. 4. 4.]
  11. [2. 6. 5.]
  12. [4. 7. 8.]]
  13. --------b = a.ravel()
  14. [6. 6. 2. 4. 0. 4. 6. 7. 6. 4. 5. 8.]
  15. --------b.T
  16. [6. 6. 2. 4. 0. 4. 6. 7. 6. 4. 5. 8.]

vstack,按照列的方向拼接两个矩阵

  1. import numpy as np
  2. a = np.floor(10 * np.random.random((2, 2)))
  3. b = np.floor(10 * np.random.random((2, 2)))
  4. print('---a')
  5. print(a)
  6. print('---b')
  7. print(b)
  8. print('---np.vstack((a, b)')
  9. print(np.vstack((a, b)))

结果

  1. ---a
  2. [[4. 3.]
  3. [8. 6.]]
  4. ---b
  5. [[8. 2.]
  6. [6. 0.]]
  7. ---np.vstack((a, b)
  8. [[4. 3.]
  9. [8. 6.]
  10. [8. 2.]
  11. [6. 0.]]

hstack,按照行的方向拼接两个矩阵

  1. import numpy as np
  2. a = np.floor(10 * np.random.random((2, 2)))
  3. b = np.floor(10 * np.random.random((2, 2)))
  4. print('---a')
  5. print(a)
  6. print('---b')
  7. print(b)
  8. print('---np.hstack((a, b)')
  9. print(np.hstack((a, b)))

结果

  1. ---a
  2. [[1. 9.]
  3. [3. 6.]]
  4. ---b
  5. [[9. 6.]
  6. [5. 0.]]
  7. ---np.hstack((a, b)
  8. [[1. 9. 9. 6.]
  9. [3. 6. 5. 0.]]

hsplit,垂直方向上等数量分割矩阵数据

  1. import numpy as np
  2. x = np.arange(16.0).reshape(2, 8)
  3. print(x)
  4. # 等个数分割为2份
  5. print(np.hsplit(x, 2))
  6. # 等个数分割为4份
  7. print(np.hsplit(x, 4))

结果

  1. [[ 0. 1. 2. 3. 4. 5. 6. 7.]
  2. [ 8. 9. 10. 11. 12. 13. 14. 15.]]
  3. [array([[ 0., 1., 2., 3.],
  4. [ 8., 9., 10., 11.]]), array([[ 4., 5., 6., 7.],
  5. [12., 13., 14., 15.]])]
  6. [array([[0., 1.],
  7. [8., 9.]]), array([[ 2., 3.],
  8. [10., 11.]]), array([[ 4., 5.],
  9. [12., 13.]]), array([[ 6., 7.],
  10. [14., 15.]])]

结果( 重新排版后)

  1. [[ 0. 1. 2. 3. 4. 5. 6. 7.]
  2. [ 8. 9. 10. 11. 12. 13. 14. 15.]]
  3. [array([[ 0., 1., 2., 3.],
  4. [ 8., 9., 10., 11.]]),
  5. array([[ 4., 5., 6., 7.],
  6. [12., 13., 14., 15.]])]
  7. [array([[0., 1.],
  8. [8., 9.]]),
  9. array([[ 2., 3.],
  10. [10., 11.]]),
  11. array([[ 4., 5.],
  12. [12., 13.]]),
  13. array([[ 6., 7.],
  14. [14., 15.]])]

vsplit,水平方向上等数量分割矩阵数据

  1. import numpy as np
  2. x = np.arange(16.0).reshape(8, 2)
  3. print(x)
  4. # 等数量分割为2份
  5. print(np.vsplit(x, 2))
  6. # 等数量分割为4份
  7. print(np.vsplit(x, 4))

结果

  1. [[ 0. 1.]
  2. [ 2. 3.]
  3. [ 4. 5.]
  4. [ 6. 7.]
  5. [ 8. 9.]
  6. [10. 11.]
  7. [12. 13.]
  8. [14. 15.]]
  9. [array([[0., 1.],
  10. [2., 3.],
  11. [4., 5.],
  12. [6., 7.]]), array([[ 8., 9.],
  13. [10., 11.],
  14. [12., 13.],
  15. [14., 15.]])]
  16. [array([[0., 1.],
  17. [2., 3.]]), array([[4., 5.],
  18. [6., 7.]]), array([[ 8., 9.],
  19. [10., 11.]]), array([[12., 13.],
  20. [14., 15.]])]

结果( 重新排版后)

  1. [[ 0. 1.]
  2. [ 2. 3.]
  3. [ 4. 5.]
  4. [ 6. 7.]
  5. [ 8. 9.]
  6. [10. 11.]
  7. [12. 13.]
  8. [14. 15.]]
  9. [array([
  10. [0., 1.],
  11. [2., 3.],
  12. [4., 5.],
  13. [6., 7.]]),
  14. array([
  15. [ 8., 9.],
  16. [10., 11.],
  17. [12., 13.],
  18. [14., 15.]])]
  19. [array([[0., 1.],
  20. [2., 3.]]),
  21. array([[4., 5.],
  22. [6., 7.]]),
  23. array([[ 8., 9.],
  24. [10., 11.]]),
  25. array([[12., 13.],
  26. [14., 15.]])]

a=b,复制

  • 说明

复制就是使用“=”。使用“=”的时候,实际上是传递的是对象的引用,当对象发生修改的时候,复制体也会发生同等的改变,无论何种改变。

  1. import numpy as np
  2. #Simple assignments make no copy of array objects or of their data.
  3. a = np.arange(12)
  4. b = a
  5. # a and b are two names for the same ndarray object
  6. print (b is a)
  7. # b的形状改变,a也跟着改变
  8. b.shape = 3,4
  9. print (a.shape)
  10. print (id(a))
  11. print (id(b))

结果

  1. True
  2. (3, 4)
  3. 1974659569504
  4. 1974659569504

view,浅复制

  • 说明

view相当于传引用,view和原始数据共享一份数据,修改一个会影响另一个。


  1. import numpy as np
  2. a = np.arange(12)
  3. # The view method creates a new array object that looks at the same data.
  4. c = a.view()
  5. # 判断c a 是否共用一个内存id
  6. print("c is a")
  7. print(c is a)
  8. # 查看a,c 的id
  9. print("id(a)")
  10. print(id(a))
  11. print("id(c)")
  12. print(id(c))
  13. # 对c 的形状进行修改
  14. c.shape = 2, 6
  15. # a的形状不发生改变
  16. print("a.shape")
  17. print(a.shape)
  18. print("c.shape")
  19. print(c.shape)
  20. # 对c 的元素进行修改,a中的对应位置的元素也跟着修改
  21. c[0, 4] = 1234
  22. print(a)
  23. print("a")
  24. print(c)
  25. print("c")

结果

  1. c is a
  2. False
  3. id(a)
  4. 34629952
  5. id(c)
  6. 36175504
  7. a.shape
  8. (12,)
  9. c.shape
  10. (2, 6)
  11. [ 0 1 2 3 1234 5 6 7 8 9 10 11]
  12. a
  13. [[ 0 1 2 3 1234 5]
  14. [ 6 7 8 9 10 11]]
  15. c
  • 拓展阅读

numpy中的copy和view - 大泽之国 - CSDN博客

https://blog.csdn.net/z0n1l2/article/details/83116775

copy,深拷贝

  • 说明

    将对象中的所有的元素全都拷贝,拷贝后,两者这件的独立的,互不影响
  1. import numpy as np
  2. a = np.arange(12)
  3. #The copy method makes a complete copy of the array and its data.
  4. # 完全拷贝
  5. d = a.copy()
  6. print (d is a)
  7. # 修改d 中的元素
  8. d[0] = 9999
  9. print (d)
  10. print (a)

结果

  1. [9999 1 2 3 4 5 6 7 8 9 10 11]
  2. [ 0 1 2 3 4 5 6 7 8 9 10 11]
  • 拓展阅读

numpy中的copy和view - 大泽之国 - CSDN博客

https://blog.csdn.net/z0n1l2/article/details/83116775

python复制,浅拷贝,深拷贝理解 - Young的博客 - CSDN博客

https://blog.csdn.net/dearyangjie/article/details/71533615

Python中复制,浅拷贝,深拷贝的区别详解 - H845165367的博客 - CSDN博客

https://blog.csdn.net/H845165367/article/details/79687387

argmax,垂直方向上找出最大值的索引

  • 说明

    垂直方向上,找出最大值的位置

    如果某一列中,有两个元素是相同的大小,那么就取靠前的数值的索引

  1. import numpy as np
  2. # data = 100*np.sin(np.arange(20)).reshape(5, 4)
  3. # data = np.floor(data)
  4. # 5行4列
  5. data = np.array(
  6. [[ 0., 84., 90., 14.,],
  7. [ -76., -96., -28., 65.,],
  8. [ 98., 41., -55., -100.,],
  9. [ -54., 42., 99., 65.,],
  10. [ -29., -97., -76., 14.,]]
  11. )
  12. print("data")
  13. print(data)
  14. # 垂直方向上,找出最大值的位置
  15. # 结果:[2 0 3 1],分别对应第0列的第2个,第1列的第0个,第2列的3个,第3列的第1个(这里是与第3个等值,故取靠前的数值)
  16. ind = data.argmax(axis=0)
  17. print("ind")
  18. print(ind)
  19. # 在1行中,还原最大值的数据
  20. data_max = data[ind, range(data.shape[1])]
  21. print("data_max")
  22. print(data_max)

结果

  1. data
  2. [[ 0. 84. 90. 14.]
  3. [ -76. -96. -28. 65.]
  4. [ 98. 41. -55. -100.]
  5. [ -54. 42. 99. 65.]
  6. [ -29. -97. -76. 14.]]
  7. ind
  8. [2 0 3 1]
  9. data_max
  10. [98. 84. 99. 65.]

tile,重复数据

  1. import numpy as np
  2. a = np.arange(0, 40, 10)
  3. print(a)
  4. # 对a中的数据进行重复,以a为单位生成新的4行3列的矩阵
  5. # 由于a是1行4列的数据,因此最终生成 4*3 =12列数据
  6. b = np.tile(a, (4, 3))
  7. print(b)
  8. print(b.shape)

结果

  1. [ 0 10 20 30]
  2. [[ 0 10 20 30 0 10 20 30 0 10 20 30]
  3. [ 0 10 20 30 0 10 20 30 0 10 20 30]
  4. [ 0 10 20 30 0 10 20 30 0 10 20 30]
  5. [ 0 10 20 30 0 10 20 30 0 10 20 30]]
  6. (4, 12)

sort,按顺序排列数据

  • 说明

    axis=0,垂直排列数据,从小到大

    axis=1,水平排列数据,从小到大

示例一,axis=1

  1. import numpy as np
  2. a = np.array([[4, 3, 5], [1, 2, 1]])
  3. print(a)
  4. # 方式一
  5. print('方式一--------b = np.sort(a, axis=1)')
  6. b = np.sort(a, axis=1)
  7. print(b)
  8. print('--------a')
  9. print(a)
  10. # 方式二
  11. print('方式二--------a.sort(axis=1)')
  12. a.sort(axis=1)
  13. print(a)

结果

  1. [[4 3 5]
  2. [1 2 1]]
  3. 方式一--------b = np.sort(a, axis=1)
  4. [[3 4 5]
  5. [1 1 2]]
  6. --------a
  7. [[4 3 5]
  8. [1 2 1]]
  9. 方式二--------a.sort(axis=1)
  10. [[3 4 5]
  11. [1 1 2]]

示例二,axis=0

  1. import numpy as np
  2. a = np.array([[4, 3, 5], [1, 2, 1]])
  3. print(a)
  4. # 方式一
  5. print('方式一--------b = np.sort(a, axis=0)')
  6. b = np.sort(a, axis=0)
  7. print(b)
  8. print('--------a')
  9. print(a)
  10. # 方式二
  11. print('方式二--------a.sort(axis=0)')
  12. a.sort(axis=0)
  13. print(a)

结果

  1. [[4 3 5]
  2. [1 2 1]]
  3. 方式一--------b = np.sort(a, axis=0)
  4. [[1 2 1]
  5. [4 3 5]]
  6. --------a
  7. [[4 3 5]
  8. [1 2 1]]
  9. 方式二--------a.sort(axis=0)
  10. [[1 2 1]
  11. [4 3 5]]

argsort()函数是将x中的元素从小到大排列,提取其对应的index(索引),然后输出到y

argsort,向量中元素从小到大排列

  • 说明

    argsort()函数是将x中的元素从小到大排列,提取其对应的index(索引),然后输出到y
  1. import numpy as np
  2. a = np.array([4, 3, 1, 2])
  3. j = np.argsort(a)
  4. print('--------')
  5. print(j)
  6. print('--------')
  7. print(a[j])

结果

  1. --------
  2. [2 3 1 0]
  3. --------
  4. [1 2 3 4]

numpy的函数使用的更多相关文章

  1. 006 numpy常用函数

    属于Numpy的函数. 一:通用函数 1.说明 是一种对ndarray中的数据执行元素级运算的函数. 2.一元函数 3.二元函数 二:矢量计算 1.numpy.where 主要有两种用法 np.whe ...

  2. NumPy 数学函数

    NumPy 数学函数 NumPy 包含大量的各种数学运算的函数,包括三角函数,算术运算的函数,复数处理函数等. 三角函数 NumPy 提供了标准的三角函数:sin().cos().tan(). 实例 ...

  3. NumPy 字符串函数

    NumPy 字符串函数 以下函数用于对 dtype 为 numpy.string_ 或 numpy.unicode_ 的数组执行向量化字符串操作. 它们基于 Python 内置库中的标准字符串函数. ...

  4. Python中numpy.apply_along_axis()函数的用法

    numpy.apply_along_axis(func, axis, arr, *args, **kwargs): 必选参数:func,axis,arr.其中func是我们自定义的一个函数,函数fun ...

  5. numpy常用函数学习

    目录numpy常用函数学习点乘法线型预测线性拟合裁剪.压缩和累乘相关性多项式拟合提取符号数组杂项点乘法该方法为数学方法,但是在numpy使用的时候略坑.numpy的点乘为a.dot(b)或numpy. ...

  6. numpy.rollaxis函数

    numpy.rollaxis numpy.rollaxis 函数向后滚动特定的轴到一个特定位置,格式如下: numpy.rollaxis(arr, axis, start) 参数说明: arr:数组 ...

  7. Numpy常用函数用法大全

    .ndim :维度.shape :各维度的尺度 (2,5).size :元素的个数 10.dtype :元素的类型 dtype(‘int32’).itemsize :每个元素的大小,以字节为单位 ,每 ...

  8. 12、numpy——数学函数

    NumPy 数学函数 NumPy 包含大量的各种数学运算的函数,包括三角函数,算术运算的函数,复数处理函数等. 1.三角函数 NumPy 提供了标准的三角函数:sin().cos().tan(). i ...

  9. 11、numpy——字符串函数

    NumPy 字符串函数 以下函数用于对 dtype 为 numpy.string_ 或 numpy.unicode_ 的数组执行向量化字符串操作. 它们基于 Python 内置库中的标准字符串函数. ...

  10. 吴裕雄--天生自然Numpy库学习笔记:NumPy 数学函数

    NumPy 包含大量的各种数学运算的函数,包括三角函数,算术运算的函数,复数处理函数等. NumPy 提供了标准的三角函数:sin().cos().tan(). import numpy as np ...

随机推荐

  1. Ajax的那点事

    Ajax中什么是同步.异步? 同步:就是用户填写完信息之后,全部提交给服务器,等待服务器的回应,是一次性全部的. 异步:当用户填写完一条信息之后,这条信息会自动向服务器提交,然后服务器响应客户端,在此 ...

  2. day 90 RBAC

    参考博客 -陈晓梅 http://www.cnblogs.com/c-x-m/p/9025478.html 登录view from django.shortcuts import render,red ...

  3. (转)SQL注入原理

    原文地址:http://www.cnblogs.com/rush/archive/2011/12/31/2309203.html SQL Injection:就是通过把SQL命令插入到Web表单递交或 ...

  4. rafy使用笔记

    1.rafy里实体字段string类型映射成数据库varchar(2000). 解决:“DomainApp“下“OnRuntimeStarting“方法里添加“DbMigrationSettings. ...

  5. centos7下查看cup核数

    centos7下查看cup核数 # 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数cat ...

  6. 微服务-技术专区-监控专区(Skywalking与Pinpoint) - 监控对比分析

    由于公司目前有200多微服务,微服务之间的调用关系错综复杂,调用关系人工维护基本不可能实现,需要调研一套全链路追踪方案,初步调研之后选取了skywalking和pinpoint进行对比; 选取skyw ...

  7. Ubuntu碎碎念

    Ubuntu-图形界面和字符界面转换.指定默认启动界面1.按ALT+CTRL+F1切换到字符界面(Linux实体机) 如果是VMware虚拟机安装的Linux系统,则切换到字符界面的时候需要以下操作 ...

  8. 搭建git服务器遇到的问题

    1.错误提示: remote: error: insufficient permission for adding an object to repository database ./objects ...

  9. 了解JSON Web令牌(JWT)

    JSON Web Token(JWT)是目前最流行的跨域身份验证解决方案.今天给大家介绍JWT的原理和用法. 1.跨域身份验证 Internet服务无法与用户身份验证分开.一般过程如下. 1.用户向服 ...

  10. 用html标签+css写出旋转的正方体

    有一段时间没写代码了,刚写有点手生,无从下手,为了能快速进入状态,就写了这一个小东西,纯用标签和样式表写.下面看一下我写的. 这一段是样式表: <style> *{ margin: 0; ...