Array Creation

There are 5 general mechanisms of 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

import numpy as np

Creating array using the list

x = np.array([2,3,1,0])

print(x)

[2 3 1 0]

Creating array using the mixture of list and tuple

x = np.array([[1,2.0],[0,0],(1+1j,3.)])
print(x)

[[1.+0.j 2.+0.j]
 [0.+0.j 0.+0.j]
 [1.+1.j 3.+0.j]]

With the help of asarray function

x = np.asarray([[1,2.0],[0,0],(1+1j,3.)])
print(x)

[[1.+0.j 2.+0.j]
 [0.+0.j 0.+0.j]
 [1.+1.j 3.+0.j]]

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

import numpy as np

x = np.zeros((3, 3))
print(x)

[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]

x = np.ones((3,3))
print(x)

[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]

Usage of arange function with different parameters.

x = np.arange(10)
print(x)
##It will create an array from 2 to 10(as last value is excluded)
y = np.arange(2, 11, dtype=np.float)
print(y)
##It will create array from 2 to 2.9(3 will be excluded) as we here defined the slice of 0.1
z = np.arange(2, 3, 0.1)
print(z)
##It is similar to the previous example, difference is of only slicing value.
z1 = np.arange(2, 3, 0.2)
print(z1)

[0 1 2 3 4 5 6 7 8 9]
[ 2.  3.  4.  5.  6.  7.  8.  9. 10.]
[2.  2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9]
[2.  2.2 2.4 2.6 2.8]

linspace

""""It is almost similar to the last function arange we have discussed with the parameters but the only difference is, 
it doest not exlude the end value."""
x = np.linspace(1., 4., 5)
print(x)

[1.   1.75 2.5  3.25 4.  ]

Leave a Comment

Your email address will not be published. Required fields are marked *