What is NumPy in Python with example?

It is one of the fundamental libraries in the scientific computing ecosystem of Python.

Here’s an example that demonstrates the basic usage of NumPy:

import numpy as np

# Creating a NumPy array
arr = np.array([1, 2, 3, 4, 5])
print(arr)  # Output: [1 2 3 4 5]

# Performing arithmetic operations on arrays
arr2 = arr + 2
print(arr2)  # Output: [3 4 5 6 7]

arr3 = arr * 3
print(arr3)  # Output: [ 3  6  9 12 15]

# Performing mathematical operations
mean = np.mean(arr)
print(mean)  # Output: 3.0

std_dev = np.std(arr)
print(std_dev)  # Output: 1.4142135623730951

# Reshaping an array
reshaped_arr = arr.reshape((5, 1))
print(reshaped_arr)
# Output:
# [[1]
#  [2]
#  [3]
#  [4]
#  [5]]

# Indexing and slicing
print(arr[0])     # Output: 1
print(arr[1:4])   # Output: [2 3 4]

# Array operations
arr4 = np.array([6, 7, 8, 9, 10])
dot_product = np.dot(arr, arr4)
print(dot_product)  # Output: 130

# Generating sequences
seq = np.arange(0, 10, 2)
print(seq)  # Output: [0 2 4 6 8]

# 2D arrays
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
print(arr_2d)
# Output:
# [[1 2 3]
#  [4 5 6]]

# Array shape and size
print(arr.shape)     # Output: (5,)
print(arr_2d.shape)  # Output: (2, 3)
print(arr.size)      # Output: 5
print(arr_2d.size)   # Output: 6
Code language: Python (python)

NumPy provides a wide range of functions and capabilities, including mathematical operations, linear algebra, Fourier transforms, random number generation, and much more. It is extensively used in fields such as data analysis, machine learning, scientific computing, and numerical simulations.

How do you create a NumPy array give an example?

To create a NumPy array, you can use the numpy.array() function and provide a Python list or tuple as input. Here’s an example:

import numpy as np

# Creating a NumPy array from a Python list
my_list = [1, 2, 3, 4, 5]
arr = np.array(my_list)
print(arr)
# Output: [1 2 3 4 5]

# Creating a NumPy array from a Python tuple
my_tuple = (6, 7, 8, 9, 10)
arr2 = np.array(my_tuple)
print(arr2)
# Output: [ 6  7  8  9 10]
Code language: Python (python)

In the above example, we first import the numpy module as np. Then we create a NumPy array by passing a Python list [1, 2, 3, 4, 5] to the np.array() function and assign it to the variable arr. Similarly, we create another NumPy array by passing a Python tuple (6, 7, 8, 9, 10) to the np.array() function and assign it to the variable arr2. Finally, we print both arrays to verify the results.

You can also create multidimensional arrays by passing nested lists or tuples to the np.array() function. For example:

import numpy as np

# Creating a 2D NumPy array from nested lists
my_2d_list = [[1, 2, 3], [4, 5, 6]]
arr_2d = np.array(my_2d_list)
print(arr_2d)
# Output:
# [[1 2 3]
#  [4 5 6]]

# Creating a 3D NumPy array from nested tuples
my_3d_tuple = ((1, 2), (3, 4), (5, 6))
arr_3d = np.array(my_3d_tuple)
print(arr_3d)
# Output:
# [[[1 2]
#   [3 4]
#   [5 6]]]
Code language: Python (python)

In this example, we create a 2D NumPy array by passing a nested list [[1, 2, 3], [4, 5, 6]] to the np.array() function and assign it to the variable arr_2d. Similarly, we create a 3D NumPy array by passing a nested tuple ((1, 2), (3, 4), (5, 6)) to the np.array() function and assign it to the variable arr_3d. The resulting arrays are then printed for verification.

Read More;

  • I am a middle python software engineer with a bachelor's degree in Software Engineering from Kharkiv National Aerospace University. My expertise lies in Python, Django, Flask, Docker, REST API, Odoo development, relational databases, and web development. I am passionate about creating efficient and scalable software solutions that drive innovation in the industry.

    View all posts

Leave a Comment