0% found this document useful (0 votes)
36 views31 pages

Machine Learning Practical File Overview

very useful

Uploaded by

Ramandeep kaur
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)
36 views31 pages

Machine Learning Practical File Overview

very useful

Uploaded by

Ramandeep kaur
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

SHAHEED BHAGAT SINGH STATE TECHNICAL

UNIVERSITY , FEROZEPUR

MACHINE LEARNING PRACTICAL FILE


(BTCS-609-18)

Submitted by :- Submitted to:-


Ramandeep kaur [Link] Singh
[Link] (CSE) Ghuman
2007553
INDEX

[Link] LIST OF EXPERIMENTS PAGE NO. DATE


1. Implement data pre-processing. 1-5

2. Deploy simple linear regression. 6-7

3. Simulate multiple linear regression. 8-9

4. Implement decision tree. 10 - 11

5. Deploy random forest classification. 12 - 15

6. Simulate naïve bayes algorithm. 16 - 18

7. Implement K – nearest neighbors (K – NN) , 19 - 20


k-means.

8. Deploy support vector machine , apriori 21 - 24


algorithm.

9. Simulate artificial neural network. 25 - 26

10. Implement the genetic algorithm code. 27 - 29


EXPERIMENT NO :- 1
AIM :- Implement Data Pre-Processing.
What is Data Preprocessing?
Data Preprocessing includes the steps we need to follow to transform or encode data so that it
may be easily parsed by the machine.
The main agenda for a model to be accurate and precise in predictions is that the algorithm
should be able to easily interpret the data's features.

Why is Data Preprocessing important?


The majority of the real-world datasets for machine learning are highly susceptible to be
missing, inconsistent, and noisy due to their heterogeneous origin.
Applying data mining algorithms on this noisy data would not give quality results as they would
fail to identify patterns effectively. Data Processing is, therefore, important to improve the
overall data quality.
 Duplicate or missing values may give an incorrect view of the overall statistics of data.
 Outliers and inconsistent data points often tend to disturb the model’s overall learning,
leading to false predictions.
Quality decisions must be based on quality data. Data Preprocessing is important to get this
quality data, without which it would just be a Garbage In, Garbage Out scenario.

Features in machine learning :-


Individual independent variables that operate as an input in our machine learning model are referred
to as features. They can be thought of as representations or attributes that describe the data and help
the models to predict the classes/labels.
For example, features in a structured dataset like in a CSV format refer to each column representing a
measurable piece of data that can be used for analysis: Name, Age, Sex, Fare, and so on.

4 Steps in Data Preprocessing :-

1
Now, let's discuss more in-depth four main stages of data preprocessing.

Data Cleaning
Data Cleaning is particularly done as part of data preprocessing to clean the data by filling missing
values, smoothing the noisy data, resolving the inconsistency, and removing outliers.
1. Missing values:-
Here are a few ways to solve this issue:
 Ignore those tuples
This method should be considered when the dataset is huge and numerous missing values are
present within a tuple.
 Fill in the missing values
There are many methods to achieve this, such as filling in the values manually, predicting the
missing values using regression method, or numerical methods like attribute mean.

2. Noisy Data:-
It involves removing a random error or variance in a measured variable. It can be done with the help
of the following techniques:
 Binning
It is the technique that works on sorted data values to smoothen any noise present in it. The data is
divided into equal-sized bins, and each bin/bucket is dealt with independently. All data in a segment
can be replaced by its mean, median or boundary values.
 Regression
This data mining technique is generally used for prediction. It helps to smoothen noise by fitting all
the data points in a regression function. The linear regression equation is used if there is only one
independent attribute; else Polynomial equations are used.
 Clustering

2
Creation of groups/clusters from data having similar values. The values that don't lie in the cluster can
be treated as noisy data and can be removed.

3. Removing outliers:-
Clustering techniques group together similar data points. The tuples that lie outside the cluster are
outliers/inconsistent data.

