Saturday, December 21, 2019

Characteristics of Computational Systems


1. Information Capacity governed by Shannon's Formula

Shannon's Formula describes the amount of information that can be saved by a system of N states.

S=-\sum _{i}P_{i}\log {P_{i}}
source: wikipedia

Information essentially is an interpretation of system's state. In general for any information system designed by humans, P(i) = P(j) = 1/n, so  formula can be

  S= - Summation(1:n) 1/n log b n 

This will come out to be log b n where b is the base and in most cases b=2. So a system with just one state cannot store any information (log 2 1 = 0 ) and a system with two states can store log 2 2 = 1 or one bit of information. 

2. Speed - Switching between states

3. Universality - Which tasks can be solved with a computational system?



Reference - Coursera



Tuesday, April 17, 2018

Introduction to NumPy & Scikit Learn

NumPy introduces an array type data structure in Python.

import numpy as np
a = np.array([1,2,3])

We can also create matrices and do all matrix operations using NumPy

m1 = np.matrix('1 2; 3 4')m2 = np.matrix('9 10; 11 12')m1 * m2
We can also import csv files as matrices.



Scikit-learn is a machine learning library. We can preprocess data, reduce redundant variables (dimensionality reduction), implement classification & regression models & fine tune the models using  scikit-learn

Creating ML Model:

1. In order to create a feature vector, we need to factorize data so that we have numeric values for all features. This can be done using factorize functionality in pandas library.

2. Next step of scale feature vector. This can be done using inbuilt scalars in sklearn like Standard Scalar.

3. After we have scaled featured vector, we can go for dimensionality reduction. We can apply Principle Component Analysis for the same.

4. After we have final set of features, we can divide the data into test data and training data. For this we can use train_test_split functionality from sklearn.

5a) Let's say we now want to apply logistic regression to classify data. We can use inbuilt functions from sklearn for the same and run on training data. We can then verify model's performance on test data by comparing values from model's predictions and actual test data results.

5b) Let's say we are interested in finding natural grouping of data, we can implement k-means clustering in that case.

We need to define number of clusters , number of time we have to run k-means algorithm with different seed values, number of times we will iterate k-means algorithm for a set of seed values and also specify tolerance - relative tolerance with regards to inertia to declare convergence.





Monday, April 16, 2018

Introduction to Pandas Library

Pandas library Provides fast data cleaning, preparation and analysis

Built on top of NumPy , so its easy to work with array (Series) and matrices (Data Frames)

DataFrames are indexable and are made up of number of Series objects. Consider this as similar to DB table / spreadsheet - rows & columns.

Series can also be considered 1-d DataFrame - DataFrame with only 1 column.

Code Examples:


import numpy as np
import pandas as pd
s = pd.Series([1,2,4, 5, 6, 8])

s is Series. Printing s would give us

0 1
1 2
2 4
3 5
4 6
5 8
dtype: int64

Data Frame can be defined as 

df = pd.DataFrame({'date' : ['2018-04-01', '2018-04-02', '2018-04-03'],
'price': [200, 380, 405]})

This is essentially a table with 2 columns date & price and 3 rows having values. We can directly load
a csv file as DataFrame using pd.read_csv

Once the data is loaded , we can leverage the power of pandas. For the table above, let's try these

1. Find entries where price is > 200

df[df['price'] > 200]

This will return entries corresponding to 380, 405

Let's try to find total sale value:

df['price'].sum()

By default DataFrame rows are numbered 0 to n. We can also give them names. In order to use name to fetch a row, we need to set index. Let's say we wanted to name the row as a combination of data & price with a # separator:

df = df.set_index(df['date'])

This will assign the mutated data frame to the old variable. We can now query using newly named indexes

df.loc['2018-04-02'] will correspond to second row.










Tuesday, December 19, 2017

Object Functional Programming

Object functional programming can be considered as a mix of object oriented programming and functional programming. While standard OOPS approach will focus on objects, and standard functional programming function will focus on functions, object functional programming will focus on object transformations - resulting in higher productivity and quality.



