ML practical Lovepreet 6-10
ML practical Lovepreet 6-10
Python Code:
# Naive Bayes
# Importing the libraries import numpy as np import matplotlib.pyplot as plt import pandas as pd
# Splitting the dataset into the Training set and Test set 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)
# Training the Naive Bayes model on the Training set from sklearn.naive_bayes import GaussianNB
classifier = GaussianNB() classifier.fit(X_train, y_train)
# Visualising the Training set results from matplotlib.colors import ListedColormap X_set, y_set =
X_train, y_train X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:,
0].max() + 1, step = 0.01),
np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max()) plt.ylim(X2.min(), X2.max()) for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
c = ListedColormap(('red', 'green'))(i), label = j)
plt.title('Naive Bayes (Training set)') plt.xlabel('Age') plt.ylabel('Estimated Salary') plt.legend()
plt.show()
Output:
Practical Outcomes:
The rain forest algorithm is a machine learning algorithm that is easy to use and flexible. It uses ensemble
learning, which enables organizations to solve regression and classification problems. This is an ideal
algorithm for developers because it solves the problem of overfitting of datasets. It’s a very resourceful tool
for making accurate predictions needed in strategic decision making in organizations.
Python Code:
# K-Nearest Neighbors (K-NN) # Importing the libraries import numpy as np import
matplotlib.pyplot as plt import pandas as pd
# Splitting the dataset into the Training set and Test set 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)
# Training the K-NN model on the Training set from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2) classifier.fit(X_train,
y_train)
Output
Practical Outcomes:
Since the KNN algorithm requires no training before making predictions, new data can be added
seamlessly, which will not impact the accuracy of the algorithm. KNN is very easy to implement. There
are only two parameters required to implement KNN—the value of K and the distance function (e.g.
Euclidean, Manhattan, etc.
Applications:
KNN is widely used in almost all industries, such as healthcare, financial services, eCommerce, political
campaigns, etc. Healthcare companies use the KNN algorithm to determine if a patient is susceptible to
certain diseases and conditions. Financial institutions predict credit card ratings or qualify loan
applications and the likelihood of default with the help of the KNN algorithm. Political analysts classify
potential voters into separate classes based on whom they are likely to vote for.
Python Code:
# Support Vector Machine (SVM)
# Importing the libraries import numpy as np import matplotlib.pyplot as plt import pandas as pd
# Splitting the dataset into the Training set and Test set 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)
# Feature Scaling from sklearn.preprocessing import StandardScaler sc = StandardScaler() X_train =
sc.fit_transform(X_train) X_test = sc.transform(X_test)
# Training the SVM model on the Training set from sklearn.svm import SVC classifier = SVC(kernel =
'linear', random_state = 0) classifier.fit(X_train, y_train)
# Visualising the Training set results from matplotlib.colors import ListedColormap X_set, y_set =
X_train, y_train X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1,
step = 0.01),
Output:
Practical Outcomes:
It works really well with a clear margin of separation It is effective in high dimensional spaces. It is effective in
cases where the number of dimensions is greater than the number of samples. It uses a subset of training points
in the decision function (called support vectors), so it is also memory efficient.
Applications:
Face detection – SVMc classify parts of the image as a face and non-face and create a square boundary around
the face. Text and hypertext categorization – SVMs allow Text and hypertext categorization for both inductive
and transductive models. They use training data to classify documents into different categories. It categorizes
on the basis of the score generated and then compares with the threshold value. Classification of images – Use
of SVMs provides better search accuracy for image classification. It provides better accuracy in comparison to
the traditional query-based searching techniques. Bioinformatics – It includes protein classification and cancer
classification. We use SVM for identifying the classification of genes, patients on the basis of genes and other
biological problems. Protein fold and remote homology detection – Apply SVM algorithms for protein remote
homology detection. Handwriting recognition – We use SVMs to recognize handwritten characters used
widely. Generalized predictive control(GPC) – Use SVM based GPC to control chaotic dynamics with useful
parameters.
Python Code:
# Importing the libraries import numpy as np import matplotlib.pyplot as plt import pandas as pd
# Splitting the dataset into the Training set and Test set 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)
# Training the SVM model on the Training set from sklearn.svm import SVC classifier = SVC(kernel =
'linear', random_state = 0) classifier.fit(X_train, y_train)
# Visualising the Training set results from matplotlib.colors import ListedColormap X_set, y_set =
X_train, y_train X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1,
step = 0.01),
Output:
Practical Outcomes:
Know the main provisions neuromathematics; Know the main types of neural networks; Know and apply the
methods of training neural networks; Know the application of artificial neural networks; To be able to formalize
the problem, to solve it by using a neural network
Applications of ANN a. Classification of data
b. Anomaly detection:
Given the details about transactions of a person, it can say that whether the transaction is fraud or not.
c. Speech recognition:
We can train our neural network to recognize speech patterns. Example: Siri, Alexa, Google assistant.
d. Audio generation:
Given the inputs as audio files, it can generate new music based on various factors like genre, singer, and others.
e. Time series analysis:
A well trained neural network can predict the stock price.
f. Spell checking:
We can train a neural network that detects misspelled spellings and can also suggest a similar meaning for words.
Example: Grammarly
g. Character recognition:
A well trained neural network can detect handwritten characters.
h. Machine translation:
We can develop a neural network that translates one language into another language.
i. Image processing:
We can train a neural network to process an image and extract pieces of information from it.
:
Based on a set of data, our trained neural network predicts whether it is a dog or a cat?
Initial Population
Fitness function
Selection
population
Cross-over
Mutation
Repeat step 2-5 again for each generation
– Initialize the population randomly based on the data.
– Find the fitness value of the each of the chromosomes(a chromosome is a set of parameters which define a
proposed solution to the problem that the genetic algorithm is trying to solve)
– Select the best fitted chromosomes as parents to pass the genes for the next generation and create a new
– Create new set of chromosome by combining the parents and add them to new population set
– Perform mutation which alters one or more gene values in a chromosome in the new population set
generated.
Mutation helps in getting more diverse opportunity. Obtained population will be used in the next
generation
Python Code:
import numpy as np import pandas as pd import random import matplotlib.pyplot from sklearn.datasets
import load_breast_cancer from sklearn.model_selection import train_test_split from sklearn.linear_model
import LogisticRegression from sklearn.metrics import accuracy_score #import the breast cancer dataset
from sklearn.datasets import load_breast_cancer cancer=load_breast_cancer() df =
pd.DataFrame(cancer['data'],columns=cancer['feature_names']) label=cancer["target"] #splitting the model
into training and testing set X_train, X_test, y_train, y_test = train_test_split(df,
#training a logistics regression model logmodel = LogisticRegression() logmodel.fit(X_train,y_train)
predictions = logmodel.predict(X_test) print("Accuracy = "+ str(accuracy_score(y_test,predictions)))
#defining various steps required for the genetic algorithm def initilization_of_population(size,n_feat):
chromosome = np.ones(n_feat,dtype=np.bool) chromosome[:int(0.3*n_feat)]=False
np.random.shuffle(chromosome) population.append(chromosome)
for chromosome in population:
logmodel.fit(X_train.iloc[:,chromosome],y_train) predictions =
logmodel.predict(X_test.iloc[:,chromosome]) scores.append(accuracy_score(y_test,predictions))
scores, population = np.array(scores), np.array(population) inds = np.argsort(scores) return
list(scores[inds][::-1]), list(population[inds,:][::-1])
def selection(pop_after_fit,n_parents):
population_nextgen.append(pop_after_fit[i])
return population_nextgen
def crossover(pop_after_sel):
population_nextgen=pop_after_sel for i in range(len(pop_after_sel)):
child=pop_after_sel[i] child[3:7]=pop_after_sel[(i+1)%len(pop_after_sel)][3:7]
population_nextgen.append(child)
return population_nextgen
def mutation(pop_after_cross,mutation_rate):
population_nextgen = [] for i in range(0,len(pop_after_cross)):
chromosome = pop_after_cross[i] for j in range(len(chromosome)):
Output:
Practical Outcomes:
The use of genetic algorithm in the field of robotics is quite big. Actually, genetic algorithm is being used to
create learning robots which will behave as a human and will do tasks like cooking our meal, do our laundry etc
Traffic and Shipment Routing (Travelling Salesman Problem)
This is a famous problem and has been efficiently adopted by many sales-based companies as it is time saving
and economical. This is also achieved using genetic algorithm.
Genetic Algorithms might be costly in computational terms since the evaluation of each individual requires the
training of a model. These algorithms can take a long time to converge since they have a stochastic nature
Genetic algorithms work on the Chromosome, which is an encoded version of potential solutions’ parameters,
rather the parameters themselves. Genetic algorithms use fitness score, which is obtained from objective
functions, without other derivative or auxiliary information
Applications: Robotics
Engineering Design
Engineering design has relied heavily on computer modeling and simulation to make design cycle process fast
and economical. Genetic algorithm has been used to optimize and provide a robust solution