codemagic
  • Home
  • About.me
  • Resume
  • Blog

expanding   our    PELLET   of   default  kernels   in   scikit

1/31/2014

0 Comments

 
I was working on an simplified action descriptors for action detection using the bounded Dense trajectory, where my final step was to predict the action by training the action descriptors with SVM. Since my whole code was in python, I wanted a python based implementation of libsvm which I found in Scikit learns.
                 
The problem with the Scikit was it had a few default kernels.   
  • linear: 
  • polynomial:
  • rbf: 
  • sigmoid:

but I wanted chi square kernel as this is the most used kernel  for histogram data and I was unable to construct  custom kernel . After a brief amount of browsing and going through the  Scikit Documentation. I found there are several ways to include the other kinds of kernels

First way was to use Scikit's Pairwise Metrics.  Apart from the above mentioned kernels, we have two new kernels

metrics.pairwise.additive_chi2_kernel (X[, Y])Computes the additive chi-squared kernel between observations in X and Y
metrics.pairwise.chi2_kernel (X[, Y, gamma])Computes the exponential chi-squared kernel X and Y.

Usage:

from  metrics.pairwise.additive import chi2_kernel
>>> Y = [0, 1, 2, 3] 
>>> clf = svm.SVC(kernel=chi2_kernel) 
>>> clf.fit(X, Y) SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0, kernel='rbf', max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False)
>>> test=[[0],[1],[1].[2]]
>>> clf.predict(test)

Second way is to use the approximations 

kernel_approximation.SkewedChi2Sampler  ( [...])Approximates feature map of the “skewed chi-squared” kernel by Monte
 
Usage:

>>> from sklearn.kernel_approximation import SkewedChi2Sampler
>>> X = [[0, 0], [1, 1], [1, 0], [0, 1]]
>>> y = [0, 0, 1, 1]
>>> chi2_feature = SkewedChi2Sampler(sample_step=1. sample_interval=3)
>>> X_features = rbf_feature.fit_transform(X) 
>>> clf = SGDClassifier() 
 >>> clf.fit(X_features, y)
 SGDClassifier(alpha=0.0001, class_weight=None, epsilon=0.1, eta0=0.0, fit_intercept=True, l1_ratio=0.15, learning_rate='optimal', loss='hinge', n_iter=5, n_jobs=1, penalty='l2', power_t=0.5, random_state=None, rho=None, shuffle=False, verbose=0, warm_) 
>>> clf.predict([[1.0],[1,1]])

you can see how these kernels can be implemented in one of my projects here

0 Comments

Problems with boost and threadpool when used together

1/29/2014

0 Comments

 
Recently while I was on our state of the art Video2Text website to check its working status. I found it had some problem while generating the text. I found that the website was broken. I and my friend thought it was because of the job dispatcher called Celery which we used to dispatch jobs to our servers and gather the results to display them, after restarting the dispatcher  and  checking all the parameter, I understood the problem was much deeper. The problem was in my video generation code which used lots of libraries. One of the external  library got accidentally updated to the latest version and the compatibility between another library was broken.
                   The Problem was between the latest version of Boost 1.55.0 and ThreadPool 0.2.5. while compiling my project code which was internally linked with both library, I was getting a error suggesting  "TIME_UTC not declared locally".  After looking through the change list of Boost. I found the "TIME_UTC" was renamed to "TIME_UTC_" .  After renaming the ThreadPool Library's  "TIME_UTC" constant to "TIME_UTC_",  the error was rectified. I have also informed the author of ThreadPool  Library regarding the compatibility issues with the latest version of boost and probably in the next release of ThreadPool, the compatibility would be resolved.
0 Comments

.Mat files  - reading  and  creating  in  python

1/20/2014

0 Comments

 
There are several versions of the .mat files v7.3, v7, v6, v4.  A significant difference between each of the version is the way the data is stored inside them.
      -v4 ,-v6  : In these versions  Matlab allowed storing a variety of structures like sparse arrays , two dimensional double  and extended its varied structure storage.
      -v7  : From this version Matlab started compressing the data. This compression and decompression  slowed down the loading and saving process but used very less space in the disk
      -v7.3  : In this version Matlab started to use HDF5 format of storing the data in a compressed chunks. The time required to load the data differed by the way the data is stored among the chunks
you can check this for detailed information regarding the versions and their features list.

you can create any version mat file using the "save" command in Matlab 

save(filename,variables,version) saves to the MAT-file version specified by version. The variables argument is optional, as described above.

eg,
A = rand(5); 
B = magic(10); 
save('example.mat','A','B','-v7.3')

due to the varied versions of mat file. Reading a mat file became a complicated task to carryout. 
Here I would describe two ways you could read and create a mat file in python.

Matlab -v4. -v6,-v7
Need to import scipy.io

