Python-Numpy超级详细讲解,数据分析,机器学习,强化学习,NLP,CV必看

59 篇文章 4 订阅
46 篇文章 3 订阅

1.为什么要用numpy

NumPy gives you an enormous range of fast and efficient ways of creating arrays and manipulating numerical data inside them. While a Python list can contain different data types within a single list, all of the elements in a NumPy array should be homogeneous. The mathematical operations that are meant to be performed on arrays would be extremely inefficient if the arrays weren’t homogeneous.

Why use NumPy?
NumPy arrays are faster and more compact than Python lists. An array consumes less memory and is convenient to use. NumPy uses much less memory to store data and it provides a mechanism of specifying the data types. This allows the code to be optimized even further.

NumPy 为您提供了大量快速有效的方法来创建数组和操作其中的数值数据。 虽然 Python 列表可以在单个列表中包含不同的数据类型,但 NumPy 数组中的所有元素都应该是同质的。 如果数组不是同质的,则打算在数组上执行的数学运算将非常低效。

为什么要使用 NumPy?

NumPy 数组比 Python 列表更快、更紧凑。 数组占用内存少,使用方便。 NumPy 使用更少的内存来存储数据,它提供了一种指定数据类型的机制。 这允许进一步优化代码。

2.什么是array

An array is a central data structure of the NumPy library. An array is a grid of values and it contains information about the raw data, how to locate an element, and how to interpret an element. It has a grid of elements that can be indexed in various ways. The elements are all of the same type, referred to as the array dtype.

An array can be indexed by a tuple of nonnegative integers, by booleans, by another array, or by integers. The rank of the array is the number of dimensions. The shape of the array is a tuple of integers giving the size of the array along each dimension.

One way we can initialize NumPy arrays is from Python lists, using nested lists for two- or higher-dimensional data.

数组是 NumPy 库的核心数据结构。 数组是值的网格,它包含有关原始数据、如何定位元素以及如何解释元素的信息。 它有一个可以以各种方式索引的元素网格。 元素都是相同的类型,称为数组 dtype。

数组可以通过非负整数元组、布尔值、另一个数组或整数来索引。 数组的秩是维数。 数组的形状是一个整数元组,给出了数组沿每个维度的大小。

我们可以初始化 NumPy 数组的一种方法是从 Python 列表中,对二维或更高维数据使用嵌套列表。
例如:

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

3.NumPy 数据类型

numpy 支持的数据类型比 Python 内置的类型要多很多,基本上可以和 C 语言的数据类型对应上,其中部分类型对应为 Python 内置的类型。下表列举了常用 NumPy 基本类型。
详细内容可以参考:
https://www.runoob.com/numpy/numpy-dtype.html

需要使用dtype方法:
数据类型对象(numpy.dtype 类的实例)用来描述与数组对应的内存区域是如何使用,它描述了数据的以下几个方面::

数据的类型(整数,浮点数或者 Python 对象)
数据的大小(例如, 整数使用多少个字节存储)
数据的字节顺序(小端法或大端法)
在结构化类型的情况下,字段的名称、每个字段的数据类型和每个字段所取的内存块的部分
如果数据类型是子数组,那么它的形状和数据类型是什么。
字节顺序是通过对数据类型预先设定 < 或 > 来决定的。 < 意味着小端法(最小值存储在最小的地址,即低位组放在最前面)。> 意味着大端法(最重要的字节存储在最小的地址,即高位组放在最前面)。

dtype 对象是使用以下语法构造的:

numpy.dtype(object, align, copy)

4.创建基本array

This section covers np.array(), np.zeros(), np.ones(), np.empty(), np.arange(), np.linspace(), dtype

To create a NumPy array, you can use the function np.array().
All you need to do to create a simple array is pass a list to it. If you choose to, you can also specify the type of data in your list. You can find more information about data types here.

要创建 NumPy 数组,可以使用函数 np.array()。

创建一个简单数组所需要做的就是将一个列表传递给它。 如果您选择,您还可以指定列表中的数据类型。 您可以在此处找到有关数据类型的更多信息。

