Updating the straight line in a loop in matplotlib

Here is the simple technique to update the straight line equation in a loop, this is very helpful to show the training process of parameters in case of a linear regression model or classification model.

# Use plt ion 
fig = plt.figure()
ax1 = fig.add_subplot(211)
for i in range(10):
    # Plot.
    ax1.scatter(xs, ys) 
    # update the figure.
    plt.plot(xs, i * xs, color='red')
    # Draw the line.
    # Clear the current plot.


Turn off auto indentation when pasting in vim

Sometimes when trying to paste code into using Ctrl + V it annoys us to see auto indentation in vim messing it up, to turn off autoindent when you paste code, there’s a special “paste” mode. Type

:set paste

Then paste your code. Note that the text in the tooltip now says -- INSERT (paste) --.

After you pasted your code, turn off the paste-mode, so that auto-indenting when you type works correctly again.

:set nopaste

The exploration-exploitation dilemma in Multiarm bandit problem


Let’s say you are in a casino and there are 4 slot machines.When played each slot machine gives you a reward of 1 or 0. The win percentage of each of the slot machines are different and they are not known. The results are truly random. The goal is to choose the slot machine with the highest win rate. The only way you could discover their relative win capabilities is by playing each these slot machines and collecting data, but this naive approach has its downside. In the process of collecting the data you would spend lot of time playing the less optimal slot machines. There is a an exploration-exploitation dilemma here.

Exploration finds more information about the environment and exploitation uses the already known information to maximize the reward. So your approach needs to include both maximizing your earning and also in the process find out the slot machine with the maximum win rate.

A number called epsilon is used which represents the probability of exploration, Here is how epsilon is used to choose between explorating a random slot machine and the machine with highest win rate at that point in time.

p = generate_random_number()
if p > e:
  pull a random arm 
  pull the arm with best win rate at this point

In the above process you’ll eventually get to know the arm with the best win rate. This is also called as the Multiarm bandit problem. There are numerous ways of approaching this problem. In next few blog posts will walk through some of the solution for a Multiarm bandit problem.

Importance of Kalman Gain parameter in Kalman filters

Kalman filter gain parameter plays a very important role in kalman filter estimation, In the last blog I’ve given an introduction on what are Kalman filters and why to use them.

Kalman filter gain decides how much of the new measurement has to be considered for the estimation. Here is the formula for Kalman gain.

Screen Shot 2017-12-06 at 7.17.40 PM

KalmanGain = Error In Estimation / (Error in estimation + Error in measurement) .
If the error in measurement in small then the value of Kalman Gain would be close to 1. If the error in estimation is large then value heads towards 0.
Here is the update equation for the state using the Kalman gain.

Screen Shot 2017-12-06 at 7.20.33 PM

As you can see in the above equation, If the Kalman gain (KG) is close to 0, this implies that the error in the measured value from the sensor is really high and thus they are unstable. In this scenario we don’t want the contribution of the measured value to be notable on the prediction, this is controlled by the additive factor for the previous estimation, this quantity is meager when the KG value is close to 0, and thus it just slightly adds up to the previous estimation, so the Kalman gain(KG) helps invest better confidence on the estimation rather than on the measurement.

On the other hand, If the Kalman Gain is close to 1, that is in case where the error in estimation is really high compared to the error in the measurement from the sensors, The additive value for the previous estimate in the update equation above will be significant, thus the updated value will considerably different from the estimation since the contribution from the measurement will be higher.
Thus Kalman filter gain helps to weigh the contribution of the difference between the previous estimate and the current measurement to update the previous estimate to arrive at the new estimate.

Code Puzzle : Making Anagram problem

Problem statement: Calculate the minimum number of characters to be removed from any of the given two strings so that they become anagram.

Strategy: Store the frequency of occurrence of every characters (from a-z) of each of the strings in separate arrays, then the summation of difference in frequency of each character gives you the solution, abs(count1[str1[i]-‘a’] – count2[str2[i]-‘a’])

Here is the code,

#include <iostream>

using namespace std;

int min_remove_anagram(string a, string b) {
    int char_count_a[26]={0}, char_count_b[26]={0}, num_needed=0;
    // Count the frequencies of all characters in string a and b
    for(int i=0; i < a.size(); i++) {
        char_count_a[a[i] - 'a']++;
    for(int i=0; i < b.size(); i++) {
        char_count_b[b[i] - 'a']++;

    // The difference in the frequency count of all the characters is the solution.
    for(int i = 0; i < 26; i++) {        
       num_needed += abs(char_count_a[i] - char_count_b[i]);
 return num_needed; 

int main()
    string str1 = "bcadeh", str2 = "hea";
    cout << min_remove_anagram(str1, str2);
    return 0;

Hello World ROS: Creating ROS workspace and a empty node.

The Robot Operating System (ROS) is a flexible framework for writing robot software. It is a collection of tools, libraries, and conventions that aim to simplify the task of creating complex and robust robot behavior across a wide variety of robotic platforms.

Creating the ROS work space

  • ROS has package management system called catkin.
  • All the packages are maintained usually in a single workspace under different directories.
  • Catkin workspace is a directory where the catkin packages are build, maintained and modified.
  • First, you need to create the top level catkin workspace directory and a sub-directory named src. The top level directory’s name is arbitrary, it can be anything, but is often called catkin_ws (an abbreviation of catkin_workspace),
$ mkdir -p ~/catkin_ws/src
$ cd ~/catkin_ws/src
  • Initialize and building the workspace.
$ catkin_init_workspace
$  cd ~/catkin_ws && catkin_make

You now have two new directories: build and devel. The aptly named build directory is the build space for C++/Python packages. The devel directory contain  a file named setup.bash. This setup.bash script must be sourced before using the catkin workspace.

Creating a package

  • ROS softwares are distributed and maintained as catkin packages.
  • Its again a directory containing variety of resources.
  • Let’s clone an existing package and add it to our newly created workspace.
  • You will start by navigating to the src directory and cloning the simple_arm package for this lesson from its github repo.
$ cd ~/catkin_ws/src
$ git clone https://github.com/udacity/simple_arm_01.git simple_arm

After the repo has finished cloning, you can change directory to the top-level of the ros workspace and build the new package.

$ cd ../../
$ catkin_make

If you see the following error

I see a CMake Error. “Could not find a package configuration file provided by controller_manager”,

The following is missing just install it using the command below,

$ sudo apt-get install ros-kinetic-controller-manager

build again

$ cd ~/catkin_ws
$ catkin_make

Once the workspace has been built, now source the setup script

$ source devel/setup.bash

 Now launch simple_arm:

$ roslaunch simple_arm robot_spawn.launch

Creating the node

Step 1: In order to create a new node in python, you must first create the scripts directory within the simple_arm package.

$ cd ~/catkin_ws/src/simple_arm/
$ mkdir scripts

Step 2: Create scripts inside the script folder, give execution permission to the script

$ cd scripts
$ echo '#!/bin/bash' >> hello
$ echo 'echo Hello World' >> hello
$ chmod u+x hello

Step 3: Rebuild the workspace

$ cd ~/catkin_ws
$ catkin_make

Step 4: Source the setup.bash

$ source devel/setup.bash

Step 5: Use rosrun to run the node

rosrun <package name> <script name>
$ rosrun simple_arm hello

We have created a empty node.