Array Creation

Array Creation

There are 5 general mechanisms for creating Numpy arrays are:
1. Conversion from other Python structures (e.g. lists, tuples).
2. Intrinsic Numpy array creation objects (e.g. arange, ones, zeros, etc).
3. Reading arrays from disk, either from standard or custom formats.
4. Creating arrays from raw bytes through the use of strings or buffers.
5. Use of special library functions (e.g. random).

We are going to discuss 2 of them in detail which generally comes in use.

A.) Converting Python array_like Objects to Numpy Arrays:  

So, in this what we do is simply take the existing python data structures like list, tuples and will convert them to Numpy array using array function of Numpy like np.array(<list>).

How to create an array using existing data structure of python such as list, tuple, asrray method

B.) Intrinsic Numpy array creation objects:  

Numpy has built-in functions to create an array from scratch.

1. zeros(shape): It is a function which will create an array filled with 0 values with the specified shape. The default dtype is float64.

2. ones(shape): It is a function which will create an array filled with 1’s with the specified shape. Rest of the properties are same as of zeros(shape).

3. arange(): It is a function which will create an array by regularly incrementing values. We will discuss different parameters of function below with the help of an example and for the detailed information, you can refer to the documentation of Numpy.

4. linspace(): It is a function which will generally take 3 parameters (beginning value, end value, space between each value). It will be more clear to you through an example.

How to create an array from the scratch

Usage of arange function with different parameters.


Close Menu