美文网首页我爱编程
NumPy Basics(NumPy基础)

NumPy Basics(NumPy基础)

作者: zhanglf | 来源:发表于2017-12-22 21:30 被阅读0次

The Basics


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 positive integers. In NumPy dimensions are called axes(轴). The number of axes is rank.

For example, the coordinates(坐标) of a point in 3D space [1, 2, 1] is an array of rank 1, because it has one axis. That axis has a length of 3. In the example pictured below, the array has rank 2 (it is 2-dimensional). The first dimension (axis) has a length of 2, the second dimension has a length of 3.

[

[1,2,3],

[4,5,6]

]

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:

ndarray.ndim(维度):

the number of axes (dimensions) of the array. In the Python world, the number of dimensions is referred to as rank.

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 rows and columns, shape will be(n,m). The length of the shape tuple is therefore the rank, or number of dimensions, 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.

An example:

>>>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)

>>> b = np.array([6,7,8])

>>> b

array([6,7,8])

>>>type(b)

相关文章

  • NumPy Basics(NumPy基础)

    The Basics NumPy’s main object is the homogeneous(同类型的) m...

  • NumPy

    # NumPy ## The Basics NumPy 主要面向的对象是齐次多维数组。在NumPy中维度(dime...

  • 2018-03-13

    Python Basics with Numpy (optional assignment) Welcome to...

  • Python Basics With Numpy v2

    Python Basics with Numpy (optional assignment) Welcome to...

  • Numpy Basics

    转载:https://morvanzhou.github.io/tutorials/data-manipulati...

  • numpy模块

    The Basics NumPy’s main object is the homogeneous multidi...

  • Numpy入门

    1、熟悉 numpy 的基础属性 2、numpy 创建 array 3、numpy的基础运算 4、numpy索引 ...

  • 吴恩达编程作业及答案(一)

    Python Basics with Numpy Hello World Exercise: Set test t...

  • numpy 基础

    numpy 基础 导入numpy 版本 np常用方法 numpy.array 的基本属性 numpy.array ...

  • Python Basics with Numpy

    1. Building basic functions with numpy 1.1 - sigmoid func...

网友评论

    本文标题:NumPy Basics(NumPy基础)

    本文链接:https://www.haomeiwen.com/subject/zmbxgxtx.html