Thursday, September 13, 2018

JavaScript - Built in Functions - Date Object

Date object is another important built in Object in JavaScript. Check out the following examples:

var currDate = new Date();
console.log(currDate.toString());

var specificDate = new Date(2018,14,1);
console.log(specificDate.toString());

var specificDate = new Date('2018/10/1');
console.log(specificDate.toString());

console.log(Date.parse('2018/10/1'));
console.log(currDate.getDate());
console.log(currDate.getDay());


Console Output:

  • "Thu Sep 13 2018 06:20:34 GMT+0530 (India Standard Time)"
  • "Fri Mar 01 2019 00:00:00 GMT+0530 (India Standard Time)"
  • "Mon Oct 01 2018 00:00:00 GMT+0530 (India Standard Time)"
  • 1538332200000
  • 13
  • 4



Practice at:




Refer:



Wednesday, September 12, 2018

JavaScript - Built in Functions - Math Object

In JavaScript, we may use some built in objects. One amongst them is the Math object.
Some useful examples using Math object:

var myPI = Math.PI;
console.log(myPI);

var myE = Math.E;
console.log(myE)

var a = -5.5648;
console.log(Math.abs(a))
console.log(Math.ceil(a))
console.log(Math.floor(a))
console.log(Math.exp(10)); // e to the power of 10
console.log(Math.log(10)); // log of 10 to the base e
console.log(Math.max(10,20,30,40,50,60,70,80,90,100)); 
console.log(Math.min(10,20,30,40,50,60,70,80,90,100)); 
console.log(Math.random()); // Generates a random number between 0 and 1
console.log(Math.random()*100); // To get a random number between 0 and 100
console.log(Math.floor(Math.random()*100 + 1)); // To get a random integer between 1 and 100




Console Output:

  • 3.141592653589793
  • 2.718281828459045
  • 5.5648
  • -5
  • -6
  • 22026.465794806718
  • 2.302585092994046
  • 100
  • 10
  • 0.1347258816281942
  • 45.48059516716245
  • 90





Refer:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math

Practice at:
http://jsbin.com

JavaScript - Built in Functions - String Functions

In JavaScript, a string is treated as an array. The following are some commonly used string functions:

var myString = "  a quick brown fox  ";
console.log(myString);
console.log(myString.length);
console.log(myString[12]);
console.log(myString.charAt(12));
console.log(myString.concat("jumped over the lazy dog"));
console.log(myString.concat("jumped over the lazy dog").toUpperCase());
console.log(myString.concat("jumped over the lazy dog").toLowerCase());
console.log(myString.concat("jumped over the lazy dog").split(" "));
console.log(myString.trim());
console.log(myString.slice(4,12));


Console Output:

  • "  a quick brown fox  "
  • 21
  • "o"
  • "o"
  • "  a quick brown fox  jumped over the lazy dog"
  • "  A QUICK BROWN FOX  JUMPED OVER THE LAZY DOG"
  • "  a quick brown fox  jumped over the lazy dog"
  • ["", "", "a", "quick", "brown", "fox", "", "jumped", "over", "the", "lazy", "dog"]
  • "a quick brown fox"
  • "quick br"




Practice at:



Refer:



JavaScript - Built in Functions - parseInt - toString - toFixed

Built In Methods covered:

  1. parseInt()
  2. toString()
  3. toFixed()

There are some other useful built in functions which allow you to transform values. Consider the following code snippet:

var myNum = "25";
console.log(myNum+" is a "+typeof(myNum));

Console Output:
"25 is a string"

Now, we can use the parseInt method - which is available on the window object. That is, parseInt is available on the Global Scope.

var myNum = "25";
console.log(parseInt(myNum)+" is a "+typeof(parseInt(myNum)));

Console Output:
"25 is a number"

Now, let me try with a string which is not a number:

var myNum = "fdfdf";
console.log(parseInt(myNum)+" is a "+typeof(parseInt(myNum)));

Console Output:
"NaN is a number"


