Despite its simplistic name, Numpy as we have seen is a powerful python package that is mainly for working with arrays and matrices. And before working with Numpy, you have to know how to create arrays. They are several ways in creating arrays in a Numpy python module.
Let’s start by creating a NumPy ndarray Object.
We used the array function to create ndarray object (array functions is group of numbers of the same type for example a list)
Example
import numpy as nparray = np.array([2,4,6,8,10])print(array)print(type(array))
The code above creates even numbers using Numpy while the type functions tells Numpy the type of code being passed to it, after which the code is run.
OUTPUT
Now we are going to see how to create Numpy arrays using Numpy functions.
Just like a normal python function, Numpy has built-in functions for creating arrays. We are going to see some of them in this article:
Creating one dimensional array
A one dimensional array is also known as array of rank 1. We used the arrange function to create arrays.
import numpy as nparray = np.arange(10)print(array)
We want to create an array of the number10. The value is passed on to the arrange function. Using Numpy, the array function creates a range of values from 0 to 9.
OUTPUT
A Two Dimensional Array.
We are going to create this type of array by using the reshape function. The reshape function changes its arrangement without altering its data.
Example
import numpy as nparray = np.arange(20).reshape(4,5)print(array)
The array created is converted to a 2-dimension array with 4 rows and 5 columns using the reshape the function.
OUTPUT
Creating three dimension
To create a three dimension, we specify three parameters
Example
import numpy as nparray = np.arange(27).reshape(3,3,3)print(array)
OUTPUT
Conversion of python data types.
We can also create Numpy arrays from data types such as list. Here we are going to use the array function. Below is syntax to use when creating arrays with a list
import numpy as nparray = np.array(list)print(array)
Example
import numpy as nparray = np.array((1, 2, 3))print(array)
OUTPUT
We can also use a list to create dimensional arrays.
For example: creating a 2 dimensional array.
import numpy as nparray = np.array([[1, 2, 3], [10, 8, 6]])print(array)
OUTPUT
Knowing the number of dimensions
We can use the attribute ndim to know the number of dimensions in a code. This attribute is found in the Numpy function.
Example
b = np.array([1, 2, 3,])c = np.array([[1, 2, 3], [10, 8, 6]])d = np.array([[[1, 2, 3], [10, 8, 6]], [[1, 2, 3], [10, 8, 6]]])print(b.ndim)print(c.ndim)print(d.ndim)
OUTPUT
Higher dimensional arrays can be created by using ndim argument. This argument is assigned to the code. It tells the code the number of dimensions of arrays to create.
Example
import numpy as nparr = np.array([1, 2, 3,], ndmin=10)print(arr)print('number of dimensions :', arr.ndim)
OUTPUT
Creating Numpy arrays is very important in performing numeric array computations. With these tools, you are now ready to create basic array operations.
We have come to the end of this session guys. Have nice coding guys.









Comments
Post a Comment
Please do not enter any spam link in the comment box.