保姆级教学,Numpy库的使用-未完待续

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


官方网站介绍https://numpy.org/

1. 介绍

NumPy 通常与 SciPy(Scientific Python)和 Matplotlib(绘图库)一起使用, 这种组合广泛用于替代 MatLab,是一个强大的科学计算环境,有助于我们通过 Python 学习数据科学或者机器学习。
官方介绍

NumPy is the fundamental package for scientific computing in Python. It is a Python library that provides a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more.

numpy还可以很好的支持tensorflow和pytorch进行深度学习训练,具有广泛应用前景。

2.库的安装

conda安装

# Best practice, use an environment rather than install in the base env
conda create -n my-env
conda activate my-env
# If you want to install from conda-forge
conda config --env --add channels conda-forge
# The actual install command
conda install numpy

pip 安装

pip3 install numpy

3.库的导入

一般都会使用如下导入方式

import numpy as np

4.基本使用

NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of non-negative integers. In NumPy dimensions are called axes.
For example, the array for the coordinates of a point in 3D space, [1, 2, 1], has one axis. That axis has 3 elements in it, so we say it has a length of 3. In the example pictured below, the array has 2 axes. The first axis has a length of 2, the second axis has a length of 3.
NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of non-negative integers. In NumPy dimensions are called axes.
For example, the array for the coordinates of a point in 3D space, [1, 2, 1], has one axis. That axis has 3 elements in it, so we say it has a length of 3. In the example pictured below, the array has 2 axes. The first axis has a length of 2, the second axis has a length of 3.

NumPy 的主要对象是齐次多维数组。 它是一个元素表(通常是数字),所有类型相同,由非负整数元组索引。 在 NumPy 中,维度称为轴。
例如,3D 空间中某个点的坐标数组 [1, 2, 1] 有一个轴。 该轴有 3 个元素,所以我们说它的长度为 3。在下图的示例中,数组有 2 个轴。 第一个轴的长度为 2,第二个轴的长度为 3。
NumPy 的主要对象是齐次多维数组。 它是一个元素表(通常是数字),所有类型相同,由非负整数元组索引。 在 NumPy 中,维度称为轴。
例如,3D 空间中某个点的坐标数组 [1, 2, 1] 有一个轴。 该轴有 3 个元素,所以我们说它的长度为 3。在下图的示例中,数组有 2 个轴。 第一个轴的长度为 2,第二个轴的长度为 3。

ndarray.ndim
the number of axes (dimensions) of the array.

ndarray.ndim
数组的轴数(维度)。

ndarray.shape
the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix with n rows and m columns, shape will be (n,m). The length of the shape tuple is therefore the number of axes, ndim.

ndarray.shape
数组的维度。 这是一个整数元组,表示每个维度中数组的大小。 对于具有 n 行和 m 列的矩阵,形状将为 (n,m)。 因此,形状元组的长度就是轴数 ndim。

ndarray.size
the total number of elements of the array. This is equal to the product of the elements of shape.

ndarray.size
数组的元素总数。 这等于形状元素的乘积。

ndarray.dtype
an object describing the type of the elements in the array. One can create or specify dtype’s using standard Python types. Additionally NumPy provides types of its own. numpy.int32, numpy.int16, and numpy.float64 are some examples.

ndarray.itemsize
the size in bytes of each element of the array. For example, an array of elements of type float64 has itemsize 8 (=64/8), while one of type complex32 has itemsize 4 (=32/8). It is equivalent to ndarray.dtype.itemsize.

ndarray.itemsize
数组中每个元素的大小(以字节为单位)。 例如,float64 类型的元素数组的项目大小为 8 (=64/8),而 complex32 类型的一个元素的项目大小为 4 (=32/8)。 它相当于 ndarray.dtype.itemsize。

ndarray.data
the buffer containing the actual elements of the array. Normally, we won’t need to use this attribute because we will access the elements in an array using indexing facilities.

ndarray.data
包含数组实际元素的缓冲区。 通常,我们不需要使用此属性,因为我们将使用索引工具访问数组中的元素。

应用举例

import numpy as np
a = np.arange(15).reshape(3, 5)
a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
a.shape
(3, 5)
a.ndim
2
a.dtype.name
'int64'
a.itemsize
8
a.size
15
type(a)
<class 'numpy.ndarray'>
b = np.array([6, 7, 8])
b
array([6, 7, 8])
type(b)
<class 'numpy.ndarray'>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值