Gradient Color 3D Surface Plots using the R Language

The R language is often used by data scientists. The language has strong numerical and statistical functions. Here’s an example which shows how to make a 3D surface plot of Rastigin’s function (a standard benchmark used to evaluate numerical optimization algorithms) with a color gradient.

RastriginGraphColorGradientUsingR

The R code is:

x<-seq(-5.12, 5.12, length=100)
y<-seq(-5.12, 5.12, length=100)
f<-function(x,y) { 20+(x^2-10*cos(2*3.14*x))+
     (y^2-10*cos(2*3.14*y)) }
z<-outer(x,y,f)
nrz<-nrow(z)
ncz<-ncol(z)
jet.colors <-  colorRampPalette(c("midnightblue","blue",
     "cyan","green", "yellow","orange","red", "darkred"))
nbcol<-64
color<-jet.colors(nbcol)
zfacet<-z[-1,-1]+z[-1,-ncz]+z[-nrz,-1]+z[-nrz,-ncz]
facetcol<-cut(zfacet,nbcol)
persp(x,y,z,col=color[facetcol],phi=20,theta=-60,
   ticktype="detailed",d=5,r=1,shade=0.1,expand=0.6) 

The first four commands set values of Rastrigin’s function in a matrix z. The next two statements set up the number of row and columns in the z matrix. The jet.colors object holds the range of colors to use in the color gradient. You can use any two or more colors here.

Variable nbcol (64) is the number of different colors to use. The zfacet equation calculates the color for each square on the plot. You can think of it as a magic equation. The cut function used with object facetcol (“facet color”) divides its arguments into intervals.

The persp function generates the 3D plot. This example can be used as a template to generate a 3D surface plot of other functions — just change the definition of f (and the ranges for x and y).

Posted in Machine Learning | Leave a comment

The 2015 Visual Studio Live Conference in San Francisco

I will be speaking at the 2015 Visual Studio Live conference, in San Francisco, from June 15 to 18. See http://www.vslive.com.

My talk is “Introduction to R for C# Programmers”. R is an open source programming language used by data scientists and by statistics people. Although R has been around for a long time, over the past 18 months or so demand for people who know R has increased greatly.

Alternatives to R include IBM’s SPSS, SAS, Mathematica, and MatLab and its clones SciLab and Octave. Although I don’t have concrete supporting data, it seems as though R has become the dominant language for data science.

VisualStudioLive2015SanFrancisco

In my talk, I’ll start by explaining why software developers should consider learning a bit about R. Then I’ll explain R from a developer’s point of view. Even though there are quite a few Web sites that have R tutorials, most of them approach R as if the person learning R is a university student majoring in mathematics or physics. I’ll show several examples of problems solved using R and using C#.

The Visual Studio Live conferences are offered in several cities during the year. In 2015 events are in Austin, San Francisco, Redmond, New York, Orlando, and Las Vegas. In my opinion, there are three really good conferences for developers who use Microsoft technologies, and Visual Studio Live is one of those three good conferences.

The 2015 San Francisco event will have about 60 sessions. The topics that interest me the most are Azure cloud computing, data and analytics, and Web development. Usually there’s a large turnout for talks on mobile and Visual Studio related topics. The conference will be in the Fairmont Hotel which has the famous Tonga Room and Hurricane Bar restaurant that I’ve always wanted to see.

Good conferences are typically too expensive for a person to pay themselves, so most attendees have their registration fees paid by their company. Conferences like Visual Studio Live are very efficient and cost effective training for developers (compared to on-site training).

If you’re a developer who uses Microsoft technologies, check out the 2015 Visual Studio Live conference in San Francisco — highly recommended.

Posted in Conferences | Leave a comment

R Language OOP using R6

The R language is used mostly by statisticians and data people. There are at least four, quite different, ways to write object oriented code in R — S3, S4, R5, R6. By far my favorite approach is to use the relatively new R6 version 2 (released October 2014).

Compared to the alternatives, the R6 OOP approach and syntax is closest to OOP in languages like C# and Python. Here’s a quick example. First, I created a text file that defines a Car class and saved it as CarClass.R on my machine:

# CarClass.R

require("R6")

Car <- R6Class("Car",
  public = list(
    make = NULL,
    price = NULL,
    initialize = function(ma, pr) {
      self$make <- ma
      self$price <- pr
    },
    setMake = function(ma) { self$make <- ma },
    # setPrice = function(pr) { self$price <- pr },
    display = function() {
      cat("Make = ", self$make,
        " Price = ", self$price, "\n")
    }
  )
)

