0% found this document useful (0 votes)
69 views33 pages

Practical Machine Learning Implementations

The document provides code examples for implementing various machine learning algorithms in Python, including decision trees, k-nearest neighbors, Apriori association rule mining, naive Bayes classification, k-means clustering, and linear regression. Code is provided to load and preprocess data, implement the algorithms, and evaluate results. Output from running the code is also displayed for some examples. The document appears to be from a student submission for a machine learning class assignment.
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)
69 views33 pages

Practical Machine Learning Implementations

The document provides code examples for implementing various machine learning algorithms in Python, including decision trees, k-nearest neighbors, Apriori association rule mining, naive Bayes classification, k-means clustering, and linear regression. Code is provided to load and preprocess data, implement the algorithms, and evaluate results. Output from running the code is also displayed for some examples. The document appears to be from a student submission for a machine learning class assignment.
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

Name: Chauhan Parth Yagneshbhai


Enrollment No: 205160694034
Subject: Machine Learning
Class: Sem-3 MCA

01 Write a python code to implement decision tree for below given dataset. Identify the
root node and all subpart or children of node and draw the tree.

1
Machine Learning

Code:
student_details.csv :

[Link] :

2
Machine Learning

Output:

Output:

3
Machine Learning

Output:

4
Machine Learning
02 Write a python code to implement K-nearest neighbourhood program for the given
dataset.

Code:
[Link] :

5
Machine Learning

[Link] :

6
Machine Learning

Output:

7
Machine Learning

03 Write a python code to implement Apriori algorithm, apply join and prune method
and find frequent item set

8
Machine Learning

Code:

from apyori import apriori

transactions = [
['Bread', 'butter', 'milk', 'soda'],
['Coke', 'egg', 'milk'],
['Bread', 'butter', 'egg'],
['Break', 'coke', 'jam'],
['Bread', 'butter'],
['Potato chips', 'soda'],
['Coke', 'fruit', 'juice'],
['Bread', 'coke', 'milk'],
['Coke', 'soda', 'jam', 'milk'],
['Bread', 'butter', 'egg', 'milk','soda'],
['Bread', 'milk'],
['Bread', 'jam']
]
results = list(apriori(transactions))

association_rules = apriori(transactions, min_support=0.093, min_confidence=1.0, min_lift=2,


min_length=2)
association_results = list(association_rules)

print("Association Results : {}".format(len(association_results)))

print("First association results : \n{}".format(association_results[0]))

Output:

for item in association_results:


# first index of the inner list
# Contains base item and add item
pair = item[0]
items = [x for x in pair]
print("Rule: " + items[0] + " -> " + items[1])

#second index of the inner list


print("Support: " + str(item[1]))

#third index of the list located at 0th


#of the third index of the inner list

9
Machine Learning
print("Confidence: " + str(item[2][0][2]))
print("Lift: " + str(item[2][0][3]))
print("=====================================")

Output:

10
Machine Learning

Code:
# Importing library
import math
import random
import csv

# the categorical class names are changed to numberic data


# eg: yes and no encoded to 1 and 0
def encode_class(mydata):
classes = []
for i in range(len(mydata)):
if mydata[i][-1] not in classes:
[Link](mydata[i][-1])
for i in range(len(classes)):
for j in range(len(mydata)):
if mydata[j][-1] == classes[i]:
mydata[j][-1] = i
return mydata

# Splitting the data


def splitting(mydata, ratio):
train_num = int(len(mydata) * ratio)
train = []
# initially testset will have all the dataset
test = list(mydata)
while len(train) < train_num:

11
Machine Learning
# index generated randomly from range 0
# to length of testset
index = [Link](len(test))
# from testset, pop data rows and put it in train
[Link]([Link](index))
return train, test

# Group the data rows under each class yes or


# no in dictionary eg: dict[yes] and dict[no]

def groupUnderClass(mydata):
dict = {}
for i in range(len(mydata)):
if (mydata[i][-1] not in dict):
dict[mydata[i][-1]] = []
dict[mydata[i][-1]].append(mydata[i])
return dict

# Calculating Mean
def mean(numbers):
return sum(numbers) / float(len(numbers))

# Calculating Standard Deviation