loadmat(file_name[, mdict, appendmat])   Load MATLAB file
savemat(file_name, mdict[, appendmat, ...])  Save a dictionary of names and arrays into a MATLAB-style .mat file.


eg,
#!/usr/bin/env python 
from scipy.io import loadmat 
x = loadmat('test.mat') 
lon = x['lon'] 
lat = x['lat'] 
# one-liner to read a single variable 
lon = loadmat('test.mat')['lon']
x['lon']='clon'
savemat('changetest.mat',x)

Matlab -v7.3
since the data is stored in the form of HDF5 chunks. we need to install python-tables or python-h5py package. which allows python to access HDF5 chunks. you could use apt-get  or download the files from these websites.
Pytables
h5py

eg
#!/usr/bin/env python 
import tables
file = tables.openFile('test.mat') 
lon = file.root.lon[:] 
lat = file.root.lat[:] 
# Alternate syntax if the variable name is in a string 
varname = 'lon' 
lon = file.getNode('/' + varname)[:]

additional references
http://wiki.scipy.org/Cookbook/Reading_mat_files
http://docs.scipy.org/doc/scipy/reference/tutorial/io.html
http://www.mathworks.com/help/matlab/import_export/mat-file-versions.html


0 Comments

Latex   with   vim   using   latex-box

1/6/2014

0 Comments

 
                  The quest to integrate two powerful tool, Vim and LaTeX started when I wanted to make a new resume ,  which would look neat and crisp. 
                   I started looking in the internet for the answers,surprisingly there was no clear documentation or procedure to start in spite of both being more than a decade old. After all the search for many days I figured out a way and thought it would be better if some how I could provide few guidelines so that you could have a quick start and explore the power of both.
                  LaTeX can be used by downloading several tools like TexMaker, LyX and Kile.  But none of them has the power of Vim.There are three popular plugins for Vim according to Redditors.
1. Vim-LaTeX      oldest,heavy, complex and feature rich.
2. LaTeXBox        light and simple.
3. Automatic LaTeX Plugin  heavy and simple.
I found LaTeX Box was the fastest and easiest to use LaTeX. But it only works if the Vim's ClientServer features has been installed in Vim. To check ,

"vim -h | grep servername"

If you don't have the feature, then need to compile the "huge" version of vim. 

Installation Instructions
1. Download Vim from here
2. before installing "huge" version run this command

sudo apt-get install libncurses5-dev libgnome2-dev libgnomeui-dev \ libgtk2.0-dev libatk1.0-dev libbonoboui2-dev \ libcairo2-dev libx11-dev libxpm-dev libxt-dev

Huge means all features are enabled. The features are enabled based on checking the  dependencies.if you don't have these packages installed then you would not have the complete feature rich Vim.
3  Configure and make the code

./configure --with-features=huge --enable-gui=gnome2 --enable-cscope 
make
make install

4. Recheck your vim installation, and check for +clientserver

vim --version | grep clientserver

LaTeX Box depends on latexmk and texlive. These are the compilers of LaTeX . You can install them simply by calling the  package manager.

sudo apt-get install latexmk texlive

now Install  the LatexBox using Vundle(package manager for vim) . follow the link to install Vundle.

Bundle "LaTeX-Box"
BundleInstall

The whole installation process is done. You can start using the plugin by opening a TeX file with servername option 

vim --servername <servername> <TeX file>

You can start compiling the code with "\ll" (leader L L) , view the pdf file by "\lv" (leader L V) and check errors with "\le" (leader L E). Enjoy using LaTeX and Vim together.
0 Comments

vim colorschemes

1/1/2014

0 Comments

 
                 I recently started using vim and got to know its amazing capabilities. While I was going through my mail stack, I stumbled on a mail from my adviser about the vim-plugin called "Solarized". I added the plugin. The color scheme was great but the comments were over highlighted instead of getting dissolved in the background.
                  So, I started browsing the net  for some different color schemes. Where I found a GitHub repo called vim-"colorschemes" by flazz. Its  a wonderful plugin which adds around 400+ vim color schemes into your system. You can just use the command : colorscheme <nameofthecolorscheme> to change the color scheme you want and if everything seems right you could add it to your ".bashrc".
                 If you still  cannot stop your cravings for the color schemes, then there is another easier way out. There is a site called "vivify" which allows you to create your own color scheme. It has a very neat UI , showing how the color scheme would look for different languages. You could Download the color scheme and add it to .vim/bundle/vim-colorschemes, if you have already gone through the above steps. If not then add it to .vim/colors. And then load the color scheme using command : colorscheme <nameofthecolorscheme> and enjoy the color schemes.
0 Comments

    Author

    Kaushal Bondada

    Archives

    January 2014

    Categories

    All
    C++
    Latex
    Machine Learning
    Matlab
    Python
    Vim

    RSS Feed

Powered by Create your own unique website with customizable templates.