Module–1: Python Libraries for AI & ML
Course: YCS8101 – Stream Laboratory 2 (AI & ML)
Q1 Create a NumPy array of 10 random integers between 1 and 100. Find its mean,
minimum, and maximum.
Answer:
import numpy as np
arr = [Link](1, 100, 10)
print(arr)
print([Link](arr), [Link](arr), [Link](arr))
Q2 Reshape the above array into a 2×5 matrix and display its shape.
Answer:
reshaped = [Link](2,5)
print(reshaped)
print([Link])
Q3 Generate a sine wave signal and apply FFT.
Answer:
import numpy as np
import [Link] as plt
t = [Link](0,1,500)
signal = [Link](2*[Link]*5*t)
fft_val = [Link](signal)
freq = [Link](len(fft_val))
[Link](freq, [Link](fft_val))
[Link]()
Q4 Solve the following system of linear equations using SciPy linear algebra module:
3𝑥 + 𝑦 = 9
𝑥 + 2𝑦 = 8
Answer:
from scipy import linalg, stats, optimize
from [Link] import csr_matrix
import numpy as np
A = [Link]([[3,1],[1,2]])
B = [Link]([9,8])
print([Link](A,B))
Q5 Find the mean and variance of a dataset using SciPy.
Answer:
from scipy import stats
data = [10, 20, 30, 40, 50]
print("Mean:", [Link](data))
print("Variance:", [Link](data))
Q6. Perform optimization to find the minimum of the function
f(x) = x² + 4x + 4.
Answer:
from scipy import optimize
f = lambda x: x**2 + 4*x + 4
result = [Link](f, x0=0)
print("Minimum at:", result.x)
Q7. Create and display a sparse matrix using SciPy.
Answer:
from [Link] import csr_matrix
sparse_mat = csr_matrix([[0, 0, 3],
[4, 0, 0],
[0, 5, 0]])
print(sparse_mat.toarray())
Q8. Plot a scatter plot for two datasets using Matplotlib.
Answer:
import [Link] as plt
x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
[Link](x, y)
[Link]("X-axis")
[Link]("Y-axis")
[Link]("Scatter Plot")
[Link]()
Q.9 Plot a histogram for a given dataset.
Answer:
data = [10, 20, 20, 30, 30, 30, 40]
[Link](data)
[Link]("Values")
[Link]("Frequency")
[Link]("Histogram")
[Link]()
Q10. Display a grayscale image using Matplotlib.
Answer:
import numpy as np
image = [Link](10, 10)
[Link](image, cmap='gray')
[Link]("Image Plot")
[Link]()
[Link]()
Q11. Demonstrate basic tensor operations using PyTorch and TensorFlow, and create a
simple model using Keras.
Answer:
PyTorch:
import torch
a = [Link]([1.0, 2.0, 3.0])
b = [Link]([4.0, 5.0, 6.0])
print(a + b)
TensorFlow:
import tensorflow as tf
x = [Link]([1, 2, 3])
y = [Link]([4, 5, 6])
print(x + y)
Keras:
from [Link] import Sequential
from [Link] import Dense
model = Sequential([
Dense(8, activation='relu', input_shape=(4,)),
Dense(1)
])
[Link]()