Machine Learning for Software Developers
As a software developer, Have you ever tried to run machine learning model? If you’re answer is big “No!”, Don’t worry this article will walk you through steps on implementing a machine learning model and explain some differences you may find in machine learning and traditional programming.
Now lets get into the topic that we really considered about, As a developer you would make a computer programme by constructing some classes,variables and methods ,right? within methods you form business logics according to the requirement using conditions,in other words you put if/else, switch statements in order to control the data flow as needed and get the output. To demonstrate such task, we take a simple example of categorizing iris flowers by its measurements.
Let’s write some code !
We can write a simple programme to output the iris flower species for given measurements as follows,
Ok, we have written some ES6 syntax to output the name of iris species in the console. Well, what’s wrong with this approach? Let me clear this for you.How many if conditions you would write in order to cover every possible measurements to match with the output, The list goes long… isn’t it ?
What “IF” we use machine learning instead ?
Many believes Iris dataset as the “Hello world” for machine learning.That’s the reason we took iris flowers in previous example written in Javascript,But that is a simple programme not a machine learning model. Hereafter we write code in Python as it’s the most popular language for machine learning tasks.
First, We will import necessary python libraries
Then we have to assign iris data set to a variable, let’s call it as iris_data.
Let’s see the names of iris species that are available in our dataset .
This will output 3 iris species available in our dataset .[‘setosa’, ‘versicolor’, ‘virginica’]. Next we will print available measurements for these iris species.
[‘sepal length (cm)’, ‘sepal width (cm)’, ‘petal length (cm)’, ‘petal width (cm)’]
Lastly, Let’s print the shape of the dataset we loaded.
This will output (150, 4), which indicates there are 150 data rows available in our dataset.
Now, we have to separate test and train data from our dataset in order to feed to our machine learning model. We do that so as follows,
As you can see we take 30 percent of data from the dataset for test the model and rest of the 70 percent take for train the model. Now, we are ready to initiate our machine learning model.
After initiating we can train the model with iris dataset.
Now we can evaluate test score for our machine learning model.
This will score 0.98 which is pretty good! Finally, we will predict iris species with the measurement feed into our model.
Predicted Iris species: [‘setosa’]
That’s all folks! I hope you find this article helpful. I had to skip visualizing components such as plots and tables since I wanted to make this article as much as simple to understand specially for those who developing softwares. In upcoming articles I’ll go into further.Till then, Happy ̶c̶o̶d̶i̶n̶g̶ machine learning!
You can grab the full code from my github repository and click “Open in Colab” button to experiment it right away using google colaboratory .
https://github.com/shalindasuresh/machine_learning_python/blob/main/classify_iris_knn.ipynb