Data Integration
Data Integration is one of the data preprocessing steps that are used to merge the data present in
multiple sources into a single larger data store like a data warehouse.
Data Integration is needed especially when we are aiming to solve a real-world scenario like detecting
the presence of nodules from CT Scan images. The only option is to integrate the images from
multiple medical nodes to form a larger database.
We might run into some issues while adopting Data Integration as one of the Data
Preprocessing steps:
 Schema integration and object matching: The data can be present in different formats,
and attributes that might cause difficulty in data integration.
 Removing redundant attributes from all data sources.
 Detection and resolution of data value conflicts.

Data Transformation
Once data clearing has been done, we need to consolidate the quality data into alternate forms by
changing the value, structure, or format of data using the below-mentioned Data Transformation
strategies.
Generalization
The low-level or granular data that we have converted to high-level information by using concept
hierarchies. We can transform the primitive data in the address like the city to higher-level
information like the country.
Normalization

3
It is the most important Data Transformation technique widely used. The numerical attributes are
scaled up or down to fit within a specified range. In this approach, we are constraining our data
attribute to a particular container to develop a correlation among different data points. Normalization
can be done in multiple ways, which are highlighted here:
 Min-max normalization
 Z-Score normalization
 Decimal scaling normalization

Attribute Selection
New properties of data are created from existing attributes to help in the data mining process. For
example, date of birth, data attribute can be transformed to another property like is_senior_citizen
for each tuple, which will directly influence predicting diseases or chances of survival, etc.

Aggregation
It is a method of storing and presenting data in a summary format. For example sales, data can be
aggregated and transformed to show as per month and year format.

Data Reduction
The size of the dataset in a data warehouse can be too large to be handled by data analysis and data
mining algorithms.
One possible solution is to obtain a reduced representation of the dataset that is much smaller in
volume but produces the same quality of analytical results.
Here is a walkthrough of various Data Reduction strategies.

Data cube aggregation


It is a way of data reduction, in which the gathered data is expressed in a summary form.

Dimensionality reduction
Dimensionality reduction techniques are used to perform feature extraction. The dimensionality of a
dataset refers to the attributes or individual features of the data. This technique aims to reduce the
number of redundant features we consider in machine learning algorithms. Dimensionality reduction
can be done using techniques like Principal Component Analysis etc.

Data compression
By using encoding technologies, the size of the data can significantly reduce. But compressing data
can be either lossy or non-lossy. If original data can be obtained after reconstruction from
compressed data, this is referred to as lossless reduction; otherwise, it is referred to as lossy
reduction.

Discretization
Data discretization is used to divide the attributes of the continuous nature into data with intervals.
This is done because continuous features tend to have a smaller chance of correlation with the target
variable. Thus, it may be harder to interpret the results. After discretizing a variable, groups

4
corresponding to the target can be interpreted. For example, attribute age can be discretized into bins
like below 18, 18-44, 44-60, above 60.

Numerosity reduction
The data can be represented as a model or equation like a regression model. This would save the
burden of storing huge datasets instead of a model.

Attribute subset selection


It is very important to be specific in the selection of attributes. Otherwise, it might lead to high
dimensional data, which are difficult to train due to underfitting/overfitting problems. Only attributes
that add more value towards model training should be considered, and the rest all can be discarded.

Data Quality Assessment


Data Quality Assessment includes the statistical approaches one needs to follow to ensure that the
data has no issues. Data is to be used for operations, customer management, marketing analysis, and
decision making—hence it needs to be of high quality.
The main components of Data Quality Assessment include:
1. The completeness with no missing attribute values
2. Accuracy and reliability in terms of information
3. Consistency in all features
4. Maintain data validity
5. It does not contain any redundancy
Data Quality Assurance process has involves three main activities.
Data profiling: It involves exploring the data to identify the data quality issues. Once the analysis of
the issues is done, the data needs to be summarized according to no duplicates, blank values etc
identified.
Data cleaning: It involves fixing data issues.
Data monitoring: It involves maintaining data in a clean state and having a continuous check on
business needs being satisfied by the data.

