NumPy stands for Numerical Python. It provides a high - performance multidimensional array object and tools for working with these arrays. The core of NumPy is the ndarray
(n - dimensional array) object, which is a homogeneous data structure where all elements must be of the same data type.
You can install NumPy 1.26.4 using pip
:
pip install numpy==1.26.4
Once installed, you can import it in your Python script:
import numpy as np
import numpy as np
# Create a 1 - D array from a list
a = np.array([1, 2, 3, 4, 5])
print("1 - D array:", a)
# Create a 2 - D array
b = np.array([[1, 2, 3], [4, 5, 6]])
print("2 - D array:", b)
# Create an array of zeros
zeros_array = np.zeros((3, 4))
print("Array of zeros:", zeros_array)
# Create an array of ones
ones_array = np.ones((2, 2))
print("Array of ones:", ones_array)
# Create an array with a range of values
range_array = np.arange(0, 10, 2)
print("Array with range:", range_array)
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
# Addition
add_result = a + b
print("Addition:", add_result)
# Subtraction
sub_result = a - b
print("Subtraction:", sub_result)
# Multiplication
mul_result = a * b
print("Multiplication:", mul_result)
# Division
div_result = a / b
print("Division:", div_result)
import numpy as np
a = np.array([1, 2, 3, 4, 5])
print("First element:", a[0])
print("Elements from index 1 to 3:", a[1:4])
b = np.array([[1, 2, 3], [4, 5, 6]])
print("Element at row 1, column 2:", b[1, 2])
print("First row:", b[0, :])
import numpy as np
a = np.array([1, 2, 3])
sqrt_result = np.sqrt(a)
print("Square root:", sqrt_result)
sin_result = np.sin(a)
print("Sine values:", sin_result)
import numpy as np
# Create a 3 - D array
c = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print("3 - D array shape:", c.shape)
print("Number of dimensions:", c.ndim)
# Reshape an array
reshaped = c.reshape(2, 4)
print("Reshaped array:", reshaped)
import numpy as np
# Generate random integers
random_ints = np.random.randint(0, 10, (3, 3))
print("Random integers:", random_ints)
# Generate random floating - point numbers
random_floats = np.random.rand(2, 2)
print("Random floats:", random_floats)
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([10, 20, 30])
result = a + b
print("Result after broadcasting:", result)
np.int8
instead of np.int64
.import numpy as np
small_ints = np.array([1, 2, 3], dtype=np.int8)
import numpy as np
a = np.arange(1000)
# Vectorized operation
squared = a**2
# Non - vectorized (using loop) would be much slower
# squared_loop = []
# for i in a:
# squared_loop.append(i**2)
NumPy 1.26.4 is a powerful library that provides a wide range of tools for numerical computing in Python. By understanding the fundamental concepts, usage methods, common practices, and best practices, you can efficiently manipulate arrays, perform numerical operations, and optimize your code for better performance. Whether you are working on data analysis, machine learning, or scientific research, NumPy is an essential tool in your Python toolkit.