Thursday, March 9, 2017

The Top 10 AI And Machine Learning Use Cases Everyone Should Know About


This is an interesting article I found at Forbes.com . This might help to understand and visualize how important and critical is working with Machine Learning technology. Possibilities are endless, but for now, we are going to focus on 10 examples.... enjoy!


by: Bernard Marr

Machine learning is a buzzword in the technology world right now, and for good reason: It represents a major step forward in how computers can learn.

Very basically, a machine learning algorithm is given a “teaching set” of data, then asked to use that data to answer a question. For example, you might provide a computer a teaching set of photographs, some of which say, “this is a cat” and some of which say, “this is not a cat.” Then you could show the computer a series of new photos and it would begin to identify which photos were of cats.

Machine learning then continues to add to its teaching set. Every photo that it identifies — correctly or incorrectly — gets added to the teaching set, and the program effectively gets “smarter” and better at completing its task over time.

It is, in effect, learning.


1. Data Security

Malware is a huge — and growing — problem. In 2014, Kaspersky Lab said it had detected 325,000 new malware files every day. But, institutional intelligence company Deep Instinct says that each piece of new malware tends to have almost the same code as previous versions — only between 2 and 10% of the files change from iteration to iteration. Their learning model has no problem with the 2–10% variations, and can predict which files are malware with great accuracy. In other situations, machine learning algorithms can look for patterns in how data in the cloud is accessed, and report anomalies that could predict security breaches.

2. Personal Security

If you’ve flown on an airplane or attended a big public event lately, you almost certainly had to wait in long security screening lines. But machine learning is proving that it can be an asset to help eliminate false alarms and spot things human screeners might miss in security screenings at airports, stadiums, concerts, and other venues. That can speed up the process significantly and ensure safer events.


3. Financial Trading

Many people are eager to be able to predict what the stock markets will do on any given day — for obvious reasons. But machine learning algorithms are getting closer all the time. Many prestigious trading firms use proprietary systems to predict and execute trades at high speeds and high volume. Many of these rely on probabilities, but even a trade with a relatively low probability, at a high enough volume or speed, can turn huge profits for the firms. And humans can’t possibly compete with machines when it comes to consuming vast quantities of data or the speed with which they can execute a trade.


4. Healthcare

Machine learning algorithms can process more information and spot more patterns than their human counterparts. One study used computer assisted diagnosis (CAD) when to review the early mammography scans of women who later developed breast cancer, and the computer spotted 52% of the cancers as much as a year before the women were officially diagnosed. Additionally, machine learning can be used to understand risk factors for disease in large populations. The company Medecision developed an algorithm that was able to identify eight variables to predict avoidable hospitalizations in diabetes patients.

5. Marketing Personalization

The more you can understand about your customers, the better you can serve them, and the more you will sell. That’s the foundation behind marketing personalisation. Perhaps you’ve had the experience in which you visit an online store and look at a product but don’t buy it — and then see digital ads across the web for that exact product for days afterward. That kind of marketing personalization is just the tip of the iceberg. Companies can personalize which emails a customer receives, which direct mailings or coupons, which offers they see, which products show up as “recommended” and so on, all designed to lead the consumer more reliably towards a sale.

6. Fraud Detection

Machine learning is getting better and better at spotting potential cases of fraud across many different fields. PayPal, for example, is using machine learning to fight money laundering. The company has tools that compare millions of transactions and can precisely distinguish between legitimate and fraudulent transactions between buyers and sellers.

7. Recommendations

You’re probably familiar with this use if you use services like Amazon or Netflix. Intelligent machine learning algorithms analyze your activity and compare it to the millions of other users to determine what you might like to buy or binge watch next. These recommendations are getting smarter all the time, recognizing, for example, that you might purchase certain things as gifts (and not want the item yourself) or that there might be different family members who have different TV preferences.

8. Online Search

Perhaps the most famous use of machine learning, Google and its competitors are constantly improving what the search engine understands. Every time you execute a search on Google, the program watches how you respond to the results. If you click the top result and stay on that web page, we can assume you got the information you were looking for and the search was a success. If, on the other hand, you click to the second page of results, or type in a new search string without clicking any of the results, we can surmise that the search engine didn’t serve up the results you wanted — and the program can learn from that mistake to deliver a better result in the future.