5
EXPERIMENT NO :- 2
AIM :- Deploy Simple Linear Regression.

Code:-
import numpy as np
import [Link] as plt

def estimate_coef(x, y):


n = [Link](x)
m_x = [Link](x)
m_y = [Link](y)
SS_xy = [Link](y*x) - n*m_y*m_x
SS_xx = [Link](x*x) - n*m_x*m_x

b_1 = SS_xy / SS_xx


b_0 = m_y - b_1*m_x

return (b_0, b_1)

def plot_regression_line(x, y, b):


[Link](x, y, color = "m",
marker = "o", s = 30)

y_pred = b[0] + b[1]*x


[Link](x, y_pred, color = "g")
[Link]('x')
[Link]('y')
[Link]()

def main():
x = [Link]([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = [Link]([1, 3, 2, 5, 7, 8, 8, 9, 10, 12])

b = estimate_coef(x, y)
print("Estimated coefficients:\nb_0 = {} \
\nb_1 = {}".format(b[0], b[1]))

plot_regression_line(x, y, b)

if __name__ == "__main__":
main()

6
output :-

And graph obtained looks like this:

7
EXPERIMENT NO :- 3
AIM :- Simulate Multiple Linear Regression.

Code :-
import numpy as np
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import [Link] as plt

def generate_dataset(n):
x = []
y = []
random_x1 = [Link]()
random_x2 = [Link]()
for i in range(n):
x1 = i
x2 = i/2 + [Link]()*n
[Link]([1, x1, x2])
[Link](random_x1 * x1 + random_x2 * x2 + 1)
return [Link](x), [Link](y)

x, y = generate_dataset(200)

[Link]['[Link]'] = 12

fig = [Link]()
ax = fig.add_subplot(projection ='3d')

[Link](x[:, 1], x[:, 2], y, label ='y', s = 5)


[Link]()
ax.view_init(45, 0)

[Link]()

8
output : -

9
EXPERIMENT NO :- 4
AIM :- Implement Decision Tree.

Code :-
import pandas as pd
import numpy as np
import [Link] as plt
from sklearn import metrics
import seaborn as sns
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from sklearn import tree

iris = load_iris()
data = [Link](data = [Link], columns = iris.feature_names)
data['Species'] = [Link]
target = [Link]([Link])
target_n = [Link](iris.target_names)
target_dict = dict(zip(target, target_n))
data['Species'] = data['Species'].replace(target_dict)
x = [Link](columns = "Species")
y = data["Species"]
names_features = [Link]
target_labels = [Link]()
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 93)
from [Link] import DecisionTreeClassifier

dtc = DecisionTreeClassifier(max_depth = 3, random_state = 93)


[Link](x_train, y_train)
[Link](figsize = (30, 10), facecolor = 'b')
Tree = tree.plot_tree(dtc, feature_names = names_features, class_names = target_lab
els, rounded = True, filled = True, fontsize = 14)
[Link]()

10
y_pred = [Link](x_test)
confusion_matrix = metrics.confusion_matrix(y_test, y_pred)
matrix = [Link](confusion_matrix)
axis = [Link]()
[Link](font_scale = 1.3)
[Link](figsize = (10,7))
[Link](matrix, annot = True, fmt = "g", ax = axis, cmap = "magma")
axis.set_title('Confusion Matrix')
axis.set_xlabel("Predicted Values", fontsize = 10)
axis.set_xticklabels([''] + target_labels)
axis.set_ylabel( "True Labels", fontsize = 10)
axis.set_yticklabels(list(target_labels), rotation = 0)
[Link]()

output :-

11
EXPERIMENT NO :- 5

AIM :- Deploy Random Forest Classification.

Implementation steps :-

1. Data Pre-Processing Step:-


Below is the code for the pre-processing step:
import numpy as nm
import [Link] as mtp
import pandas as pd
data_set= pd.read_csv('user_data.csv')
x= data_set.iloc[:, [2,3]].values
y= data_set.iloc[:, 4].values
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test= train_test_split(x, y, test_size= 0.25, random_state=0)
from [Link] import StandardScaler
st_x= StandardScaler()
x_train= st_x.fit_transform(x_train)
x_test= st_x.transform(x_test)

In the above code, we have pre-processed the data. Where we have loaded the dataset, which
is given as:

12
2. Fitting the Random Forest algorithm to the training set:-
Now we will fit the Random forest algorithm to the training set. To fit it, we will import
the RandomForestClassifier class from the [Link] library. The code is given
below:
#Fitting Decision Tree classifier to the training set
from [Link] import RandomForestClassifier
classifier= RandomForestClassifier(n_estimators= 10, criterion="entropy")
[Link](x_train, y_train)

output :-

3. Predicting the Test Set result:-


Since our model is fitted to the training set, so now we can predict the test result. For
prediction, we will create a new prediction vector y_pred. Below is the code for it:
#Predicting the test set result
y_pred= [Link](x_test)

output :-

13
4. Creating the Confusion Matrix:-
Now we will create the confusion matrix to determine the correct and incorrect
predictions. Below is the code for it:
#Creating the Confusion matrix
from [Link] import confusion_matrix
cm= confusion_matrix(y_test, y_pred)

output :-

5. Visualizing the training Set result:-


Below is the code for it:
from [Link] import ListedColormap
x_set, y_set = x_train, y_train
x1, x2 = [Link]([Link](start = x_set[:, 0].min() -
1, stop = x_set[:, 0].max() + 1, step =0.01),
[Link](start = x_set[:, 1].min() - 1, stop = x_set[:, 1].max() + 1, step = 0.01))
[Link](x1, x2, [Link]([Link]([[Link](), [Link]()]).T).reshape([Link])alp
ha = 0.75, cmap = ListedColormap(('purple','green' )))
[Link]([Link](), [Link]())
[Link]([Link](), [Link]())
for i, j in enumerate([Link](y_set)):
[Link](x_set[y_set == j, 0], x_set[y_set == j, 1],
c = ListedColormap(('purple', 'green'))(i), label = j)
[Link]('Random Forest Algorithm (Training set)')
[Link]('Age')
[Link]('Estimated Salary')
[Link]()
[Link]()

14
Output :-

6. Visualizing the test set result:-


Below is the code for it:
#Visulaizing the test set result
from [Link] import ListedColormap
x_set, y_set = x_test, y_test
x1, x2 = [Link]([Link](start = x_set[:, 0].min() -
1, stop = x_set[:, 0].max() + 1, step =0.01),
[Link](start = x_set[:, 1].min() - 1, stop = x_set[:, 1].max() + 1, step = 0.01))
[Link](x1, x2, [Link]([Link]([[Link](), [Link]()]).T).reshape([Link]),
alpha = 0.75, cmap = ListedColormap(('purple','green' )))
[Link]([Link](), [Link]())
[Link]([Link](), [Link]())
for i, j in enumerate([Link](y_set)):
[Link](x_set[y_set == j, 0], x_set[y_set == j, 1],
c = ListedColormap(('purple', 'green'))(i), label = j)
[Link]('Random Forest Algorithm(Test set)')
[Link]('Age')
[Link]('Estimated Salary')
[Link]()
[Link]()
output :-

15
EXPERIMENT NO :- 6

AIM :-Simulate Naïve Bayes Algorithm.

Code :-
import math
import random
import csv
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

def splitting(mydata, ratio):


train_num = int(len(mydata) * ratio)
train = []
test = list(mydata)
while len(train) < train_num:
# 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

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

def mean(numbers):
return sum(numbers) / float(len(numbers))

16
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)]
del info[-1]
return info

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