In even this tiny demo, there’s a lot going on. The R6 package was pre-installed in the R version 3.1.2 I was using. The Car class has public fields “make” and “price”, a constructor named initialize, and methods “setMake” and “display”. Because the fields are public, I don’t really need function setMake.

RLanguageClassesWithR6v2

To call the Car class, I did this:

> setwd("C:\\Data\\Junk\\RStuff")
> source("CarClass.R")

By running the CarClass.R script, the definition of a Car class is created and can be used. To create a Car object and display it:

> someCar = Car$new("Audi", 40000)
> # display using class defined display()
> someCar$display()
Make =  Audi  Price =  40000 
> # display using built-in print()
> print(someCar)

Objects created using R6 get a built-in print() capability. To modify the Car object:

> someCar$setMake("BMW")
> someCar$price  = 50000
> someCar$display()
Make =  BMW  Price =  50000

I modified the Car make field using the set-function, but modified the public price field directly. R6 objects are reference objects so you have to be careful when assigning one object to another. My bottom line: R6, very nice.

Posted in Machine Learning, Miscellaneous | Leave a comment

Neural Network Train-Validate-Test Stopping

I wrote an article titled “Neural Network Train-Validate-Test Stopping” in the May 2015 issue of Visual Studio Magazine. See https://visualstudiomagazine.com/articles/2015/05/01/train-validate-test-stopping.aspx.

VMSTrainValidateTest

One way to think of a neural network is as a complex math function that has many numeric constants, called weights and biases. In order to make a useful neural network you must find the values of the weight and biases. This is called training the neural network.

Training is accomplished by using a set of so-called training data that has known input and correct output values. Training searches for values of weights and biases so that the NN’s computed output values closely match the known correct output values in the training data.

One of the main challenges of NN training is that because NNs are so complex, it’s possible to find values for the weights and biases so that the computed output values exactly match the target training output values. But then when the NN is presented with new, previously unseen data, the NN predicts very poorly. This phenomenon is called over-fitting.

TrainValidateTestGraphWithBorder

There are several strategies to try and deal with over-fitting. One of them is train-validate-test stopping. The idea is to divide your data into three groups. Training data is used to find the values for weights and biases. During training, every now and then during the search, the current weights and biases are applied to the validation set. When error start to increase, over-fitting is starting to happen so you stop training. When finished, the test set is used to estimate the final accuracy of the model.

Posted in Machine Learning | Leave a comment

Himmelblau’s Function

I came across an interesting function I hadn’t seen before, called Himmelblau’s function. Its definition is pretty simple:

z = f(x, y) = (x^2 + y – 11)^2 + (x + y^2 – 7)^2

HimmelblausFunctionUsingR

I graphed the function using the R language with these commands:

x<-seq(-5, 5, length=50)
y<-seq(-5, 5, length=50) 
f<-function(x,y) { (x^2 + y -11)^2 +
  (x + y^2 - 7)^2 }
z<-outer(x,y,f)
nrz<-nrow(z)
ncz<-ncol(z)
jet.colors <- colorRampPalette(c("blue","cyan","green",
 + "yellow","orange","orange","red", "red", "red"))
nbcol<-64
color<-jet.colors(nbcol)
zfacet<-z[-1,-1]+z[-1,-ncz]+z[-nrz,-1]+z[-nrz,-ncz]
facetcol<-cut(zfacet,nbcol)
persp(x,y,z,col=color[facetcol],phi=40,theta=45,d=5,r=1,
 + ticktype="detailed",shade=0.1,expand=0.7)

Himmelblau’s function is used when experimenting with numerical optimization algorithms. Unlike most benchmark functions which have one minimum value to search for, the Himmelblau function has four minimum points:

z = 0 at x = 3.00, y = 2.00
z = 0 at x = -2.805118, y = 3.131312
z = 0 at x = -3.779310, y = -3.283186
z = 0 at x = 3.584428, y = -1.848126

Because there are multiple targets, the function is called a multi-modal function.

Posted in Machine Learning

A Recap of Science Fiction Movies of 2014

Like many geeks, I really like science fiction movies. The year 2014 is now long over so I figured I’d review the science fiction films released in 2014. It wasn’t a good year. Here are 10 significant (not necessarily good; meaning only that I saw them) sci-fi movies from 2014, with my ratings, from best to worst. I didn’t include super hero movies like “Captain America: The Winter Soldier”, and “X-Men: Days of Future Past” because they belong in a separate category in my mind.


1. Lucy. Grade = B. Scarlett Johansson is Lucy. Bad drug dealers (are there good drug dealers?) sew bags of some weird new drug inside her. The bag breaks and Lucy gets super powers. Not good news for the drug dealers. Directed by Luc Besson, one of my all-time favorites, even if he is French. (Just joking, I’m part-French).

