Data Science (16 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (16 Blogs)
  • Data Science (16 Blogs)
  • Mastering Python (16 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

Machine Learning Tutorial for Beginners

11.4K Views

Become a Certified Professional

I have already covered the higher level concepts in of Machine Learning in my blog What is Machine Learning? As promised here is a tutorial blog in the series, titled “Machine Learning Tutorial”. This tutorial blog will help you to understand about:

Understanding Machine Learning with an Analogy

 

As a Human: Let’s suppose one day you went for shopping mangoes. The vendor had a cart full of mangoes from where you could handpick the mangoes, get them weighed and pay according to the rate fixed per Kg. 

Shopping Mangoes - Machine Learning Tutorial - Edureka

Task: How will you choose the best mangoes?

Given below is set of learning, human gains from his experience of shopping mangoes, you can drill it down to have a further look at it in detail. Go through it once, you will relate it to machine learning very easily.

Learning 1: Bright yellow mangoes are sweeter than pale yellow ones

Experience 1:

You were informed that bright and yellow mangoes are sweeter than pale and yellow ones.

So you make a simple rule: pick only from the bright yellow mangoes. You check the colour of the mangoes, pick the bright yellow ones, pay up, and return home. Right?

Learning 2: The smaller and bright yellow mangoes are sweet only half the time

Experience 2:

Now when you went home and tasted the mangoes, some of them were not sweet as you thought. You are worried as your wisdom was insufficient. You concluded that when it comes shopping mangoes, you have to look for more than just the colours.

After a lot of pondering and tasting different types of mangoes,

you concluded that the bigger and bright yellow mangoes are guaranteed to be sweet, while the smaller, bright yellow mangoes are sweet only half the time (i.e. if you bought 100 bright yellow mangoes (50 will be big in size and rest 50 will be small), then the 50 big mangoes will all be sweet, while out of the 50 small ones, only 25 mangoes will turn out to be sweet). You will then update your rule about the mango shopping and from next time you will keep this in mind. 

Learning 3: Small, pale yellow ones are the sweetest of all

Experience 3:

Tragedy: Next time at the market, you see that your favourite vendor has gone out of town. You decide to buy from a different vendor, who supplies mangoes grown from a different part of the country. Now, you realize that the rule which you had learnt (that big, bright yellow mangoes are the sweetest) is no longer applicable. You have to learn from scratch. You taste a mango of each kind from this vendor and realize that the small, pale yellow ones are in fact the sweetest of all.

Learning 4: Soft mangoes are jucier

Experience 4:

One day your cousin visits you from another city. You decide to treat her with mangoes. But she is like “I don’t care about the sweetness of a mango, I only want the juiciest ones”. Now once again, you run your experiments, tasting all kinds of mangoes, and realizing that the softer ones are juicier.

Learning 5: Green mangoes are tastier than yellow ones

Experience 5:

Later on, you move to a different part of the world and you found that the mangoes here taste surprisingly different from your home country. You realized that for this country the green mangoes are tastier than the yellow ones.

Learning 6: You don't need mangoes anymore

Experience 6:

You marry someone who hates mangoes but loves oranges instead. Now you go for shopping oranges instead of mangoes. Now, all your accumulated knowledge about mangoes is worthless. Now you have to learn everything about the correlation between the physical characteristics and the taste of apples, by the same method of experimentation. 

What if you have to write a code for it?

As a Human Written Code: Now, imagine you were asked to write a computer program to choose your mangoes (or oranges). You might write the following rules/algorithm:

if is bright yellow and size is big and sold by: mango is sweet.
if (soft): mango is juicy

You would use these rules to choose the mangoes.

Conclusion as a human:

But every time you make a new observation from your experiments, you have to modify the list of rules manually.

You have to understand the details of all the factors affecting the quality of mangoes. If the problem gets complicated enough, it might get difficult for you to make accurate rules by hand that covers all possible types of mangoes. This will take a lot of research and effort and not everyone has this amount of time.

This is where Machine Learning comes into the picture

Machine Learning Tutorial: What is Machine Learning?

Definition: Machine Learning is a concept which allows the machine to learn from examples and experience, and that too without being explicitly programmed. So instead of you writing the code, what you do is you feed data to the generic algorithm, and the algorithm/machine builds the logic based on the given data.

Learning Machine - Machine Learning Tutorial - Edureka

Machine Learning algorithms are an evolution of normal algorithms. They make your programs “smarter”, by allowing them to automatically learn from the data you provide. The algorithm is mainly divided into:

  • Training Phase
  • Testing phase

Training Phase

You take a randomly selected specimen of mangoes from the market (training data), make a table of all the physical characteristics of each mango, like color, size, shape, grown in which part of the country, sold by which vendor, etc (features), along with the sweetness, juiciness, ripeness of that mango (output variables). You feed this data to the machine learning algorithm (classification/regression), and it learns a model of the correlation between an average mango’s physical characteristics, and its quality.

training - machine learning tutorial
Testing - Machine Learning Tutorial

Testing Phase

Next time when you go shopping, you will measure the characteristics of the mangoes which you are purchasing(test data)and feed it to the Machine Learning algorithm. It will use the model which was computed earlier to predict if the mangoes are sweet, ripe and/or juicy. The algorithm may internally use the rules, similar to the one you manually wrote earlier (for eg, a decision tree). Finally, you can now shop for mangoes with great confidence, without worrying about the details of how to choose the best mangoes.

Conclusion as an Algorithm

You know what! you can make your algorithm improve over time (reinforcement learning) so that it will improve its accuracy as it gets trained on more and more training dataset. In case it makes a wrong prediction it will update its rule by itself.

The best part of this is, you can use the same algorithm to train different models. You can create one each for predicting the quality of apples, grapes, bananas, or whatever you want.

For more detailed explanation on Machine Learning Tutorial feel free to go through this video:

Machine Learning Tutorial | Machine Learning Algorithms | Edureka

Subscribe to our YouTube channel to stay updated with our fresh content

Biggest Confusion AI vs ML vs Deep Learning

Let’s move ahead in this Machine Learning tutorial and discuss one of the biggest confusion. People think all three of them the AI, ML and the Deep Learning are same. But this is WRONG!, let me clarify things for you.

Artificial Intelligence

Artificial Intelligence is the broader concept of machines being able to carry out tasks in a smarter way. It covers anything which enables the computers to behave like humans.  

Artificial Intelligence - Reasons to learn Python - Edureka
Artificial Intelligence - Machine Learning Tutorial - Edureka

Machine Learning

Machine Learning is a subset of AI and is based on the idea that machines should be given the access to data, and should be left to learn and explore for themselves. It deals with the extraction of patterns from a large data sets.

Deep Learning

Deep Learning is a subset of Machine Learning where similar Machine Learning Algorithms are used to train Deep Neural Networks so as to achieve better accuracy in those cases where former was not performing up to the mark.

Let’s categorize Machine Learning into its subparts and see what each of them are, how they work, and how each one of them is used in the real life.

Starting with Supervised Learning, So what is it?

Understanding Supervised Learning

Let’s see the mathematical definition of Supervised Learning.

Supervised learning is the one where you have input variables (x) and an output variable (Y) and you use an algorithm to learn the mapping function from the input to the output. it,

Y = f(X)

The goal is to approximate the mapping function so well that whenever you get some new input data (x), the machine can easily predict the output variables (Y) for that data.

What is Supervised Learning - Machine Learning Tutorial - EdurekaLet me rephrase you this in simple terms:

In Supervised machine learning algorithm, every instance of the training dataset consists of input attributes and expected output. The training dataset can take any kind of data as an input like values of a database row, the pixels of an image, or even an audio frequency histogram.

Now let me tell you why this category of machine learning is termed as supervised learning?

This category is termed as supervised learning because the process of an algorithm learning from the training dataset can be thought of as a teacher teaching his students. The algorithm continuously predicts the result on the basis of training data and is continuously corrected by the teacher. The learning continues until the algorithm achieves an acceptable level of performance.

Supervised Learning Usecases

Machine Learning Use Case - What is Machine Learning - Edureka

Cortana

Cortana or any speech automated system in your mobile phone trains your voice and then starts working based on this training. This is an application of Supervised Learning

Weather Apps

Predicts the upcoming weather by analyzing the parameters for a given time on some prior knowledge (when its sunny, temperature is higher; when its cloudy, humidity is higher, etc.).

How to create Android widgets: Information widget
machine learning tutorial- biometric-attendance

Biometric Attendance

In Biometric Attendance you can train the machine with inputs of your biometric identity – it can be your thumb, iris or ear-lobe, etc. Once the machine is trained it can validate your future input and can easily identify you.

Understanding Unsupervised Learning

 

So What is Unsupervised Learning? 

Mathematically, Unsupervised learning is where you only have input data (X) and no corresponding output variables.

The goal for unsupervised learning is to model the underlying structure or distribution in the data in order to learn more about the data.

Unsupervised Learning - Machine Learning TutorialLet me rephrase it for you in simple terms:

In the unsupervised learning approach, the sample of a training dataset does not have an expected output associated with them. Using the unsupervised learning algorithms you can detect patterns based on the typical characteristics of the input data. Clustering can be considered as an example of machine learning task that uses the unsupervised learning approach. The machine then groups similar data samples and identify different clusters within the data.

Now let me tell you why this category of machine learning is known as unsupervised learning?

Well, this category of machine learning is known as unsupervised because unlike supervised learning there is no teacher. Algorithms are left on their own to discover and return the interesting structure in the data.

Looking for a complete Machine Learning course?

Unsupervised Learning Usecases

Unsupervised Learning Example - Machine Learning Tutorial - Edureka

A friend invites you to his party where you meet totally strangers. Now you will classify them using unsupervised learning (no prior knowledge) and this classification can be on the basis of gender, age group, dressing, educational qualification or whatever way you would like. Since you didn’t have any prior knowledge about people and so you just classified them “on-the-go”.

Let’s suppose you have never seen a Football match before and by chance watch a video on internet, now you can classify players on the basis of different criterion like Players wearing the same sort of kits are in one class, Players of one style are in one class (players, goalkeeper, referee), or on the basis of playing style(attacker or defender) or whatever way you would observe, you can classify it.

football match - machine learning tutorial - edureka

Understanding Reinforcement Learning

So what is Reinforcement Learning?

Reinforcement learning can be thought of as a hit and trial method of learning. The machine gets a Reward or Penalty point for each action it performs. If the option is correct, the machine gains the reward point or gets a penalty point in case of a wrong response.

Reinforcement Training - Machine Learning Tutorial - EdurekaThe reinforcement learning algorithm is all about the interaction between the environment and the learning agent. The learning agent is based on exploration and exploitation.

Exploration is when the learning agent acts on trial and error and Exploitation is when it performs an action based on the knowledge gained from the environment. The environment rewards the agent for every correct action, which is the reinforcement signal. With the aim of collecting more rewards obtained, the agent improves its environment knowledge to choose or perform the next action.

Let see how Pavlov trained his dog using reinforcement training?

Pavlov divided the training of his dog into four stages.

In the first part, Pavlov gave meat to the dog, and in response to the meat, the dog started salivating.

In the next stage he created a sound with a bell, but this time the dogs did not respond anything.

In the third stage, he tried to train his dog by using the bell and then giving them food. Seeing the food the dog started salivating.

Eventually, the dogs started salivating just after hearing the bell, even if the food was not given as the dog was reinforced that whenever the master will ring the bell, he will get the food.Pavlov Training - Machine Learning Tutorial - Edureka

Machine Learning Using Python


On a new tool, it is always good to start with a small project. For example, in this case, classification of iris flowers on the iris dataset.

It’s a good project and is really very easy to understand.

  • All the attributes within the dataset are numeric, you just have to figure out how to load and handle data

  • It is a multi-class classification problem thereby allowing you to practice the supervised learning algorithm

  • 4 attributes and 150 rows, meaning it is small and easily fits into memory

  • All of the numeric attributes are in the same units and the same scale, not requiring any special scaling or transforms to get started

# Check the versions of libraries
# Python version
import sys
print('Python: {}'.format(sys.version))
# scipy
import scipy
print('scipy: {}'.format(scipy.__version__))
# numpy
import numpy
print('numpy: {}'.format(numpy.__version__))
# matplotlib
import matplotlib
print('matplotlib: {}'.format(matplotlib.__version__))
# pandas
import pandas
print('pandas: {}'.format(pandas.__version__))
# scikit-learn
import sklearn
print('sklearn: {}'.format(sklearn.__version__))

# Load libraries
import pandas
from pandas.plotting import scatter_matrix
import matplotlib.pyplot as plt
from sklearn import model_selection
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC

# Load dataset
url = https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data
names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
dataset = pandas.read_csv(url, names=names)

# shape
print(dataset.shape)

# head
print(dataset.head(20))

# head
print(dataset.head(20))

# descriptions
print(dataset.describe())

# class distribution
print(dataset.groupby('class').size())

# box and whisker plots
dataset.plot(kind='box', subplots=True, layout=(2,2), sharex=False, sharey=False)
plt.show()

# histograms
dataset.hist()
plt.show()

# scatter plot matrix
scatter_matrix(dataset)
plt.show()

# Split-out validation dataset
array = dataset.values
X = array[:,0:4]
Y = array[:,4]
validation_size = 0.20
seed = 7
X_train, X_validation, Y_train, Y_validation = model_selection.train_test_split(X, Y, test_size=validation_size, random_state=seed)
# Test options and evaluation metric
seed = 7
scoring = 'accuracy'

# Spot Check Algorithms
models = []
models.append(('LR', LogisticRegression()))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC()))

