Python NumPy square with examples

In this Python tutorial, we will discuss Python NumPy square and also we will cover the below examples:

  • Python numpy square root
  • Python numpy square sum
  • Python numpy squared norm
  • Python numpy square array
  • Python numpy square wave
  • Python numpy square difference
  • Python numpy square vector
  • Python numpy square vs **

Python numpy square

  • In this section we will learn about Python numpy square.
  • It is a statistical function that helps the user to calculate the square value of each value in the array.
  • It always returns an array with the square value of each array.
  • The source array remains unchanged.
  • In this method, we can easily use the function np. square().
  • The numpy square() function calculates the square of numeric input.

Syntax:

Here is the Syntax of numpy square()

numpy.square
            (
             x,
             out=None,
             *,
             Where=True,
             casting='same_kind'
             dtype=None
            )
  • It consists of few parameters
    • X: The x parameter enables you to specify the input to the function (i.e., the argument).
    • OUT: A location into which the result is stored. If provided, it should have a shape that the inputs provided. If not provided or None, a new-allocated array is returned.
    • Where: This condition is provided over the input. At a position where the condition is True, the out numpy array will be set to the ufunc result.
    • Returns: Element-wise x*x, of the same shape and data type as x.

Example:

Let’s take an example to check how to use NumPy square.

Example1: Calculate the square of values in 1d array

  • First, we have to import a numpy library and then create a numpy array using the numpy array function.
  • Now that we have an array, we can run numpy square by using the numpy square function.
import numpy as np

arr1 = np.array([4, 5, 6, 7])
res = np.square(arr1)
print(res)

Here is the Screenshot of the following given code

Python numpy square
Python numpy square

Example2: Calculate the square of values in complex number arrays

import numpy as np

arr1 = np.array([1+2j, 2+3j, 6+5j, 7+8j])
res = np.square(arr1)
print(res)

Here is the Screenshot of the following given code

Python numpy square complex no
Python numpy square complex no

Read: Python NumPy to list and Python NumPy max

Python numpy square root

  • In this section we will learn about Python numpy square root.
  • In this method we can easily use the function numpy sqrt().
  • This function is used to determine the positive square root of an array element-wise.
  • The numpy square root function helps the user to calculate the square root of input values.
  • So if you give an input x, the numpy square function will calculate and give the result in the form of an under the root of x.

Syntax:

Here is the syntax of the numpy square root

numpy.sqrt
          (
           x,
           out=None,
           *,
           Where=True,
           casting='same_kind'
           dtype=None
           )
  • It consists of few parameters.
    • X: The x parameter enables you to specify the input to the np. sqrt function.
    • OUT: The out parameter enables you to specify an array where the output will be stored.
    • Returns: An array of the same shape as x, containing the positive square root of each element in x.

Examples:

Let’s take an example to check how to use numpy square root.

Example1: Calculate the square root of values in 1d array

  • First, we have to import a numpy library and then create a numpy array using the numpy array function.
  • Now that we have an array, we can run numpy square root by using the numpy sqrt() function.
import numpy as np

arr1 = np.array([36,25,49,4,9,121])
res = np.sqrt(arr1)
print(res)

Here is the Screenshot of the following given code

Python numpy squareroot
Python numpy square root

Example2: Calculate the square root of values in 2d array

  • First, we create a 2dimension array using the function numpy.array and then use this array as the input to the sqrt function.
  • When we use a 2Dimension array as the input, the np.sqrt function easily calculates the square root of every value of the array.
import numpy as np

arr1 = np.array([[36,25,49],[16,64,81]])
res = np.sqrt(arr1)
print(res)

Here is the Screenshot of the following given code

Python numpy square root 2d array
Python numpy square root 2d array

Read: Python NumPy read CSV

Python numpy square sum

  • In this section we will learn about Python numpy square sum.
  • In this method first, we create an array by using the function np.array.Now that we can have an array we have to square the values of each array by using np.square function.
  • After that, the np.sum() function calculates the given square elements of an array.

Example:

Let’s take an example to check how to use NumPy square sum.

import numpy as np

arr1 = np.array([[36,25,49],[16,64,81]])
res= np.square(arr1)
print(res)
new_res = np.sum(res)
print(new_res)

Here is the Screenshot of the following given code

Python numpy square sum
Python numpy square sum

Python numpy squared norm

  • In this section, we will learn about numpy squared norm.
  • In simple words, the norm is a quantity that defines the size of a vector.
  • The vector means a set of whole numbers.
  • In this method we can easily use the function numpy.linalg.norm.
  • This function is able to return one of the different matrix norms or one of an infinite number of vector norms depending on the value of the ord argument.

Syntax:

Here is the syntax of the numpy norm

numpy.linalg.norm
                 (
                  x,
                  ord=None,
                  axis=None
                 )
  • It consists of few parameters.
    • X: It is an input array. If the axis is None, x must be 1-D or 2-D, unless ord is None. If both axis and ord are None, the 2nd norm will be returned.
    • ord: It is an optional parameter. The default value is none.
    • Axis: If the axis is an integer, it specifies the axis of x along which to compute the vector norms. If the axis is a 2-tuple, it specifies the axes that hold 2-D matrices, and the matrix norms of these matrices are computed.
    • Returns: Norm of the vector.

Example:

Let’s take an example to check how to use NumPy square norm

import numpy as np

arr = np.array([[ 2, 4, 6],
              [ 2, 2, 3]])
e = np.square(arr)
print(e)
d = np.linalg.norm(e, ord=1, axis=1)
print(d)

In the above-given example, we have created a NumPy array using the function np. array and square the values of the given array after that use lin. alg. norm function to calculate the size of the vector.

Here is the Screenshot of the following given code

Python numpy square norm
Python numpy square norm

Read: Python NumPy log + Examples

Python numpy square array

  • In this section we will learn about Python numpy square array.
  • In this method we will compute the square of the values in a numpy array.
  • First we have to create a numpy array using the function np.array and pass the values in an arguments.
  • Take a variable in which you have to store a result in the form of array and also take a numpy square() function to calculates the square of a numeric input.

Syntax:

Here is the Syntax of the numpy square

numpy.square
            (
             x,
             out=None,
             *,
             Where=True,
             casting='same_kind'
             dtype=None
            )

Examples:

Let’s take an example to check how to use a numpy square array.

Example1: Calculate the square of values in a 2d numpy array

import numpy as np

arr = np.array([[ 4, 5, 6],
              [ 2, 2, 3]])
res = np.square(arr)
print(res)

Here is the Screenshot of the following given code

Python numpy square array
Python numpy square array

Example2: Calculate the square of values in a 3d NumPy array

  • First we will create a 3d numpy array with the values from 1 to 9 and arrange in 3 by 3 reshape.
  • We will use the numpy arange function with the combination of reshape function.
  • Now the np.sqrt function simply calculates the square root of every element of the array.
import numpy as np

arr = np.arange(1,10).reshape((3,3))
res = np.square(arr)
print(res)

Here is the Screenshot of the following given code

Python numpy square array 3d
Python numpy square array 3d

Read: Python NumPy where with examples

Python numpy square wave

  • In this section we will learn about Python numpy square wave.
  • Square waves are period waveforms.
  • In this method, we can easily use the function scipy.signal.square.
  • It will return the result a periodic square wave waveform.
  • A square wave is a periodic waveform in which the amplitude alternates at a steady frequency between the fixed minimum and maximum values, with the same duration at minimum and maximum.

Syntax:

Here is the Syntax of numpy square wave

scipy.signal.square
                   (
                    t,
                    duty=0.5
                   )
  • It consists of few parameters.
    • T: The input time array.
    • duty: Default value is 0.5.
    • Returns: Output array containing the square waveform.

Example:

import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
t = np.linspace(0, 1, 500, endpoint=False)
plt.plot(t, signal.square(2 * np.pi * 5 * t),'b')
plt.ylim(-2, 2)
plt.grid()
plt.show()

Here is the Screenshot of the following given code

Python numpy square wave
Python numpy square wave

Read Python reverse NumPy array

Python numpy square difference

  • In this section we will learn about Python numpy square difference.
  • In this method first we create an array by using a function np.array.Now that we can have an array we have to square the values of each array by using np.square function.
  • After that the numpy diff() function calculates the given square values of an array.

Example:

Let’s take an example to check how to use NumPy square diff

import numpy as np

arr1 = np.array([[4,5,6],[2,3,4]])
res= np.square(arr1)
print(res)
new_res = np.diff(res)
print(new_res)

Here is the Screenshot of the following given code

Python numpy square diff
Python numpy square diff

Read: Python NumPy linspace

Python numpy square vector

  • In this section we will learn about Python numpy square vector.
  • A vector is a numpy array with a single dimension (there’s no difference between row and column vectors), while a matrix refers to an array with two dimensions array.
  • We can think of a vector as a list of numbers, and vector algebra as operations performed in the list.
  • In this method, we can easily use the function numpy square function to display the vector ndarray shape.

Syntax:

numpy.square
            (
             x,
             out=None,
             *,
             Where=True,
             casting='same_kind'
             dtype=None
            )

Example:

import numpy as np

vect = np.array([[4, 5, 6, 7],
                 [3,4,5,6 ],
                 [5,6,7,8]])
res_vect = np.square(vect)
print(res_vect)

Here is the Screenshot of the following given code

Python numpy square vector
Python numpy square vector

Python numpy square vs **

  • In this section we will learn about Python numpy square vs **.
  • The standard pythonic ** is easier and faster than the numpy. square().
  • The numpy functions are often more flexible.

Example:

import numpy as np 

Arr1 = np.array([[3, 3],[3, 3]]) 
res=np.square(Arr1)
b = Arr1 ** 2 
print(res)
print(b)

Here is the Screenshot of the following given code

Python numpy square vs **
Python numpy square vs **

You may like the following Python tutorials:

In this Python tutorial, we will discuss Python NumPy square and also we will cover the below examples:

  • Python numpy square root
  • Python numpy square sum
  • Python numpy squared norm
  • Python numpy square array
  • Python numpy square wave
  • Python numpy square difference
  • Python numpy square vector
  • Python numpy square vs **