Maxbox Starter60 Machine Learning
Maxbox Starter60 Machine Learning
Maxbox Starter60 Machine Learning
Machine Learning
____________________________________________________________________________
maXbox Starter 60 II - Data Science with ML.
"In the face of ambiguity, refuse the temptation to guess."
– The Zen of Python
This tutor introduces the basic idea of machine learning with a very simple
example. Machine learning teaches machines (and me too) to learn to carry out
tasks and concepts by themselves. It is that simple, so here is an overview:
http://www.softwareschule.ch/examples/machinelearning.jpg
For example, say your business needs to adopt a new technology in Sentiment
Analysis and there’s a shortage of experienced candidates who are qualified to
fill the relevant positions (also known as a skills gap).
You can also skip collecting data by your own and expose the topic straight to
an Internet service API like REST to forward clustered data traffic directly to
your server being accessed. How important collect, cluster and classify is
points out next 3 definitions;
At its core, most algorithms should have a proof of classification and this is
nothing more than keeping track of which feature gives evidence to which class.
The way the features are designed determines the model that is used to learn.
This can be a confusion matrix, a certain confidence interval, a T-Test
statistic, p-value or something else used in hypothesis1 testing.
http://www.softwareschule.ch/examples/decision.jpg
Lets start with some code snippets to grape the 5 steps, assuming that you have
Python or maXbox already installed (everything at least as recent as 2.7 should
be fine or better 3.6 as we do), we need to install NumPy and SciPy for
numerical operations, as well as matplotlib and sklearn for visualization:
"Collaboration"
1/8
import itertools
import numpy as np
import matplotlib.pyplot as plt
import maxbox as mx
"Collecting"
class BlogSpider(scrapy.Spider):
name = 'blogspider'
start_urls = ['https://blog.scrapinghub.com']
We are going to create a class called LinkParser that inherits some methods from
HTMLParser which is why it is passed into the definition. This snippet can be
used to run scrapy spiders independent of scrapyd or the scrapy command line
tool and use it from a script.
2/8
"Clustering"
The 2 arrays you can see is X as the feature array and y as the predict array
(array object as a list)! We create a fake income / age clustered data that we
use for our K-Means clustering example above for the simplicity.
"Classification"
Now we will use linear SVC to partition our graph into clusters and split the
data into a training set and a test set for further predictions.
3/8
# Run classifier, using a model that is too regularized (C too low) to see
# the impact on the results
By setting up a dense mesh of points in the grid and classifying all of them, we
can render the regions of each cluster as distinct colors:
def plotPredictions(clf):
xx, yy = np.meshgrid(np.arange(0, 250000, 10),
np.arange(10, 70, 0.5))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
plt.figure(figsize=(8, 6))
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8)
plt.scatter(X[:,0], X[:,1], c=y.astype(np.float))
plt.show()
print(svc.predict([[100000, 60]]))
print(svc.predict([[50000, 30]]))
You should choose to ask a more meaningful question. Without some context,
you might as well flip a coin.
4/8
"Conclusion"
The last step as an example of confusion matrix usage to evaluate the quality of
the output on the data set. The diagonal elements represent the number of points
for which the predicted label is equal to the true label, while off-diagonal
elements are those that are mislabeled by the classifier. The higher the
diagonal values of the confusion matrix the better, indicating many correct
predictions.
print(cm)
Write some code, run some tests, fiddle with features, run a test, fiddle with
features, realize everything is slow, and decide to use more layers or factors.
5/8
"Comprehension"
Principal component analysis (PCA) is often the first thing to try out if you
want to cut down number of features and do not know what feature extraction
method to use.
PCA is limited as its a linear method, but chances are that it already goes far
enough for your model to learn well enough.
Add to this the strong mathematical properties it offers and the speed at which
it finds the transformed feature data space and is later able to transform
between original and transformed features; we can almost guarantee that it also
will become one of your frequently used machine learning tools.
6/8
The script can be found at:
http://www.softwareschule.ch/examples/811_mXpcatest_dmath_datascience.pas
..\examples\811_mXpcatest_dmath_datascience.pas
• The first principal factor is negatively correlated with the second and
fourth variables, and positively correlated with the third variable
• The table of principal factors show that the highest scores are usually
associated with the first two principal factors, in agreement with the
previous results
Const
N = 11; { Number of observations }
Nvar = 4; { Number of variables }
Of course, its not always this and that simple. Often, we dont know what number
of dimensions is advisable in upfront. In such a case, we leave n_components or
Nvar parameter unspecified when initializing PCA to let it calculate the full
transformation. After fitting the data, explained_variance_ratio_ contains an
array of ratios in decreasing order: The first value is the ratio of the basis
vector describing the direction of the highest variance, the second value is the
ratio of the direction of the second highest variance, and so on.
Being a linear method, PCA has, of course, its limitations when we are faced
with strange data that has non-linear relationships. We wont go into much more
details here, but its sufficient to say that there are extensions of PCA.
Ref:
Building Machine Learning Systems with Python
Second Edition March 2015
DMath Math library for Delphi, FreePascal and Lazarus May 14, 2011
http://www.softwareschule.ch/box.htm
http://fann.sourceforge.net
http://neuralnetworksanddeeplearning.com/chap1.html
http://people.duke.edu/~ccc14/pcfb/numpympl/MatplotlibBarPlots.html
Doc:
Neural Networks Made Simple: Steffen Nissen
http://fann.sourceforge.net/fann_en.pdf
http://www.softwareschule.ch/examples/datascience.txt
https://maxbox4.wordpress.com
https://www.tensorflow.org/
7/8
https://sourceforge.net/projects/maxbox/files/Examples/13_General
/811_mXpcatest_dmath_datascience.pas/download
https://sourceforge.net/projects/maxbox/files/Examples/13_General
/809_FANN_XorSample_traindata.pas/download
https://stackoverflow.com/questions/13437402/how-to-run-scrapy-from-within-a-
python-script
Plots displaying the explained variance over the number of components is called
a Scree plot. A nice example of combining a Screeplot with a grid search to find
the best setting for the classification problem can be found at
http://scikit-learn.sourceforge.net/stable/auto_examples/plot_digits_pipe.html.
ValueError: The truth value of an array with more than one element is ambiguous. Use
a.any() or a.all()
PERSONS REPRESENTED.
SCENE. Elsinore.
8/8