Thursday, 26 October 2017

Machine Learning Notes




Machine learning notes on Clustering

Clustering:
  The method of identifying similar groups(patterns) of data in a dataSet is called clustering.
  clustering can be divided into two subgroups :
      Hard Clustering
      Soft Clustering

    Types of clustering algorithms:

    Connectivity models:
    Centroid models:
      K-Means clustering algorithm
    Distribution models:
      example:- Normal, Gaussian
    Density Models:

    K-Means clustering algorithm:
 

   










Hierarchical Clustering:

Applications:-

·        Recommendation engines
·        Market segmentation
·        Social network analysis
·        Search result grouping
·        Medical imaging

·        Image segmentation

Monday, 25 September 2017

Intro to Machine learning:python setup

Tried many versions/flavours of python but this is finally working:

while setting up python for machine learning.
I followed the below steps :-
Install python-2.7.13.msi
• set PATH=C:\Python27
• set PATH=C:\Python27\Scripts
Downloaded:- • numpy-1.13.1+mkl-cp27-cp27m-win32.whl 
                       • scipy-0.18.0-cp27-cp27m-win32.whl
Installing numpy: pip install numpy-1.13.1+mkl-cp27-cp27m-win32.whl
Installing scipy: pip install scipy-0.18.0-cp27-cp27m-win32.whl
You can test the correctness using below cmds:-
>>> import numpy
>>> import scipy
>>> import sklearn
>>> numpy.version.version
'1.13.1'
>>> scipy.version.version
'0.19.1'
>>>

Good things of doing this course:-

  • Helped me in beliving that I can learn Machine Learning.
  • Very interactive,you will not get bored
  • Nice simple exercises,me being new to python running my code in python is very Joyful.
  • Andrew's classes were very therotic and mathematical,one needs great concentration in understanding those concepts.

Friday, 22 September 2017

Dive in to Python world from Java


 Personal review:-

  •  python is very easy to learn if you are coming from java background.
  • Like Java, python is very vast,Target what you want to learn. For me I needed python knowledge as I am starting working on Machine Learning.
    • It has lot of math tools n libraries required for Machine Learning

Thursday, 21 September 2017

Hibernate short notes


  • Transient
  • Persistance
  • Detached


Save() - hibernate function,it will return generated key
Persist() - JPA function works same as save(),return type is void
Merge() - JPA function,return type is void
Update() - hibernate function
saveOrupdate() - hibernate function


Different between session.get() and session.load():-

Actually, both functions are use to retrieve an object with different mechanism, of course.

1. session.load()
• It will always return a “proxy” (Hibernate term) without hitting the database. In Hibernate, proxy is an object with the given identifier value, its properties are not initialized yet, it just look like a temporary fake object.
• If no row found , it will throws an ObjectNotFoundException.
2. session.get()
• It always hit the database and return the real object, an object that represent the database row, not proxy.
• If no row found , it return null.

Wednesday, 20 September 2017

Spring Notes

Bean Scopes in Spring:

singleton

prototype

request

session

global-session

Good question: How spring Handles singleton?
https://stackoverflow.com/questions/42376756/how-spring-singleton-scope-for-dao-class-works-internally

Notes on Singleton Pattern and Immutable Objects

Where exactly the Singleton Pattern is used in real application?

  • ·       Logger
  • ·       DB Connection

 A Strategy for Defining Immutable Objects

  1. Don't provide "setter" methods — methods that modify fields or objects referred to by fields.
  2. Make all fields final and private.
  3. Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods.
  4. If the instance fields include references to mutable objects, don't allow those objects to be changed:
    • Don't provide methods that modify the mutable objects.
    • Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.

Monday, 18 September 2017

Autowiring in Spring

Autowiring can be done on:


  • directly into fields/attributes
  • via explicit setter methods
  • via explicit constructor parameters

Sunday, 17 September 2017

Spring Boot application in AWS

Hosted my first Spring Boot application in AWS today,with mysql DB.

Reference:- https://aws.amazon.com/blogs/devops/deploying-a-spring-boot-application-on-aws-using-aws-elastic-beanstalk/