9. Natural Language Processing (NLP)

NLP is being used in all sorts of exciting applications across disciplines. Machine learning algorithms with natural language can stand in for customer service agents and more quickly route customers to the information they need. It’s being used to translate obscure legalese in contracts into plain language and help attorneys sort through large volumes of information to prepare for a case.

10. Smart Cars

IBM recently surveyed top auto executives, and 74% expected that we would see smart cars on the road by 2025. A smart car would not only integrate into the Internet of Things, but also learn about its owner and its environment. It might adjust the internal settings — temperature, audio, seat position, etc. — automatically based on the driver, report and even fix problems itself, drive itself, and offer real time advice about traffic and road conditions.





Programming thought of the day:

  • Maybe if we start telling people the brain is an app they will start using it.


Friday, February 3, 2017

Java 8 - Intro


I hope I'm not too late to the party =).... Java 8 is the most awaited and is a major feature release of Java programming language.

Java 8 is a giant step forward for the Java language. Writing this tutorial has forced me to learn a lot more about it. In Project Lambda, Java gets a new closure syntax, method-references, and default methods on interfaces. It manages to add many of the features of functional languages without losing the clarity and simplicity Java developers have come to expect.

With this short tutorial, you should have a basic understanding of the new features and be ready to start using it. (well, that's the idea)


Implementing Runnable using Lambda expression
 
One of the first thing, I did with Java 8 was  trying to replace anonymous class with lambda expressions, and what could have been best example of anonymous class then implementing Runnable interface. Look at the code of implementing runnable prior to Java 8, it's taking four lines, but with lambda expressions, it's just taking one line. What we did here? the whole anonymous class is replaced by () -> {} code block.


//Old way
new Thread(new Runnable() 
{ 
@Override public void run() 
        { 
         System.out.println("Before Java8, too much code for too little to do"); 
 } 
}).start();

  
//Java 8 way: 
new Thread( () -> System.out.println("In Java8, Lambda expression rocks !!") ).start();



Event handling using Java 8 Lambda expressions
 
If you have ever done coding in Swing API, you will never forget writing event listener code. This is another classic use case of plain old Anonymous class, but no more. You can write better event listener code using lambda expressions as shown below.



// Before Java 8: 
JButton show = new JButton("Show"); 
show.addActionListener(new ActionListener() { 
 @Override public void actionPerformed(ActionEvent e) { 
  System.out.println("Event handling without lambda expression is boring"); 
 } 
}); 
  
// Java 8 way: 
show.addActionListener((e) -> { System.out.println("Light, Camera, Action !! Lambda expressions Rocks"); });



Iterating over List using Lambda expressions
 
If you are doing Java for few years, you know that most common operation with Collection classes are iterating over them and applying business logic on each elements, for example processing a list of orders, trades and events. Since Java is an imperative language, all code looping code written prior to Java 8 was sequential i.e. their is on simple way to do parallel processing of list items. If you want to do parallel filtering, you need to write your own code, which is not as easy as it looks. Introduction of lambda expression and default methods has separated what to do from how to do, which means now
Java Collection knows how to iterate, and they can now provide parallel processing of Collection elements at API level. In below example, I have shown you how to iterate over List using with and without lambda expressions, you can see that now List has a forEach() method, which can iterate through all objects and can apply whatever you ask using lambda code.



List<String> features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");
  
//Old way, Prior Java 8 : 
for (String feature : features) 
{ 
 System.out.println(feature); 
} 
  
  
//In Java 8:   
features.forEach(n -> System.out.println(n)); 
 
  
// Even better use Method reference feature of Java 8 
// method reference is denoted by :: (double colon) operator 
// looks similar to score resolution operator of C++ 
features.forEach(System.out::println); 


Using Lambda expression and Functional interface Predicate

Apart from providing support for functional programming idioms at language level, Java 8 has also added a new package called java.util.function, which contains lot of classes to enable functional programming in Java. One of them is Predicate, By using java.util.function. Predicate functional interface and lambda expressions, you can provide logic to API methods to add lot of dynamic behaviour in less code. Following examples of Predicate in Java 8 shows lot of common ways to filter Collection data in Java code. Predicate interface is great for filtering.



public static void main(String[] args) {
List languages = Arrays.asList("Jamaica", "Peru", "Argentina", "France", "United States");

  
System.out.println("Countries which starts with J :");
filter(languages, (str)->((String) str).startsWith("J"));
  
  
System.out.println("Countries which ends with a "); 
filter(languages, (str)->((String) str).endsWith("a")); 
  
  
System.out.println("Print all Countries :"); 
filter(languages, (str)->true); 
  
  
System.out.println("Print no Countries : "); 
filter(languages, (str)->false); 
  
  
System.out.println("Print Countries whose length greater than 4:"); 
filter(languages, (str)->((String) str).length() > 4);
    

}
@SuppressWarnings("unchecked")
public static void filter(List names, Predicate condition) { 
 names.stream().filter((name) -> (condition.test(name))).forEach((name) -> { 
   System.out.println(name + " "); 
 }); 
}

the output for the above code is:


Countries which starts with J :
Jamaica 
-----------------------------------
Countries which ends with a 
Jamaica 
Argentina 
-----------------------------------
Print all Countries :
Jamaica 
Peru 
Argentina 
France 
United States 
-----------------------------------
Print no Countries : 
-----------------------------------
Print Countries whose length greater than 4:
Jamaica 
Argentina 
France 
United States 


If you want to see more Java 8 examples, download the complete project from my GitHub account:

https://github.com/rolando-febrero/Java8-Examples





Programming thought of the day:


  • A clean house is the sign of a broken computer.



Friday, January 27, 2017

log4j with maven and Java - Project Example


log4j is a reliable, fast and flexible logging framework (APIs) written in Java, which is distributed under the Apache Software License. log4j is a popular logging package written in Java. log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages.

This utility will help you to keep track of everything your code does (and the government will keep track of everything you do in your life, just kidding -__-). Well, I'm going to show a short but solid example on how to use log4j. In this project we are going to log everything into a text file.

For this exercise we are going to use:
  • Java 1.7
  • Maven
  • log4j
  • Eclipse IDE

First of all, let's create a Maven project in Eclipse. (if you don't know how to install Maven in your PC, please go here)



