Thursday, March 28, 2019

Metrics DS

RMSE - Root mean squared error
MSE - Mean squared error

MSE and RMSE are similar but behave differently for gradient based methods.
MAE - Mean absolute error

MAE optimal target constant is median - hence handles outliers better.
MSE optimal target constant is mean.

MAPE and MSPE are weighted versions of above.
P is for percentage.
They penalize based on the target absolute value.
For e.g. MSE and MAE will treat error of 1 equally for 9/10 and 999/1000.
But MAPE and MSPE won't. They will penalize more for 9/10 rather than
999/1000 since relative error is higher for 9/10.

Another one is RMSLE - uses logs. It penalizes relative errors and also unbiased towards smaller targets unlike MAPE and MSPE.

Sunday, March 24, 2019

cross validation strategies

leave one out cross validation - used when very few samples are available.
k fold - regular train/validate split(also called hold out) k times.

stratified cross validation

Advantages:
While validating, split the data in such a way that all classes are
represented in both train/validation sets.

Good for small and unbalanced datasets.

Disadvantages:
1. One specific issue that is important across even unbiased or
balanced algorithms, is that they tend not to be able to learn or test
a class that isn't represented at all in a fold, and furthermore even
the case where only one of a class is represented in a fold doesn't
allow generalization to performed resp. evaluated.

2. Also, supervised stratification compromises the technical purity of
the evaluation as the labels of the test data shouldn't affect
training, but in stratification are used in the selection of the
training instances.

tl;dr
Stratification is recommended if very few samples are available. For
large datasets, law of large numbers kicks in i.e. samples in
train/validation data will also be huge and representative of the
actual data distribution.

Sunday, March 3, 2019

Cartesian product code in python php

---php----
$a[0] = ['please', 'could you', ''];
$a[1] = ['create', 'make', ''];
$a[2] = ['a', 'an', ''];
$a[3] = ['quick', 'short',''];

$combined = array();
for($i = 0; $i < count($a); ++$i) {
$combined = combine($combined, $a[$i]);
}

print_r($combined);

function combine($a, $b) {
if(!$a) return $b;
if(!$b) return $a;
$out = array();
for($i = 0; $i < count($a); ++$i) {
for ($j = 0; $j < count($b); ++$j) {
$curr = trim($a[$i].' '.$b[$j]);
$out[] = $curr;
}
}
return $out;
}

---python 1 ---
def combine(a, b):
if not a:
return b
if not b:
return a
out = []
for ai in a:
for bi in b:
out.append((ai + ' ' + bi).strip())
return out

arr = []
arr.append(['please', 'cortana please', ''])
arr.append(['create', 'make', ''])
arr.append(['a', 'an', ''])
arr.append(['quick', 'short',''])
arr.append(['note'])

combined = []
for ci in arr:
combined = combine(combined, ci)
print(len(combined))
for e in combined:
print(e)
--------------------------

Python 2:
def find(all1, ans, i):
if i == len(all1):
print(ans)
return
j = 0
while j < len(all1[i]):
ans.append(all1[i][j])
find(all1, ans, i+1)
ans.pop()
j += 1


all1 = []
all1.append(["hi", "hey"])
all1.append(["there", "here"])
all1.append(["cute", "handsome"])

ans = []
find(all1, ans, 0)

Monday, February 25, 2019

mouse pointer getting stuck in bottom right corner

Windows 10
Dual monitor setup
Mouse: Microsoft Sculpt Ergonomic
Problem: Mouse pointer stuck in bottom right corner of screen
Solution: Remove the batteries and put them back.

Few days ago, I wasn't able to move my mouse pointer - so I changed batteries and it started working.

Again today the same thing. But today I noticed that the pointer was stuck in the bottom right corner.
May be it was the same earlier too but I didn't notice due to the dual monitor setup.

So I just removed the batteries and put them back again - it somehow resets something in the mouse and it starts working.

Tuesday, January 29, 2019

c# matching multiple word boundaries in a single string - regex OR pattern

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;


class MainClass {
public static void Main (string[] args) {
var input = "my bull dog is missing cat";
var topics = new List<string>();
topics.Add("cat");
topics.Add("bull dog");
string pattern = string.Join("|", topics.Select(x => @"\b" + @x + @"\b"));

var regex = new Regex(pattern, RegexOptions.IgnoreCase);

var m = regex.Matches(input);
foreach (Match match in m)
{
int i = match.Index;
Console.WriteLine(i);
}
}
}

Blog Archive