References

http://www.artima.com/weblogs/viewpost.jsp?thread=275983
https://academy.realm.io/posts/altconf-saul-mora-object-orientated-functional-programming/

Sunday, December 3, 2017

Transducing in JS

Say there is a list

var list = [3,4,7,8,11,14,15]

And we want to apply following function to each element

function add1 (n) { return n+1;}

And then filter out only the odd numbers

function isOdd (n) { return n%%2=1;}

And then get sum of those numbers

function sum (total, n) { return total +n };


This can be done :

list
.map(add1)
.filter(isOdd)
.reduce(sum);

This can be reduced to a one line code by using transducer which takes up a sum function as combiner.

Transducer will compose mapReducer and filterReducer together.



Further Reading

frontendmasters.com
Full code - https://gist.github.com/getify/7ba2b8f5a50116f3efa2849e4c6d1f79
http://jlongster.com/Transducers.js--A-JavaScript-Library-for-Transformation-of-Data




Proper Tail Calls, CPS, Trampolines

Proper Tail Calls allows us to run a program with recursive calls without blowing up stack memory.  Tail calls are functions which can be executed without growing stack.

In addition to having proper tail calls, we can implement TCO - Tail Call Optimizations to improve performance of tail recursive functions.

Continuation Passing Style can be used for advanced recursion.

Using Trampolines, we will be calling functions one by one and can get around limitations of having no PTC support. A Trampoline expects a continuation function and executes the same whenever it gets one.

Further Reading
http://lucasfcosta.com/2017/05/08/All-About-Recursion-PTC-TCO-and-STC-in-JavaScript.html
frontendmasters.com
http://www.datchley.name/recursion-tail-calls-and-trampolines/

Saturday, December 2, 2017

Closures, Partial functions and Currying

Closure is when a function "remembers" the variables around it even when the function is executed elsewhere.

Any language that implements lexically scoped name binding with first class functions will have closures. It can be considered as a record storing function and its environment.

Partial functions and Currying are two different techniques for specializing a generalized functions. Partial functions will take some arguments at one time and rest at other time. Currying on the other hand provides arguments one by one for specialization. Currying is kind of calling partial function automatically at each level with one argument




Further Reading

frontendmasters.com

Porter's Five Forces Framework

Analysis of forces close to a company that affect its ability to serve its customers and make a profit.

Based on structure, conduct, performance paradigm in IO economics

Five Forces are

- Bargaining Power of Suppliers
- Threat of New Entrants
- Threat of Substitutes
- Bargaining Power of Buyers
- Industrial Rivalry

Friday, December 1, 2017

The Circles Method

C - Comprehend situation : What , Who, Why, How?

I -  Identify Personas / customers

R - Report customer needs - as ___, I want ___ that ____

C - Cut, through prioritization > ROI Estimate?

L - List Solutions

E - Evaluate trade offs: thoughtful, analytical, objective

S - Summarize recommendation - what, why vs others, recap.




References & Further Reading

https://www.impactinterview.com/2016/08/circles-method-product-design-framework/

Jupyter Notebooks - 1

Jupyter Notebook is essentially a web app that allows us to combine explanatory text, code, equations, visualizations etc in a document that can be shared.

Notebooks are a form of literate programming.

Notebook server which is the core of all interactions via Notebook support a number of kernels.

Read more about how IPython (kernel for python) & Jupyter Notebook works at

https://jupyter.readthedocs.io/en/latest/architecture/how_jupyter_ipython_work.html

Here's a reference diagram from that documentation:





Further Reading:
Setting up remote kernel - http://jupyter-notebook.readthedocs.io/en/latest/public_server.html

Monday, November 27, 2017

Anomaly Detection - 1

On a broad level, anomalies can be classified as point anomalies, contextual anomalies and collective anomalies.

Here's some of techniques that can be used to detect anomalies

1.  Statistical Methods

