At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

An Introduction to Machine Learning With Sample Program

  • By Gowtham S
  • April 25, 2018
  • 1954 Views

Hi Geeks, As we all are very curious over Machine Learning, Just like the same I started involving myself in exploring more about Machine Learning just like you. Thankfully I had found all the basics introductions of Machine Learning and all about its advancements, but to be precise, i didn’t had an idea about where to start or how.
So the idea behind this article is to help you guys to kick start machine learning with the sample program.

Machine Learning

Machine learning (ML) is a type of artificial intelligence (AI) that allows software applications to become more accurate in predicting outcomes without being explicitly programmed. The basic premise of Machine Learning is to build algorithms that can receive input data and use statistical analysis to predict.

Categories of Machine Learning

There are 3 types in Machine Learning as listed below,
(i) Supervised Learning
(ii) Unsupervised Learning
(iii) Reinforcement learning

Supervised Learning

Supervised learning is the process of enabling the model to predict the future output values based on the previous examples. And the previous datasets will have the label. For example, The data set will have the inputs based on the outputs, Therefore, Supervised learning will predict the data based on the given examples.
Ex: Linear regression, logistic regression, random forest, decision tree, etc.

Unsupervised learning

In unsupervised learning the model to predict the future output values based on datasets which don’t have labels. Additionally, This model will group or cluster the datasets to detect the hidden patterns.
Ex: Apriori algorithm, K-means.

Reinforcement learning

Reinforcement learning works like a computational approach which will work based on the goal based learning and decision-making model. Which allows software agents and machines to automatically determine the ideal behavior within a specific context, to maximize its performance. Here the machine will have the direct interaction with the live environment.
Reinforcement learning will targets the explicit goals but Instead of that, reinforcement learning is forced to learn optimal goal by giving trial and error

Sample Program Of Supervised learning Using Python

As we mentioned, Here we start with the sample program of Supervised learning using python.
Before that, we need to install Python, Sklearn in our system.

Creating Dataset:

Create a file like linear_ex.py
DatasetPart:

from random import randint
Limit=1000
Count = 100
TRAIN_INPUT = list()
TRAIN_OUTPUT = list()
for i in range(Count):
a = randint(0, Limit)
b = randint(0, Limit)
c = randint(0, Limit)
op = a + (2*b) + (3*c)
TRAIN_INPUT.append([a, b, c])
TRAIN_OUTPUT.append(op)

 
1. First we are import the randint
2. Then setting random int limit as 1000
3. We set the count of the data set limit as 100
4. we create two lists named as TRAIN_INPUT and TRAIN_OUTPUT
5. Finally, We are creating the data set with the below code

for i in range(Count):
a = randint(0, Limit)
b = randint(0, Limit)
c = randint(0, Limit)
op = a + (2*b) + (3*c)

 
6. Appending the data set into our TRAIN_INPUT and TRAIN_OUTPUT

Execution

Step 1:
Importing LinearRegression from sklearn.linear_model

from sklearn.linear_model import LinearRegression

 
Step 2:
Below codes involves to train the model

predictor = LinearRegression(n_jobs=-1)
predictor.fit(X=TRAIN_INPUT, y=TRAIN_OUTPUT)

 
Step 3:
Finally, we can predict a value by the trained model

X_TEST=[[20,30,45]]
outcome= predictor.predict(X=X_TEST)
coefficients=predictor.coef_
Print (‘output:{}\ncoefficients:{}”.format(outcome, coefficients))

 
Step 4:
Setting an outcome variable to get an output.
Step 5:
Finally, we can get the coefficients of the inputs as you can see in below code and final output.

Source Code

Linear_ex.py:
from random import randint
from sklearn.linear_model import LinearRegression
Limit=1000
Count = 100
TRAIN_INPUT = list()
TRAIN_OUTPUT = list()
for i in range(Count):
a = randint(0, Limit)
b = randint(0, Limit)
c = randint(0, Limit)
op = a + (2*b) + (3*c)
TRAIN_INPUT.append([a, b, c])
TRAIN_OUTPUT.append(op)
predictor = LinearRegression(n_jobs=-1)
predictor.fit(X=TRAIN_INPUT, y=TRAIN_OUTPUT)
X_TEST=[[20,30,45]]
outcome= predictor.predict(X=X_TEST)
coefficients=predictor.coef_
Print (‘output:{}\ncoefficients:{}”.format(outcome, coefficients))

 

Result

Output[215]
Coefficients[1.2.3]

 
To start with the basics, I just covered with supervised learning and eventually will explore more about Machine Learning in upcoming blogs. In case if you have any queries in this blog leave your feedback in the comment section or if you found it helpful share it with your other friends too. Let’s grow each other.