Learn to create arrays using NumPy in Python. The Numpy Array Creation of different dimensions has been illustrated with the help of examples.

Table of Contents

## Creating Arrays using NumPy

NumPy focuses on working with a multi-dimensional array, and these are those arrays that have more than two dimensions. These multidimensional arrays also are known as matrices. The functions that we can implement on these are traverse, insertion, deletion, search and update. There are some mechanisms for creating an array in NumPy which are as follows:

- Transforming arrays from other Python structures like lists and tuples.
- Inherent NumPy arrays creation objects, so it makes them look similar.
- Interpreting arrays from disk in custom or standard forms.
- Using the special library functions.
- Also creating arrays from raw bytes.

The arrays in NumPy are referred to as `ndarray`

. We can create an array object in the NumPy using `array( )`

functions. Let us go through an example of creating which will help us understand it better.

Here is an example:

import numpy as np array1=np.array([11,12,13,14]) print(array1)

**Output:**

[11 12 13 14]

This is a simple example of printing an array with some value in it and also first we are trying to import the NumPy as `np`

. We can use `type()`

as a result, we can find the type of array it is. let us go through another example:

import numpy as np array1=np.array([11,12,13,14]) print(array1) print(type(array1))

**Output:**

[11 12 13 14] <class 'numpy.ndarray'>

Here it is predicting that array is with the `ndarray`

type.

In order to create an array as we discussed earlier that we could transform python structure like lists and tuples into the arrays, .we can pass these to a method which will give us an array. Let us go through an example to where will be a python structure and it will return us an array.

import numpy as np array1=np.array((20,30,40,50,60,70,80)) print(array1) print(type(array1))

**Output:**

[20 30 40 50 60 70 80] <class 'numpy.ndarray'>

Here we are trying to pass a list which will be passed as a list and then changed into a NumPy array with the type as` ndarray`

.

## Different Dimensions of Arrays

NumPy arrays are multi-dimensional arrays so they could have any number dimension. A dimension in an array is one level of depth of an array. An array can have any number of depths as per the requirement. Some of the dimension in the array are as follow:

### 0-D Arrays

These arrays constitute all the values present in the array. These are also known as scalar which forms each element of the array. And also each value of the array in these are 0-D array. Let us go through an example:

import numpy as np array1=np.array(20) print(array1)

**Output:**

20

Here we are passing value in the array which is returning us a NumPy array.

### 1-D Arrays (One-Dimensional Arrays)

These arrays are the result of more that one 0-D array so they can have any number of 0-D arrays and they all combining will form a 1-D array. They are also known as uni-dimensional arrays, and these are the most common and basic types of arrays. Let us go through an example of a 1-D array:

import numpy as np array1=np.array([3,5,13,15]) print(array1)

Output:

[3 5 13 15]

This is a 1-D array with many 0-D arrays in it.

### 2-D Arrays (Two-Dimensional Arrays)

These arrays are the result of combining more than one 1-D array, and they all combining will form the 2-D array. To access elements from these arrays, we can use commas and Brackets. They help us in representing a matrix with X and Y elements. Let us go through an example for the 2-D array:

import numpy as np array1=np.array([[2,5,13,17],[2,4,6,5]]) print(array1)

**Output:**

[[ 2 5 13 17] [ 2 4 6 5]]

here we are getting the output in the form of a matrix in two dimensions. The format is in the form of rows and columns which makes up the whole matrix.

### 3-D Arrays (Three-Dimensional Arrays)

These arrays are the result of combining more than one 2-D array, and they all combining will form a 3-D array. To access elements from these arrays, we can use commas and Brackets. They help us in representing a matrix with X and Y elements. Let us go through an example for the 3-D array:

import numpy as np array1=np.array([[[1,2,3,4],[5,6,7,8]],[[9,8,10,11],[12,14,16,15]]]) print(array1)

Output:

[[[1 2 3 4] [5 6 7 8]] [[9 8 10 11] [12 14 16 15]]]

Here we are going to get a 3-D array with all the elements, and they are also familiar as 3rd order tensor.

## Checking the Number of Dimensions:

There could be more than the dimensions we have discussed above but NumPy also provides an attribute with which we can the number of dimensions present in an array. This attribute is known as `ndim and `

it helps us in telling how many dimensions the array has in itself. Let us go through an example where we would check dimensions of various arrays:

import numpy as np array1=np.array([3,5,13,15]) print(array1.ndim)

**Output:**

1

Here this attribute is specifying that it is a 1-D array. let us look at some other examples:

import numpy as np array1=np.array([[2,5,13,17],[2,4,6,5]]) array2=np.array([[[1,2,3,4],[5,6,7,8]],[[9,8,10,11],[12,14,16,15]]]) print('Dimensions for this array are:',array1.ndim) print('Dimensions for this array are:',array2.ndim)

**Output:**

Dimensions for this array are: 2 Dimensions for this array are: 3

Here we are getting the dimensions of different arrays by using `ndim`

an attribute.

## Defining Dimensions of Arrays

As we know an array can have any number of dimension; as a result, we have this argument which we can use to specify the dimensions of the array. The attribute for this purpose is known as` ndmin`

an argument.

Let us go through an example of using this attribute:

import numpy as np array1=np.array([3,5,13,15], ndmin=6) print(array1) print('no of dimensions:', array1.ndim)

Output:

[[[[[[ 3 5 13 15]]]]]] no of dimensions: 6

So this argument is helping us specify the dimensions of the array.

Other ways of creating NumPy Arrays:

We can create arrays that consist of integers, floats and complex numbers. Let us go through an example to elaborate on this:

import numpy as np array1=np.array([3.2,5.4,13,15], ndmin=6) print(array1) print('no of dimensions:', array1.ndim)

**Output:**

[[[[[[ 3.1 5.4 13. 15. ]]]]]] no of dimensions: 6

Here we are trying to make an array with six dimensions and some float elements in it, and as a result, it is specifying the number of dimensions and also giving us an array with six dimensions.

We can also create arrays of zeros and ones. Here we will be using `zeros`

and `ones`

methods.

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!