numpy最基础

2020年7月6日 / 6次阅读 / Last Modified 2020年7月6日
NumPy

numpy是事实上python生态里面用来科学计算最最重要的基础构建包,无数高级厉害的东西,都是建立在numpy基础上。

学习numpy,首先要搞清楚几个关键点:

  • numpy主要用于同类型多维数据的快速计算,其ndarray中的nd,就是N-dimensional的缩写;
  • numpy快,是因为它底层是C写的;
  • numpy表达式是向量化的(vectorization),即用一行更像数学公式的代码,来代替一大堆嵌套的for-loops(底层有for,只是我们看不到);
>>> import numpy as np
>>> np.version.version
'1.19.0'
>>>
>>> a = np.arange(20).reshape(4,5)
>>> a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])
>>> a.shape
(4, 5)
>>> a.ndim
2
>>> a.dtype
dtype('int32')
>>> a.dtype.name
'int32'
>>> a.size
20
>>> a.itemsize
4
>>> type(a)

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.

numpy使用axis这个词来代表维度,axis的复数是axes,我们一般说一个ndarray有多少axis,就是表示它是几维的数据。真正的数据本身,都是在最高维度内存放着的!而低维度的axis,存放的是其它axis而已。

-- EOF --

本文链接:https://www.pynote.net/archives/2153

留言区

电子邮件地址不会被公开。 必填项已用*标注


前一篇:
后一篇:

More


©Copyright 麦新杰 Since 2019 Python笔记

go to top