NumPy ndarray creation

原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/12244066.html

ndarray - A Multidimensional Array Object

One of the key features of NumPy is its N-dimensional array object, or ndarray, which is a fast, flexible container for large datasets in Python. Arrays enable you to perform mathematical operations on whole blocks of data using similar syntax to the equivalent operations between scalar elements.

e.g

An ndarray is a generic multidimensional container for homogeneous data; that is, all of the elements must be the same type.

Every array has a shape, a tuple indicating the size of each dimension, and a dtype, an object describing the data type of the array:

ndarray - attributes

Creating ndarrays

The easiest way to create an array is to use the array function. This accepts any sequence-like object (including other arrays) and produces a new NumPy array containing the passed data.

In addition to np.array, there are a number of other functions for creating new arrays. As examples, zeros and ones create arrays of 0s or 1s, respectively, with a given length or shape. empty creates an array without initializing its values to any particular value. To create a higher dimensional array with these methods, pass a tuple for the shape

Note: It’s not safe to assume that np.empty will return an array of all zeros. In some cases, it may return uninitialized “garbage” values.

arange is an array-valued version of the built-in Python range function:

arange 或 linspace 可以很方便地创建连续数组,起到的作用都是一样的,都是创建等差数组。

arange 类似Python内置函数 range(),通过指定初始值、终值、步长 来创建等差数列的一维数组,默认是不包括终值的

linspace 代表线性等分向量的含义,通过指定初始值、终值、元素个数 来创建等差数列的一维数组,默认是包括终值的

 

Array creation functions

See the table below for a short list of standard array creation functions. Since NumPy is focused on numerical computing, the data type, if not specified, will in many cases be float64 (floating point).

Reference

Python for Data Analysis Second Edition

https://www.runoob.com/python3/python3-func-range.html

原文地址:https://www.cnblogs.com/agilestyle/p/12244066.html