NumPy基础(1. 准备!)

基于NumPy 1.17.4的学习和练习
以下介绍、摘录、翻译来自官方学习手册NumPy User Guide 1.17.0

pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple


Prerequisites

  • Before reading this tutorial you should know a bit of Python. If you would like to refresh your memory, take a look at the Python tutorial.
  • If you wish to work the examples in this tutorial, you must also have some software installed on your computer. Please see
    https://scipy.org/install.html for instructions.
  • 学习NumPy需要有基本的Python基础,当然我已经复习了一下Python,笔记在这:Python学习总结

What is NumPy?

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 对 Python的一些重要增强

  1. NumPy提供了数组类型而Python中没有数组类型(Array与List的区别是Array长度固定而List长度可以通过追加元素不断增加)。
  2. NumPy提供的Array中元素类型必须统一(Python中List存放元素为动态类型),这样保证了元素占用相同的内存大小(可以理解为在内存寻址的时候因为固定地址长度而提高执行效率)。
  3. NumPy提供一些其他数据类型(如:int64, int32 ,float64 ,float32 …),用法和Python内建的数据类型差不多。

The Basics

NumPy’s array class is called ndarray. It is also known by the
alias array. Note that numpy.array is not the same as the Standard
Python Library class array.array, which only handles one-dimensional
arrays and offers less functionality. The more important attributes of
an ndarray object are:

NumPy的核心是提供名为ndarray的对象其实就是array,列举常用属性:

  • ndarray.ndim the number of axes (dimensions) of the array.
  • 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.size the total number of elements of the array. This is equal to the product of the elements of shape.
  • 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.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.

Array types and conversions between types

NumPy 支持非常多类型

The primitive types supported are tied closely to those in C:

NumPy 是C写的,所以数据类型的性质可参照C type:

Numpy typeC typeDescription
np.boolbool Boolean(True or False) stored as a byte
np.bytesigned charPlatform-defined
np.ubyteunsigned charPlatform-defined
np.shortshortPlatform-defined
np.ushortunsigned shortPlatform-defined
np.intcintPlatform-defined
np.uintcunsigned intPlatform-defined
np.int_longPlatform-defined
np.uintunsigned longPlatform-defined
np.longlonglong longPlatform-defined
np.ulonglongunsigned long longPlatform-defined
np.half/np.float16Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
np.singlefloatPlatform-defined single precision float: typically sign bit, 8 bits exponent,23 bits mantissa
np.doubledoublePlatform-defined double precision float: typically sign bit, 11 bits exponent,52 bits mantissa.
np.longdoublelong doublePlatform-defined extended-precision float
np.csinglefloat complexComplex number, represented by two single-precision floats (real and imaginary components)
np.cdoubledouble complexComplex number, represented by two double-precision floats (real and imaginary components).
np.clongdoublelong double complexComplex number, represented by two extended-precision floats (real and imaginary components).

Since many of these have platform-dependent definitions, a set of
fixed-size aliases are provided:

以下主要是为了见名知大小:

Numpy typeC typeDescription
np.int8int8_tByte (-128 to 127)
np.int16int16_tInteger (-32768 to 32767)
np.int32int32_tInteger (-2147483648 to 2147483647)
np.int64int64_tInteger (-9223372036854775808 to 9223372036854775807)
np.uint8uint8_tUnsigned integer (0 to 255)
np.uint16uint16_tUnsigned integer (0 to 65535)
np.uint32uint32_tUnsigned integer (0 to 4294967295)
np.uint64uint64_tUnsigned integer (0 to 18446744073709551615)
np.intpintptr_tInteger used for indexing, typically the same as ssize_t
np.uintpuintptr_tInteger large enough to hold a pointer
np.float32float
np.float64/ np.float_doubleNote that this matches the precision of the builtin python float.
np.complex64float complexComplex number, represented by two 32-bit floats (real and imaginarycomponents)
np.complex128 / np.complex_double complexNote that this matches the precision of the builtin python complex.

开始动手练习吧

安装NumPy:

pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple

继续看我撸NumPy User Guide?点这里:
NumPy基础 ndarray操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
numpy 1.19.2 是一个Python库,旨在提供用于科学计算的高性能多维数组对象和工具。它提供了广泛的数学函数和操作符,并具有强大的N维数组对象。numpy 1.19.2 在数据处理和分析中非常有用,尤其是在处理大规模数值计算和数据集时。它还提供了高效存储和操作大型数据集的方式,可以与其他Python库和工具无缝集成。 与此同时,scikit-learn 是一个用于机器学习的Python库,它构建在numpy和其他相关库的基础上。scikit-learn 提供了各种机器学习算法和工具,包括分类、回归、聚类、降维等。它还提供了许多预处理和特征选择方法,以帮助用户准备输入数据。scikit-learn 提供了易于使用的API和丰富的文档,使得使用机器学习算法变得简单而直观。 numpy 1.19.2 和scikit-learn 具有良好的兼容性。在scikit-learn 的内部实现中,它广泛使用了numpy 数组,因为numpy 数组提供了高效的数据存储和操作方法。numpy 的各种操作可以用于预处理数据,特征选择和数据转换等步骤。此外,numpy 数组还可以方便地与scikit-learn 的模型进行交互,对于训练和预测过程都非常有用。 综上所述,numpy 1.19.2 是scikit-learn 的一个必要组成部分,它为scikit-learn 提供了高效的数组操作和处理工具。numpy 的功能和性能使得scikit-learn 在机器学习任务中更加强大和易用。通过结合使用这两个库,可以更好地处理和分析数据,构建高效的机器学习模型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值