Select option "Create a simple project"





Then fill all the fields according the bellow image. Here we are putting the info needed for our pom.xml




When done with these settings, open your pom.xml file and add the next lines:



        <properties>
  <jdk.version>1.7</jdk.version>
  <log4j.version>1.2.17</log4j.version>
 </properties>

 <dependencies>

  <dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>${log4j.version}</version>
  </dependency>

 </dependencies> 



Your pom.xml should look like this:




What we just did, was add some dependencies to our project, so with the help of mighty Maven, we are going to install them. Right click on pom.xml and select "Maven install"... Maven will pull all the jars needed (to your .M2 reposotory), in this case log4j






You should get a "BUILD SUCCESS" message in your console:





Log4j needs some properties file in which we specify its configuration. Let's create a "log4j.properties" file and added to our project:






Once created, double click on it, and add the following code:



# Root logger option
log4j.rootLogger=ERROR, stdout, file
log4j.rootLogger=INFO, stdout, file
# Redirect log messages to console log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n # Rirect log messages to a log file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=D:\\log4j-application.log log4j.appender.file.MaxFileSize=5MB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


Here we are specifying how and where to append the logs. Take a look at those lines and change accordingly.


Now we are going to proceed to write our code. Create a class and let's name it "HelloExample.java", here is the code:



package com.rolandoFebrero;

import org.apache.log4j.Logger;

public class HelloExample {
 
 final static Logger logger = Logger.getLogger(HelloExample.class);

 public static void main(String[] args) {
  
  HelloExample obj = new HelloExample();
  obj.runMe("Log4j Example");
  
 }
 
 private void runMe(String parameter){
  
  if(logger.isDebugEnabled()){
   logger.debug("This will be logged as DEBUG : " + parameter);
  }
  
  if(logger.isInfoEnabled()){
   logger.info("This will be logged as INFO : " + parameter);
  }
  
  logger.warn("This will be logged as WARN : " + parameter);
  logger.error("This will be logged as ERROR : " + parameter);
  logger.fatal("This will be logged as FATAL: " + parameter);
  
 }
 
}



now, create another class, this time let's name it "HelloExampleException.java". Here is the code:


package com.rolandoFebrero;

import org.apache.log4j.Logger;