URL:- http://springbooteb-svk-prod.us-east-2.elasticbeanstalk.com/
 http://springbooteb-svk-prod.us-east-2.elasticbeanstalk.com/people

 Its easier than i expected,though its not a cake walk for beginners, one with prior knowledge of deployment in various scenarios only will be able to succeed in the first go.

Thursday, 14 September 2017

Tools i use for my development


   ☐ Intellij for Core Java development
   ☐ Eclipse for J2EE development
   ☐ Sublime Text for text editing
   ☐ cmDer for cmd prompt alternative
   ☐ heidisql as a DB client
   ☐ chrome browser
   ☐ one note for note taking-online, everything

Wednesday, 13 September 2017

Docker

Started studying about the Docker as the topic is very fascinating.
Developed a spring application n deployed it in the Docker.

Github link:- Docker Sample java Application

docker is nothing but a container to hold your appication and seperating the OS there by many such dockers can run in the same system sharing same OS just the high layer varies.

There's a concept same as Maven called DockerHub,
From where you download all repos i.e images of Java,Tomcat,DB etc and you can also upload your Docker image to the docker.

Docker creates a local repo just like maven .m2 and uses it for later usage.

Steps:
install docker in windows 10 machine
mvn clean install
docker build -t gs-spring-boot-docker-0.1.0 .
docker run -p 8080:8080 gs-spring-boot-docker-0.1.0

What is this Serverless?How is it useful?

I have been seeing this catchy word over the internet so was bit curious to know what exactly it is.

So serverless doesn't exactly mean no need of server,but instead it means that you no need to worry setting up the server n configuring for the whole day as we saw few devOps team guys will be working.

Its just that you embed your server say tomcat or jetty in our own application and running.But for this the application should be as small as possible i.e microservices.

Saturday, 9 September 2017

Lambdas

Lambda basics:-

whay lambdas?
-> helps functional programming

Advantages:-

  • concise code
  • parallel processing

Drawback of oops:
All code blocks should be associated with class and objects.

Lambda Expression examples

(int a) -> a * 2; // Calculate the double of a
a -> a * 2; // or simply without type
(a, b) -> a + b; // Sum of 2 parameters
If the lambda is more than one expression we can use { } and return

(x, y) -> {
int sum = x + y;
int avg = sum / 2;
return avg;
}
A lambda expression cannot stand alone in Java, it need to be associated to a functional interface.

interface MyMath {
    int getDoubleOf(int a);
}

MyMath d = a -> a * 2; // associated to the interface
d.getDoubleOf(4); // is 8

HATEOAS

HATEOAS:- Hypertext As The Engine Of Application State
.
The way of sending the response with links which directs the next possible operations.
{
"name": "Vikram",
"id": 1
"link" [
{
"href":"http://localhost:8080/myApp/1",
"rel":"self"
}
]
}

Richardson maturity Modal:-


Friday, 8 September 2017

Spring Framework Complete

understand the complete flow and add spring security,cloud,log,database change and add an article in dzone. sept 8-10

worked on Spring application pet Clinic accepted as the Reference project for all spring features since 2007.
Github link:- PETCLINIC GITHUB
in Spring site:- link

Its Developed by spring developers and opensource contributors.
Spring-Petclinic is spread accross different repos in github each with different approach to solve the same common requirement.

List of features it explains:-
Spring MVC
  With XMl
  With Annotations+xml
  With JavaConfig
  With JSP UI
  With Thymeleaf UI


Spring Rest - HATEOAS

  With JDBC
  With jpa - EntityManager
  Spring Jpa

For both H2 Database and Mysql.

Spring Rest + Angular JS

Spring boot

Spring Microservices
  Eureka for node register and discovery


Docker 

Kill the process running at port 8080

netstat -a -o -n

taskkill /F /PID 28344

Thursday, 7 September 2017

Revision Day 8: Functional Programming with Java 8 Functions

I was very much curious to know more about this functional programmimg,Since its buzzing with the release of java 8.

below is the notes that I got to understand after reading this tutorial:- <link>

Revision Day 7: Multi-threading- synchronized : Class level vs object level

Class level vs object level

Class Level:-

