ML Lab
ML Lab
import numpy as np
def calculate_std_dev(car_speeds):
# Convert the list of speeds to a numpy array
speeds_array = np.array(car_speeds)
# Calculate the standard deviation
std_dev = np.std(speeds_array)
return std_dev
car_speeds = [60, 62, 61, 65, 63, 64, 60, 59]
std_deviation = calculate_std_dev(car_speeds)
print("OUTPUT")
print(f"The standard deviation of car speeds is: {std_deviation:.2f} units")
OUTPUT:
The standard deviation of car speeds is: 1.98 units
2. Find the percentage of Marks of Students
OUTPUT:
Enter Name of the Student Anu
Enter the first mark 100
Enter the Second mark 98
Enter the Third mark 99
OUTPUT
import numpy as np
import matplotlib.pyplot as plt
# Generate data following a normal distribution
mu = 0 # mean
sigma = 1 # standard deviation
num_samples = 1000
data = np.random.normal(mu, sigma, num_samples)
# Plot histogram
plt.figure(figsize=(8, 6))
plt.hist(data, bins=30, density=True, alpha=0.6, color='g')
# Plot the theoretical probability density function (PDF) for comparison
xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 100)
p = np.exp(-0.5 * ((x - mu) / sigma)**2) / (sigma * np.sqrt(2 * np.pi))
plt.plot(x, p, 'k', linewidth=2)
plt.title('Histogram of a Normal Distribution')
plt.xlabel('Value')
plt.ylabel('Density')
plt.legend(['Normal Distribution PDF', 'Histogram'])
plt.grid(True)
plt.show()
OUTPUT
Program Explanation
numpy is imported as np for numerical operations.
matplotlib.pyplot is imported as plt for plotting.
mu is the mean of the normal distribution, set to 0.
sigma is the standard deviation of the normal distribution, set to 1.
num_samples is the number of random samples to generate, set to 1000.
data is an array of 1000 random samples drawn from a normal distribution
with mean 0 and standard deviation 1.
plt.figure(figsize=(8, 6)) sets the size of the figure to 8x6 inches.
plt.hist(data, bins=30, density=True, alpha=0.6, color='g') creates a
histogram of data with:
● plt.plot(x, p, 'k', linewidth=2): plots the PDF as a black line ('k') with a
line width of 2.
4. Draw the Scatter plot
import numpy as np
import matplotlib.pyplot as plt
# Generate random data
np.random.seed(0)
x = np.random.randn(100) # random x values
y = np.random.randn(100) # random y values
# Create scatter plot
plt.figure(figsize=(8, 6))
plt.scatter(x, y, color='b', alpha=0.6) # 'b' for blue, alpha for transparency
# Customize plot labels and title
plt.title('Scatter Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# Display grid
plt.grid(True)
# Show plot
plt.show()
OUTPUT
Program Explanation
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Polynomial Regression')
plt.legend()
plt.grid(True)
plt.show()
OUTPUT
6. Find the program to generate Decision tree
import numpy as np
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
OUTPUT