def std_dev(numbers):
avg = mean(numbers)
variance = sum([pow(x - avg, 2) for x in numbers]) / float(len(numbers) - 1)
return [Link](variance)

def MeanAndStdDev(mydata):
info = [(mean(attribute), std_dev(attribute)) for attribute in zip(*mydata)]
# eg: list = [ [a, b, c], [m, n, o], [x, y, z]]
# here mean of 1st attribute =(a + m+x), mean of 2nd attribute = (b + n+y)/3
# delete summaries of last class
del info[-1]
return info

# find Mean and Standard Deviation under each class


def MeanAndStdDevForClass(mydata):
info = {}
dict = groupUnderClass(mydata)
for classValue, instances in [Link]():
info[classValue] = MeanAndStdDev(instances)
return info

# Calculate Gaussian Probability Density Function


def calculateGaussianProbability(x, mean, stdev):
expo = [Link](-([Link](x - mean, 2) / (2 * [Link](stdev, 2))))
return (1 / ([Link](2 * [Link]) * stdev)) * expo

12
Machine Learning
# Calculate Class Probabilities
def calculateClassProbabilities(info, test):
probabilities = {}
for classValue, classSummaries in [Link]():
probabilities[classValue] = 1
for i in range(len(classSummaries)):
mean, std_dev = classSummaries[i]
x = test[i]
probabilities[classValue] *= calculateGaussianProbability(x, mean, std_dev)
return probabilities

# Make prediction - highest probability is the prediction


def predict(info, test):
probabilities = calculateClassProbabilities(info, test)
bestLabel, bestProb = None, -1
for classValue, probability in [Link]():
if bestLabel is None or probability > bestProb:
bestProb = probability
bestLabel = classValue
return bestLabel

# returns predictions for a set of examples


def getPredictions(info, test):
predictions = []
for i in range(len(test)):
result = predict(info, test[i])
[Link](result)
return predictions

# Accuracy score
def accuracy_rate(test, predictions):
correct = 0
for i in range(len(test)):
if test[i][-1] == predictions[i]:
correct += 1
return (correct / float(len(test))) * 100.0

# add the data path in your system


filename = r'E:\user\MACHINE LEARNING\machine learning algos\Naive bayes\[Link]'

# load the file and store it in mydata list


mydata = [Link](open(filename, "rt"))
mydata = list(mydata)
mydata = encode_class(mydata)
for i in range(len(mydata)):
mydata[i] = [float(x) for x in mydata[i]]

# split ratio = 0.7


# 70% of data is training data and 30% is test data used for testing

13
Machine Learning
ratio = 0.7
train_data, test_data = splitting(mydata, ratio)
print(Age: ', len(mydata))
print('income: ', len(train_data))
print("Student: ", len(test_data))
print("credit rating: ", len(test_data))
print("buys-computer: ", len(test_data))
# prepare model
info = MeanAndStdDevForClass(train_data)

# test model
predictions = getPredictions(info, test_data)
accuracy = accuracy_rate(test_data, predictions)
print("Accuracy of your model is: ", accuracy)

Output:

05 Python code for Preparing to Model (Appendix B.2, Page No. 365 – 373) and Feature
Engineering (Appendix B.4, Page No. 378 – 385).
Code:
import pandas as pd
import numpy as np
import [Link] as plt
import datetime
pd.set_option('display.max_columns', None)data_train = pd.read_excel('Data_Train.xlsx')data_test =
pd.read_excel('Data_Test.xlsx')

price_appendix = data_appendix.Price
# Concatenate appendix and page number = [Link]([data_page [Link](['Price'], axis=1),
data_test])

<class '[Link]'>
Int64Index: 13354 entries, 0 to 2670
appendix non -null
page number non nullobject

data = data.drop_duplicates()

14
Machine Learning

