Introducing scikit-learn for Machine Learning in Python

In this tutorial, we will go through a simple steps to classify whether a voltage is a digital 0 or 1. We will make things pretty straight forward with large margin so that we can easily visualize. The idea is just to introduce some of the functions in scikit-learn that we can use for linear classification.

The simple problem we want to do is given a set of voltages and its digital logic, we want to be able to determine what would be the digital logic given a new set of voltages. In this example, we use supervised learning, where we label the data and have the training set. Then given a new data, we want to determine which class it belongs to. We will use linear classifier from Support Vector Machine.

First given a training data sets of voltages, e.g.

>>> import numpy as np
>>> data=[0.5,0.1,0.2,0.15,0,0.3,0.4,0.5,0.35,0.45]
>>> data=np.array(data).reshape(len(data),1)

The first line of the above code import numpy module so that we can use its array data stucture. The second line create a list of voltages as a Python list. The third line, convert the Python list to Numpy array and change from a row vector to a column vector.

We can then train the data set by labeling the voltages. For simplicity, let’s label any voltages below 0.2 to be digital logic 0, and anything above 0.3 to be logic 1. To do this we create a target list according to our data set.

>>> target=np.array([0,0,0,0,0,1,1,1,1,1])

Now we can create our linear classifier using SVM.

>>> from sklearn import svm
>>> clf = svm.SVC(kernel='linear')

The first line import the module svm, and the second line creates a linear classifier. Next, we have to fit the data with the target.

>>> clf.fit(data,target)

And now we can predict new data. Let’s say we want to determine what the digital logic is for voltage 0.21V, we can type:

>>> clf.predict(0.21)
array([0])

Or we can also give an array:

>>> newdata=[0,0.5,0.22, 0.1,0.44]
>>> newdata=np.array(newdata).reshape(len(newdata),1)
>>> clf.predict(newdata)
array([0, 1, 0, 0, 1])

As you can see that voltages around 0.2 and below is classified as logic 0 while those around 0.3 and above is classified as logic 1.

Advertisements

Using SublimeText to write JSim file

50.002 Computation Structure course in SUTD requires students to write a kind of SPICE-like code using JSim, an MIT software to simulate circuit design. The editor, however, is pretty basic, and I have noticed that some students prefer to use SublimeText to write the JSim file. Here is the instruction to setup SublimeText to use with JSim.

  1. Download SublimeText.
  2. In Mac OS X, run SublimeText, press “CMD-SHIFT-P” to open the Command Palette. Otherwise, go to Menu -> Tools -> Command Palette.
  3. Type “browse packages” this will open a new Finder window with the location to install the packages.
  4. Create a new Folder called “JSim”.
  5. Download JSim syntax highlighter. Copy jsim.tmlanguage to the newly created folder “JSim”.
  6. Associate jsim extention to open with SublimeText. To do this (in Mac OS X):
    1. Find a JSim file, e.g. lab3.jsim. Right click and choose “Get Info”.
    2. Under “Open With”, choose “Sublime Text”.
    3. Click “Change All”.

UPDATE: There is some issue when using SublimeText in Ubuntu. A solution was to rename jsim.tmlanguage to jsim.tmLanguage (Note the capital “L”).

A free alternative to create Evernote notes from your email

As of July 2015, Evernote has changed its feature to create note from email to a “paid” service. Luckily, there are always alternatives. This post share with you how to do it for free using IFTTT.

  1. Create a free account with ifttt.com using the EMAIL that you want to use to create notes in Evernote. You will need to confirm your account by clicking the link from your email inbox.
  2. Once you are set, you can start creating recipe by clicking “Create a Recipe”  and then click “This”. create_recipeclick_this
  3. Select “Email” for your channel. trigger_channel
  4. And then select the trigger. choose_a_trigger
  5. To complete the trigger, key in the hash tag you want. You can create just one recipe with one hastag, let’s say “#evernote” or you can create multiple recipes with different hash tags. trigger_fields
  6. After that create the “Do” part. click_that
  7. Choose “Evernote” as the channel.  choose_evernote
  8. You will then have to authorize IFTTT to connect to your Evernote account by logging in to Evernote. connect_evernote
  9. Then, you can complete the action fields. In my body I chose “{{BodyHTML}}” rather than Body and I added “{{AttachmentUrl}}”. I decided to create a special Notebook to dump all my notes created from IFTTT. But as I said before, you can just create multiple recipes with different hash tags and for each hash tag you can set your notebook destination. action_fields
  10. Once, you are done, you can send email to create an Evernote note. email_evernote

