Introduction - IBM Watson/Bluemix Services
We've already had two blog posts on IBM Watson so far. One blog post for Watson's Tone Analyzer service, and another blog post for Watson's Visual Recognition service. We're going to go through this article quickly since we already have a lot of experience using Watson. If this is your first time, see the Tone Analyzer service since it's meant for people getting started.
IBM's Personality Insight service "predicts personality characteristics, needs, and values through written text." We're going to go through setting the service up, and creating a node project to use that service. Create The Service On IBM's Website
First, we need to set the Personality Insight service up on IBM's website. If you don't have a Bluemix account, register for one. (Free, very easy, no credit card.)
Next, go to the Personality Insights page and click the "Get started free" button. (Log in if you need too.) You'll be prompted to create a new project. For the project name, enter "srcmakePersonality" and click the "Create Project" button on the bottom right of the screen. You'll be taken to your project page. Click the "Show" button and make note of the username and password under the Credentials section. We'll need to use them soon to authenticate our API calls to the Personality Insights API. (Did this section seem fast? Remember to watch the youtube video for this topic or go back and read the Tone Analyzer article, since we went through these steps in detail.) So we have our API credentials, now we need to actually write some code to use the service. Setting Up Our Node Project
We're going to make a project using Node to test using the Personality Insights service. Since this is just going to be a basic project (that we've already went over in detail in the Tone Analyzer article), just copy the code from GitHub. In your terminal in the folder you want the project to be stored in:
git clone https://github.com/srcmake/watson-node-starter cd watson-node-starter
It's a basic node project with the Watson library installed, and a config.js file to store our credentials.
Open "config.js" and for the username and password, enter the usename and password that we got in the previous section after creating the srcmakePersonality project on the Bluemix website. Adding The Personality Insight Code
Our project is set up, but doesn't actually do anything useful yet. We want to test the Personality Insight service. Specifically, we want to make an API call to it and pass it some text to be analyzed. We're going to write code that does that, with the help of the API reference documentation.
Add the following code to "index.js".
We simply include the watson library and our config library. The text we want to analyze is Martin Luther King Jr's "I Have a Dream" speech. We call the Personality Insights API on this text, and output the result.
Testing Our Code
Testing our code is simple. In the command line, run index.js.
node index.js
You'll see the following output:
Actually, the output was 440 lines long! But I cut it down to only the first three traits. You can see that Watson returns some personality names, and some numbers that correspond to what the speaker/writer of the text is of that personality. Conclusion
In this article, we looked at and coded for the Watson Personality Insight service. It was pretty simple, all we did was create a project on the Bluemix website to get a username and password, then we wrote some Node code to call the API that controls the Personality Insight service.
Obviously, you can spin the project off and customize it for whatever your need is, but we at least know how to use the service.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake Introduction
In the previous article, we had an Introduction to Machine Learning. That article was just to learn about the concepts, but now we're going to be looking at some very simple coding examples using a basic python library.
We're going to start by installing the necessary python libraries (and python if we don't already have it), and then we'll look at some basic code for a simple example. Installation - Python and SKLearn
First, install Python, Pip, and Pipenv if you don't already have it. (If you don't know about Python virtual envionments, read this quick article and come back here.) In an Ubuntu 16.06 terminal:
sudo apt-get install python3 sudo apt-get install python3-pip sudo pip3 install pipenv
Next, in your project folder (create it if you didn't already), install the libraries we need using pipenv.
pipenv install sklearn pipenv install numpy pipenv install scipy
Out installation is complete, hopefully. Test it out.
Start Our Project - Make Sure Our Installation Works
We need to make sure that our installation works. In the project folder (which you should already be in, since we were installing our libraries into the folder using pipenv before), create a file named "main.py".
touch main.py
Add the following code to main.py.
You can see that the code just imports our sklearn library, and prints hello world. We need to run this file through pipenv.
pipenv run python3 main.py
Hopefully you see the "Hello world from srcmake, machine learning" in your console. If you did, great, our project is set up.
Machine Learning Coding - A Basic Classifier
Our library (and project) is working, so now we just need to add some useful machine learning code to our file.
We're going to start things off with a very basic classifier. Let's imagine that we have some features and labels. For example, let's say that for a certain height (x) and weight (y) pair, we know if someone is physically fit (red) or not physically fit (blue). We can draw this out on a graph.
Notice that the points divide themselves in such a way that we can draw an orange line between them. It separates the points nicely.
Of course, this is a very crude example, but we can see that the "classifier" in this case can easily determine if someone is physically fit or not by plotting their height and weight, and then seeing which side of the line the point will be on.
The actual "technique" that we're going to use to determine this line will be the machine learning algorithm "Naive Bayes". Let's write some code for it, in simple Python.
We simply import our libraries, declare our coordinates in the X variable, declare our labels in the Y variable, we create a classifier, and we train it. Then we print two predicted values.
pipenv run python3 main.py
It should print [0] [1], which is what we expect. Run the code with:
Conclusion
We did it! We used Python to train a machine learning classifier. Of course, this was just a simple line, but if the training data was more complicated, we'd have to tune some parameters to make the classifier better.
In the next article, we'll look at a cheat sheet that talks about different machine learning techniques/algorithms and when it's best to use them.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake Introduction To The SeriesWe're going to be exploring Machine Learning in this article and a few others. This article is going to be the introduction. We'll learn about fundamentals of machine learning. In further articles, we'll look at specific techniques and libraries. What Is Machine Learning?So what is Machine Learning? Honestly, machine learning is a term that encompasses a bunch of different techniques, and defining just one term is sort of an insult to the scope of the many types of machine learning. It would be like asking what Calculus is. Calculus is a type of math that mainly covers derivatives and integrals, but it goes over so much more than that and the complexity can get really deep. Machine learning is similar to that, where normal people treat it like magic, but the people who actually know machine learning know that it's sort of a buzzword. Machine Learning is basically teaching a computer program how to answer a question. This is done by giving the program "training data". The program should take a new piece of data and predict something about that new data. The training data usually has an input and an output. The inputs are called "features", and the outputs are called "labels". The features and labels should be correlated, to make a good machine learning program. For example, we could try to predict if someone likes the color pink, based on their age and gender. The "features" are the age and gender, and the "label" is whether they like the color pink. We can have a bunch of examples as training data (like a 12 year old girl likes pink, a 14 year old girl likes pink, a 30 year old man doesn't like pink, etc.), and use it to train a machine learning classifier. Once trained, we can use the machine learning classifier to make predictions for new ages/genders. That's a very simple example, but other more interesting examples include Visual Recognition (a program knowing what an image is of) and Natural Language Processing (programs understand speech). However, we're going to keep things basic for now and learning about the two types of machine learning types. Classification vs. RegressionThere are generally two types of machine learning types: Classification and Regression. The difference is really simple. In classification, we literally ask a machine learning program to literally classify something. "Here's some input. What is it? The input is our features (age/gender, or even features extracted from words or images), and the output is the label. The machine learning classifier will predict what something is given the features. Regression is more math-like. We give the machine some features, and try to use a model to predict (usually) some number output. For example, a house with 2 bedrooms and 1 bathroom sells for $5. A house with 4 bedrooms and 2 bathrooms sell for $10. A house with 6 bedrooms and 3 bathrooms sells for $15. How much do you think a house with 8 bedrooms and 4 bathrooms sell for? The answer (is predictably) $20. Easy so far, right? "Classifications" literally classify features into specific labels. "Regressions" are like math plots, where the features are used to predict a number (or some non-specific label). Next we'll look at two more machine learning concepts, Supervised and Unsupervised learning. Supervised vs. UnsupervisedSupervised and Unsupervised learning are actually simple concepts. In "Supervised" learning, we tell the machine learning the answer. We say that the output is in the training data. A 12 year old girl "likes pink". A 2 bedroom/1 bathroom house sells for "$5". In Unsupervised learning, there is no specific output label. This is also known as clustering. This is helpful for making observations about the training data. For example, if we ran a bookstore and recorded the details of which book each customer buys (for example "A 20 year old male buys The Great Gatsby"). We use unsupervised learning to try to find similarities between groups. For instance, the same people who buy "The Lord of the Rings" books probably buy "Game of Thrones" books. There's no correct input/output, we're just observing similarities. So now we know about Supervised and Unsupervised machine learning. We're getting to the final section, where we bring everything together...Technique! Technique!We've gone over what machine learning is, but how do we actually use it? Okay, so real talk...It depends. First, you need to decide what it is that you're using the machine learning program for. What question is it answering? Depending on what it is you're trying to do, you pick a machine learning algorithm to try out. You feed it training data and test the output. If you're satisfied with the output, then we keep the program. If we're not satisfied, then usually we tweak the algorithm's parameters a bit, possibly tweak our features (input). We can also try different algorithms. I know what I said sounds vague, but honestly that's what it is: trying different things and checking if the accuracy is good. So assuming that you DID know all the machine learning algorithms for all of the machine learning question types, then the question remains...how do we code it? Honestly, there are a lot of different machine learning libraries out there. There's almost no reason to make your own, so check out open source libraries for your favorite programming language. We'll go over these in another article. ConclusionOkay, so we didn't do much, but the foundation is set. We know what machine learning is, and we know the types of machine learning. In the next article, we're going to go over some machine learning examples using simple Python libraries, to get some hands-on experience. From there, we'll expand our horizons by learning about all sorts of machine learning questions and algorithms. We'll also look at a bunch of libraries that have those algorithms already implemented, making our lives easier. Finally, we'll go deep by using Neural Networks and Deep Learning, as well as special areas, such as NLP and Visual Recognition. Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake |
AuthorHi, I'm srcmake. I play video games and develop software. Pro-tip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
License: All code and instructions are provided under the MIT License.
|