0% found this document useful (0 votes)
6 views4 pages

Session - 2 Numpy

Numpy matrix programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Session - 2 Numpy

Numpy matrix programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SESSION-II: DEVELOP PYTHON PROGRAMS USING NUMPY MODULE

import numpy as np

# Generate random integers between 1 and 100

random_integers = [Link](1, 101, size=5)

print("Random Integers:", random_integers)

# Generate a random 2x2 matrix

random_matrix = [Link](2, 2)

print("\nRandom Matrix:")

print(random_matrix)

import numpy as np

# Create a 1D NumPy array

arr1d = [Link]([1, 2, 3, 4, 5])

print("1D Array:")

print(arr1d)

# Create a 2D NumPy array

arr2d = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

print("\n2D Array:")

print(arr2d)

# Create a random NumPy array

random_arr = [Link](3, 3)

print("\nRandom 3x3 Array:")

print(random_arr)
import numpy as np

# Element-wise operations

arr1 = [Link]([1, 2, 3])

arr2 = [Link]([4, 5, 6])

result_add = arr1 + arr2

result_sub = arr1 - arr2

result_mul = arr1 * arr2

result_div = arr1 / arr2

print("Addition:", result_add)

print("Subtraction:", result_sub)

print("Multiplication:", result_mul)

print("Division:", result_div)

# Dot product

dot_product = [Link](arr1, arr2)

print("\nDot Product:", dot_product)

import numpy as np

arr = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Slicing

slice1 = arr[0:2, 1:3] # Rows 0 and 1, Columns 1 and 2

print("Sliced Array:")

print(slice1)
import numpy as np

arr = [Link]([1, 2, 3, 4, 5])

# Mean, Median, and Standard Deviation

mean_value = [Link](arr)

median_value = [Link](arr)

std_deviation = [Link](arr)

print("Mean:", mean_value)

print("Median:", median_value)

print("Standard Deviation:", std_deviation)

import numpy as np

# Create an array of random numbers

data = [Link](1000)

# Calculate mean and standard deviation

mean = [Link](data)

std_deviation = [Link](data)

print("Mean:", mean)

print("Standard Deviation:", std_deviation)

import numpy as np

arr = [Link]([1, 2, 3, 4, 5])

# Calculate the sum, product, and cumulative sum

sum_value = [Link](arr)
product_value = [Link](arr)

cumulative_sum = [Link](arr)

print("Sum:", sum_value)

print("Product:", product_value)

print("Cumulative Sum:", cumulative_sum)

import numpy as np

# Coefficient matrix A and constant vector B

A = [Link]([[2, 1], [1, 3]])

B = [Link]([8, 9])

# Solve the linear equation Ax = B

solution = [Link](A, B)

print("Solution:", solution)

You might also like