0% found this document useful (0 votes)
4 views20 pages

Machine Learning Lab

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

Machine Learning Lab

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

MACHINE LEARING LAB RECORD

INDEX

SNO NAME OF PROGRAM DATE OF SUBMISSION

1 Find s algorithm

2 Mean mode variance

3 Median ,Standard deviation

4 Candidate elimination algorithm

5 Perceptron training

6 Simple linear regression

7 Multilinear regression

8 Id3 decision tree algorithm

9 Decision tree using GINI INDEX

10 KNN algorithm

11 K means algorithm

1
MACHINE LEARING LAB RECORD

1. Find_s

Code:
import pandas as pd

df = pd.read_csv("EnjoySport.csv")

target = input("Enter the target column: ")

target_index = df.columns.get_loc(target)

h = None

for _, row in df.iterrows():

if str(row[target]).strip().lower() == 'yes':

current = row[:target_index].tolist()

if h is None:

h = current

else:

h = ['?' if h[j] != current[j] else h[j] for j in range(target_index)]

print("Current hypothesis:", h)

print(f"\nFinal specific hypothesis: {h}")

OUTPUT:

2
MACHINE LEARING LAB RECORD

2.Mean Mode & Variance:


Code:
import math

datap0543 = [4,8,6,5,3,7,6,7]

#mean

sump0543 = 0

i=0

n = len(datap0543)

while i < n:

sump0543 += datap0543[i]

i = i+1

mean = sump0543/n

print("Mean:",mean)

# mode/

c = []

for i in range(n):

c.append(0)

c[i] = datap0543.count(datap0543[i])

high = c[0]

while i in range(n):

if high < c[i]:

high = c[i]

i = i+1

mode = []

for i in range(n):

if c[i] == high:

3
MACHINE LEARING LAB RECORD

mode.append(datap0543[i])

print("Mode:",set(mode))

# variance

num = 0

for i in range(n):

num =num + math.pow(datap0543[i]-mean,2)

var = num/n

print("Variance:",var)

OUTPUT:

3.Median & Standard deviation:


Code:
import math

datap0543 = [4,8,6,5,3,7,6,7]

#mean

sump0543 = 0

i=0

n = len(datap0543)

while i < n:

sump0543 += datap0543[i]

i = i+1

4
MACHINE LEARING LAB RECORD

mean = sump0543/n

print("Mean:",mean)

# median

if n%2==0:

median = (datap0543[int(n/2)]+datap0543[int(n/2 - 1)])/2

else:

median = datap0543[int(n/2)]

print("Median:",median)

# variance

num = 0

for i in range(n):

num =num + math.pow(datap0543[i]-mean,2)

var = num/n

print("Variance:",var)

# stddev

std = math.sqrt(var)

print("Standard Deviation:",std)

OUTPUT:

4.CandidateElimination:

5
MACHINE LEARING LAB RECORD

Code:
import pandas as pd

df = pd.read_csv("EnjoySport.csv")

target = input("Enter target column: ").strip()

# Initialize Specific (S) and General (G) hypotheses

S = list(df.iloc[0, :-1])

G = [["?"] * len(S)]

print("\nInitial S:", S)

print("Initial G:", G)

# Iterate over dataset

for i, row in df.iterrows():

x = row.iloc[:-1]

y = row.loc[target] # Use .loc to access by label (avoid warning)

if str(y).lower() == "yes":

# Generalize S

S = ["?" if S[j] != x.iloc[j] else S[j] for j in range(len(S))]

# Remove inconsistent hypotheses from G

G = [g for g in G if all(g[j] == "?" or g[j] == x.iloc[j] for j in range(len(S)))]

else:

new_G = []

for g in G:

for j in range(len(S)):

if g[j] == "?" and S[j] != "?":

6
MACHINE LEARING LAB RECORD

h = g.copy()

h[j] = S[j]

if any(h[k] != x.iloc[k] or h[k] == "?" for k in range(len(S))):

new_G.append(h)

# Add new consistent hypotheses

G = G + [h for h in new_G if h not in G]

print(f"\nAfter instance {i+1} ({y}):")

print("S:", S)

print("G:", G)

print("\nFinal S:", S)

print("Final G:", G)

OUTPUT:

5.Perceptron:
Code:
7
MACHINE LEARING LAB RECORD

print("Give init weights")

w0 = float(input("w0:"))

w1 = float(input("w1:"))

w2 = float(input("w2:"))

n = float(input("Enter learning_rate:"))

x0 = -1

ch = int(input("Enter input OR:0 XOR=1 AND=2"))

if ch == 0:

inp = [[0,0,0],[0,1,1],[1,0,1],[1,1,1]]

elif ch == 1:

inp = [[0,0,1],[0,1,0],[1,0,0],[1,1,1]]

else:

inp = [[0,0,0],[0,1,0],[1,0,0],[1,1,1]]

i=0

while(i<len(inp)):

print("\n")

print(f"Iteration",i+1)

targ = inp[i][2]

x1 = inp[i][0]

x2 = inp[i][1]

y = w0*x0 + w1*x1 + w2*x2

i=i+1

y=1 if y>0 else 0

# check with target

if y == targ:

print("No need to update")

8
MACHINE LEARING LAB RECORD

else:

w0 = w0 + n*(targ-y)*x0

w1 = w1 + n*(targ-y)*x1

w2 = w2 + n*(targ-y)*x2

print("Updated weights:")

print(f"w0:",w0,"\n","w1:",w1,"\n","w2:",w2,"\n")

OUTPUT:

6.Simple linear regression


Code:
import numpy as np

9
MACHINE LEARING LAB RECORD

