Introduction to Numpy

Introduction

NumPy is a Python library that provides a high-performance multidimensional array object. NumPy arrays are much faster and more efficient than Python lists, and they are essential for scientific computing and machine learning.

Some of the most frequently used NumPy functions and methods include:

  • array(): This function creates a NumPy array from a Python list or tuple.
  • reshape(): This function changes the shape of a NumPy array.
  • append(): This method appends an element to a NumPy array.
  • insert(): This method inserts an element into a NumPy array at a specific index.
  • delete(): This method deletes an element from a NumPy array at a specific index.
  • concatenate(): This function concatenates two or more NumPy arrays.
  • vstack(): This function stacks two or more NumPy arrays vertically.
  • hstack(): This function stacks two or more NumPy arrays horizontally.
  • sort(): This method sorts the elements of a NumPy array.
  • mean(): This function calculates the mean of the elements of a NumPy array.
  • std(): This function calculates the standard deviation of the elements of a NumPy array.
  • min(): This function finds the minimum element of a NumPy array.
  • max(): This function finds the maximum element of a NumPy array.

These are just a few of the many NumPy functions and methods that are available. For more information, you can refer to the NumPy documentation: https://numpy.org/doc/stable/

Example

Here are some examples of how to use NumPy functions and methods:

import numpy as np

# Create a NumPy array
array_1 = np.array([1, 2, 3, 4, 5])

# Reshape the array
array_2 = array_1.reshape(2, 3)

# Append an element to the array
array_1.append(6)

# Insert an element into the array
array_1.insert(2, 7)

# Delete an element from the array
array_1 = array_1[:2]

# Concatenate two arrays
array_3 = np.concatenate((array_1, array_2))

# Stack two arrays vertically
array_4 = np.vstack((array_1, array_2))

# Stack two arrays horizontally
array_5 = np.hstack((array_1, array_2))

# Sort the array
array_1.sort()

# Calculate the mean of the array
mean_1 = np.mean(array_1)

# Calculate the standard deviation of the array
std_1 = np.std(array_1)

# Find the minimum element of the array
min_1 = np.min(array_1)

# Find the maximum element of the array
max_1 = np.max(array_1)

Generating random numbers

import numpy as np

# Generate a sequence of 100 random numbers
random_numbers = np.random.rand(100)

# Print the first 10 random numbers
print(random_numbers[:10])

Calculating statistical functions

import numpy as np

# Calculate the mean, standard deviation, minimum, and maximum of a sequence of numbers
numbers = np.array([1, 2, 3, 4, 5])

mean = np.mean(numbers)
std = np.std(numbers)
min = np.min(numbers)
max = np.max(numbers)

print('Mean:', mean)
print('Standard deviation:', std)
print('Minimum:', min)
print('Maximum:', max)

Manipulating images

import numpy as np
import matplotlib.pyplot as plt

# Load an image
image = np.load('image.npy')

# Plot the image
plt.imshow(image)
plt.show()

# Blur the image
blurred_image = np.blur(image, (3, 3))

# Plot the blurred image
plt.imshow(blurred_image)
plt.show()

Working with matrices

import numpy as np

# Create a matrix
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Calculate the determinant of the matrix
determinant = np.linalg.det(matrix)

# Print the determinant
print(determinant)

Implementing machine learning algorithms

import numpy as np
from sklearn.linear_model import LinearRegression

# Create a dataset
X = np.array([1, 2, 3, 4, 5])
y = np.array([6, 7, 8, 9, 10])

# Create a linear regression model
model = LinearRegression()

# Fit the model to the data
model.fit(X, y)

# Make predictions
predictions = model.predict(X)

# Print the predictions
print(predictions)