Skip to content

The Data Scientist

the data scientist logo

An Introduction to Machine Learning with Examples (in Python and R)


Wanna become a data scientist within 3 months, and get a job? Then you need to check this out !

Introduction to machine learning

Machine learning is one of the favorite topics of this blog. In this post, we will show some simple machine learning examples for aspiring data scientists, in the simplest possible terms.

Machine Learning is a sub-type of Artificial Intelligence (AI) that predicts outcomes of scenarios without being explicitly programmed to do so. Machine Learning takes some input ‘x’ and predicts an outcome ‘y’.

Now let’s try to understand machine learning in simpler terms. Suppose, you’ve rolled a piece of paper into a ball. Now you’re trying to throw it in the bin.

The first time you apply too much force, it goes over the bin. The second time you apply less force and change your angle a bit, you realize the paper ball lands closer to the bin. So, you adjust your angle a bit and in the third attempt, you successfully throw the paper ball into the bin.

What happened was that you learned from your experiences, improved your aim, and finally managed to get the paper ball into the bin.

This led Alan Turing to ask the question, “Can machines do what we (as thinking elites) can do?”

Yes, and No. Real-world problems are often too complex and sometimes it can be extremely difficult to devise algorithms to solve them. But there are scenarios where machine learning thrives as well, such as predicting whether someone has cancer, forecasting bitcoin prices in the future, and classifying the sentiments of sentences. Now let’s discuss the different kinds of machine learning.

Supervised Learning vs Unsupervised Learning

There are three main types of machine learning but here we will only be discussing two of them. The types of machine learning you use depend on the type of data you have.

Machine learning example 1: Supervised Learning

In supervised learning, the machine learning model is provided with labeled data, and the input and the output are also specified. Training examples in which the inputs and the outputs are specified are provided to the model, the model trains on them and learns them, and then it is used to make predictions. For Example, in Linear Regression we give our model some past inputs and its outputs. The model learns from them and plots a line to show dependent variable changes with an independent variable.

Let’s look at how to do it in Python and R.

Here’s the code in Python:

import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt


x = np.array([151, 174, 138, 186, 128, 136, 179, 163, 152, 131])
y = np.array([63, 81, 56, 91, 47, 57, 76, 72, 62, 48])

x=x.reshape(10,1)
y=y.reshape(10,1)

model = LinearRegression()
model.fit(x, y)
print(model.predict([[170]]))

plt.figure(figsize=(9,6))
plt.scatter(y,x)
lx = np.linspace(40,90,10)
ly = np.linspace(120, 190, 10)
plt.plot(lx, ly)
plt.title("Height & Weight Regression")
plt.xlabel("Weight in Kg")
plt.ylabel("Height in cm")
plt.show()

It gives us the following plot:

Here’s the code in R:

x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)

relation <- lm(y~x)

a <- data.frame(x = 170)
result <-  predict(relation,a)
print(result)

plot(y,x,col = "blue",main = "Height & Weight Regression",
abline(lm(x~y)),xlab = "Weight in Kg",ylab = "Height in cm")

It gives us the following plot:

Now let’s take a look at unsupervised learning.

Machine learning example 2: Unsupervised Learning

In Unsupervised Learning, the data that we have is unlabelled. So, the algorithm finds and groups common elements in its data. Unsupervised learning takes a look at complex data and tries to organize and find meaning in it. It resembles how humans look at different objects and determine how similar or dissimilar they are and then group them. For example, K means Clustering is an algorithm that divides a data set into subgroups or clusters based on their similarities.

Let’s look at how to do it in Python and R.

Here’s the code in Python:
import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from sklearn.cluster import KMeans from sklearn.metrics import silhouette_score from sklearn.preprocessing import MinMaxScaler from sklearn import datasets

iris = datasets.load_iris()
x = iris.data[:, [0, 1, 2, 3]]
y = iris.target

kmeans = KMeans(n_clusters = 3, init = ‘k-means++’, max_iter = 300, n_init = 10, random_state = 0)
y_kmeans = kmeans.fit_predict(x)

plt.figure(figsize=(9,6))
plt.scatter(x[y_kmeans == 0, 0], x[y_kmeans == 0, 1], s = 100, c = ‘red’, label = ‘Setosa’)
plt.scatter(x[y_kmeans == 1, 0], x[y_kmeans == 1, 1], s = 100, c = ‘orange’, label = ‘Versicolour’)
plt.scatter(x[y_kmeans == 2, 0], x[y_kmeans == 2, 1], s = 100, c = ‘yellow’, label = ‘Virginica’)

plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:,1], s = 100, c = ‘black’, label = ‘Centroids’)

plt.legend()

It gives us the following plot:

Here’s the code in R:
library("dplyr") library("ggfortify") library("ggplot2")

data <- select(iris, c(1:4))
kmean <- kmeans(data, 3)
kmean$centers

autoplot(kmean, data, frame = TRUE)

It gives us the following plot:

Conclusion

In this article, we saw two very simple examples of supervised and unsupervised learning, but this only scratches the surface. Machine learning is a vast field, and transformational technology, but navigating this landscape alone, can often be scary. That’s why we are here. Feel free to reach out if you want to learn more.

If you want to become a data scientist, you can just visit the website of Beyond Machine, or just get in touch, to learn how we can help you.


Wanna become a data scientist within 3 months, and get a job? Then you need to check this out !