Lucy


2. Edge of Tomorrow. Grade = C+. Tom Cruise (yes, I know he’s kind of a nut case, but he’s a good actor) and Emily Blunt are soldiers fighting an alien invasion. After getting covered in alien goo, Cruise gets the ability to travel backwards in time if he gets killed. Movie would have been much better if edited down a bit — it was just too long; we must have seen Cruise get killed a dozen times.

EdgteOfTomorrow


3. Guardians of the Galaxy. Grade = C+. Most of my friends liked this movie much more than I did. It made a huge amount of money. We have space pirates, orphans-turned-assassin, talking trees and raccoons, and a story that made little sense. Lots of action, little plot.

GuardiansOfTheGalaxy


4. The Signal. Grade = C. A low-budget film I didn’t see until it came to cable. Some college students are on a road trip (uh oh) and so they naturally decide to track a hacker who is taunting them to a deserted house in the middle of the dessert. What could possibly go wrong? (Other than abduction by aliens?)

TheSignal


5. Godzilla. Grade = C. Sci-fi remakes are almost always a disaster so I had low expectations but this film was not as bad as I feared it would be. Not good by any stretch of the imagination but not terrible. Godzilla is a good guy and defeats two huge bad monsters that want to breed in San Francisco.

Godzilla


6. Under the Skin. Grade = C-. A low-budget film with Scarlett Johansson as an alien who seduces men then kills them. Might be worth it. The film had a very arty feel to it. One of those movies where the cinematography is more important than the story.

UnderTheSkin


7. Interstellar. Grade = C-. This movie was a huge disappointment for me because it was directed by Christopher Nolan who did Inception (2010), a brilliant film. Interstellar is three boring hours of Matthew McConaughey’s character talking like he’s drugged and barely awake, and Anne Hathaway’s character just being whiny, stupid, and annoying (not really much of a stretch for her). What a dud movie.

Interstellar


8. The Maze Runner. Grade = D. I hate movies that are doing nothing more than trying to set up a sequel. Teenage boys live in the Glade by themselves. It’s surrounded by the Maze to keep them in. Grievers are creatures that roam around. This movie was a complete waste of time.

MazeRunner


9. The Hunger Games: Mockingjay – Part 1. Grade = D. I REALLY hate movies that are doing nothing more than trying to set up a sequel. I didn’t like the first Hunger Games movie, but was pleasantly surprised by the second movie. Not so for this third one. Katniss shoots her arrows at things. This Hunger movie was a just a way for the producers to squeeze money out of a concept that has been beaten to death.

Mockingjay


10. Divergent. Grade = F. If a director and writer could somehow take all the things I don’t like in a sci-fi movie and combine them into one horrible experience, you’d have something like Divergent. Beatrice is Divergent. She becomes Tris. Tobias is Four. Everyone looks like a model. Everyone is as dumb as a model. Really bad movie.

Divergent


Posted in Top Ten

Recap of the 2015 Interop Conference

The annual Interop conference is one of the largest IT conferences in the world. I spoke at the 2015 event, which ran from April 27 to May 1, in Las Vegas. I estimate the event had well over 10,000 people attending. See http://www.interop.com

CrowdAtInteropCropped

My talk was titled “Solving Business Problems with Neural Networks”. I described the three types of prediction problems neural networks can solve, explained how neural networks work so attendees can understand the vocabulary of the field, and compared four ways that an IT department can implement neural networks.

JamesAtBeginningOfTalk

The typical Interop attendee (at least based on the people I spoke to) was a fairly senior member of the IT staff of a large company. But with 10,000+ attendees, there were certainly all kinds of people there.

A major topic at Interop 2015, as it has been for the past few years, was SDN (software-defined networks). The idea is to allow network engineers to manage complex computer network, which are often made up of quite different hardware and software products from dozens or hundreds of different companies, through a software layer of abstraction that provides a consistent interface. OpenFlow is a set of protocols that is one form of SDN.

InsideTheExpo

Another topic that was featured at Interop 2015 was DevOps. DevOps is really a concept which means that, in an enterprise, software applications and systems should be developed in cooperation and harmony with the enterprise’s IT systems. To be honest, I see DevOps as simply placing a buzz word on common sense.

I had a good time at the 2015 Interop — I learned a lot, believe I represented Microsoft (my company) well by delivering useful information to attendees, met interesting people, and got renewed enthusiasm for my work. I highly recommend the Interop conference and hope to be there in 2016.

Posted in Conferences