def calculateGaussianProbability(x, mean, stdev):


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

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
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

def getPredictions(info, test):


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

17
[Link](result)
return predictions
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

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


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]]

ratio = 0.7
train_data, test_data = splitting(mydata, ratio)
print('Total number of examples are: ', len(mydata))
print('Out of these, training examples are: ', len(train_data))
print("Test examples are: ", len(test_data))

info = MeanAndStdDevForClass(train_data)

predictions = getPredictions(info, test_data)


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

output :-

18
EXPERIMENT NO :- 7

AIM :- Implement K-Nearest Neighbors (K-NN), K-Means.

Code :-
from [Link] import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from [Link] import load_iris
import numpy as np
import [Link] as plt

irisData = load_iris()

X = [Link]
y = [Link]

X_train, X_test, y_train, y_test = train_test_split(


X, y, test_size = 0.2, random_state=42)

neighbors = [Link](1, 9)
train_accuracy = [Link](len(neighbors))
test_accuracy = [Link](len(neighbors))

for i, k in enumerate(neighbors):
knn = KNeighborsClassifier(n_neighbors=k)
[Link](X_train, y_train)

train_accuracy[i] = [Link](X_train, y_train)


test_accuracy[i] = [Link](X_test, y_test)

[Link](neighbors, test_accuracy, label = 'Testing dataset Accuracy')