import numpy as np
a = np.array([1, 2, 3])

Be aware that these visualizations are meant to simplify ideas and give you a basic understanding of NumPy concepts and mechanics. Arrays and array operations are much more complicated than are captured here!
Besides creating an array from a sequence of elements, you can easily create an array filled with 0’s:

请注意,这些可视化旨在简化想法并让您对 NumPy 概念和机制有基本的了解。 数组和数组操作比这里捕获的要复杂得多!

除了从一系列元素创建一个数组之外,您还可以轻松地创建一个用 0 填充的数组:

np.zeros(2)
array([0., 0.])

Or an array filled with 1’s:

np.ones(2)
array([1., 1.])

Or even an empty array! The function empty creates an array whose initial content is random and depends on the state of the memory. The reason to use empty over zeros (or something similar) is speed - just make sure to fill every element afterwards!

# Create an empty array with 2 elements
np.empty(2)
array([ 3.14, 42.  ])  # may vary

5.Adding, removing, and sorting elements方法

This section covers np.sort(), np.concatenate()

Sorting an element is simple with np.sort(). You can specify the axis, kind, and order when you call the function.

arr = np.array([2, 1, 5, 3, 7, 4, 6, 8])
np.sort(arr)
array([1, 2, 3, 4, 5, 6, 7, 8])

In addition to sort, which returns a sorted copy of an array, you can use:

  • argsort, which is an indirect sort along a specified axis,
  • lexsort, which is an indirect stable sort on multiple keys,
  • searchsorted, which will find elements in a sorted array, and
  • partition, which is a partial sort. To read more about sorting an array, see: sort.

除了返回数组的排序副本的 sort 之外,您还可以使用:

  • argsort,它是沿指定轴的间接排序,
  • lexsort,这是对多个键的间接稳定排序,
  • searchsorted,它将在排序数组中查找元素,以及
  • 分区,这是一种部分排序。
  • 要了解有关对数组进行排序的更多信息,请参阅:sort。

6. shape and size方法

ndarray.ndim will tell you the number of axes, or dimensions, of the array.
ndarray.size will tell you the total number of elements of the array. This is the product of the elements of the array’s shape.
ndarray.shape will display a tuple of integers that indicate the number of elements stored along each dimension of the array. If, for example, you have a 2-D array with 2 rows and 3 columns, the shape of your array is (2, 3).

ndarray.ndim 将告诉您数组的轴数或维度数。

ndarray.size 会告诉你数组元素的总数。 这是数组形状元素的乘积。

ndarray.shape 将显示一个整数元组,指示沿数组每个维度存储的元素数。 例如,如果您有一个 2 行 3 列的二维数组,则数组的形状为 (2, 3)。

array_example = np.array([[[0, 1, 2, 3],
                           [4, 5, 6, 7]],

                          [[0, 1, 2, 3],
                           [4, 5, 6, 7]],

                          [[0 ,1 ,2, 3],
                           [4, 5, 6, 7]]])
array_example.ndim
3
array_example.size
24
array_example.shape
(3, 2, 4)

7.reshape 方法

Using arr.reshape() will give a new shape to an array without changing the data. Just remember that when you use the reshape method, the array you want to produce needs to have the same number of elements as the original array. If you start with an array with 12 elements, you’ll need to make sure that your new array also has a total of 12 elements.

使用 arr.reshape() 将在不更改数据的情况下为数组提供新形状。 请记住,当您使用 reshape 方法时,您要生成的数组需要与原始数组具有相同数量的元素。 如果你从一个有 12 个元素的数组开始,你需要确保你的新数组也有总共 12 个元素。

a = np.arange(6)
print(a)
[0 1 2 3 4 5]
b = a.reshape(3, 2)
print(b)
[[0 1]
 [2 3]
 [4 5]]
np.reshape(a, newshape=(1, 6), order='C')
array([[0, 1, 2, 3, 4, 5]])
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值