## Introduction to NumPy axis

The directions along the rows and columns in a two-dimensional array are called axes in NumPy. The direction along the rows is axis 0 and is the first axis which runs down the rows in multi-dimensional arrays, and the direction along the columns is axis 1 and is the second axis which runs across the columns in multi-dimensional arrays. These axes in NumPy are numbered, beginning from zero. One should always be alert on what parameters are passed to the axis function and how they impact the function on which they are used, and the axis function in NumPy can be used on several functions.

**Syntax:**

`NumPy.function(arrayname, axis = 0/1)`

Where NumPy.function is any function on which the axis function must be applied, arrayname is the name of the array on which the NumPy.function must be performed along the axis specified by axis function whose value can be either 0 or 1.

### Working of axis function in NumPy

- The directions along the rows and columns in a two-dimensional array are called axes in NumPy.
- The direction along the rows is axis 0 and is the first axis that runs down the rows in multi-dimensional arrays.
- The direction along the columns is axis 1 and is the second axis which runs across the columns in multi-dimensional arrays.
- The axes in NumPy are numbered, beginning from zero.
- One should always be alert on what parameters are passed to the axis function and how they impact the function on which they are used, and the axis function in NumPy can be used on several functions.

### Examples

Different examples are mentioned below:

#### Example #1

Python program to demonstrate NumPy axis function to create an array using array function and find the sum of the elements of the array along the axis = 0 and along the axis = 1:

** Code:**

`#we are importing the package NumPy as num`

import numpy as num

#creating a two dimensional array using array function and storing it in a avariable called array1

array1 = num.array([[1,2,3],[4,5,6]])

#displaying the elements of newly created array

print("The elements of the newly created array are:")

print(array1)

#using axis function on the newly created array along with sum function to find the sum of the elements of the array along the axis = 0

arraysum = num.sum(array1, axis = 0)

print("\n")

#displaying the sum of the elements of the array along the axis = 0

print("The sum of the elements of the newly created array along the axis = 0 are:")

print(arraysum)

print("\n")

#using axis function on the newly created array along with sum function to find the sum of the elements of the array along the axis = 1

arraysum1 = num.sum(array1, axis = 1)

#displaying the sum of the elements of the array along the axis = 1

print("The sum of the elements of the newly created array along the axis = 1 are:")

print(arraysum1)

**Output:**

In the above program, the package NumPy is imported as num to be able to make use of array function, sum function, and axis function. Then a two-dimensional array is created using the array function and stored in a variable called array1. Then the elements of the array array1 are displayed. Then sum function is used on the array array1 to find the sum of the elements of the array along axis = 0, and the result is displayed as the output on the screen; then again sum function is used on the array array2 to find the sun of the elements of the array along axis = 1 and the result is displayed as the output on the screen. The output is shown in the snapshot above.

#### Example #2

Python program to demonstrate NumPy axis function to create an array using array function and find the sum of the elements of the array along the axis = 0 and along the axis = 1:

4.5 (6,869 ratings)

View Course

** Code:**

`#we are importing the package NumPy as num`

import numpy as num

#creating a two dimensional array using array function and storing it in a avariable called array1

array1 = num.array([[5,4,3],[2,1,0]])

#displaying the elements of newly created array

print("The elements of the newly created array are:")

print(array1)

#using axis function on the newly created array along with sum function to find the sum of the elements of the array along the axis = 0

arraysum = num.sum(array1, axis = 0)

print("\n")

#displaying the sum of the elements of the array along the axis = 0

print("The sum of the elements of the newly created array along the axis = 0 are:")

print(arraysum)

print("\n")

#using axis function on the newly created array along with sum function to find the sum of the elements of the array along the axis = 1

arraysum1 = num.sum(array1, axis = 1)

#displaying the sum of the elements of the array along the axis = 1

print("The sum of the elements of the newly created array along the axis = 1 are:")

print(arraysum1)

**Output:**

In the above program, the package NumPy is imported as num to be able to make use of array function, sum function, and axis function. Then a two-dimensional array is created using the array function and stored in a variable called array1. Then the elements of the array array1 are displayed. Then sum function is used on the array array1 to find the sum of the elements of the array along axis = 0, and the result is displayed as the output on the screen; then again sum function is used on the array array2 to find the sun of the elements of the array along axis = 1 and the result is displayed as the output on the screen. The output is shown in the snapshot above.

### Recommended Articles

This is a guide to the NumPy axis. Here we discuss the concept of the NumPy axis function in Python through definition, syntax, and the working of axis function in python through programming examples and outputs. You may also have a look at the following articles to learn more –