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操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值