public class HelloExampleException {

final static Logger logger = Logger.getLogger(HelloExampleException.class);
 
 public static void main(String[] args) {
 
  HelloExampleException obj = new HelloExampleException();
  
  try{
   obj.divide();
  }catch(ArithmeticException ex){
   logger.error("Something wrong!!!, you are getting an exception ", ex);
  }
  
  
 }
 
 private void divide(){
  
  int i = 3 / 0;

 } 
}



If you look at the code, in our first class, HelloExample.java, we are just printing a message, but with different log levels, such as debug, info, warn, error, fatal. This helps us to identify the kind of message we want to log. On the other file, HelloExampleExceptio.java, we are performing some math operation. In the main method we are making a call to "divide()". This divide() method just performs one single math operation, which is "3 / 0". Obviously, we can't divide a number by zero, so we will get an exception, in this specific case, an Arithmetic exception. I did that on purpose so we can see how an exception is logged by log4j.

Now, just right click on either HelloExample.java or HelloExampleExceptio.java; you might see the message in you console. Then go to where "log4j-application.log" file is located (hint: specified in log4j.properties) and there you'll find all the logs...


If you want to download the code for this project just go to my github account:

https://github.com/rolando-febrero/log4j-maven-Example.git




Programming thought of the day:


  • If at first you don’t succeed; call it version 1.0.


Friday, January 20, 2017

JAX-RS, Rest Service in Java with Jersey


JAX-RS stands for JAVA API for RESTful Web Services. JAX-RS is a JAVA based programming language API and specification to provide support for created RESTful Webservices. JAX-RS makes heavy use of annotations available since Java SE 5 to simplify development of JAVA based web services creation and deployment. It also provides supports for creating clients for RESTful web services..

In this case, we are going to use build a RESTful Web Service in Java using Jersey. But, what is Jersey? According to Jersey website (https://jersey.java.net/): 

"Jersey framework is more than the JAX-RS Reference Implementation. Jersey provides it’s own API that extend the JAX-RS toolkit with additional features and utilities to further simplify RESTful service and client development. Jersey also exposes numerous extension SPIs so that developers may extend Jersey to best suit their needs.

Goals of Jersey project can be summarized in the following points:
  • Track the JAX-RS API and provide regular releases of production quality Reference Implementations that ships with GlassFish;
  • Provide APIs to extend Jersey & Build a community of users and developers; and finally
  • Make it easy to build RESTful Web services utilising Java and the Java Virtual Machine."
For this exercise we are going to use:
  • Java 1.7
  • Tomcat 7
  • Jersey
  • Eclipse IDE
Open Eclipse and create a Web Dynamic Project. Let's name the project JAX-RS-Jersey. We don't need to create an EAR to our project, so unselect that option.



then, click next and on Web Module screen, select "Generate web.xml deployment descriptor" option 




Then, we need to place Jersey .jars in: Webcontent/WEB-INF/lib folder. You can get them @ https://jersey.java.net/ . Also I'll include all these files in my GitHub account, I'll explain at the end of this tutorial. Your workspace should show you the jars. If you don't see them, refresh the project



In Eclipse, go to Java Resources folder, right click on "src" and add a new package, let's name it: com.rolandoFebrero.rest (you can change these names to anything you want, this is a free country, don't worry). Now, create a new class inside that package: Hello.java

Again, create another package com.rolandoFebrero.restclient and inside that, create another java class, let's name it ClientTest.java. Your workspace should look like this:




In Hello.java, paste this code:



package com.rolandoFebrero.rest;

/**
 * Created by: Carlos Rolando Febrero
 * 
 */

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class Hello {
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayPlainTextHello() {
    return "Hello there, RESTfull service with Jersey (plain)";
  }
  // This method is called if XML is request
  @GET
  @Produces(MediaType.TEXT_XML)
  public String sayXMLHello() {
    return "<?xml version=\"1.0\"?>" + "<hello> Hello there, RESTfull service with Jersey (xml)" + "</hello>";
  }

  // This method is called if HTML is request
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String sayHtmlHello() {
    return "<html> " + "<title>" + "Hello Jersey" + "</title>"
        + "<body><h1>" + "Hello there, RESTfull service with Jersey (html)" + "</h1></body>" + "</html> ";
  }

} 