private void lock() throws InterruptedException {
System.out.println(Thread.currentThread().getName());
synchronized (Foo.class) {
System.out.println("in Class  " + Thread.currentThread().getName());
Thread.sleep(4000);
System.out.println("in Class " + Thread.currentThread().getName() + " end");
}

}
main(){
Foo b1 = new Foo();
Thread t1 = new Thread(b1);
Foo b2 = new Foo();
Thread t3 = new Thread(b2);
t1.setName("t1");
t3.setName("t3");
t1.start();
t3.start();
}

Output:-
t1
t3
in Class  t1
in Class t1 end2
in Class  t3
in Class t3 end2

in case of Class level blocking,only one thread can run the class-synchronized method at a time,Also to note an instance synchronized method can also run simultaneously

As seen above eventhough t1 and t3 entered the block at the same time,only after completion of t1, t3 can start accessing the method.

Object Level:-

public void Lock() throws InterruptedException {
System.out.println(Thread.currentThread().getName());
synchronized (this) {
System.out.println("in obj " + Thread.currentThread().getName());
Thread.sleep(4000);
System.out.println("in obj " + Thread.currentThread().getName() + " end");
}
}

main(){
Foo b1 = new Foo();
Thread t1 = new Thread(b1);
Foo b2 = new Foo();
Thread t3 = new Thread(b2);
t1.setName("t1");
t3.setName("t3");
t1.start();
t3.start();
}

Output:-
t1
t3
in obj t1
in obj t3
in obj t3 end
in obj t1 end

In case of instance synchronized method,both t1 and t3 can start accessing the method at the same time because both are acting on 2 different objects.

Tuesday, 5 September 2017

Notes on Threads and Multi-threading Application

MultiThread Project takes the directory as input,and gives the PDF and TXT files with their size,name and path.

Github path:- MultiThread_Application

Notes:-

Concurrency 
· BlockingQueue
- interface - extremely useful in producer-consumer problems
o ArrayBlockingQueue,- implementation of BlockingQueue interface
o PriorityBlockingQueue - Same as BlockingQueue but its contents should be comparable either
 naturl ordering or by                 customizing
o DelayQueue - Same as BlockingQueue but its contents can be accessed only after certain time say 5 sec.
o SynchronousQueue - A special type of BlockingQueue with the size of zero.you can insert into it only 
when the consumer is waiting
o LinkedBlockingQueue - linkedlist approach of ArrayBlockingQueue -- implementation of BlockingQueue
 interface
· BlockingDeQueue - Same as BlockingQueue but with Dequque functions
o LinkedBlockingDeque - Same as BlockingQueue but with linkedlist + Dequque functions
· Java Thread Pools
o Executor - Its an interface with execute function.
o Executors Class - its like a factory class that supports all ExecutorService inteface functions.
o ExecutorService - Interface extends Executor class, Executor framework.
o ScheduledExecutorService - creates threads upon specified time.
· ConcurrentMap
o ConcurrentHashMap - HashMap with all methods synchronized
· ConcurrentNavigableMap
o ConcurrentSkipListMap
· ConcurrentSkipListSet
· ConcurrentLinkedQueue
· CopyOnWriteArrays - thread-safe variant of ArrayList - The collection internally copies its contents over 
to a new array upon any modification, so readers accessing the contents of the array incur no synchronization 
costs
· Callable and Future - callable is alternative to Runnable where object has to be returned,and Future is
 going to hold the returned object.
· Condition Class - Condition factors out the Object monitor methods (wait, notify and notifyAll) into distinct 
objects to give the effect of having multiple wait-sets per object, by combining them with the use of arbitrary
 Lock implementations.
· CyclicBarrier - allows multiple threads to wait for each other (barrier) before proceeding.
· CountDownLatch - just a counter need to decrease and call await method till then all threads after the 
current thread are held.
· ReentrantLock Class - alternate to using Synchronized block.
eg:-
private Lock lock = new ReentrantLock();
lock.lock();
try {
increment();
} finally {
lock.unlock();
}

· ReentrantReadWriteLock - ReentrantLock with additional readlock and writeLock functionalities.
· Semaphore - counter is greater than zero, then access is allowed. If it is zero, then access is denied.
use sem.acquire();,sem.release(); to get the permit and release
· Exchanger - an CLass which helps to exchange the classes between threads.

