Sunday, February 24, 2019

NumPy library -2 (functions that generate ndarrays)


The NumPy library provides a set of functions that generate ndarrays with initial content, created with different values depending on the function. We have already seen some of these functions in the previous post which allows a single line of code to generate large amounts of data.

1. The zeros() function

This function creates a full array of zeros with dimensions defined by the shape argument. Let's create a two-dimensional array 3x3 using the zeros() function:

import numpy as np

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


The output shows the created two-dimensional array 3x3 with all zeros:

[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
------------------
(program exited with code: 0)

Press any key to continue . . .


The created array is with the float64 data type.

2. The ones() function

This function creates a full array of ones with dimensions defined by the shape argument. Let's create a two-dimensional array 3x3 using the ones() function:

import numpy as np

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


The output shows the created two-dimensional array 3x3 with all ones:

[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
------------------
(program exited with code: 0)

Press any key to continue . . .


The created array is with the float64 data type.

3. The arange() function

This function generates NumPy arrays with numerical sequences that respond to particular rules depending on the passed arguments. For example, if you want to generate a sequence of values between 0 and 10, you will be passed only one argument to the function, that is the value with which you want to end the sequence. See the following code:

import numpy as np

arr = np.arange(0, 10)
print(arr)


The output shows the created array:

[0 1 2 3 4 5 6 7 8 9]

------------------
(program exited with code: 0)

Press any key to continue . . .

In case if we wish to start from any other number than 0 then we can modify the first argument of the arange() as shown below:

import numpy as np

arr = np.arange(5, 10)
print(arr)


The output shows the created array:

[ 5 6 7 8 9]



------------------
(program exited with code: 0)

Press any key to continue . . . 


We can also specify a third argument the range function which represent the gap between one value and the next one in the sequence of values. Thus it is possible to generate a sequence of values with precise intervals between them. The following program creates an array of numbers in range 0 to 20 with an interval of 4:

import numpy as np

arr = np.arange(0, 20, 4)


print(arr)


The output shows the created array:


[ 0  4  8 12 16]

------------------
(program exited with code: 0)

Press any key to continue . . .


The third argument can also be a float type as shown in the following program:

import numpy as np

arr = np.arange(0, 2, 0.4)


print(arr)


The output shows the created array:

[0.  0.4 0.8 1.2 1.6]

------------------
(program exited with code: 0)

Press any key to continue . . .


The output shows the created arrays with arange() function are by default one dimensional arrays. To generate two-dimensional arrays using the arange() function, combine it with the reshape() function. This function divides a linear array in different parts in the manner specified by the shape argument. See the example below:

import numpy as np

arr = np.arange(0, 12).reshape(3, 4)
print(arr)

The output shows the created array:

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
------------------
(program exited with code: 0)

Press any key to continue . . .


4. The linspace() function

This function takes as its first two arguments the initial and end values of the sequence, the third argument defines the number of elements into which we want the interval to be split.

import numpy as np

arr = np.linspace(0,10,5)
print(arr)


The output shows the created array:

[ 0.   2.5  5.   7.5 10. ]
------------------
(program exited with code: 0)

Press any key to continue . . . 


5. The linspace() function

This function will generate an array with many elements as specified in the argument. The obtained arrays contains random values. See the following program:

import numpy as np

arr = np.random.random(5)
print(arr)


The output shows the created array:

[0.09067762 0.15551911 0.23330312 0.01154011 0.87073787]

------------------
(program exited with code: 0)

Press any key to continue . . .

Here I am ending today's post. In the next post we shall further explore NumPy library and see how to apply various operations to them . Till we meet next keep practicing and learning Python as Python is easy to learn!

Share:

0 comments:

Post a Comment