In ClientTest.java, paste this code:



package com.rolandoFebrero.restClient;

/**
 * Created by: Carlos Rolando Febrero
 * 
 */

import java.net.URI;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.client.ClientConfig;

public class ClientTest {
  public static void main(String[] args) {
    ClientConfig config = new ClientConfig();
    Client client = ClientBuilder.newClient(config);
    WebTarget target = client.target(getBaseURI());

    System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_PLAIN).get(String.class));
    System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_XML).get(String.class));
    System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_HTML).get(String.class));
  }

  private static URI getBaseURI() {
    return UriBuilder.fromUri("http://localhost:8080/restfuljersey").build();
  }
} 


In web.xml paste this code:



<?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" id="WebApp_ID" version="3.0">
 <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
     <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.rolandoFebrero.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app> 



Now, let's add Tomcat server to our workspace. Click in Server tab and add a new server...





Select Apache Tomcat server:




Select you Tomcat path and click Finish:



Now, that you have added Tomcat, right click and start:



Go to Console tab and make sure there are no error, if so, deelete Tomcat and added it again. After that add our project to Tomcat and restart the server:




At this point, if everything is OK, you should be able to go to: http://localhost:8080/JAX-RS-Jersey/rest/hello

You'll get the following:




Want to download the code for this project?

go to my github account: https://github.com/rolando-febrero/JAX-RS-Jersey





Programming thought of the day:


  • Unix is user friendly. It’s just selective about who its friends are.


Monday, January 9, 2017

What is the difference between Data Analytics, Data Analysis, Data Mining, Data Science, Machine Learning, and Big Data?


Lately, I've been doing some research on Machine Learning, which seems to be very interesting and impressive from my point of view. Creating software was always interesting, but coding to "educate" your software in a way that it can learn from previous experiences makes this even more interesting and more impressive. However, if you try to find information about Machine Learning, you will see some other topics that are closely related to it, which are: Data Analytics, Data Analysis, Data Mining, Data Science, and Big Data but, How do they differ from each other?

Here are some core concepts:

Data AnalyticsAnalytics is about applying a mechanical or algorithmic process to derive the insights for example running through various data sets looking for meaningful correlations between them. 

Data AnalysisAnalysis is really a heuristic activity, where scanning through all the data the analyst gains some insight

Data Miningthis term was most widely used in the late 90's and early 00's when a business consolidated all of its data into an Enterprise Data Warehouse. All of that data was brought together to discover previously unknown trends, anomalies and correlations such as the famed 'beer and diapers' correlation (Diapers, Beer, and data science in retail).

Data Sciencea combination of mathematics, statistics, programming, the context of the problem being solved, ingenious ways of capturing data that may not be being captured right now plus the ability to look at things 'differently' (like this Why UPS Trucks Don't Turn Left ) and of course the significant and necessary activity of cleansing, preparing and aligning the data.

Machine Learningthis is one of the tools used by data scientist, where a model is created that mathematically describes a certain process and its outcomes, then the model provides recommendations and monitors the results once those recommendations are implemented and uses the results to improve the model

In addition, I found a discussion about this topic and I wanted to share some thoughts which I consider it can help to clarify:

"The way I see it, machine learning is concerned with algorithms whose performance at some task improves as it gains experience at that task, while data mining is concerned with analysing data for the purpose of discovering unforeseen patterns or properties.

So the similarities are obvious, they both look at data, and hope to extract something of value from it. As I see it, the main difference is whether the goal is to reproduce known knowledge (I know that some of these pictures are cats, and some are dogs, now can some algorithm learn that?), or if the goal is to discover unknown knowledge (is there any interesting structure in this data set?). The two are, unsurprisingly, intertwined, as many of the properties or structure one may be searching for in data mining can be identified by machine learning algorithms. For instance, in data mining, one might be interested in determining if clusters of a certain form appear in the data, and could use a machine learning algorithm like k-means. K-means is a learning algorithm, in that if data has a known structure, it can learn it (under specific conditions, blah blah blah).

So data mining is exploratory, machine learning is focused on solving specific tasks well. That's my take on it, anyway." (by: Jordan Frank)


The following graphic nicely summarizes what all is involved in data science.










Programming thought of the day:


  • The truth is out there. Anybody got the URL?