· java.util.concurrent.atomic - increment the integer in synchronized way.

 ExecutorService service = Executors.newFixedThreadPool(4);//total 4 threads availabe in the pool
        new Thread(new FileOperations()).start();        for (int i = 0; i < 2; i++) {
            service.submit(new PDFreader());//2 threads given to PDFReader
        }
        for (int i = 0; i < 2; i++) {
            service.submit(new TxtReader());//2 threads given to TxtReader

        }

Revision Day 5: Spring BOOT


Revision Day 5: Gradle and CSS

CSS Selectors:-


The element Selector

{
    text-align: center;
    color: red;
}

The id Selector

#para1 {
    text-align: center;
    color: red;
}

The class Selector

.center {
    text-align: center;
    color: red;
}

Grouping selectors:-
h1, h2, p {
    text-align: center;
    color: red;
}

GRADLE:-


buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.6.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'gs-accessing-data-rest'
    version = '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-rest")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("com.h2database:h2")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

Revision Day 4: Spring Java Config

completed the spring Java config example with lot of difficulty.
used plain hibernate initially later changed to EntityManager and was successfully able to run the code.

Thursday, 31 August 2017

Wednesday, 30 August 2017

Revision Day 2: SPRING MVC

Completed the simple mvc application.
Changed it from spring jdbctemplate to hibernate for Dao implementation.


Web.xml:-

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://java.sun.com/xml/ns/javaee"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
       version="3.0">

       <display-name>Archetype Created Web Application</display-name>

       <welcome-file-list>
             <welcome-file>home.jsp</welcome-file>
       </welcome-file-list>

       <servlet>
             <servlet-name>spring-mvc</servlet-name>
             <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
             <load-on-startup>1</load-on-startup>
       </servlet>
       <servlet-mapping>
             <servlet-name>spring-mvc</servlet-name>
             <url-pattern>/</url-pattern>
       </servlet-mapping>

<!-- <context-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>/WEB-INF/spring-mvc-servlet.xml</param-value>
       </context-param>

       <listener>
             <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
       </listener> -->
</web-app>

Wasted lot of time in understanding how this “spring-mvc-servlet.xml” getting loaded i.e. how spring is getting initiated when I am not specifying which file to fetch nor the file path??

After so much of investigation I came to know that
<servlet-name>spring-mvc</servlet-name>
             <url-pattern>/</url-pattern>

Here server will take servlet-name as the standard file name and appends -servlet at the end like this:-
Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-mvc-servlet.xml

When you want to put your Servlet file in your custom location or with custom name, rather than the default naming convention [servletname]-servlet.xml and path under Web-INF/ ,then you can use ContextLoaderListener.

I ran simple java main class to test the application functionality by placing the spring-mvc-servlet.xml in resources folder.

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import jbr.springmvc.model.Login;
import jbr.springmvc.model.User;
import jbr.springmvc.service.UserService;

public class App {
  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-mvc-servlet.xml");

    UserService obj = (UserService) context.getBean("userService");
    Login login = new Login();
    login.setUsername("admin");
    login.setPassword("admin123");
    User user = obj.validateUser(login);
    System.out.println(user.toString());
  }
}

<build>
             <finalName>SpringMvcUser</finalName>
             <plugins>
                    <plugin>
                           <groupId>org.mortbay.jetty</groupId>
                           <artifactId>jetty-maven-plugin</artifactId>
                           <version>8.1.16.v20140903</version>
                           <configuration>
                                 <scanIntervalSeconds>10</scanIntervalSeconds>
                           </configuration>
                    </plugin>
             </plugins>
       </build>

Use mvn jetty:run

SPRING:-

Spring framework provides flexibility to configure beans in multiple ways such as XML, Annotations, and JavaConfig.



What is dependency injection and its types?

A technique where the instance is not directly passed but its specified how to create the instance, this helps in loose coupling and testing.

Types:-
  1.              Setter injection
  2.              Constructor injection
We prefer setter injection over constructor injection because it helps in resolving cyclic dependency.

Scenario:- If A is dependent on B and vice-versa, then we can neither create instance of A nor B because A requires instance of B passed in the constructor and B requires instance of A be passed in the constructor, This can be resolved by setter injection where the instance A is created first then we can set the B’s instance.

Installing Docker and Minikube

  install docker-   sudo apt install docker.io   set user to docker group:- sudo gpasswd -a   {user} docker   imp commands:- ...