Skip to main content

Step by Step approach on Machine Learning using Python

Machine Learning has been defined as “the future” and is being deployed in many organisations to achieve real business results. With its dramatic improvements in past few years, Machine Learning is expected to go far beyond the highest level of accuracy and understanding.

Step 1: Install Python 3.5 or higher version

The Installation procedure for Windows and Linux will be different.

In case of Windows:

Go to this link Python for Windows download page, and download the link depending on whether the windows system is 32-bit or 64-bit configuration. Then run the downloaded link, choose the Customise installation and choose the path where exactly the software need to be installed. Then open system properties and set the path of the python where it has been installed. Now open the command prompt and check whether python has been installed properly or not by typing the cmd,

python3 –version

Now it should show which version of python has been installed.

In case of Linux:

Execute the following steps one after the another in the command prompt

<span class="gp">$</span> python3 --version
$ sudo apt-get update

$ sudo apt-get install python3.6
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt-get update
$ sudo apt-get install python3.6
<span class="gp">$</span> sudo dnf install python3

Step 2: Creating a sample program Hello world

Open any text editor and create a new file sample.py. The python file always should have an extension of ".py" . In that file, type

print(<span class="hljs-string">"Hello, World!"</span>)

Save that file and got to command prompt and type

python hello.py

Now the ouput will be

Hello, World!

Step 3: Installing IDE

We can use even Eclipse to run a Python or Machine Learning program by just including a plugin called "Pydev".

There are actually many IDEs for the development of the ML or Python project like Pycharm, IDLE, Anaconda etc.

For the Pycharm Installation on Windows,

Go to this link http://www.python.org/downloads/, and follow and install the steps. Choose Customise installation to install in a particular folder.

For the Pycharm Installation on Linux,

Go to this link https://www.jetbrains.com/pycharm/, and execute the below cmds in the command prompt

cd ~/Downloads
ls pycharm*
tar -xvzf pycharm-professional-2016.2.3.tar.gz -C ~

To run Pycharm, first navigate to your source folder

cd ~
ls
cd pycharm-2016.2.3/bin
sh pycharm.sh &amp;

Step 4: Creating a simple Machine Learning program using Python

image1

There are many packages in Machine Learning which helps in tasks like text classification, probability and sentimental analysis etc. For the text classification nltk (Natural Language Toolkit) is the most used library. For calculation or deep learning numpy, pandas, tensorflow etc are useful.

Now we see how to get the count of positive and negative words using Supervised Classification in Machine Learning. Here first we need to import the nltk package which is required for text classification. Then we are creating two lists for both positive and negative words.

After that we can pass whichever sentences are needed to test. Those sentences every time goes inside a loop where the length(count) has been calculated and sent to find the ratio of positive and negative words. The counts would be divided by the length of words and would be taken as the final ratio of the respective positive and negative words. The output would be in the ratio of 0.3333333333333333, 0.25 respectively.

<span class="kwd">from</span><span class="pln"> __future__ </span><span class="kwd">import
</span><span class="pln"> division 

import nltk

neg_words </span><span class="pun">=</span> <span class="pun">[</span><span class="str">'poor'</span><span class="pun">,</span> <span class="str">'horrible', 'unpleasant', 'unwelcome'</span><span class="pun">]</span><span class="pln">

pos_words </span><span class="pun">=</span> <span class="pun">[</span><span class="str">'excellent'</span><span class="pun">,</span> <span class="str">'nice', 'outstanding', 'awesome'</span><span class="pun">]</span><span class="pln">
 
Sentences </span><span class="pun">=</span> <span class="pun">[</span><span class="str">"the picture is horrible and unpleasant"</span><span class="pun">,</span> <span class="str">"that was really nice"</span><span class="pun">]</span>
 
<span class="kwd">for</span><span class="pln"> element </span><span class="kwd">in</span><span class="pln"> Sentences</span><span class="pun">:</span>

<span class="com"># split the <span class="pln">element </span>into words:</span><span class="pln">

words </span><span class="pun">=</span><span class="pln"> element </span><span class="pun">.</span><span class="pln">split</span><span class="pun">()</span> 
 
<span class="com"># create lists of positive and negative words </span>

<span class="com"># words in the current <span class="pln">element and finding the length of words</span> to get the counts in </span><span class="com">each list:</span><span class="pln">

count_pos </span><span class="pun">=</span><span class="pln"> len</span><span class="pun">([</span><span class="pln">w </span><span class="kwd">for</span><span class="pln"> w </span><span class="kwd">in</span><span class="pln"> words </span><span class="kwd">if</span><span class="pln"> w </span><span class="kwd">in</span><span class="pln"> pos_words</span><span class="pun">])</span><span class="pln">

count_neg </span><span class="pun">=</span><span class="pln"> len</span><span class="pun">([</span><span class="pln">w </span><span class="kwd">for</span><span class="pln"> w </span><span class="kwd">in</span><span class="pln"> words </span><span class="kwd">if</span><span class="pln"> w </span><span class="kwd">in</span><span class="pln"> neg_words</span><span class="pun">])</span>
 
 <span class="com"># calculating the ratio by taking the count and the length of words:</span><span class="pln">

ratio_pos </span><span class="pun">=</span><span class="pln"> count_pos </span><span 
     class="pun">/</span><span class="pln"> len</span><span class="pun">(</span><span 
     class="pln">words</span><span class="pun">)</span><span class="pln">

ratio_neg </span><span class="pun">=</span><span class="pln"> count_neg </span><span 
     class="pun">/</span><span class="pln"> len</span><span class="pun">(</span><span 
     class="pln">words</span><span class="pun">)</span>
 
<span class="kwd">print</span><span class="pun">("ratio of positive words",</span><span 
     class="pln">ratio_pos</span><span class="pun">)</span>

<span class="kwd">print</span><span class="pun">("ratio of negative words",</span><span 
     class="pln">ratio_neg)

</span>

 

Share this post

Comments (0)