0% found this document useful (0 votes)
7 views12 pages

ML Lab

The document contains multiple Python code snippets demonstrating various data analysis and visualization techniques using libraries like NumPy and Matplotlib. It includes calculating standard deviation of car speeds, finding student marks percentage, generating histograms for normal distributions, creating scatter plots, performing polynomial regression, and generating decision trees. Each section provides code along with explanations and sample outputs.

Uploaded by

Kasthuri L
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)
7 views12 pages

ML Lab

The document contains multiple Python code snippets demonstrating various data analysis and visualization techniques using libraries like NumPy and Matplotlib. It includes calculating standard deviation of car speeds, finding student marks percentage, generating histograms for normal distributions, creating scatter plots, performing polynomial regression, and generating decision trees. Each section provides code along with explanations and sample outputs.

Uploaded by

Kasthuri L
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
You are on page 1/ 12

1.

Find the Standard Deviation foe speed of a cars using numpy

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

name= input("Enter Name of the Student ")


mark1=int(input("Enter the first mark "))
mark2=int(input("Enter the Second mark "))
mark3=int(input("Enter the Third mark "))
# Calculate overall percentage
overall_percentage = (mark1 + mark2 + mark3) / 3
print("OUTPUT\n")
print(f"Name of Student: {name}")
print(f"Overall Percentage: {overall_percentage:.2f}%")

OUTPUT:
Enter Name of the Student Anu
Enter the first mark 100
Enter the Second mark 98
Enter the Third mark 99
OUTPUT

Name of Student: Anu


Overall Percentage: 99.00%
3. Find the Histogram for Normal Distribution

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:

● bins=30: 30 bins for the histogram.


● density=True: normalizes the histogram so that the area under the
histogram sums to 1.
● alpha=0.6: sets the transparency of the histogram bars to 60%.
● color='g': sets the color of the histogram bars to green.

 xmin, xmax = plt.xlim(): gets the current x-axis limits.


 x = np.linspace(xmin, xmax, 100): generates 100 evenly spaced points
between xmin and xmax.
 p = np.exp(-0.5 * ((x - mu) / sigma)**2) / (sigma * np.sqrt(2 * np.pi)):
calculates the values of the normal distribution PDF at each point in x using the
formula: f(x)=1σ2πexp⁡(−(x−μ)22σ2)f(x) = \frac{1}{\sigma \sqrt{2\pi}} \exp\
left(-\frac{(x - \mu)^2}{2\sigma^2}\right)f(x)=σ2π1exp(−2σ2(x−μ)2)

●  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

 numpy is imported as np for numerical operations.


 matplotlib.pyplot is imported as plt for plotting.
 np.random.seed(0) sets the seed for the random number generator to 0 to
ensure reproducibility of the random values.
 x = np.random.randn(100) generates 100 random values from a standard
normal distribution for the x-axis.
 y = np.random.randn(100) generates 100 random values from a standard
normal distribution for the y-axis.
 plt.figure(figsize=(8, 6)) sets the size of the figure to 8x6 inches.
 plt.scatter(x, y, color='b', alpha=0.6) creates a scatter plot of the x and y
values with:

● color='b': sets the color of the points to blue.


● alpha=0.6: sets the transparency of the points to 60%.
●  plt.title('Scatter Plot'): sets the title of the plot to 'Scatter Plot'.
●  plt.xlabel('X-axis'): labels the x-axis as 'X-axis'.
●  plt.ylabel('Y-axis'): labels the y-axis as 'Y-axis'.
 plt.grid(True): adds a grid to the plot for better readability.
●  plt.show(): displays the plot.

5. Find the Polynomial Regression


import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
# Generate some synthetic data
np.random.seed(0)
x = 2 - 3 * np.random.normal(0, 1, 20) # Random x values
y = x - 2 * (x ** 2) + 0.5 * (x ** 3) + np.random.normal(-3, 3, 20)
# Polynomial relationship with noise
# Transform the data to include another axis to fit the polynomial model
x = x[:, np.newaxis]
y = y[:, np.newaxis]
# Polynomial regression transformation
poly_features = PolynomialFeatures(degree=3)
x_poly = poly_features.fit_transform(x)
# Fit linear regression model
model = LinearRegression()
model.fit(x_poly, y)
# Predictions
y_poly_pred = model.predict(x_poly)
# Sort the values of x before plotting
sort_axis = np.argsort(x.flatten())
x_sort = x[sort_axis]
y_poly_pred_sort = y_poly_pred[sort_axis]
# Plotting the actual points as scatter plot
plt.figure(figsize=(6, 4))
plt.scatter(x, y, s=30, color='blue', label='Data Points')
# Plotting the predicted line
plt.plot(x_sort, y_poly_pred_sort, linewidth=2, color='red', label='Polynomial
Fit')

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

# Generate a synthetic dataset


# Let's create a dataset with 2 features and 3 classes
np.random.seed(0)
X = np.random.rand(5, 2) # 5 samples, 2 features
y = np.random.randint(0, 2, 5) # 5 samples, 3 classes (0, 1)

# Split the dataset into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=0)

# Train a decision tree model


clf = DecisionTreeClassifier(random_state=0)
clf.fit(X_train, y_train)

# Predict on the test set and calculate accuracy


y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")

# Visualize the decision tree


plt.figure(figsize=(8, 8))
plot_tree(clf, filled=True, feature_names=["Feature 1", "Feature 2"],
class_names=["Class 0", "Class 1"])
plt.title('Decision Tree Visualization')
plt.show()

OUTPUT

You might also like