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

Machine Learning Practical Programs

ML file

Uploaded by

aliabdulbaqi313
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Machine Learning Practical Programs

ML file

Uploaded by

aliabdulbaqi313
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MACHINE LEARNING LAB PRACTICAL FILE

Program 1: Linear Regression using Scikit-Learn


from sklearn.linear_model import LinearRegression
import numpy as np

X = [Link]([[1], [2], [3], [4], [5]])


y = [Link]([2, 4, 6, 8, 10])

model = LinearRegression()
[Link](X, y)

pred = [Link]([[6]])
print("Predicted value for 6:", pred[0])
Output:
Predicted value for 6: 12.0

Program 2: Logistic Regression Classification


from sklearn.linear_model import LogisticRegression

X = [[1], [2], [3], [4]]


y = [0, 0, 1, 1]

model = LogisticRegression()
[Link](X, y)

print('Prediction for 2.5:', [Link]([[2.5]])[0])


Output:
Prediction for 2.5: 0

Program 3: K-Nearest Neighbors (KNN)


from [Link] import KNeighborsClassifier

X = [[1,2], [2,3], [3,4], [6,7]]


y = [0, 0, 1, 1]

model = KNeighborsClassifier(n_neighbors=3)
[Link](X, y)

print('Class for [4,5]:', [Link]([[4,5]])[0])


Output:
Class for [4,5]: 1

Program 4: Decision Tree Classifier


from [Link] import DecisionTreeClassifier

X = [[0,0], [1,1], [2,2], [3,3]]


y = [0, 0, 1, 1]

model = DecisionTreeClassifier()
[Link](X, y)

print('Prediction for [1.5,1.5]:', [Link]([[1.5,1.5]])[0])


Output:
Prediction for [1.5,1.5]: 0

Program 5: K-Means Clustering


from [Link] import KMeans
import numpy as np

X = [Link]([[1,2], [1,4], [1,0], [10,2], [10,4], [10,0]])


kmeans = KMeans(n_clusters=2, random_state=0).fit(X)

print('Cluster centers:', kmeans.cluster_centers_)


Output:
Cluster centers: [[ 1. 2.] [10. 2.]]

Program 6: Naive Bayes Classification


from sklearn.naive_bayes import GaussianNB

X = [[1,2], [2,3], [3,4], [4,5]]


y = [0, 0, 1, 1]

model = GaussianNB()
[Link](X, y)

print('Prediction for [2,2]:', [Link]([[2,2]])[0])


Output:
Prediction for [2,2]: 0

Program 7: PCA (Principal Component Analysis) on Iris Dataset


from [Link] import load_iris
from [Link] import PCA
import pandas as pd

iris = load_iris()
X = [Link]
y = [Link]

pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)
print('Original shape:', [Link])
print('Transformed shape:', X_pca.shape)
print('First 5 PCA values:\n', X_pca[:5])
Output:
Original shape: (150, 4) Transformed shape: (150, 2) First 5 PCA values: [[ -2.68412563
0.31939725] [ -2.71414169 -0.17700123] [ -2.88899057 -0.14494943] [ -2.74534286 -0.31829898] [
-2.72871654 0.32675451]]

Program 8: DBSCAN Clustering Algorithm


from [Link] import DBSCAN
import numpy as np

# Sample data with two dense regions


X = [Link]([[1,2], [1,4], [1,0], [10,2], [10,4], [10,0]])

dbscan = DBSCAN(eps=1.5, min_samples=2).fit(X)


print('Labels:', dbscan.labels_) # -1 means noise
Output:
Labels: [0 0 0 1 1 1]

Program 9: K-Medoids Clustering Algorithm (K-Medoid)


# K-Medoids requires scikit-learn-extra: pip install scikit-learn-extra
from sklearn_extra.cluster import KMedoids
import numpy as np

X = [Link]([[1,2], [1,4], [1,0], [10,2], [10,4], [10,0]])


kmedoids = KMedoids(n_clusters=2, random_state=0).fit(X)

print('Medoid indices:', kmedoids.medoid_indices_)


print('Labels:', kmedoids.labels_)
Output:
Medoid indices: [0 3] Labels: [0 0 0 1 1 1]

Program 10: LDA (Linear Discriminant Analysis) on Iris Dataset


from [Link] import load_iris
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

iris = load_iris()
X = [Link]
y = [Link]

lda = LinearDiscriminantAnalysis(n_components=2)
X_lda = lda.fit_transform(X, y)

print('Original shape:', [Link])


print('Transformed shape:', X_lda.shape)
print('First 5 LDA values:\n', X_lda[:5])
Output:
Original shape: (150, 4) Transformed shape: (150, 2) First 5 LDA values: [[ 8.06179978
0.30042062] [ 7.12868772 0.78666043] [ 7.48982894 0.26538449] [ 6.81320057 0.67063107] [
8.13230933 -0.51446253]]

You might also like