ML Record Print
ML Record Print
ML Record Print
Source code:
import csv
data = []
print("\nThe given training examples are:")
for row in readcsv:
print(row)
if row[len(row)-1].upper() == "YES":
data.append(row)
TotalExamples = len(data)
i=0
j=0
k=0
print("The steps of the Find-s algorithm are :\n", hypo)
i=1
for i in range(TotalExamples):
for k in range(d):
if hypo[k] != data[i][k]:
hypo[k] = '?'
else:
hypo[k] = hypo[k]
print(hypo)
i=i+1
print("\nThe maximally specific Find-s hypothesis for the given training examples is :")
list = []
for i in range(d):
list.append(hypo[i])
print(list)
Output :
<_csv.reader object at 0x00000260B3FFB3A0>
The maximally specific Find-s hypothesis for the given training examples is :
['Sunny', 'Warm', '?', 'Strong', '?', '?']
2. Implement Linear Regression?
Source code:
import numpy as np
import matplotlib.pyplot as plt
# putting labels
plt.xlabel('x')
plt.ylabel('y')
plt.show()
def main():
# observations / data
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = np.array([1, 3, 2, 5, 7, 8, 8, 9, 10, 12])
# estimating coefficients
b = estimate_coef(x, y)
print("Estimated coefficients:\nb_0 = {} \nb_1 = {}".format(b[0], b[1]))
Output:
Estimated coefficients:
b_0 = 1.2363636363636363
b_1 = 1.1696969696969697
3. Implement RANDOM-FOREST REGRESSION?
Source code:
# Import necessary libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report
import warnings
warnings.filterwarnings('ignore')
Output :
Accuracy: 0.80
Classification Report:
precision recall f1-score support
Source code:
import numpy as np
X = np.array(([2, 9], [1, 5], [3, 6]), dtype=float) # X = (hours sleeping, hours studying)
y = np.array(([92], [86], [89]), dtype=float) # y = score on test
# scale units
X = X / np.amax(X, axis=0) # maximum of X array
y = y / 100 # max test score is 100
class Neural_Network(object):
def __init__(self):
# Parameters
self.inputSize = 2
self.outputSize = 1
self.hiddenSize = 3
# Weights
self.W1 = np.random.randn(self.inputSize, self.hiddenSize) # (3x2) weight matrix from input to
hidden layer
self.W2 = np.random.randn(self.hiddenSize, self.outputSize) # (3x1) weight matrix from hidden to
output layer
output:
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Loss:
0.09014121131592957
5. Implement LOCALLY WEIGHTED REGRESSION?
Source code:
from math import ceil
import numpy as np
from scipy import linalg
import matplotlib.pyplot as plt
residuals = y - yest
s = np.median(np.abs(residuals))
delta = np.clip(residuals / (6.0 * s), -1, 1)
delta = (1 - delta ** 2) ** 2
return yest
n = 100
x = np.linspace(0, 2 * np.pi, n)
y = np.sin(x) + 0.3 * np.random.randn(n)
f = 0.25
iterations = 3
yest = lowess(x, y, f, iterations)
plt.plot(x, y, "r.")
plt.plot(x, yest, "b-")
plt.show()
output:
6. For a given set of training data example store in csv.file implement
and demonstrate ELIMINATION algorithm?
Source code:
import numpy as np
import pandas as pd
output:
sky airTemp humidity wind water forecast enjoySport
0 Sunny Warm Normal Strong Warm Same Yes
1 Sunny Warm High Strong Warm Same Yes
2 Rainy Cold High Strong Warm Change No
3 Sunny Warm High Strong Cool Change Yes
Concepts:
[['Sunny' 'Warm' 'Normal' 'Strong' 'Warm' 'Same']
['Sunny' 'Warm' 'High' 'Strong' 'Warm' 'Same']
['Rainy' 'Cold' 'High' 'Strong' 'Warm' 'Change']
['Sunny' 'Warm' 'High' 'Strong' 'Cool' 'Change']]
Target:
['Yes' 'Yes' 'No' 'Yes']
Final General_h: [['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?']]
7. Write a program to demonstrate the working of the DECISION TREE
based ID3 algorithm?
Source code:
import numpy as np
import math
import csv
def read_data(filename):
with open(filename, 'r') as csvfile:
datareader = csv.reader(csvfile, delimiter=',')
headers = next(datareader)
metadata = headers # No need to loop over headers to append to metadata
traindata = []
for row in datareader:
traindata.append(row)
return metadata, traindata
class Node:
def __init__(self, attribute):
self.attribute = attribute
self.children = []
self.answer = ""
def __str__(self):
return self.attribute
for x in range(items.shape[0]):
for y in range(data.shape[0]):
if data[y, col] == items[x]:
count[x] += 1
for x in range(items.shape[0]):
dict[items[x]] = np.empty((count[x], data.shape[1]), dtype=data.dtype)
pos = 0
for y in range(data.shape[0]):
if data[y, col] == items[x]:
dict[items[x]][pos] = data[y]
pos += 1
if delete:
dict[items[x]] = np.delete(dict[items[x]], col, 1)
return items, dict
def entropy(S):
items, counts = np.unique(S, return_counts=True)
entropy_value = 0
for i in range(len(items)):
p_i = counts[i] / len(S)
entropy_value -= p_i * math.log2(p_i)
return entropy_value
for x in range(items.shape[0]):
ratio = dict[items[x]].shape[0] / total_size
entropies[x] = ratio * entropy(dict[items[x]][:, -1])
intrinsic[x] = ratio * math.log2(ratio) if ratio != 0 else 0
split = np.argmax(gains)
node = Node(metadata[split])
new_metadata = np.delete(metadata, split)
for x in range(items.shape[0]):
child = create_node(dict[items[x]], new_metadata)
node.children.append((items[x], child))
return node
def empty(size):
return " " * size
output:
Outlook
Overcast
Yes
Rainy
Windy
False
Yes
True
No
Sunny
Humidity
High
No
Normal
Yes
8. Write a program NAIVE BAYESIAN CLASSIFIER for or a sample training
data set stored?
Source code:
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
y = data.iloc[:, -1]
print("\nThe first 5 values of the train output are:\n", y.head())
le_temperature = LabelEncoder()
X['Temperature'] = le_temperature.fit_transform(X['Temperature'])
le_humidity = LabelEncoder()
X['Humidity'] = le_humidity.fit_transform(X['Humidity'])
le_windy = LabelEncoder()
X['Windy'] = le_windy.fit_transform(X['Windy'])
le_play_tennis = LabelEncoder()
y = le_play_tennis.fit_transform(y)
print("\nNow the train output is:\n", y)
output:
The first 5 values of the data are:
Outlook Temperature Humidity Windy PlayTennis
0 Sunny Hot High False No
1 Sunny Hot High True No
2 Overcast Hot High False Yes
3 Rainy Mild High False Yes
4 Rainy Cool Normal False Yes