[Link](neighbors, train_accuracy, label = 'Training dataset Accuracy')

[Link]()
[Link]('n_neighbors')
[Link]('Accuracy')
[Link]()

19
output :-

20
EXPERIMENT N0 :- 8

AIM :- Deploy Support Vector Machine , Apriori Algorithm.

1. Support Vector Machine :-


Code :-
from [Link].samples_generator import make_blobs
X, Y = make_blobs(n_samples=500, centers=2,
random_state=0, cluster_std=0.40)
import [Link] as plt
[Link](X[:, 0], X[:, 1], c=Y, s=50, cmap='spring');
[Link]()

xfit = [Link](-1, 3.5)


[Link](X[:, 0], X[:, 1], c=Y, s=50, cmap='spring')
for m, b, d in [(1, 0.65, 0.33), (0.5, 1.6, 0.55), (-0.2, 2.9, 0.2)]:
yfit = m * xfit + b
[Link](xfit, yfit, '-k')
plt.fill_between(xfit, yfit - d, yfit + d, edgecolor='none',
color='#AAAAAA', alpha=0.4)

[Link](-1, 3.5);
[Link]()

output :-

21
2. Apriori Algorithm :-
Implementation steps :-
Step 1: Importing the required libraries:-
import numpy as np
import pandas as pd
from mlxtend.frequent_patterns import apriori, association_rules

Step 2: Loading and exploring the data:-


cd C:\Users\Dev\Desktop\Kaggle\Apriori Algorithm

data = pd.read_excel('Online_Retail.xlsx')
[Link]()

[Link]

[Link]()

Step 3: Cleaning the Data:-


data['Description'] = data['Description'].[Link]()

[Link](axis = 0, subset =['InvoiceNo'], inplace = True)


data['InvoiceNo'] = data['InvoiceNo'].astype('str')

data = data[~data['InvoiceNo'].[Link]('C')]

Step 4: Splitting the data according to the region of transaction:-

22
basket_France = (data[data['Country'] =="France"]
.groupby(['InvoiceNo', 'Description'])['Quantity']
.sum().unstack().reset_index().fillna(0)
.set_index('InvoiceNo'))

basket_UK = (data[data['Country'] =="United Kingdom"]


.groupby(['InvoiceNo', 'Description'])['Quantity']
.sum().unstack().reset_index().fillna(0)
.set_index('InvoiceNo'))

basket_Por = (data[data['Country'] =="Portugal"]


.groupby(['InvoiceNo', 'Description'])['Quantity']
.sum().unstack().reset_index().fillna(0)
.set_index('InvoiceNo'))

basket_Sweden = (data[data['Country'] =="Sweden"]


.groupby(['InvoiceNo', 'Description'])['Quantity']
.sum().unstack().reset_index().fillna(0)
.set_index('InvoiceNo'))

Step 5: Hot encoding the Data:-


