当前位置:脚本大全 > > 正文

numpy如何创建数组(NumPy 数组使用大全)

时间:2021-10-13 00:54:57类别:脚本大全

numpy如何创建数组

NumPy 数组使用大全

numpy 是一个python 库,用于 python 编程中的科学计算。在本教程中,你将学习如何在 numpy 数组上以多种方式添加、删除、排序和操作元素。

numpy 提供了一个多维数组对象和其他派生数组,例如掩码数组和掩码多维数组。

为什么要用 numpy

numpy 提供了一个 ndarray 对象,可以使用它来对任何维度的数组进行操作。 ndarray 代表 n 维数组,其中 n 是任意数字。这意味着 numpy 数组可以是任何维度的。

与 python 的 list 相比,numpy 具有许多优势。我们可以在 numpy 阵列上执行高性能操作,例如:

  1. 对数组成员进行排序
  2. 数学和逻辑运算
  3. 输入/输出功能
  4. 统计和线性代数运算

安装 numpy

要安装numpy,你的电脑上要先有 python 和 pip。

在终端中运行以下命令:

  • ?
  • 1
  • pip install numpy
  • 然后你就可以在脚本中导入 numpy 了,如下所示:

  • ?
  • 1
  • import numpy
  • 添加数组元素

    可以用 numpy 模块的 append() 方法向 numpy 数组中添加元素。

    append() 的语法如下:

  • ?
  • 1
  • numpy.append(array, value, axis)
  • value 会被追加到在数组的末尾,并返回一个包含所有元素的 ndarray。

    参数 axis 是一个可选的整数,用于定义数组的显示方式。如果没有指定,则数组结构将展平,稍后会演示用法。

    以下示例,其中首先声明数组,然后用 append 方法向数组添加更多的值:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • import numpy
  • a = numpy.array([1, 2, 3])
  • newarray = numpy.append (a, [10, 11, 12])
  • print(newarray)
  • # 输出:[ 1 2 3 10 11 12]
  • 添加一列

    也可以用numpy 的 append() 方法插入一列。

    在下面的例子中,我们创建了一个二维数组并插入了两列:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • import numpy
  •  
  • a = numpy.array([[1, 2, 3], [4, 5, 6]])
  • b = numpy.array([[400], [800]])
  • newarray = numpy.append(a, b, axis = 1)
  • print(newarray)
  •  
  • """
  • 输出:
  • [[ 1  2  3 400]
  •  [ 4  5  6 800]]
  • """
  • 如果没有使用 axis 参数,则会输出:

    [ 1 2 3 4 5 6 400 800]

    这就是数组结构的扁平化。

    在 numpy 中,还可以用 insert() 方法插入元素或列。 两者之间的区别在于 insert() 方法可以指定要在哪个索引处添加元素,但 append() 方法会在数组的末尾添加一个值。

    consider the example below:

    考虑以下示例:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • import numpy
  • a = numpy.array([1, 2, 3])
  • newarray = numpy.insert(a, 1, 90)
  • print(newarray)
  • # 输出:[ 1 90 2 3]
  • 这里 insert() 方法在索引1处添加元素。在python中数组索引从0开始。

    追加一行

    也可以用 append() 方法向数组添加行,就像在数组中附加元素一样简单:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • import numpy
  • a = numpy.array([[1, 2, 3], [4, 5, 6]])
  • newarray = numpy.append(a, [[50, 60, 70]], axis = 0)
  • print(newarray)
  • """
  • 输出“
  • [[ 1 2 3]
  •  [ 4 5 6]
  •  [50 60 70]]
  • """
  • 删除元素

    可以用 numpy 模块的 delete() 方法删除 numpy 数组元素:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • import numpy
  • a = numpy.array([1, 2, 3])
  • newarray = numpy.delete(a, 1, axis = 0)
  • print(newarray)
  • # 输出:[1 3]
  • 在本例子中,我们有一个一维数组,用 delete() 方法从数组中删除了索引 1 处的元素。

    删除一行

    同样,你也可以用 delete() 方法删除行。

    下面的例子中我们从二维数组中删除了一行:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • import numpy
  • a = numpy.array([[1, 2, 3], [4, 5, 6], [10, 20, 30]])
  • newarray = numpy.delete(a, 1, axis = 0)
  • print(newarray)
  • """
  • 输出:
  • [[ 1 2 3]
  •  [10 20 30]]
  • """
  • 在 delete() 方法中,首先给出数组,然后给出要删除的元素的索引。在上例中,我们删除了索引为 1 的元素。

    检查 numpy 数组是否为空

    可以用 size 方法返回数组中元素的总数。

    在下面的例子中有一个 if 语句,通过 ndarray.size 检查数组中是否有元素,其中 ndarray 可以是任何给定的 numpy 数组:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • import numpy
  •  
  • a = numpy.array([1, 2, 3])
  • if(a.size == 0):
  •   print("the given array is empty")
  • else:
  •   print("the array = ", a)
  • # 输出:the array = [1 2 3]
  • 在上面的代码中,数组中有三个元素,因此它不是空的,判断条件将返回false。如果数组中没有元素,则 if 条件会变为 true 并且将打印空消息。如果数组等于:

  • ?
  • 1
  • a = numpy.array([])
  • 上述代码将会输出:

    the given array is empty

    查找值的索引

    要查找值对应的索引,可以用 numpy 模块的 where() 方法,如下例所示:

  • ?
  • 1
  • 2
  • 3
  • 4
  • import numpy
  • a = numpy.array([1, 2, 3, 4, 5])
  • print("5 is found at index: ", numpy.where(a == 5))
  • # 输出:5 is found at index: (array([4]),)
  • 如果你只想得到索引,可以这样写:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • import numpy
  •  
  • a = numpy.array([1, 2, 3, 4, 5])
  • index = numpy.where(a == 5)
  • print("5 is found at index: ", index[0])
  • #输出: 5 is found at index: [4]
  • numpy 数组切片

    数组切片是从给定数组中提取子集的过程。你可以用冒号( : )运算符对数组进行切片,并指定数组索引的开始和结束位置,例如:

  • ?
  • 1
  • array[from:to]
  • 下面的例子中提取从索引 2 到索引 5 的元素:

  • ?
  • 1
  • 2
  • 3
  • 4
  • import numpy
  • a = numpy.array([1, 2, 3, 4, 5, 6, 7, 8])
  • print("a subset of array a = ", a[2:5])
  • # 输出:a subset of array a = [3 4 5]
  • 如果想要提取最后三个元素,可以通过用负切片来完成操作,如下所示:

  • ?
  • 1
  • 2
  • 3
  • 4
  • import numpy
  • a = numpy.array([1, 2, 3, 4, 5, 6, 7, 8])
  • print("a subset of array a = ", a[-3:])
  • # 输出:a subset of array a = [6 7 8]
  • 将函数作用于所有数组元素

    在下面的例子中,我们将创建一个 lambda 函数,并传入一个数组,以其应用于所有元素:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • import numpy
  • addition = lambda x: x + 2
  • a = numpy.array([1, 2, 3, 4, 5, 6])
  • print("array after addition function: ", addition(a))
  • # 输出:array after addition function: [3 4 5 6 7 8]
  • 在此例中,创建了一个 lambda 函数,它使每个元素都递增 2。

    numpy 数组的长度

    要得到 numpy 数组的长度,可以用 size 属性,如下所示:

  • ?
  • 1
  • 2
  • 3
  • 4
  • import numpy
  • a = numpy.array([1, 2, 3, 4, 5, 6])
  • print("the size of array = ", a.size)
  • # 输出:the size of array = 6
  • 从 list 创建 numpy 数组

    假设你有一个列表:

    l = [1, 2, 3, 4, 5]

    现在要根据这个列表创建一个数组,可以用 numpy 模块的 array() 方法:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • import numpy
  • l = [1, 2, 3, 4, 5]
  • a = numpy.array(l)
  • print("the numpy array from python list = ", a)
  • # 输出:the numpy array from python list = [1 2 3 4 5]
  • 同样,使用 array() 方法,也可以从元组创建 numpy 数组。如下所示:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • import numpy
  • t = (1, 2, 3, 4, 5)
  • a = numpy.array(t)
  • print("the numpy array from python tuple = ", a)
  • # 输出:the numpy array from python tuple = [1 2 3 4 5]
  • 将 numpy 数组转换为 list

    要将数组转换为list,可以使用 numpy 模块的 tolist()方法。

  • ?
  • 1
  • 2
  • 3
  • 4
  • import numpy
  • a = numpy.array([1, 2, 3, 4, 5])
  • print("array to list = ", a.tolist())
  • # 输出:array to list = [1, 2, 3, 4, 5]
  • 在这段代码中,我们简单地调用了 tolist() 方法,该方法将数组转换为列表。然后将新创建的列表打印到输出屏幕。

    把 numpy 数组导出为 csv

    要将数组导出为 csv 文件,可以用 numpy 模块的 savetxt() 方法,如下所示:

  • ?
  • 1
  • 2
  • 3
  • import numpy
  • a = numpy.array([1, 2, 3, 4, 5])
  • numpy.savetxt("myarray.csv", a)
  • 此代码将在 python 代码文件所在路径下生成 csv 文件。当然你也可以指定路径。

    该文件的内容如下:

    1.000000000000000000e+00
    2.000000000000000000e+00
    3.000000000000000000e+00
    4.000000000000000000e+00
    5.000000000000000000e+00

    你可以把额外填充的零删除,如下所示:

  • ?
  • 1
  • numpy.savetxt("myarray.csv", a,fmt='%.2f')
  • 对 numpy 数组排序

    可以用 numpy 模块的 sort() 方法对 numpy 数组进行排序:

    sort() 函数有一个可选参数 axis(整数),默认为 -1。axis 指定我们要对数组进行排序的轴。 -1 表示将根据最后一个轴对数组进行排序。

  • ?
  • 1
  • 2
  • 3
  • 4
  • import numpy
  • a = numpy.array([16, 3, 2, 6, 8, 10, 1])
  • print("sorted array = ", numpy.sort(a))
  • # 输出:sorted array = [ 1 2 3 6 8 10 16]
  • 在这个例子中,我们在 print 语句中调用了 sort() 方法。数组 a 被传递给 sort 函数。

    归一化数组

    归一化数组是指将数组的值置于某个定义范围的过程。例如,我们想要在 -1 和 1 之间对数组进行归一化,依此类推。

    归一化的公式如下:

    x = (x – xmin) / (xmax – xmin)

    现在把这个公式用于我们的数组。要查找数组中的最大和最小项,可以分别用 numpy 的 max() 和 min() 方法。

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • import numpy
  • x= numpy.array([400, 800, 200, 700, 1000, 2000, 300])
  • xmax = x.max()
  • xmin = x.min()
  • x = (x - xmin)/(xmax - xmin)
  • print("after normalization array x = \n", x)
  • """
  • 输出:
  • after normalization array x =
  •  [0.11111111 0.33333333 0.     0.27777778 0.44444444 1.
  •  0.05555556]
  • """
  • 数组索引

    索引指向数组中的一个元素。在下面的例子中,分别用到了一维和二维数组中的索引:

  • ?
  • 1
  • 2
  • 3
  • 4
  • import numpy
  • a = numpy.array([20, 13, 42, 86, 81, 9, 11])
  • print("element at index 3 = ", a[3])
  • # 输出:element at index 3 = 86
  • 下面是二维数组:

  • ?
  • 1
  • 2
  • 3
  • 4
  • import numpy
  • a = numpy.array([[20, 13, 42], [86, 81, 9]])
  • print("element at index a[1][2] = ", a[1][2])
  • # 输出:element at index a[1][2] = 9
  • 索引 [1][2] 表示第二行和第三列(索引从 0 开始)。因此在屏幕上输出 9 。

    将 numpy 数组附加到另​一个数组上

    可以用 append() 方法将 numpy 数组附加到另​​一个 numpy 数组上。

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • import numpy
  • a = numpy.array([1, 2, 3, 4, 5])
  • b = numpy.array([10, 20, 30, 40, 50])
  • newarray = numpy.append(a, b)
  • print("the new array = ", newarray)
  • # 输出:the new array = [ 1 2 3 4 5 10 20 30 40 50]
  • 在此例中,创建两个 numpy 数组 a, b 。然后把两个数组传给 append()。当数组 b 作为第二个参数传递时,将被添加到数组 a 的末尾。

    总结

    正如大家所见,numpy 数组用起来非常简单。在使用很多机器学习库时,numpy 数组非常重要。可以说numpy 是人工智能的大门。

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。

    原文链接:https://segmentfault.com/a/1190000018975446

    标签:
    上一篇下一篇

    猜您喜欢

    热门推荐