Numpy Library
Program 1: Matrix Operations using NumPy
This program creates two matrices and performs matrix addition, subtraction, and multiplication.
import numpy as np
# Define two matrices
matrix1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
matrix2 = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])
# Matrix Addition
addition = matrix1 + matrix2
# Matrix Subtraction
subtraction = matrix1 - matrix2
# Matrix Multiplication (Element-wise)
multiplication = matrix1 * matrix2
# Matrix Dot Product
dot_product = np.dot(matrix1, matrix2)
# Print the results
print("Matrix 1:\n", matrix1)
print("Matrix 2:\n", matrix2)
print("\nMatrix Addition:\n", addition)
print("\nMatrix Subtraction:\n", subtraction)
print("\nElement-wise Multiplication:\n", multiplication)
print("\nMatrix Dot Product:\n", dot_product)
Output:
Matrix 1:
[[1 2 3]
[4 5 6]
[7 8 9]]
Matrix 2:
[[9 8 7]
[6 5 4]
[3 2 1]]
Matrix Addition:
[[10 10 10]
[10 10 10]
[10 10 10]]
Matrix Subtraction:
[[-8 -6 -4]
[-2 0 2]
[ 4 6 8]]
Element-wise Multiplication:
[[ 9 16 21]
[24 25 24]
[21 16 9]]
Matrix Dot Product:
[[ 30 24 18]
[ 84 69 54]
[138 114 90]]
Explanation:
Program 1:
- Demonstrates how to work with matrices in NumPy.
- Includes addition, subtraction, element-wise multiplication, and the dot product.
Program 2: Statistical Analysis Using NumPy
This program demonstrates statistical operations like mean, median, variance, and standard deviation.
import numpy as np
# Create an array of data
data = np.array([12, 15, 14, 10, 18, 20, 15, 17, 22, 19])
# Calculate Mean
mean = np.mean(data)
# Calculate Median
median = np.median(data)
# Calculate Variance
variance = np.var(data)
# Calculate Standard Deviation
std_dev = np.std(data)
# Print results
print("Data:", data)
print("Mean:", mean)
print("Median:", median)
print("Variance:", variance)
print("Standard Deviation:", std_dev)
Output:
Data: [12 15 14 10 18 20 15 17 22 19]
Mean: 16.2
Median: 16.0
Variance: 11.76
Standard Deviation: 3.43
Explanation:
-
Program 2:
- Shows NumPy's built-in functions for statistical analysis.
- Computes mean, median, variance, and standard deviation for a dataset.
Program 3: Generate and Sort Random Numbers
This program generates a random array and sorts it in ascending order.
import numpy as np
# Generate a random array of 10 elements
random_numbers = np.random.randint(1, 100, size=10)
print("Original Array:", random_numbers)
# Sort the array
sorted_array = np.sort(random_numbers)
print("Sorted Array:", sorted_array)
Sample Output:
Original Array: [23 67 12 89 45 56 34 10 78 90]
Sorted Array: [10 12 23 34 45 56 67 78 89 90]
Program 4: Reshape a 1D Array into a 2D Matrix
This program reshapes a 1D array into a 2D matrix.
import numpy as np
# Create a 1D array with 12 elements
arr = np.arange(1, 13) # Numbers from 1 to 12
print("Original 1D Array:", arr)
# Reshape into a 3x4 matrix
matrix = arr.reshape(3, 4)
print("\nReshaped 3x4 Matrix:\n", matrix)
Output:
Original 1D Array: [ 1 2 3 4 5 6 7 8 9 10 11 12]
Reshaped 3x4 Matrix:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
Program 5: Element-Wise Comparison of Two Arrays
This program compares two arrays element-wise and returns a Boolean result.
import numpy as np
# Define two arrays
arr1 = np.array([10, 20, 30, 40, 50])
arr2 = np.array([10, 25, 30, 45, 50])
# Element-wise comparison
comparison = arr1 == arr2
print("Array 1:", arr1)
print("Array 2:", arr2)
print("Comparison (Equal):", comparison)
# Check if all elements are equal
all_equal = np.all(arr1 == arr2)
print("Are all elements equal?:", all_equal)
Output:
Array 1: [10 20 30 40 50]
Array 2: [10 25 30 45 50]
Comparison (Equal): [ True False True False True]
Are all elements equal?: False
Program 6: Sum of Diagonal Elements of a Matrix
This program calculates the sum of diagonal elements in a square matrix.
import numpy as np
# Create a 3x3 matrix
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print("Matrix:\n", matrix)
# Sum of the diagonal elements
diagonal_sum = np.trace(matrix)
print("Sum of Diagonal Elements:", diagonal_sum)
Output:
Matrix:
[[1 2 3]
[4 5 6]
[7 8 9]]
Sum of Diagonal Elements: 15
Program 7: Replace Negative Values with Zero
This program replaces all negative values in an array with zeros.
import numpy as np
# Create an array with both positive and negative values
arr = np.array([10, -5, 20, -10, 30, -2, 40])
print("Original Array:", arr)
# Replace negative values with zero
arr[arr < 0] = 0
print("Modified Array:", arr)
Output:
Original Array: [ 10 -5 20 -10 30 -2 40]
Modified Array: [10 0 20 0 30 0 40]
Summary of Programs:
- Random Number Generation & Sorting: Generate random numbers and sort them.
- Array Reshaping: Convert a 1D array into a 2D matrix.
- Element-Wise Comparison: Compare two arrays and find matching elements.
- Sum of Diagonal Elements: Use
np.trace()
to sum diagonal elements of a matrix. - Replace Negative Values: Use conditional masking to replace negatives with zeros.
Comments
Post a Comment