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 type C type Description
np.bool bool Boolean (True or False) stored as a byte
np.byte signed char Platform-defined
np.ubyte unsigned char Platform-defined
np.short short Platform-defined
np.ushort unsigned short Platform-defined
np.intc int Platform-defined
np.uintc unsigned int Platform-defined
np.int_ long Platform-defined
np.uint unsigned long Platform-defined
np.longlong long long Platform-defined
np.ulonglong unsigned long long Platform-defined
np.half/ np.float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
np.single float Platform-defined single precision float: typically sign bit, 8 bits exponent,23 bits mantissa
np.double double Platform-defined double precision float: typically sign bit, 11 bits exponent,52 bits mantissa.
np.longdouble long double Platform-defined extended-precision float
np.csingle float complex Complex number, represented by two single-precision floats (real and imaginary components)
np.cdouble double complex Complex number, represented by two double-precision floats (real and imaginary components).
np.clongdouble long double complex Complex 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 type C type Description
np.int8 int8_t Byte (-128 to 127)
np.int16 int16_t Integer (-32768 to 32767)
np.int32 int32_t Integer (-2147483648 to 2147483647)
np.int64 int64_t Integer (-9223372036854775808 to 9223372036854775807)
np.uint8 uint8_t Unsigned integer (0 to 255)
np.uint16 uint16_t Unsigned integer (0 to 65535)
np.uint32 uint32_t Unsigned integer (0 to 4294967295)
np.uint64 uint64_t Unsigned integer (0 to 18446744073709551615)
np.intp intptr_t Integer used for indexing, typically the same as ssize_t
np.uintp uintptr_t Integer large enough to hold a pointer
np.float32 float
np.float64/ np.float_ double Note that this matches the precision of the builtin python float.
np.complex64 float complex Complex number, represented by two 32-bit floats (real and imaginarycomponents)
np.complex128 / np.complex_ double complex Note 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操作

发布了45 篇原创文章 · 获赞 2 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_22096121/article/details/103386189