parseInt takes another argument optionally. Here I can specify the base digits of system I am operating.  The default is the decimal system, But, I can change it to hexadecimal system.  For example:

var myNum = "fbb23";
console.log(parseInt(myNum,16)+" is a "+typeof(parseInt(myNum)));

And, now if I hit run:

"1030947 is a number"

JavaScript has transformed from Hexadecimal system to Decimal System.

var myNum = 123;
var myNumStr = myNum.toString();
console.log(myNum+ " is now a "+typeof(myNumStr));

Console Output:
"123 is now a string"

toString is a method defined on the Object.prototype. This is the base prototype of all the objects. Therefore every object has by default has this toString() method. We can apply this on any object in JavaScript.

var myNum = 10.3;
var myNumFixed = myNum.toFixed();
console.log(myNumFixed+ " is now a "+typeof(myNumFixed));

Console Output:
"10 is now a string"

We can also specify the number of decimal places

var myNum = 3.1425;
var myNumFixed = myNum.toFixed(2);
console.log(myNumFixed+ " is now a "+typeof(myNumFixed));

Console Output:
"3.14 is now a string"


These are some basic built in parsers - used for transforming values.

Practice at:
http://jsbin.com



Tuesday, September 11, 2018

JavaScript - Built in Functions - setTimeout and setInterval

There are two very import built-in functions in JavaScript. This registered in window object.

  1. setTimeout
  2. setInterval


setTimeout: This function accepts two arguments.

  1.  Function , named or closures (anonymous functions)
  2.  Time period in milliseconds, this setTimeout must wait before executing the function.


Example:

setTimeout(function(){console.log("Here I am");},2000);

setInterval: Another important function is the setInterval function which can be used to call periodically a function.

We can use setInterval in combination with setTimeout to create really interesting stuffs. Below is one example.



var interval = setInterval(function(){console.log("Inside setInterval");},200);

setTimeout(function(){clearInterval(interval);},1000)





Refer:
https://alligator.io/js/settimeout-setinterval/

Practice at:
http://jsbin.com

Wednesday, August 15, 2018

Python - Machine Learning - using tree with Iris dataset to predict

We can use the below code snippet to perform prediction on Iris dataset.
from sklearn.datasets import load_iris
from sklearn import tree
iris = load_iris()
print(list(iris.target_names))
classifier = tree.DecisionTreeClassifier()
classifier = classifier.fit(iris.data, iris.target)
print(classifier.predict([[5.1,3.5,1.4,1.5]]))


Refer:

Python - Machine Learning - Install scikit-learn in Ubuntu



To install scikit-learn package, we need to install its dependencies:

$ sudo pip install scipy
The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/ubuntu/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting scipy
  Downloading https://files.pythonhosted.org/packages/2a/f3/de9c1bd16311982711209edaa8c6caa962db30ebb6a8cc6f1dcd2d3ef616/scipy-1.1.0-cp27-cp27mu-manylinux1_x86_64.whl (30.8MB)
    100% |████████████████████████████████| 30.8MB 38kB/s 
Requirement already satisfied: numpy>=1.8.2 in ./.local/lib/python2.7/site-packages (from scipy)
Installing collected packages: scipy
Successfully installed scipy-1.1.0
$


Now Install sklearn:

$ sudo pip install sklearn
The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/ubuntu/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting sklearn
  Downloading https://files.pythonhosted.org/packages/1e/7a/dbb3be0ce9bd5c8b7e3d87328e79063f8b263b2b1bfa4774cb1147bfcd3f/sklearn-0.0.tar.gz
Collecting scikit-learn (from sklearn)
  Downloading https://files.pythonhosted.org/packages/bc/67/370aa248f54769a56216707ad7b9af19745e85a603fafa47bde353f327fb/scikit_learn-0.19.2-cp27-cp27mu-manylinux1_x86_64.whl (5.0MB)
    100% |████████████████████████████████| 5.0MB 179kB/s 
Installing collected packages: scikit-learn, sklearn
  Running setup.py install for sklearn ... done
Successfully installed scikit-learn-0.19.2 sklearn-0.0
$

Refer: