What is NumPy in Python?

What is NumPy?

NumPy is a Python library for arrays. It also has functions for working in algebra, Fourier transforms, and matrices. Travis Oliphant created NumPy in 2005. It is an open-source project, and you can use it freely.

It stands for Numerical Python.

NP is a Python library written partially in Python, but most of the parts that require fast computation are written in C and C++.

Example:

NumPy image for understanding.

import numpy as np      #Importing numpy 
x = np.array([[1,3,2,4,3], [3,1,5,4,6]])

print(x, x.ndim)
Output:

[[1 3 2 4 3]
[3 1 5 4 6]] 2

Know the Documentation of NP:

NumPy Documentation

Dimensions in NumPy Array:

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

  • O-D Array
  • 1-D Array
  • 2-D Array
  • 3-D Array
  • n-D Array
  • Multi-Dimensional

Data Types :

NumPy supports a variety of data types, which are used to define the type of elements in arrays. These are much like standard Python data types but are more efficient for numerical operations and better suited for large-scale computations.

  1. i = Integer
  2. b = Boolean
  3. u = Unsigned Integer
  4. f = Float
  5. c = Complex Float
  6. m = Time delta
  7. M = Data time
  8. O = Object
  9. S= String
  10. U = Unicode string
  11. V = Void

Importance of NumPy in Python:

  • Wide variety of mathematical operations on array.
  • It supplies an enormous library of high-level mathematical functions that operate on these array and matrices.
  • Mathematical, logical, shape manipulation, sorting, selecting, I/O, discreate Fourier transform, basic linear algebra, basic statistician operations random simulation and much more.

Special NP Array:

  • Array filled with 0’s.
  • Array filled with 1’s.
  • Create an empty array.
  • An array with a range of elements.
  • Array diagonals elements filled with 1’s.
  • Create an array with value that are spaced linearly I am specified interval.

Arithmetic Operation in NP Array:

np.add (a, b)
np.subtract  (a, b)
np.multiply (a, b)
np.divide (a, b)
np.mod (a, b)
np.power (a, b)
np.reciprocal (a, b)

Arithmetic Function in NP Array:

np.min (x)
np.max (x)
np.argmin (x)
np.argmax (x)
np.sqrt (x)
np.sin (x)
np.cos (x)
np.cumsum (x)

Code:
import numpy as np

# Create a simple 1D array
arr1 = np.array([10, 20, 30, 40])
print("1D Array:", arr1)

# Create a 2D array (matrix)
arr2 = np.array([[1, 2], [3, 4]])
print("2D Array:\n", arr2)

# Check data type
print("Data type of arr1:", arr1.dtype)

# Perform element-wise operations
arr3 = arr1 + 5
print("arr1 + 5:", arr3)

# Basic statistics
print("Max value:", arr1.max())
print("Mean value:", arr1.mean())

# Reshape an array
reshaped = arr1.reshape((2, 2))
print("Reshaped to 2x2:\n", reshaped)

# Create an array of zeros and ones
zeros = np.zeros((2, 3))
ones = np.ones((2, 3))
print("Zeros:\n", zeros)
print("Ones:\n", ones)

# Create an array with a range of numbers
range_array = np.arange(0, 10, 2)
print("Range array:", range_array)
Output:

1D Array: [10 20 30 40]
2D Array:
[[1 2]
[3 4]]
Data type of arr1: int64
arr1 + 5: [15 25 35 45]
Max value: 40
Mean value: 25.0
Reshaped to 2x2:
[[10 20]
[30 40]]
Zeros:
[[0. 0. 0.]
[0. 0. 0.]]
Ones:
[[1. 1. 1.]
[1. 1. 1.]]
Range array: [0 2 4 6 8]

Also visit for more learning:

Converting Pandas DataFrame to NumPy Array and Vice Versa
Creating and Managing NumPy Arrays for Data Analysis

How to Securely Store and Hash Passwords in Python (bcrypt, argon2)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top