CHARAN’S DEGREE COLLEGE
(Affiliated to Bengaluru City University)
No 326, Ist Main Road, Cambridge Layout, Halasuru, BENGALURU-560 008
www.charans.in Tel: 080 25574656
MACHINE LEARNING PROGRAMS
1) INSTALL AND SET UP PYTHON AND ESSENTIAL LIBRARIES LIKE NUMPY
AND PANDAS.
i) Download Python:-
Go to the official Python website download the latest version suitable for your operating
system (Windows, macOS, or Linux).
ii) Install Python:-
For Windows: Download python software and install it.
iii) Verify Installation:-
• Open Jupyter Notebook
• Run python –version
a) Python version:
import sys
print(sys.version)
b) Pip upgrade:
Using pip:
• Pip is Python's package manager.
• It usually comes installed with Python.
• Open a terminal/command prompt.
Py –m install pip –upgrade pip
JACOB J
ASSOCIATE PROFESSOR
CHARAN’S DEGREE COLLEGE
(Affiliated to Bengaluru City University)
No 326, Ist Main Road, Cambridge Layout, Halasuru, BENGALURU-560 008
www.charans.in Tel: 080 25574656
c) Installing packages :
NumPy :
pip install numpy
Press run. This command will download and install NumPy.
Pandas:
pip install pandas
Press run.This command will download and install pandas.
Matplotlib:
pip install matplotlib
Press run.This command will download and install matplotlib.
Seaborn:
pip install seaborn
Press run. This command will download and install seaborn.
d) Verify installations:
For Numpy:
import numpy as np
print(np.__version__)
JACOB J
ASSOCIATE PROFESSOR
CHARAN’S DEGREE COLLEGE
(Affiliated to Bengaluru City University)
No 326, Ist Main Road, Cambridge Layout, Halasuru, BENGALURU-560 008
www.charans.in Tel: 080 25574656
For Pandas:
import pandas as pd
print(pd.__version__)
For Matplotlib:
For Seaborn:
JACOB J
ASSOCIATE PROFESSOR
CHARAN’S DEGREE COLLEGE
(Affiliated to Bengaluru City University)
No 326, Ist Main Road, Cambridge Layout, Halasuru, BENGALURU-560 008
www.charans.in Tel: 080 25574656
2) INTRODUCE SCIKIT-LEARN AS A MACHINE LEARNING LIBRARY.
Scikit-learn (Sklearn) is the most useful and robust library for machine learning in
Python.
It provides a selection of efficient tools for machine learning and statistical modeling
including classification, regression, clustering and dimensionality reduction via a
consistence interface in Python.
This library, which is largely written in Python, is built upon NumPy, pandas,SciPy and
Matplotlib.
Installation
If you already installed NumPy and Scipy, the following are the two easiest ways to install
scikit-learn --
Using pip
The following command can be used to install scikit-learn via pip
pip install scikit-learn
Features
Rather than focusing on loading, manipulating and summarising data,
Scikit-learn library is focused on modeling the data. Some of the most popular groups of
models provided by Sklearn are as follows
Supervised Learning algorithms − Almost all the popular supervised learning algorithms, like
Linear Regression, Support Vector Machine (SVM), Decision Tree etc., are the part of scikit-
learn.
Unsupervised Learning algorithms − On the other hand, it also has all the popular
unsupervised learning algorithms from clustering, factor analysis, PCA (Principal Component
Analysis) to unsupervised neural networks.
Clustering − This model is used for grouping unlabeled data.
Cross Validation − It is used to check the accuracy of supervised models on unseen data.
JACOB J
ASSOCIATE PROFESSOR
CHARAN’S DEGREE COLLEGE
(Affiliated to Bengaluru City University)
No 326, Ist Main Road, Cambridge Layout, Halasuru, BENGALURU-560 008
www.charans.in Tel: 080 25574656
3) INSTALL AND SET UP SCIKIT-LEARN AND OTHER NECESSARY TOOLS.
Scikit-learn (Sklearn) is the most useful and robust library for machine learning in
Python.
It provides a selection of efficient tools for machine learning and statistical modeling
including classification, regression, clustering and dimensionality reduction via a
consistence interface in Python.
This library, which is largely written in Python, is built upon NumPy, pandas, SciPy and
Matplotlib.
Pip upgrade:
Using pip:
• Pip is Python's package manager.
• It usually comes installed with Python.
Open Jupyter notebook
Type pip install scikit-learn numpy scipy matplotlib pandas
Run. This command will download and install scikit-learn and other packages.
JACOB J
ASSOCIATE PROFESSOR
CHARAN’S DEGREE COLLEGE
(Affiliated to Bengaluru City University)
No 326, Ist Main Road, Cambridge Layout, Halasuru, BENGALURU-560 008
www.charans.in Tel: 080 25574656
Verify installations:
Open a Juypter notebook and enter the below command.
PROGRAM:
import numpy
import pandas
import scipy
import sklearn
import matplotlib
print("Numpy version: ")
print(numpy.__version__)
print("Numpy library is successfully installed ")
print("Pandas version: ")
print(pandas.__version__)
print("Pandas library is successfully installed ")
print("scipy version: ")
print(scipy.__version__)
print("scipy library is successfully installed ")
print("sklearn version: ")
print(sklearn.__version__)
print("sklearn library is successfully installed ")
print("matplotlib version: ")
print(matplotlib.__version__)
print("matplotlib library is successfully installed ")
JACOB J
ASSOCIATE PROFESSOR
CHARAN’S DEGREE COLLEGE
(Affiliated to Bengaluru City University)
No 326, Ist Main Road, Cambridge Layout, Halasuru, BENGALURU-560 008
www.charans.in Tel: 080 25574656
OUTPUT:
JACOB J
ASSOCIATE PROFESSOR
CHARAN’S DEGREE COLLEGE
(Affiliated to Bengaluru City University)
No 326, Ist Main Road, Cambridge Layout, Halasuru, BENGALURU-560 008
www.charans.in Tel: 080 25574656
4) WRITE A PROGRAM TO LOAD AND EXPLORE THE DATASET OF .CVS AND
EXCEL FILES USING PANDAS.
PROGRAM:
import pandas as pd
data=pd.read_csv("Mtest.csv")
data
OUTPUT:
JACOB J
ASSOCIATE PROFESSOR
CHARAN’S DEGREE COLLEGE
(Affiliated to Bengaluru City University)
No 326, Ist Main Road, Cambridge Layout, Halasuru, BENGALURU-560 008
www.charans.in Tel: 080 25574656
5) WRITE A PROGRAM TO VISUALIZE THE DATASET TO GAIN INSIGHTS
USING MATPLOTLIB OR SEABORN BY PLOTTING SCATTER PLOTS, BAR
CHARTS.
PROGRAM:
import matplotlib.pyplot as plt
import pandas as pd
plt.style.use('bmh')
df = pd.read_csv('Mtest.csv')
x = df['Model']
y = df['Price']
plt.xlabel('Model', fontsize=16)
plt.ylabel('price($)', fontsize=14)
plt.scatter(x,y)
plt.bar(x,y)
plt.plot(x, y)
plt.show()
OUTPUT:
JACOB J
ASSOCIATE PROFESSOR
CHARAN’S DEGREE COLLEGE
(Affiliated to Bengaluru City University)
No 326, Ist Main Road, Cambridge Layout, Halasuru, BENGALURU-560 008
www.charans.in Tel: 080 25574656
6) WRITE A PROGRAM TO IMPLEMENT A K-NEAREST NEIGHBOURS (K-NN)
CLASSIFIER USING SCIKITLEARN AND TRAIN THE CLASSIFIER ON THE
DATASET AND EVALUATE ITS PERFORMANCE.
PROGRAM:
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, classification_report
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data # Features
y = iris.target # Target variable
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
k = 3 # Define the value of k
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy of k-NN classifier on test set: {accuracy:.2f}')
print('\nClassification Report:')
print(classification_report(y_test, y_pred, target_names=iris.target_names))
JACOB J
ASSOCIATE PROFESSOR
CHARAN’S DEGREE COLLEGE
(Affiliated to Bengaluru City University)
No 326, Ist Main Road, Cambridge Layout, Halasuru, BENGALURU-560 008
www.charans.in Tel: 080 25574656
OUTPUT:
JACOB J
ASSOCIATE PROFESSOR
CHARAN’S DEGREE COLLEGE
(Affiliated to Bengaluru City University)
No 326, Ist Main Road, Cambridge Layout, Halasuru, BENGALURU-560 008
www.charans.in Tel: 080 25574656
7) WRITE A PROGRAM TO IMPLEMENT A LINEAR REGRESSION MODEL FOR
REGRESSION TASKS AND TRAIN THE MODEL ON A DATASET WITH
CONTINUOUS TARGET VARIABLES.
PROGRAM:
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error,r2_score
import matplotlib.pyplot as plt
X,y = make_regression(n_samples=1000,n_features=1,noise=20,random_state=42)
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=42)
Linear_reg=LinearRegression()
Linear_reg.fit(X_train,y_train)
predictions=Linear_reg.predict(X_test)
mse=mean_squared_error(y_test,predictions)
r2=r2_score(y_test,predictions)
print("Mean Squared Error (MSE) :\n",mse)
print("R-squared:\n",r2)
plt.scatter(X_test,y_test,color='blue')
plt.plot(X_test,predictions,color='red',linewidth=3)
plt.xlabel('X')
plt.ylabel('y')
plt.title('Linear Regression')
plt.show()
JACOB J
ASSOCIATE PROFESSOR
CHARAN’S DEGREE COLLEGE
(Affiliated to Bengaluru City University)
No 326, Ist Main Road, Cambridge Layout, Halasuru, BENGALURU-560 008
www.charans.in Tel: 080 25574656
OUTPUT:
JACOB J
ASSOCIATE PROFESSOR
CHARAN’S DEGREE COLLEGE
(Affiliated to Bengaluru City University)
No 326, Ist Main Road, Cambridge Layout, Halasuru, BENGALURU-560 008
www.charans.in Tel: 080 25574656
8) WRITE A PROGRAM TO IMPLEMENT A DECISION TREE CLASSIFIER USING
SCIKIT LEARN AND VISUALIZE THE DECISION TREE AND UNDERSTAND ITS
SPLITS.
PROGRAM:
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, plot_tree
import matplotlib.pyplot as plt
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.2, random_state=42)
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, y_train)
plt.figure(figsize=(12, 8))
plot_tree(decision_tree, feature_names=iris.feature_names, class_names=iris.target_names,
filled=True)
plt.show()
JACOB J
ASSOCIATE PROFESSOR
CHARAN’S DEGREE COLLEGE
(Affiliated to Bengaluru City University)
No 326, Ist Main Road, Cambridge Layout, Halasuru, BENGALURU-560 008
www.charans.in Tel: 080 25574656
OUTPUT:
JACOB J
ASSOCIATE PROFESSOR
CHARAN’S DEGREE COLLEGE
(Affiliated to Bengaluru City University)
No 326, Ist Main Road, Cambridge Layout, Halasuru, BENGALURU-560 008
www.charans.in Tel: 080 25574656
9) WRITE A PROGRAM TO IMPLEMENT K-MEANS CLUSTERING AND
VISUALIZE CLUSTERS.
PROGRAM:
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
X,y=make_blobs (n_samples=300,centers=4,cluster_std=1.0,random_state=42)
kmeans=KMeans(n_clusters=4)
kmeans.fit(X)
cluster_labels=kmeans.predict(X)
plt.figure(figsize=(7,5))
plt.scatter(X[:,0],X[:,1],c=cluster_labels,cmap='viridis',edgecolors='k')
plt.scatter(kmeans.cluster_centers_[:,0],kmeans.cluster_centers_[:,1],marker='o',s=200,color='r
ed',label='Centroids')
plt.title('K-Means Clustering')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.show()
JACOB J
ASSOCIATE PROFESSOR
CHARAN’S DEGREE COLLEGE
(Affiliated to Bengaluru City University)
No 326, Ist Main Road, Cambridge Layout, Halasuru, BENGALURU-560 008
www.charans.in Tel: 080 25574656
OUTPUT:
JACOB J
ASSOCIATE PROFESSOR
CHARAN’S DEGREE COLLEGE
(Affiliated to Bengaluru City University)
No 326, Ist Main Road, Cambridge Layout, Halasuru, BENGALURU-560 008
www.charans.in Tel: 080 25574656
10) WRITE A PROGRAM TO HANDLE MISSING DATA, ENCODE CATEGORICAL
VARIABLES, AND PERFORM FEATURE SCALING.
JACOB J
ASSOCIATE PROFESSOR