import pandas as pd

import matplotlib.pyplot as plt

from sklearn.datasets import load_iris

from sklearn.linear_model import LinearRegression

from sklearn.model_selection import train_test_split

from sklearn.metrics import mean_squared_error

# Load the Iris dataset

iris = load_iris()

X = iris.data[:, 2].reshape(-1, 1) # Petal length (as input feature)

y = iris.data[:, 3]

# Split the data into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

model = LinearRegression()

model.fit(X_train, y_train)

y_pred = model.predict(X_test)

# Calculate the mean squared error

mse = mean_squared_error(y_test, y_pred)

print(f"Mean Squared Error: {mse}")

# Visualizing the regression line and data points

plt.scatter(X_test, y_test, color='blue', label='Actual')

plt.plot(X_test, y_pred, color='red', label='Regression Line')

plt.xlabel('Petal Length')

plt.ylabel('Petal Width')

plt.title('Linear Regression on Iris Dataset (Petal Length vs Petal Width)')

10
MACHINE LEARING LAB RECORD

plt.legend()

plt.show()

OUTPUT:

7.Multiple linear regression:


Code:
import numpy as np

import pandas as pd

11
MACHINE LEARING LAB RECORD

import matplotlib.pyplot as plt

from sklearn.datasets import load_iris

from sklearn.linear_model import LinearRegression

from sklearn.model_selection import train_test_split

from sklearn.metrics import mean_squared_error

iris = load_iris()

X = iris.data[:, [0, 1, 2]] # Sepal length, Sepal width, Petal length (multiple features)

y = iris.data[:, 3] # Petal width (target variable)

# Split the data into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

model = LinearRegression()

model.fit(X_train, y_train)

y_pred = model.predict(X_test)

# Calculate the mean squared error

mse = mean_squared_error(y_test, y_pred)

print(f"Mean Squared Error: {mse}")

# Visualizing the actual vs predicted values

plt.scatter(y_test, y_pred)

plt.xlabel('Actual Petal Width')

plt.ylabel('Predicted Petal Width')

plt.title('Multiple Linear Regression: Actual vs Predicted')

plt.show()

# Display the coefficients and intercept

12
MACHINE LEARING LAB RECORD

print("Coefficients:", model.coef_)

print("Intercept:", model.intercept_)

OUTPUT:

8.ID3:
Code:

import pandas as pd

import matplotlib.pyplot as plt

13
MACHINE LEARING LAB RECORD

from sklearn.tree import DecisionTreeClassifier, plot_tree

df = pd.read_csv("play_tennis.csv")

print(df)

df_encoded = df.copy()

for col in df.columns:

df_encoded[col] = df[col].astype('category').cat.codes

x = df_encoded.drop("PlayTennis", axis=1)

y = df["PlayTennis"]

model = DecisionTreeClassifier(criterion="entropy", random_state=0)

model.fit(x, y)

plt.figure(figsize=(20, 12))

plot_tree(

model,

feature_names=x.columns.tolist(),

class_names=df["PlayTennis"].unique().tolist(),

filled=True,

rounded=True,

precision=2,

fontsize=12

plt.show()

14
MACHINE LEARING LAB RECORD

OUTPUT:

8.GINI IMPURITY
Code
import pandas as pd

import matplotlib.pyplot as plt

from sklearn.tree import DecisionTreeClassifier, plot_tree

15
MACHINE LEARING LAB RECORD

df = pd.read_csv("play_tennis.csv")

print(df)

df_encoded = df.copy()

for col in df.columns:

df_encoded[col] = df[col].astype('category').cat.codes

x = df_encoded.drop("PlayTennis", axis=1)

y = df["PlayTennis"]

model = DecisionTreeClassifier(criterion="gini", random_state=0)

model.fit(x, y)

plt.figure(figsize=(20, 12))

plot_tree(

model,

feature_names=x.columns.tolist(),

class_names=df["PlayTennis"].unique().tolist(),

filled=True,

rounded=True,

precision=2,

fontsize=12

plt.show()

16
MACHINE LEARING LAB RECORD

OUTPUT:

10.KNN
Code
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

17
MACHINE LEARING LAB RECORD

# Load the Iris dataset

iris = load_iris()

X = iris.data

y = iris.target

# Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

#KNN classifier with k=3

knn = KNeighborsClassifier(n_neighbors=3)

# Fit the model on training data

knn.fit(X_train, y_train)

# Make predictions on test data

y_pred = knn.predict(X_test)

# Evaluate the model

print("Accuracy:", accuracy_score(y_test, y_pred))

print("\nClassification Report:\n", classification_report(y_test, y_pred))

OUTPUT

18
MACHINE LEARING LAB RECORD

11.K-MEANS ALGORITHM
Code
from sklearn.datasets import load_iris

from sklearn.cluster import KMeans

import matplotlib.pyplot as plt

from sklearn.decomposition import PCA

# Load the Iris dataset

iris = load_iris()

X = iris.data

# Create KMeans model with 3 clusters (since Iris has 3 flower types)

kmeans = KMeans(n_clusters=3, random_state=42)

kmeans.fit(X)

# Predict cluster labels

labels = kmeans.labels_

19
MACHINE LEARING LAB RECORD

# Reduce dimensionality for visualization

pca = PCA(n_components=2)

X_pca = pca.fit_transform(X)

plt.scatter(X_pca[:, 0], X_pca[:, 1], c=labels, cmap='viridis', marker='o', edgecolor='k')

plt.title("K-Means Clustering (Iris Dataset)")

plt.xlabel("Sepal length")

plt.ylabel("Sepal width")

plt.show()

OUTPUT

20

You might also like