ML EXPT 2
# Using the NumPy library
import numpy as np # numpy
# Creating two vectors (1D arrays)
vector1 = np.array([1, 2, 3])
vector2 = np.array([4, 5, 6])
# Display the vectors
print("Vector 1:", vector1)
print("Vector 2:", vector2)
# Vector addition
add_result = vector1 + vector2
print("Addition:", add_result)
# Scalar multiplication
scalar_mult = 3 * vector1
print("Scalar Multiplication (3 * vector1):", scalar_mult)
# Dot product
dot_product = np.dot(vector1, vector2)
print("Dot Product:", dot_product)
# Mean and standard deviation of vector1
mean_v1 = np.mean(vector1)
std_v1 = np.std(vector1)
print("Mean of vector1:", mean_v1)
print("Standard Deviation of vector1:", std_v1)
OUTPUT
Vector 1: [1 2 3]
Vector 2: [4 5 6]
Addition: [5 7 9]
Scalar Multiplication (3 * vector1): [3 6 9]
Dot Product: 32
Mean of vector1: 2.0
Standard Deviation of vector1: 0.816496580927726
2]
# Using The Scipy Library
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm # scipy
# Generate data for a normal distribution
x = np.linspace(-5, 5, 100)
y = norm.pdf(x, loc=0, scale=1) # mean=0, std=1
# Plot the normal distribution
plt.plot(x, y, label='Normal Distribution')
plt.title("SciPy Normal Distribution")
plt.xlabel("x")
plt.ylabel("Probability Density")
plt.legend()
plt.grid(True)
plt.show()
3]
# Using The Sklearn Library
from sklearn.tree import DecisionTreeClassifier # scikit-learn
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import pandas as pd
# Load dataset
X, y = load_iris(return_X_y=True)
names = load_iris().target_names
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=42)
# Model training
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
# Prediction
y_pred = clf.predict(X_test)
# Show 5 rows of results
df = pd.DataFrame(X_test[:5], columns=load_iris().feature_names)
df["True Label"] = [names[i] for i in y_test[:5]]
df["Predicted"] = [names[i] for i in y_pred[:5]]
print(df)
# Accuracy
print("\nAccuracy:", accuracy_score(y_test, y_pred))
OUTPUT:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) \
0 6.1 2.8 4.7 1.2
1 5.7 3.8 1.7 0.3
2 7.7 2.6 6.9 2.3
3 6.0 2.9 4.5 1.5
4 6.8 2.8 4.8 1.4
True Label Predicted
0 versicolor versicolor
1 setosa setosa
2 virginica virginica
3 versicolor versicolor
4 versicolor versicolor
Accuracy: 1.0
4]
import pandas as pd # pandas
# Create a smaller DataFrame
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
# Display the DataFrame
print("DataFrame:\n", df)
# Filter example: People older than 25
print("\nPeople older than 25:\n", df[df['Age'] > 25])
OUTPUT
DataFrame:
Name Age
0 Alice 25
1 Bob 30
People older than 25:
Name Age
1 Bob 30
5]
import matplotlib.pyplot as plt # matplotlib
# Data for plotting
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
# Plot the data
plt.plot(x, y)
# Add titles and labels
plt.title('Simple Line Plot')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
# Show the plot
plt.show()
OUTPUT