def hot_encode(x):
if(x<= 0):
return 0
if(x>= 1):
return 1
basket_encoded = basket_France.applymap(hot_encode)
basket_France = basket_encoded
basket_encoded = basket_UK.applymap(hot_encode)
basket_UK = basket_encoded
basket_encoded = basket_Por.applymap(hot_encode)
basket_Por = basket_encoded
basket_encoded = basket_Sweden.applymap(hot_encode)
basket_Sweden = basket_encoded

Step 6: Building the models and analyzing the results:-


a) France:
frq_items = apriori(basket_France, min_support = 0.05, use_colnames = True)
rules = association_rules(frq_items, metric ="lift", min_threshold = 1)
rules = rules.sort_values(['confidence', 'lift'], ascending =[False, False])
print([Link]())

23
b) United Kingdom:
frq_items = apriori(basket_UK, min_support = 0.01, use_colnames = True)
rules = association_rules(frq_items, metric ="lift", min_threshold = 1)
rules = rules.sort_values(['confidence', 'lift'], ascending =[False, False])
print([Link]())

c) Portugal:
frq_items = apriori(basket_Por, min_support = 0.05, use_colnames = True)
rules = association_rules(frq_items, metric ="lift", min_threshold = 1)
rules = rules.sort_values(['confidence', 'lift'], ascending =[False, False])
print([Link]())

d) Sweden:
frq_items = apriori(basket_Sweden, min_support = 0.05, use_colnames = True)
rules = association_rules(frq_items, metric ="lift", min_threshold = 1)
rules = rules.sort_values(['confidence', 'lift'], ascending =[False, False])
print([Link]())

24
EXPERIMENT NO :- 9

AIM :- Simulate Artificial Intelligence Neural Network.

Code :-
import numpy as np
from [Link] import expit as activation_function
from [Link] import truncnorm

def truncated_normal(mean=0, sd=1, low=0, upp=10):


return truncnorm(
(low - mean) / sd, (upp - mean) / sd, loc=mean, scale=sd)

class Nnetwork:

def __init__(self,
no_of_in_nodes,
no_of_out_nodes,
no_of_hidden_nodes,
learning_rate):
self.no_of_in_nodes = no_of_in_nodes
self.no_of_out_nodes = no_of_out_nodes
self.no_of_hidden_nodes = no_of_hidden_nodes
self.learning_rate = learning_rate
self.create_weight_matrices()

def create_weight_matrices(self)
rad = 1 / [Link](self.no_of_in_nodes)
X = truncated_normal(mean=0, sd=1, low=-rad, upp=rad)
self.weights_in_hidden = [Link]((self.no_of_hidden_nodes,
self.no_of_in_nodes))
rad = 1 / [Link](self.no_of_hidden_nodes)
X = truncated_normal(mean=0, sd=1, low=-rad, upp=rad)
self.weights_hidden_out = [Link]((self.no_of_out_nodes,
self.no_of_hidden_nodes))

def train(self, input_vector, target_vector):


pass
def run(self, input_vector):

input_vector = [Link](input_vector, ndmin=2).T