# Spot Check Algorithms
models = []
models.append(('LR', LogisticRegression()))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC()))

# evaluate each model in turn
results = []
names = []
for name, model in models:
	kfold = model_selection.KFold(n_splits=10, random_state=seed)
	cv_results = model_selection.cross_val_score(model, X_train, Y_train, cv=kfold, scoring=scoring)
	results.append(cv_results)
	names.append(name)
	msg = %s: %f (%f) % (name, cv_results.mean(), cv_results.std())
	print(msg)

# Make predictions on validation dataset
knn = KNeighborsClassifier()
knn.fit(X_train, Y_train)
predictions = knn.predict(X_validation)
print(accuracy_score(Y_validation, predictions))
print(confusion_matrix(Y_validation, predictions))
print(classification_report(Y_validation, predictions))

I hope this Machine Learning Tutorial blog was useful, feel free to visit our edureka’s youtube page where you will find tons of technical videos on several topics. If you want to excel in your career you can take Machine Learning Certification Training using Python from edureka. Don’t just blindly trust someone’s word, here is the link to the Course Curriculum, have a look at it and see if you find it really worth investing your time and effort. If you want to learn something more, add your suggestion in the comment box below, If the suggestion is good enough we might even plan to add it to our main course for you.

Machine Learning Training using Python

Comments
2 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.