Output:
Index([‘apendix’, 'b1,b2,b3,b4,b5', 'page number', '365-390',
apendix 5
page number 25

06 Apply k-means clustering approach with k = 2 to the following dataset.

Code:

Import libraries:

import numpy as np # linear algebra


import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import [Link] as plt # for data visualization
import seaborn as sns # for statistical data visualization
%matplotlib inline

# Input data files are available in the "../input/" directory.


# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input dire
ctory

import os
for dirname, _, filenames in [Link]('/kaggle/input'):
for filename in filenames:
print([Link](dirname, filename))

15
Machine Learning

Output:

Import dataset:
data = '/kaggle/input/facebook-live-sellers-in-thailand-uci-ml-repo/[Link]'
df = pd.read_csv(data)
[Link]()

Output:

[Link]()

16
Machine Learning
Explore status_id variable :
# view the labels in the variable

df['status_id'].unique()

K-Means model :
from [Link] import KMeans

kmeans = KMeans(n_clusters=2, random_state=0)

[Link](X)

Output:

K-Means model parameters study :


kmeans.cluster_centers_

Output:

17
Machine Learning
07 Implement a python program that takes interest rate (x), finds the equation that best
fits the data and is able to forecast out median home price for given interest rate using
the data given below. (Use linear regression)

Code:
[Link]:

18
Machine Learning
[Link]:

#import library
import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns
from sklearn.linear_model import LinearRegression
#load dataset
df = pd.read_csv('[Link]')
[Link](figsize=(8,6))
[Link](x=df['Interest Rate'], y=df['Median Home'], hue=df['Interest Rate'])

Output:

[Link](data = df)

Output:

19
Machine Learning

[Link](df['Interest Rate'])

Output:

[Link](df['Median Home'])

Output:

20
Machine Learning

x = df['Interest Rate'].values
y = df['Median Home'].values
x_data = [Link](x)
y_data = [Link](y)
x_data = x_data.reshape(-1,1)
y_data = y_data.reshape(-1,1)

#import library for model split


from sklearn.model_selection import train_test_split
#modal split
X_train, X_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.33, random_state=42)
lm = LinearRegression()
print([Link](X_train,y_train))

Output:

#final output
predict = [Link](X_test)
print(predict)

Output:

21
Machine Learning
# Plot outputs
[Link](X_test, y_test, color='blue')
[Link](X_test, [Link](X_test), color='red', linewidth=3)
[Link]()

08 Apply following supervised machine learning algorithms for All classification


problems as shown under:

Code:
Visualizing Decision Tree:
from [Link] import export_graphviz
from [Link] import StringIO
from [Link] import Image
import pydotplus
dot_data = StringIO()
export_graphviz(clf, out_file=dot_data,
filled=True, rounded=True,
special_characters=True,feature_names = feature_cols,class_names=[‘0’.’1’])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png(‘[Link]’)
Image(graph.create_png())

Output:

22
Machine Learning

Logistic Regression:

# import pandas
import pandas as pd

# import numpy
import numpy as np

# import seaborn
import seaborn as sb

# import matplotlib
import [Link] as plt

# creating Dataframe object


df = pd.read_csv(R'D://xdatasets/[Link]')
print([Link]())
print([Link]())
print([Link]())

Output:

23
Machine Learning
Visualization :

[Link](bins=25,figsize=(10,10))
# display histogram
[Link]()

Output:

K-Nearest Neighborhood :

Preparing the data:


import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns
data = pd.read_csv('../input/[Link]');

print("\n \t The data frame has {0[0]} rows and {0[1]} columns. \n".format([Link]))
[Link]()

[Link](3)

24
Machine Learning

Output:

Visualizing the data


features_mean= list([Link][1:11])

[Link](figsize=(10,10))
[Link](data[features_mean].corr(), annot=True, square=True, cmap='coolwarm')
[Link]()

25
Machine Learning

Output:

Naïve Bayes :

Loading data using Pandas :

#importing pandas library


import pandas as pd

#loading data
titanic = pd.read_csv('...\input\[Link]')

Printing data head :

# View first five rows of the dataset


[Link]()

26
Machine Learning

Output:

Graphical Analysis:

import seaborn as sns


import [Link] as plt

# Countplot
[Link](x ="Sex", hue ="Survived", kind ="count", data = titanic)

Output:

SVM :

import numpy as np
import pandas as pd
import [Link] as stats
from matplotlib import pyplot as plt
%matplotlib inline
from [Link] import StandardScaler
from [Link] import [Link](“/content/gdrive”)

27
Machine Learning
data = pd.read_csv('/content/gdrive/My Drive/Colab Notebooks/data/[Link]')
len(data)
X = [Link]() #dataset has been copied to X
[Link](10)

Output:

[Link]().sum() #No null values

Output:

Visualization:

[Link]["[Link]"] = (10, 6)
[Link](X["age"], dist="norm", plot=plt)
[Link]()

Output:

28
Machine Learning

09 Build a prediction model using regression technique for (1) Boston house-prices
(from [Link] import load_boston) (2) Diabetes (from [Link] import
load_diabetes) datasets. Also, evaluate the model
Code:
import pandas as pd
import [Link] as sm
from [Link] import StandardScaler
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import [Link] as plt
#import dataset
dataset = pd.read_csv("Multivariate Linear Regression [Link]")
Y = dataset["Price_of_House"]
X = dataset[['Squar_Feet','Number_of_Bed_Rooms']]
scale = StandardScaler()
X_scaled = scale.fit_transform(X[['Squar_Feet','Number_of_Bed_Rooms']].as_matrix())

est = [Link](Y, X).fit()


print ([Link]())

fig = [Link]()

ax = fig.add_subplot(1,2,1, projection='3d')
[Link](dataset["Squar_Feet"], dataset["Number_of_Bed_Rooms"], dataset["Price_of_House"],
c='r', marker='^')

ax.set_xlabel('Squar_Feet')
ax.set_ylabel('Number_of_Bed_Rooms')
ax.set_zlabel('Price_of_House')

29
Machine Learning
frst_col_surface = [Link][0:len(dataset),0]
#selection de la première colonne de notre dataset

scnd_col_nb_chambre = [Link][0:len(dataset),1]
third_col_prix = [Link][0:len(dataset),2]
def predict_price_of_house(Squar_Feet,Number_of_Bed_Rooms):
return 140.8611 * Squar_Feet + 1.698e+04 * Number_of_Bed_Rooms
def predict_all(lst_sizes, lst_nb_chmbres):
predicted_prices = []

for n in range(0, len(Y)):


predicted_prices.append(predict_price_of_house(lst_sizes[n], lst_nb_chmbres[n]))
return predicted_prices
ax = fig.add_subplot(1, 2, 2, projection='3d')
ax.plot_trisurf(dataset["Squar_Feet"], dataset["Number_of_Bed_Rooms"],
predict_all(dataset["Squar_Feet"], dataset["Number_of_Bed_Rooms"]))

print (predict_price_of_house(4500,5))

10 Implement support vector machine approach to predictive modelling for (1) Boston
house-prices (from [Link] import load_boston) (2) Diabetes (from
[Link] import load_diabetes) datasets. Also, evaluate the model.

Code:
import the required libraries.
Import
numpy
as np
import [Link] as plt

import pandas as pd
import seaborn as sns

%matplotlib inline

scikit-learn library
from
[Link]
import
load_boston
boston_dataset = load_boston()

Output:

30
Machine Learning
boston_dataset.DESCR The description of all the features is given below:

boston = [Link](boston_dataset.data, columns=boston_dataset.feature names)


[Link]()

Output:

boston['MEDV'] = boston_dataset.target

Data preprocessing
[Link]().sum()

Output:

31
Machine Learning

distplot function from the seaborn library.

[Link](rc={'[Link]':(11.7,8.27)})
[Link](boston['MEDV'], bins=30)
[Link]()

Output:

32
Machine Learning

Model evaluation

#model evaluation for training set

`y_train_predict = lin_model.predict(X_train)
rmse = ([Link](mean_squared_error(Y_train, y_train_predict)))
r2 = r2_score(Y_train, y_train_predict)

print("The model performance for training set")


print("--------------------------------------")
print('RMSE is {}'.format(rmse))
print('R2 score is {}'.format(r2))
print("\n")

# model evaluation for testing set


y_test_predict = lin_model.predict(X_test)
rmse = ([Link](mean_squared_error(Y_test, y_test_predict)))
r2 = r2_score(Y_test, y_test_predict)

print("The model performance for testing set")


print("--------------------------------------")
print('RMSE is {}'.format(rmse))
print('R2 score is {}'.format(r2))

Output:

33

You might also like