25
input_hidden = activation_function(self.weights_in_hidden @ input_vector)
output_vector = activation_function(self.weights_hidden_out @ input_hidden)
return output_vector
simple_network = Nnetwork(no_of_in_nodes=2,
no_of_out_nodes=2,
no_of_hidden_nodes=4,
learning_rate=0
simple_network.run([(3, 4)])

output :-

26
EXPERIMENT NO :- 10

AIM :- Impement The Genetic Algorithm Code.

Code :-
import random
POPULATION_SIZE = 20
GENES = '''abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP
QRSTUVWXYZ 1234567890, .-;:_!"#%&/()=?@${[]}'''
TARGET = "genetic algorithm"

class Individual(object):
def __init__(self, chromosome):
[Link] = chromosome
[Link] = self.cal_fitness()

@classmethod
def mutated_genes(self):
global GENES
gene = [Link](GENES)
return gene

@classmethod
def create_gnome(self):
global TARGET
gnome_len = len(TARGET)
return [self.mutated_genes() for _ in range(gnome_len)]

def mate(self, par2):


child_chromosome = []
for gp1, gp2 in zip([Link], [Link]):

prob = [Link]()
if prob < 0.45:
child_chromosome.append(gp1)
elif prob < 0.90:
child_chromosome.append(gp2)
else:
child_chromosome.append(self.mutated_genes())
return Individual(child_chromosome)
def cal_fitness(self):

27
global TARGET
fitness = 0
for gs, gt in zip([Link], TARGET):
if gs != gt: fitness+= 1
return fitness
def main():
global POPULATION_SIZE
generation = 1
found = False
population = []

for _ in range(POPULATION_SIZE):
gnome = Individual.create_gnome()
[Link](Individual(gnome))

while not found:


population = sorted(population, key = lambda x:[Link])
if population[0].fitness <= 0:
found = True
break

new_generation = []
s = int((10*POPULATION_SIZE)/100)
new_generation.extend(population[:s])
s = int((90*POPULATION_SIZE)/100)
for _ in range(s):
parent1 = [Link](population[:50])
parent2 = [Link](population[:50])
child = [Link](parent2)
new_generation.append(child)

population = new_generation

print("Generation: {}\tString: {}\tFitness: {}".\


format(generation,
"".join(population[0].chromosome),
population[0].fitness))

generation += 1
print("Generation: {}\tString: {}\tFitness: {}".\
format(generation,population[0].fitness))

if __name__ == '__main__':
main()

28
output :-

29

Common questions

Powered by AI

A Random Forest classifier is configured using parameters like the number of estimators and criterion (e.g., entropy). It aggregates decisions from multiple trees to enhance prediction robustness by fitting the model to a training set and generating predictions for unseen data, as demonstrated through iterative steps including confusion matrix creation and visualization .

Strategies for handling missing data include ignoring tuples with missing values when the dataset is large and has numerous missing values in each tuple, or filling them using methods like manual entry, regression predictions, or numerical techniques like replacing with the attribute mean . The choice depends on dataset size and the nature/tendency of missing data .

Decision trees classify data by segmenting it based on feature thresholds into leaf nodes that represent decisions . Visualization can be achieved by plotting the decision tree with features and class names to understand the decision paths taken by the model .

Data discretization converts continuous variables into discrete categories, making it easier to spot patterns and relationships within the data and assess correlation with target variables. It changes the form of data, impacting how results and trends are interpreted .

Binning smooths noisy data by dividing it into equal-sized bins and replacing data in each bin with measures like the mean or median . Regression helps by fitting data points to a function, reducing variance, and clustering groups data into similar value sets, treating outliers outside clusters as noise .

Data integration addresses challenges by merging data from multiple sources to form a comprehensive dataset, useful in real-world scenarios like detecting medical conditions from diverse sources . Problems include schema integration issues, object matching difficulties, and redundant attribute removal .

Data cube aggregation summarizes data, reducing its complexity, while dimensionality reduction shrinks the feature set, minimizing redundancy and improving model training efficiency . These techniques enhance performance by focusing on relevant data aspects and reducing computational load .

Min-max normalization scales data to a specific range, z-score normalizes data by centering and scaling based on standard deviation, and decimal scaling normalizes by reducing the influence of magnitude . These techniques are crucial for ensuring data attributes fit a useful range for model correlation and analysis .

Data preprocessing is critical in machine learning because it improves the overall quality of data, ensuring that algorithms can more effectively interpret the data's features for accurate and precise predictions . It addresses key issues such as missing and inconsistent values, noisy data, and outliers that can lead to poor quality results if not handled properly . Without preprocessing, models may give false predictions due to garbage-in, garbage-out scenarios .

Data transformation involves altering data value, structure, or format to enhance interpretability . Techniques like generalization convert granular data to high-level concepts, such as transforming a city name into a country, which aids in understanding patterns on a broader scale .

You might also like