Flag data points that deviate from common statistical properties of a distribution - mean, mode, median, quantile.

We can use rolling average or moving average to smoothen short term fluctuations. A low pass filter can be used by n-period simple moving average.

2.  Density Based Anomaly Detection

This technique assumes that normal data points will occur around a dense neighborhood and the abnormalities will be far away (based on k-nearest neighbors algorithm or local outlier factor).

3. Clustering (e.g. K-means clustering algorithm)

4. Support Vector Machine - Variants of SVM like One-Class SVM can be used for anomaly detection.



Further Reading: 

1.https://en.wikipedia.org/wiki/Anomaly_detection
2.http://www.nehalemlabs.net/prototype/blog/2013/04/05/an-introduction-to-smoothing-time-series-in-python-part-i-filtering-theory/
3.https://bugra.github.io/work/notes/2014-03-31/outlier-detection-in-time-series-signals-fft-median-filtering/

NLP Basics - 1

Tokenization - Process of segmenting running text into words and sentences.

Normalization - Steps we take to condense terms into lexical units.

Named Entity Extraction - Locate and classify named entities in text into pre-defined categories

Stemming - Reducing inflected words to their root form.

Lemmatization - Grouping together inflected forms of a words so that they can be analyzed as a single item.




API  Links:

1. Watson - https://www.ibm.com/watson/services/natural-language-classifier/
2. Google Cloud Platform - https://cloud.google.com/natural-language/
3. Amazon Lex - https://aws.amazon.com/lex/
4. Facebook Deep Text - https://code.facebook.com/posts/181565595577955/introducing-deeptext-facebook-s-text-understanding-engine/
5. Microsoft Cognitive Services - https://azure.microsoft.com/en-in/services/cognitive-services/directory/lang/



References:

https://www.ibm.com/developerworks/community/blogs/nlp/entry/tokenization?lang=en

Saturday, November 25, 2017

Interpreting ML Models - 1

Data Leakage - When information available in the context of model training is unavailable in deployment setting

Feature Importance Algorithm - Describes magnitude of dependance a model has on a feature

FairML can be used to measure model's dependence on its inputs

1. Seeing Data - glyphs, correlation graphs, 2-d projections, partial dependence plots, residual analysis

2. Related Models - OLS regression : penalized regression, generalized additive models, quantile regression

3. Understanding complex learning models: Surrogate modes, Local Interpretable Model agnostic explanations (LIME) , LOCO, Tree Interpreter.







References & Further Reading:

http://blog.fastforwardlabs.com/2017/03/09/fairml-auditing-black-box-predictive-models.html
https://www.oreilly.com/ideas/ideas-on-interpreting-machine-learning
http://www.nature.com/news/can-we-open-the-black-box-of-ai-1.20731
https://homes.cs.washington.edu/~marcotcr/blog/lime/

Friday, November 24, 2017

AI Environments

Here's some basic terminology related to environments in AI


1. Fully vs Partially Observable Environment / System

An environment is called fully observable if the what the intelligent agent senses is good enough to make an optimal decision.

In partially observable systems, an intelligent agent will generally need some memory to make the best possible action.

2. Deterministic vs Stochastic Environment

If we can uniquely determine outcomes from agent's actions, we are in an deterministic environment. If there is an element of randomness that impacts the outcome, we are in a stochastic environment.

3.  Discrete vs Continous

Discrete: We have finitely many action choices or finitely many things that can be sensed (e.g. Chess)
Continous: Space of possible actions or things we can sense may be infinite

4. Benign vs Adversarial Environments

Benign : Environment might be random/ stochastic but it has no objectives of its own to contradict your own objectives

Adversarial: There is an agent which actively observes you and counteracts what you are trying to achieve

Monday, August 1, 2016

Big Data & 3 Vs

What is Big Data?

In simple terms it can be considered as data which is too large to be processed on a machine or to be processed by traditional data processing applications. Generally 3V model is used to describe Big Data.