Android emulator device not showing in Running Devices

Sometimes with the android virtual device running, I can’t seem to see them under the Running Devices when I click “run app”. To solve that, I need to restart my adb. To do that go to the “Android SDK/platform-tools” folder. In my case for Mac OS X, it is at:

/Users/my_user_name/Library/Android/sdk/platform-tools

From there I need to run the following command:

./adb kill-server
./adb start-server

Then you can check whether the device has been attached by:

./adb devices

Fixing PyInstaller on Mac Yosemite

I tried to install pyinstaller using PIP:

<code>$ pip install pyinstaller </code>

and this installed PyInstaller version 2.1, as you can check using

<code>$ pyinstaller –version</code>

When I tried PyInstaller to one of my python script, it gives me an error. I run PyInstaller in this way:

<code>$ pyinstaller minesweeper.py</code>

Then I got the following error:

<code> struct.error: argument for ‘s’ must be a string</code>

The error is caused from a file

<code>/User/lib/python2.7/site-packages/PyInstaller/loader/pyi_carchive.py</code>

from line 84 and 367. To solve this, I replaced the following lines:

Line 84

<code> rslt.append(struct.pack(self.ENTRYSTRUCT + repr(nmlen) + ‘s’, nmlen + entrylen, dpos, dlen, ulen, flag, typcd, nm + pad))</code>

with the following:

<code> 84: rslt.append(struct.pack(self.ENTRYSTRUCT + repr(nmlen) + ‘s’, nmlen + entrylen, dpos, dlen, ulen, flag, typcd, nm.encode(‘utf8’) + pad)) </code>

and Line 367

<code>cookie = struct.pack(self._cookie_format, self.MAGIC, totallen, tocpos, self.toclen, pyvers, self._pylib_name)</code>

with the following:

<code>cookie = struct.pack(self._cookie_format, self.MAGIC, totallen, tocpos, self.toclen, pyvers, self._pylib_name.encode(‘utf8’)) </code>

After that, my pyinstaller works.

After msfupdate, postgresql selected, no connection

After I did a “sudo msfupdate”, somehow my msfconsole is not connected to the database. when I type:
msf> db_status
postgresql selected, no connection

So, to connect back, I need to provide the location of database.yml, as in

msf> db_connect -y /usr/local/share/metasploit-framework/config/database.yml

And it connects again. yeah. To do this everytime msfconsole is called, we can create an alias in .bashrc.

alias msfconsole='msfconsole -d db_connect -y /opt/framework/config/database.yml'

Installing Metasploit on Xubuntu 14.04

  1. Grap the script file from: http://www.darkoperator.com/installing-metasploit-in-ubunt/:
    $ wget https://raw.githubusercontent.com/darkoperator/MSF-Installer/master/msf_install.sh
  2. Run the script.
    $ chmod a+x ./msh_install.sh
    $ ./msh_install.sh
  3. Now, by right, we can start running msfconsole, but I got some error when I tried to do so.
    $ msfconsole
    /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- /usr/local/bin/config/boot (LoadError)
    	from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    	from /usr/local/bin/msfconsole:23:in `'
    
  4. What I did was to edit /usr/local/bin/msfconsole line 23, and replace that line with:
    require File.expand_path('/usr/local/share/metasploit-framework/config/boot.rb', __FILE__)
    
  5. Make sure that the path for config/boot.rb is correct for your system.
  6. Now, when I run msfconsole again, it gives me some warning of missing permission to write some log file.
    Rails Error: Unable to access log file. Please ensure that /usr/local/share/metasploit-framework/log/production.log exists and is chmod 0666. The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
    
  7. So I need to create the directory and make sure it is accessible.
    $ cd /usr/local/share/metasploit-framework/
    $ mkdir log
    $ chmod 777 log
    
  8. Now, I can run msfconsole. Before reaching the prompt, however, I got the following warning:
    [-] WARNING! The following modules could not be loaded!
    [-] 	/usr/local/share/metasploit-framework/modules/auxiliary/scanner/smb/smb_login.rb: LoadError cannot load such file -- robots