Training a model in Machine Learning

Training a model in Machine Learning means a lot of things. To make it simple, we can say that a model learns from previous data first.

For example, by examining the model, the machine learning algorithms try to read and learn from the pattern of the past data.

As a result, it minimizes the error or loss.

Finally, it predicts the correct output.

Let us think about a dataset that builds on simple linear regression.

sep



If you are a complete beginner your journey to learn TensorFlow might start from here.

For the TensorFlow beginners we have a dedicated category – TensorFlow for Beginners.
But besides that, you may need to learn several other machine learning and data science libraries.

As a result, you may check these categories as well – NumPy, Pandas, Matplotlib.

However, without learning Python, you cannot learn the usages of these libraries. Why? Because they all use Python as the Programming language.

Therefore please learn Python at the very beginning and start learning TensorFlow.

And, finally please check our Mathematics, Discrete Mathematics and Data Structures categories specially. We have tried to discuss from basic to intermediate level so that you can pick up the core ideas of TensorFlow.


As height increases, the width decreases. It means they have a negative relationship.

Therefore if we keep increasing the length the width keeps diminishing. 

Consider a flower’s petal length and width.

In reality, after a certain length it does not grow if the relationship is negative with the width.

Because mathematically at one point, the width becomes a negative value which is not possible in reality.

It proves that nature uses mathematics very heavily while creating this universe.

Come to our point.

Let’s see the data table first. One can download the CSV file used in this example from the GitHub repository.

It looks like below.


Data table with a negative relationship
Data table with a negative relationship

The above data set clearly shows that as length increases, the width decreases.

We can give an output like above by importing corresponding libraries like Pandas, scikit-learn, and NumPy.

import pandas as pd
import numpy as np
from sklearn import datasets, linear_model, model_selection
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt

url = 'https://raw.githubusercontent.com/sanjibsinha/Machine-Learning-Primer/main/newpetals%20-%20Sheet1.csv'
data_frame = pd.read_csv(url)
data_frame

Consequently, we can graph the points considering the x axis represents the length, and the y axis represents the width.

Remember the two variable linear regression equations we have discussed before.

Therefore we use the following code to plot the points.

y = data_frame.width
x = data_frame.height
     

ax = plt.axes()
ax.scatter(x, y)

As a result, it produces the following plot.


Plotting the negative relationship
Plotting the negative relationship

Now we can train the model with the help of NumPy and get the correct prediction line with the help of Matplotlib.

Next we will use the NumPy polyfit() function and pass the degree as 1.

(m, b) = np.polyfit(x, y, 1)
yp = np.polyval([m, b], x)
plt.plot(x, yp)
plt.grid(True)
plt.scatter(x,y)

That will give use the output.


The model shows the prediction line
The model shows the prediction line

In the next section we will see how this value corresponds to the best prediction.

For that we will use the scikit-learn library.

For more such machine learning primer code, please visit the respective branch of GitHub Repository.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

TensorFlow, Machine Learning, AI and Data Science

Flutter, Dart and Algorithm

C, C++, Java and Game Development

Twitter

Comments

Leave a Reply