1. Volume - Refers to the size of data we are dealing with. 


2. Velocity - Refers to sources and format of data we are dealing with.


3. Variety - Refers to speed at which data is generated or speed at which data is made available for processing.






References:


1. The 3 V's were first defined in a research report by Douglas Laney in 2001 titled "3D Data Management: Controlling Data Volume, Velocity and Variety" .


2. Wikipedia

Friday, July 22, 2016

Machine Learning - 3 : Evaluating a Learning Algorithm


Some of the promising things that we can try in case our learning algorithm is erroneous. Some of the strategies for evaluating a learning algorithm are as of follows

1. Splitting Data Set.

Let's say we spilt the training data in some ratio (70:30). 70% of the data is used as the training set, rest of the data (30%) is set as test data. If the data is not randomly ordered that it's always better to do so .

We then compute test set error, which is the cumulative error when using test data against the hypothesis obtained from training data.

2. Model Selection

What degree of polynomial should we pick?

Split the test data into three categories : training data, cross validation data & test data. (Ratio may be 60:20:20). Now we can define three types of error

Training error
Cross Validation error
Test Data error

Now we can pick up model based on which model gives us lowest cross validator error. We can then use the test data to find out generalization error for the hypothesis chosen.

3. Identifying if the learning algorithm is suffering from high bias or high variance.

Plot a graph of error vs degree of polynomial to visualize or simply calculate training error and cross validation error. In case of  high bias (under-fitting data) , both training error and cross validation will be high. If the training error is low but cross validation error is high, it means we have high variance and the data is over-fitting.

In case we regularize parameters, a higher lambda will lead to high bias and a low lambda will lead to high variance.


4. Learning curves

Plot a graph of errors (training error/cross validation error) vs no. of training set (m). We can conclude following from the graph

- If learning algorithm has high bias, adding more training examples will not help.
- If learning algorithm has high variance, adding more training examples will help.



Summary here's what we can do to improve our learning algorithm:


1. Get more training examples (Helpful in case of high variance)

2. Try smaller set of features (Helpful in case of high variance)

3. Try getting additional features (Helpful in case of high bias)

4. Try adding polynomial features (Helpful in case of high bias)

5. Try decreasing lambda (Helpful in case of high bias)

6. Try increasing lambda (Helpful in case of high variance)


In case of support vector machines:

A larger C  value (small lambda) will result in lower bias, high variance. And low value will result in high bias, low variance

Larger (sigma^2) -> Features vary more smoothly. High bias, low variance. In case of small (sigma^2) value -> lower bias , high variance - features vary less smoothly.

So in case of overfitting, decrease C, increase (sigma^2)



References -

1. https://www.coursera.org/learn/machine-learning

Tuesday, July 12, 2016

Training Neural Networks : Notes

Steps to Train Neural Network 

1. Randomly initialize weights (theta)

2.  For any input/feature set, get the value obtained from hypothesis using forward propagation (Getting activation terms)

3. Compute cost function

4. Compute partial derivatives using backward propagation (Getting delta terms)

5. Gradient checking: Compare derivative values obtained via backward propagation wrt the values obtained using numerical estimation. Disable gradient checking if there is no significant difference.

6. Use any optimization algorithm to minimize cost function.





References -

1. https://www.coursera.org/learn/machine-learning

Sunday, July 10, 2016

Machine Learning - 2 : Regression Learning Algorithms

Building up on the previous article, we review some basic learning algorithms in this article.

A learning algorithm will work on training data set (generally denoted by m) and output a hypothesis (h).

y=h(x) where 'x' is the input. And the hypothesis h is derived by using the dataset m. For any new input 'x', we can predict the value 'y' using the hypothesis 'h'.

Linear Regression: 

Hypothesis for a linear regression (simple) learning algorithm with a variable x will be

Our learning algorithm will find out values of theta zero and theta one which will be a good fit for the data - gives us values close to 'y' for every 'x' in dataset. In order to do so, we define something called a cost function, which is essentially a squared error function.

