Tribhuvan University
Faculty of Humanities and Social Sciences
Machine Learning
A
LAB REPORT
Submitted to
Department of Computer Application
Shahid Smarak College
In partial fulfillment of the requirements for the Bachelors in Computer Application
Submitted by:
Saroj Maharjan
Instructor External Examiner
Rajesh Shahi Thakuri
Table of Contents
1. Explain some Machine learning Python libraries like pandas, Numpy, Matplotlib, Sklearn
etc. 1
2. Using Pandas and matplotlib libraries import and explore your dataset and use some statistic
functions on them and plot the different graphs (bar graphs, pie-chart). .......................................2
3. Write a python program to create a neuron and predict its output using the threshold
activation function.......................................................................................................................4
4. Implement Backpropagation algorithm to train an ANN of configuration 3 inputs node 2
hidden nodes 1 output nodes. And it takes inputs (1, 0, 1) and output will be 1. ...........................5
5. Write and implement Linear Regression using Sklearn library in python. .............................6
6. Implement backpropagation algorithm .................................................................................7
7. Write and implement Logistic Regression using Sklearn library in python. ..........................9
8. Write and implement SVM using Sklearn library in python. ............................................... 10
9. Write a program to implement k-Nearest Neighbour algorithm to classify the iris data
set…… ..................................................................................................................................... 11
10. Write and implement k-means Clustering Using Sklearn Library in python. ................... 12
1. Explain some Machine learning Python libraries like pandas, Numpy,
Matplotlib, Sklearn etc.
a) NumPy (Numerical Python)
i. Purpose: Efficient numerical computations and array operations.
ii. Features:
Provides multi-dimensional arrays (ndarray) and matrices.
Supports mathematical functions like mean, median, standard deviation, etc.
Efficient element-wise operations, broadcasting, and linear algebra.
iii. Example Use: Storing and manipulating large datasets, performing vectorized operations.
b) Pandas
i. Purpose: Data manipulation and analysis.
ii. Features:
Provides DataFrame and Series for structured data.
Functions for reading/writing CSV, Excel, SQL, and JSON files.
iii. Example Use: Loading a dataset, preprocessing, and exploring data before training ML models.
c) Matplotlib
i. Purpose: Data visualization.
ii. Features:
Plot graphs like line, bar, histogram, scatter, and pie charts.
Customize plots with labels, legends, colors, and styles.
iii. Example Use: Visualizing data distribution, trends, and ML model results.
d) Scikit-learn (sklearn)
i. Purpose: Machine Learning and data mining.
ii. Features:
Provides tools for classification, regression, clustering, and dimensionality reduction.
Includes preprocessing (scaling, encoding), model selection, and evaluation metrics.
iii. Example Use: Building ML models like decision trees, linear regression, k-means
clustering, etc.
1
2. Using Pandas and matplotlib libraries import and explore your dataset and
use some statistic functions on them and plot the different graphs (bar
graphs, pie-chart).
Program
import pandas as pd
import matplotlib.pyplot as plt
data = {
'Category': ['Electronics', 'Clothing', 'Groceries', 'Books', 'Toys'],
'Sales': [2500, 1500, 3000, 1200, 800],
'Profit': [500, 200, 600, 150, 100]
}
df = pd.DataFrame(data)
print("First 5 rows:\n", df.head())
print("\nDataset Info:\n", df.info())
print("\nStatistical Summary:\n", df.describe())
total_sales = df['Sales'].sum()
average_profit = df['Profit'].mean()
max_sales_category = df.loc[df['Sales'].idxmax(), 'Category']
print(f"\nTotal Sales: {total_sales}")
print(f"Average Profit: {average_profit}")
print(f"Category with Max Sales: {max_sales_category}")
plt.figure(figsize=(8,5))
plt.bar(df['Category'], df['Sales'], color='skyblue')
plt.title('Sales by Category')
2
plt.xlabel('Category')
plt.ylabel('Sales')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
plt.figure(figsize=(6,6))
plt.pie(
df['Profit'],
labels=df['Category'],
autopct='%1.1f%%',
startangle=140,
colors=plt.cm.Paired.colors
)
plt.title('Profit Distribution by Category')
plt.tight_layout()
plt.show()
Output
3
3. Write a python program to create a neuron and predict its output using the
threshold activation function.
Program
def neuron(inputs, weights, bias):
total = sum(i*w for i, w in zip(inputs, weights)) + bias
if total >= 0:
return 1
else:
return 0
inputs = [1, 0, 1]
weights = [0.5, -0.6, 0.8]
bias = -0.1
output = neuron(inputs, weights, bias)
print(f"Predicted output: {output}")
Output
4
4. Implement Backpropagation algorithm to train an ANN of configuration 3
inputs node 2 hidden nodes 1 output nodes. And it takes inputs (1, 0, 1) and
output will be 1.
Program
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (1 - x)
X = np.array([[1, 0, 1]])
y = np.array([[1]])
np.random.seed(42)
weights_input_hidden = np.random.rand(3, 2)
weights_hidden_output = np.random.rand(2, 1)
bias_hidden = np.random.rand(1, 2)
bias_output = np.random.rand(1, 1)
lr = 0.5
for epoch in range(10000):
hidden_input = np.dot(X, weights_input_hidden) + bias_hidden
hidden_output = sigmoid(hidden_input)
final_input = np.dot(hidden_output, weights_hidden_output) + bias_output
final_output = sigmoid(final_input)
error = y - final_output
d_output = error * sigmoid_derivative(final_output)
d_hidden = d_output.dot(weights_hidden_output.T) * sigmoid_derivative(hidden_output)
weights_hidden_output += hidden_output.T.dot(d_output) * lr
5
weights_input_hidden += X.T.dot(d_hidden) * lr
bias_output += d_output * lr
bias_hidden += d_hidden * lr
print("Predicted output after training:", final_output)
Output
5. Write and implement Linear Regression using Sklearn library in python.
Program
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
X = np.array([1, 2, 3, 4, 5, 6]).reshape(-1, 1)
y = np.array([3, 4, 2, 5, 6, 7])
model = LinearRegression()
model.fit(X, y)
y_pred = model.predict(X)
print("Slope (Coefficient):", model.coef_[0])
print("Intercept:", model.intercept_)
plt.scatter(X, y, color='blue', label='Actual Data')
plt.plot(X, y_pred, color='red', label='Linear Regression Line')
plt.xlabel('X')
plt.ylabel('y')
plt.title('Linear Regression Example')
plt.legend()
6
plt.show()
Output
6. Implement backpropagation algorithm
Program
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (1 - x)
X = np.array([[0, 0, 1],
[1, 1, 1],
[1, 0, 1],
[0, 1, 1]])
y = np.array([[0],
[1],
[1],
[0]])
7
np.random.seed(42)
input_nodes = X.shape[1]
hidden_nodes = 2
output_nodes = 1
weights_input_hidden = np.random.rand(input_nodes, hidden_nodes)
weights_hidden_output = np.random.rand(hidden_nodes, output_nodes)
bias_hidden = np.random.rand(1, hidden_nodes)
bias_output = np.random.rand(1, output_nodes)
lr = 0.5
for epoch in range(10000):
hidden_input = np.dot(X, weights_input_hidden) + bias_hidden
hidden_output = sigmoid(hidden_input)
final_input = np.dot(hidden_output, weights_hidden_output) + bias_output
final_output = sigmoid(final_input)
error = y - final_output
d_output = error * sigmoid_derivative(final_output)
d_hidden = d_output.dot(weights_hidden_output.T) * sigmoid_derivative(hidden_output)
weights_hidden_output += hidden_output.T.dot(d_output) * lr
weights_input_hidden += X.T.dot(d_hidden) * lr
bias_output += np.sum(d_output, axis=0, keepdims=True) * lr
bias_hidden += np.sum(d_hidden, axis=0, keepdims=True) * lr
print("Predicted output after training:\n", final_output)
Output
8
7. Write and implement Logistic Regression using Sklearn library in python.
Program
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
X = np.array([[0.5], [1.5], [2.0], [3.5], [4.0], [5.5], [6.0], [7.0]])
y = np.array([0, 0, 0, 0, 1, 1, 1, 1])
model = LogisticRegression()
model.fit(X, y)
y_pred = model.predict(X)
y_prob = model.predict_proba(X)[:, 1]
print("Model coefficients:", model.coef_)
print("Model intercept:", model.intercept_)
print("Predicted classes:", y_pred)
print("Predicted probabilities:", y_prob)
plt.scatter(X, y, color='blue', label='Actual Data')
plt.plot(X, y_prob, color='red', label='Predicted Probability')
plt.xlabel('X')
plt.ylabel('Probability of class 1')
plt.title('Logistic Regression Example')
plt.legend()
plt.show()
Output
9
8. Write and implement SVM using Sklearn library in python.
Program
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
X = np.array([[1, 2], [2, 3], [3, 3], [5, 5], [6, 5], [7, 7]])
y = np.array([0, 0, 0, 1, 1, 1])
model = svm.SVC(kernel='linear', C=1.0)
model.fit(X, y)
y_pred = model.predict(X)
print("Predicted classes:", y_pred)
xx, yy = np.meshgrid(np.linspace(0, 8, 100), np.linspace(0, 8, 100))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.3, cmap=plt.cm.Paired)
plt.scatter(X[:, 0], X[:, 1], c=y, s=100, edgecolors='k', cmap=plt.cm.Paired)
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('SVM with Linear Kernel')
plt.show()
Output
10
9. Write a program to implement k-Nearest Neighbour algorithm to classify
the iris data set.
Program
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, classification_report
import numpy as np
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
k=3
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
11
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy*100:.2f}%")
print("\nClassification Report:\n", classification_report(y_test, y_pred,
target_names=iris.target_names))
sample = np.array([[5.1, 3.5, 1.4, 0.2]])
pred_class = iris.target_names[knn.predict(sample)[0]]
print(f"Predicted class for sample {sample[0]}: {pred_class}")
Output
10. Write and implement k-means Clustering Using Sklearn Library in
python.
Program
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
X, _ = make_blobs(n_samples=300, centers=3, cluster_std=0.7, random_state=42)
k=3
kmeans = KMeans(n_clusters=k, random_state=42)
kmeans.fit(X)
labels = kmeans.labels_
centroids = kmeans.cluster_centers_
print("Cluster labels for first 10 points:", labels[:10])
12
print("Cluster centroids:\n", centroids)
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', s=50, alpha=0.6)
plt.scatter(centroids[:, 0], centroids[:, 1], color='red', marker='X', s=200, label='Centroids')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('k-Means Clustering Example')
plt.legend()
plt.show()
Output
13