codingstreets
Search
Close this search box.

Introduction to Creation of Python Numpy Arrays

In This Article You Will Learn About Python Numpy Arrays.

Python NumPy Arrays – Before moving ahead, let’s know a little bit about Python Numpy Array

Make a NumPy object from ndarray

NumPy can be used to manipulate arrays. NumPy’s array object is called “ndarray”.

You can use the array() function to create a NumPy object.

Example – Creating a NumPy ndarray object.

import numpy as np
Array = np.array([1, 2, 3, 4, 5])

print(Array)
print(type(Array))
Output   -

[1 2 3 4 5]

<class 'numpy.ndarray'>

As a result, it returned an array and type of an array.

type(): This Python function tells you the type of object. It shows, as in the above code, that Array is numpy.ndarray.

An array() method can be used to create an ndarray. It will take a list, tuple, or other array-like object and convert it into an ndarray.

Example – Using a tuple to create a NumPy array.

import numpy as np
Array = np.array((1, 2, 3, 4, 5))

print(Array)
Output -

[1 2 3 4 5]

Dimensions in Arrays

An array dimension is one level of depth (nested arrays).

Nested array: These arrays have arrays as their element.

0-D Arrays

Scalars and 0-D arrays are represented elements within an array. Every value in an array can be described as a 0-D array.

Example – Creating a 0-D array with value any numerical value.

import numpy as np
Array = np.array(51)

print(Array)
Output -

51

1-D Arrays

A 1-D or uni-dimensional array is an array with 0-D arrays for its elements.

Example – Creating an array with a 1-D array containing the numerical values.

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

print(Array)
Output -

[11 12 13 14 15]

In conclusion, it returned a 1-D array containing the numerical values.

2-D Arrays

A 2-D array is one that contains 1-D arrays as its elements.

These are used often to represent matrix or second-order tensors.

NumPy offers a sub-module that is dedicated to matrix operations, numpy.mat.

Example – Creating an array with a 2-D array containing the numerical values.

import numpy as np
Array = np.array([[11, 12, 13, 14, 15], [ 16, 17, 18, 19, 20]])

print(Array)
Output -

[[11 12 13 14 15]
[16 17 18 19 20]]

As shown above, it returned an array with a 2-D array containing the numerical values.

3-D array

A 3-D array is an array with 2-D arrays (matrices as its elements)

These are used often to represent a third-order array.

Example – Make a 3-D array by creating two 2-D arrays each containing the numerical values.

import numpy as np
Array = np.array([[[11, 12, 13], [14, 15, 16]], [[11, 12, 13], [14, 15, 16]]])

print(Array)
Output - 

[[[11 12 13]
   [14 15 16]]

 [[11 12 13]
   [14 15 16]]]

In the final analysis, it returned a 3-D array by creating two 2-D arrays each containing the numerical values.

What is the Number of Dimensions?

NumPy arrays have the ndim attribute, which returns an integer that indicates how many dimensions the array has.

Example – Checking the dimensions of the arrays:

import numpy as np

p = np.array(20)
q = np.array([11, 12, 13, 14, 15])
r = np.array([[1, 2, 3], [4, 5, 6]])
s = np.array([[[11, 12, 13], [14, 15, 16]], [[11, 12, 13], [14, 15, 16]]])

print("Dimension of Array is", p.ndim)
print("Dimension of Array is", q.ndim)
print("Dimension of Array is", r.ndim)
print("Dimension of Array is", s.ndim)
Output -

Dimension of Array is 0
Dimension of Array is 1
Dimension of Array is 2
Dimension of Array is 3

As a result, it returned the dimensions of the array.

Higher Dimensional Arrays

An array can be any size.

After the array has been created, the ndmin argument can be used to define the number of dimensions.

Example – Creating an array with 5 dimensions.

import numpy as np
Array = np.array([11, 12, 13, 14], ndmin=5)

print(Array.ndim)
python-numpy-arrays

Accordingly, this array’s last index number dimension (5th Dim) having 14 elements. The 4th dim has the vector element, the 3rd dim contains the matrix with the vector element, and the 2nd dim has the 3D array element. The 1st dim has the element that’s a 4D array.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on