To find the correct values of theta zero and theta one, we have to minimize the cost function over theta zero and theta one.

Mathematically, that means that we are trying to find a straight line which best fits in all the provided training data. We can use gradient descent algorithm to minimize our cost function:

In this we iteratively find new values of theta zero and theta one simulatneously.

Repeat until convergence (for j =0 & j=1) {

}

Alpha = Learning rate of the gradient descent. 

In case of multiple variables or features (multivariate linear regression), our functions will change

For example new hypothesis function will be:

where we have n features or variables. Similarly cost function and gradient descent functions will incorporate all n features.

Feature scaling: In order to make the gradient descent converge , we can scale all features in the same range. So let's if feature x1 was taking values from 0-1 and feature x2 was taking values from 0-5000, then we can scale down x2 by a factor of 5000 so that it also take ups values in the range similar to x1.




Polynomial Regression: 

We can also have hypothesis functions in which we use squared or cubic or squared root values of features. Here is an example for polynomial regression:


If number of features are not very large we can use normal equation to compute theta.




Logistic Regression:

Logistic regression is unlike the name suggests a classification algorithm. Predicted output from logistic regressions is always between 0 and 1. The hypothesis function is based on the sigmoid function.








List of machine learning posts :

1.  Nuts: Machine Learning -1 , Basics of machine learning
2.  Machine Learning - 2 : Regression Learning Algorithms




References -

1. https://www.coursera.org/learn/machine-learning
2. http://cs229.stanford.edu


Tuesday, July 5, 2016

Node.js - 1

Node.js = Javascript Runtime

Built on top of Chrome's V8 Javascript Engine.

And? And it also uses libuv. So while Node.js is single threaded from development perspective, it's actually using libuv internally for threading, implementing thread pool and managing file system events. 

How does async programming work in Node.js?

Node.js applications act on events i.e. the flow of the application is governed by events (like ) . Node.js manages this asynchronously via event loop.

What's event loop?
That explanation requires another post. Event loop essentially takes up any pending tasks in task queue/ callback queue and puts them on stack.

All functions to be executed by V8 are pushed to a stack and popped when they are executed. Event Loop will wait for the Stack to be empty and if it's so it will push a function from callback queue to stack.

How event loop works is explained beautifully by Philip Roberts at https://youtu.be/8aGhZQkoFbQ


And checkout the tool demoed in the talk above at  latentflip.com/loupe

Machine Learning - 1

Machine Learning = Science of getting computer (machine) to learn without being explicitly programmed


How do machines learn? What are the major types of machine learning problems.


Supervised Learning: 

Using data supplied for learning (supervised learning). In this case , algorithm uses previous data to predict the outcome for new data. So we feed in "right answers" to the algorithm.

Using supervised learning algo we can solve a regression or a classification problem .

Predicting house prices based on given sample data (let's say a mapping of area and actual price) will be a regression problem.

Regression = Predicting continuous value output.

Predicting the probability of cancer being malignant or benign based on dataset (tumor size and cancer type) for these classes or types of cancer will be a classification problem.

Classification = Predicting a discrete output (categorization into different classes)

In above examples, we had only a feature or parameter based on which we were predicting outcomes. But there will many parameters in real world machine learning problems.

Even for house pricing example, we can add parameters like age of house, nearest market etc.

So, essentially in case of supervised learning, algorithm is explicitly told what is the so called right answer.


Unsupervised Learning: 

In unsupervised learning problems, algorithms make sense of the data set on their own - find if there is any structure in the data set. The algorithm will segregate data in different clusters (hence cluster algorithm).

Once of the example here will be grouping different types of people together given a set of DNA microarray data.



List of machine learning posts :

1.  Nuts: Machine Learning -1 , Basics of machine learning
2.  Machine Learning - 2 : Learning Algorithms



References -

1. https://www.coursera.org/learn/machine-learning
2. http://cs229.stanford.edu