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

ML Lab Programs

The document outlines various machine learning lab programs, including implementations for scatter plots, linear regression, logistic regression, decision trees, naive Bayes, KNN, SVM, and K-means clustering. Each program includes code snippets demonstrating how to use different libraries and techniques for data visualization and model training. The examples utilize popular datasets like Iris and include methods for prediction and accuracy evaluation.

Uploaded by

Junaidul hasan
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 views2 pages

ML Lab Programs

The document outlines various machine learning lab programs, including implementations for scatter plots, linear regression, logistic regression, decision trees, naive Bayes, KNN, SVM, and K-means clustering. Each program includes code snippets demonstrating how to use different libraries and techniques for data visualization and model training. The examples utilize popular datasets like Iris and include methods for prediction and accuracy evaluation.

Uploaded by

Junaidul hasan
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 PROGRAMS

1. Scatter Plot Program


-----------------------
import [Link] as plt
x = [5,7,8,7,2,17,2,9,4,11,12,9,6]
y = [99,86,87,88,111,86,103,87,94,78,77,85,86]
[Link](x, y)
[Link]()

2. Linear Regression
--------------------
import numpy as np
import [Link] as plt
from sklearn.linear_model import LinearRegression
X = [Link]([1, 2, 3, 4, 5]).reshape(-1, 1)
y = [Link]([20, 40, 60, 80, 100])
model = LinearRegression()
[Link](X, y)
hours = [Link]([[6]])
predicted_marks = [Link](hours)
print(predicted_marks)
[Link](X, y)
[Link](X, [Link](X))
[Link]()

3. Logistic Regression
----------------------
import numpy as np
import [Link] as plt
from sklearn.linear_model import LogisticRegression
X = [Link]([1,2,3,4,5,6,7,8,9]).reshape(-1, 1)
y = [Link]([0,0,0,0,1,1,1,1,1])
model = LogisticRegression()
[Link](X, y)
hours = [Link]([[4.5]])
prediction = [Link](hours)
print(prediction)

4. Decision Tree
----------------
from sklearn import tree
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import accuracy_score
iris = load_iris()
X = [Link]
y = [Link]
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3,random_state=42)
clf = [Link]()
[Link](X_train,y_train)
y_pred = [Link](X_test)
print(accuracy_score(y_test,y_pred))

5. Naive Bayes
--------------
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
iris = load_iris()
X = [Link]
y = [Link]
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3,random_state=42)
model = GaussianNB()
[Link](X_train,y_train)
print([Link](X_test))

6. KNN (Manual)
---------------
import numpy as np
def distance(x1, x2):
return [Link]([Link]((x1 - x2)**2))
X_train = [Link]([[1,2],[2,3],[3,3],[6,5],[7,7],[8,6]])
y_train = [Link]([0,0,0,1,1,1])
X_test = [Link]([[5,5]])
k=3
for test_point in X_test:
distances=[]
for i in range(len(X_train)):
dist = distance(test_point,X_train[i])
[Link]((dist,y_train[i]))
[Link](key=lambda x:x[0])
neighbours=distances[:k]
classes=[label for (_,label) in neighbours]
predicted=max(set(classes),key=[Link])
print(predicted)

7. SVM
------
from sklearn import datasets
from sklearn.model_selection import train_test_split
from [Link] import SVC
iris=datasets.load_iris()
X=[Link][:,:2]
y=[Link]
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3,random_state=1)
model=SVC(kernel='linear')
[Link](X_train,y_train)
print([Link](X_test))

8. K-Means
----------
from [Link] import KMeans
import numpy as np
X=[Link]([[1,2],[1.5,1.8],[5,8],[8,8],[1,0.6],[9,11],[8,2],[10,2],[9,3]])
kmeans=KMeans(n_clusters=3)
[Link](X)
print(kmeans.cluster_centers_)
print(kmeans.labels_)

You might also like