In [ ]:
from __future__ import division, print_function, absolute_import, unicode_literals
Broadly speaking, machine-learning methods constitute a diverse collection of data-driven algorithms designed to classify/characterize/analyze sources in multi-dimensional spaces. The topics and studies that fall under the umbrella of machine learning is growing, and there is no good catch-all definition. The number (and variation) of algorithms is vast, and beyond the scope of these exercises. While we will discuss a few specific algorithms today, more importantly, we will explore the scope of the two general methods: unsupervised learning and supervised learning and introduce the powerful (and dangerous?) Python package scikit-learn
.
By AA Miller (Jet Propulsion Laboratory, California Institute of Technology.)
(c) 2016 California Institute of Technology. Government sponsorship acknowledged.
In [ ]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
scikit-learn
At the most basic level, scikit-learn
makes machine learning extremely easy within Python. By way of example, here is a short piece of code that builds a complex, non-linear model to classify sources in the Iris data set that we learned about yesterday:
from sklearn import datasets
from sklearn.ensemble import RandomForestClassifier
iris = datasets.load_iris()
RFclf = RandomForestClassifier().fit(iris.data, iris.target)
Those 4 lines of code have constructed a model that is superior to any system of hard cuts that we could have encoded while looking at the multidimensional space. This can be fast as well: execute the dummy code in the cell below to see how "easy" machine-learning is with scikit-learn
.
In [ ]:
# execute dummy code here
from sklearn import datasets
from sklearn.ensemble import RandomForestClassifier
iris = datasets.load_iris()
RFclf = RandomForestClassifier().fit(iris.data, iris.target)
Generally speaking, the procedure for scikit-learn
is uniform across all machine-learning algorithms. Models are accessed via the various modules (ensemble
, SVM
, neighbors
, etc), with user-defined tuning parameters. The features (or data) for the models are stored in a 2D array, X
, with rows representing individual sources and columns representing the corresponding feature values. [In a minority of cases, X
, represents a similarity or distance matrix where each entry represents the distance to every other source in the data set.] In cases where there is a known classification or scalar value (typically supervised methods), this information is stored in a 1D array y
.
Unsupervised models are fit by calling .fit(X)
and supervised models are fit by calling .fit(X, y)
. In both cases, predictions for new observations, Xnew
, can be obtained by calling .predict(Xnew)
. Those are the basics and beyond that, the details are algorithm specific, but the documentation for essentially everything within scikit-learn
is excellent, so read the docs.
To further develop our intuition, we will now explore the Iris dataset a little further.
Problem 1a What is the pythonic type of iris
?
In [ ]:
You likely haven't encountered a scikit-learn Bunch
before. It's functionality is essentially the same as a dictionary.
Problem 1b What are the keys of iris?
In [ ]:
Most importantly, iris contains data
and target
values. These are all you need for scikit-learn
, though the feature and target names and description are useful.
Problem 1c What is the shape and content of the iris
data?
In [ ]:
print(np.shape( # complete
print( # complete
Problem 1d What is the shape and content of the iris
target?
In [ ]:
print(np.shape( # complete
print( # complete
Finally, as a baseline for the exercises that follow, we will now make a simple 2D plot showing the separation of the 3 classes in the iris dataset. This plot will serve as the reference for examining the quality of the clustering algorithms.
Problem 1e Make a scatter plot showing sepal length vs. sepal width for the iris data set. Color the points according to their respective classes.
Hint - determine which columns of data correspond to sepal length and sepal width.
In [ ]:
plt.scatter( # complete
plt.xlabel('sepal length')
plt.ylabel('sepal width')
Unsupervised machine learning, sometimes referred to as clustering or data mining, aims to group or classify sources in the multidimensional feature space. The "unsupervised" comes from the fact that there are no target labels provided to the algorithm, so the machine is asked to cluster the data "on its own." The lack of labels means there is no (simple) method for validating the accuracy of the solution provided by the machine (though sometimes simple examination can show the results are terrible).
For this reason [note - this is my (AAM) opinion and there many be many others who disagree], unsupervised methods are not particularly useful for astronomy. Supposing one did find some useful clustering structure, an adversarial researcher could always claim that the current feature space does not accurately capture the physics of the system and as such the clustering result is not interesting or, worse, erroneous. The one potentially powerful exception to this broad statement is outlier detection, which can be a branch of both unsupervised and supervised learning. Finding weirdo objects is an astronomical pastime, and there are unsupervised methods that may help in that regard in the LSST era.
To begin today we will examine one of the most famous, and simple, clustering algorithms: $k$-means. $k$-means clustering looks to identify $k$ convex clusters, where $k$ is a user defined number. And here-in lies the rub: if we truly knew the number of clusters in advance, we likely wouldn't need to perform any clustering in the first place. This is the major downside to $k$-means. Operationally, pseudocode for the algorithm can be summarized as the following:
initiate search by identifying k points (i.e. the cluster centers)
loop
assign each point in the data set to the closest cluster center
calculate new cluster centers based on mean position of all cluster points
if diff(new center - old center) < threshold:
stop (i.e. clusters are defined)
The threshold is defined by the user, though in some cases the total number of iterations is also An advantage of $k$-means is that the solution will always converge, though the solution may only be a local minimum. Disadvantages include the assumption of convexity, i.e. difficult to capture complex geometry, and the curse of dimensionality (though you can combat that with dimensionality reduction after yesterday).
In scikit-learn
the KMeans
algorithm is implemented as part of the sklearn.cluster
module.
Problem 2a Fit two different $k$-means models to the iris data, one with 2 clusters and one with 3 clusters. Plot the resulting clusters in the sepal length-sepal width plane (same plot as above). How do the results compare to the true classifications?
Hint - the .labels_
attribute of the KMeans
object will return the clusters measured by the algorithm.
In [ ]:
from sklearn.cluster import KMeans
Kcluster = KMeans( # complete
# complete
plt.figure()
plt.scatter( # complete
plt.xlabel('sepal length')
plt.ylabel('sepal width')
Kcluster = KMeans( # complete
# complete
plt.figure()
plt.scatter( # complete
plt.xlabel('sepal length')
plt.ylabel('sepal width')
With 3 clusters the algorithm does a good job of separating the three classes. However, without the a priori knowledge that there are 3 different types of iris, the 2 cluster solution would appear to be superior.
Problem 2b How do the results change if the 3 cluster model is called with n_init = 1
and init = 'random'
options? Use rs
for the random state [this allows me to cheat in service of making a point].
*Note - the respective defaults for these two parameters are 10 and k-means++
, respectively. Read the docs to see why these choices are, likely, better than those in 2b.
In [ ]:
rs = 14
Kcluster1 = KMeans(# complete
plt.figure()
plt.scatter(# complete
plt.xlabel('sepal length')
plt.ylabel('sepal width')
A random aside that is not particularly relevant here
$k$-means evaluates the Euclidean distance between individual sources and cluster centers, thus, the magnitude of the individual features has a strong effect on the final clustering outcome.
Problem 2c Calculate the mean, standard deviation, min, and max of each feature in the iris data set. Based on these summaries, which feature is most important for clustering?
In [ ]:
print("feature\t\t\tmean\tstd\tmin\tmax")
for featnum, feat in enumerate(iris.feature_names):
print("{:s}\t{:.2f}\t{:.2f}\t{:.2f}\t{:.2f}".format( # complete
Petal length has the largest range and standard deviation, thus, it will have the most "weight" when determining the $k$ clusters.
The truth is that the iris data set is fairly small and straightfoward. Nevertheless, we will now examine the clustering results after re-scaling the features. [Some algorithms, cough Support Vector Machines cough, are notoriously sensitive to the feature scaling, so it is important to know about this step.] Imagine you are classifying stellar light curves: the data set will include contact binaries with periods of $\sim 0.1 \; \mathrm{d}$ and Mira variables with periods of $\gg 100 \; \mathrm{d}$. Without re-scaling, this feature that covers 4 orders of magnitude may dominate all others in the final model projections.
The two most common forms of re-scaling are to rescale to a guassian with mean $= 0$ and variance $= 1$, or to rescale the min and max of the feature to $[0, 1]$. The best normalization is problem dependent. The sklearn.preprocessing
module makes it easy to re-scale the feature set. It is essential that the same scaling used for the training set be used for all other data run through the model. The testing, validation, and field observations cannot be re-scaled independently. This would result in meaningless final classifications/predictions.
Problem 2d Re-scale the features to normal distributions, and perform $k$-means clustering on the iris data. How do the results compare to those obtained earlier?
Hint - you may find 'StandardScaler()'
within the sklearn.preprocessing
module useful.
In [ ]:
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler().fit( # complete
Kcluster = KMeans( # complete
plt.figure()
plt.scatter( # complete
plt.xlabel('sepal length')
plt.ylabel('sepal width')
These results are almost identical to those obtained without scaling. This is due to the simplicity of the iris data set.
How do I test the accuracy of my clusters?
Essentially - you don't. There are some methods that are available, but they essentially compare clusters to labeled samples, and if the samples are labeled it is likely that supervised learning is more useful anyway. If you are curious, scikit-learn
does provide some built-in functions for analyzing clustering, but again, it is difficult to evaluate the validity of any newly discovered clusters.
What if I don't know how many clusters are present in the data?
An excellent question, as you will almost never know this a priori. Many algorithms, like $k$-means, do require the number of clusters to be specified, but some other methods do not. As an example DBSCAN
. In brief, DBSCAN
requires two parameters: minPts
, the minimum number of points necessary for a cluster, and $\epsilon$, a distance measure. Clusters are grown by identifying core points, objects that have at least minPts
located within a distance $\epsilon$. Reachable points are those within a distance $\epsilon$ of at least one core point but less than minPts
core points. Identically, these points define the outskirts of the clusters. Finally, there are also outliers which are points that are $> \epsilon$ away from any core points. Thus, DBSCAN
naturally identifies clusters, does not assume clusters are convex, and even provides a notion of outliers. The downsides to the algorithm are that the results are highly dependent on the two tuning parameters, and that clusters of highly different densities can be difficult to recover (because $\epsilon$ and minPts
is specified for all clusters.
In scitkit-learn
the
DBSCAN
algorithm is part of the sklearn.cluster
module. $\epsilon$ and minPts
are set by eps
and min_samples
, respectively.
Problem 2e Cluster the iris data using DBSCAN
. Play around with the tuning parameters to see how they affect the final clustering results. How does the use of DBSCAN
compare to $k$-means? Can you obtain 3 clusters with DBSCAN
? If not, given the knowledge that the iris dataset has 3 classes - does this invalidate DBSCAN
as a viable algorithm?
Note - DBSCAN labels outliers as $-1$, and thus, plt.scatter()
, will plot all these points as the same color.
In [ ]:
from sklearn.cluster import DBSCAN
dbs = DBSCAN( # complete
dbs.fit( # complete
dbs_outliers = # complete
plt.figure()
plt.scatter( # complete
plt.scatter( # complete
plt.xlabel('sepal length')
plt.ylabel('sepal width')
I was unable to obtain 3 clusters with DBSCAN
. While these results are, on the surface, worse than what we got with $k$-means, my suspicion is that the 4 features do not adequately separate the 3 classes. [See - a nayseyer can always make that argument.] This is not a problem for DBSCAN
as an algorithm, but rather, evidence that no single algorithm works well in all cases.
The following query will select 10k likely galaxies from the SDSS database and return the results of that query into an astropy.Table
object. (For now, if you are not familiar with the SDSS DB schema, don't worry about this query, just know that it returns a bunch of photometric features.)
In [ ]:
from astroquery.sdss import SDSS # enables direct queries to the SDSS database
GALquery = """SELECT TOP 10000
p.dered_u - p.dered_g as ug, p.dered_g - p.dered_r as gr,
p.dered_g - p.dered_i as gi, p.dered_g - p.dered_z as gz,
p.petroRad_i, p.petroR50_i, p.deVAB_i
FROM PhotoObjAll AS p JOIN specObjAll s ON s.bestobjid = p.objid
WHERE p.mode = 1 AND s.sciencePrimary = 1 AND p.clean = 1 AND p.type = 3
"""
SDSSgals = SDSS.query_sql(GALquery)
SDSSgals
I have used my own domain knowledge to specificly choose features that may be useful when clustering galaxies. If you know a bit about SDSS and can think of other features that may be useful feel free to add them to the query.
One nice feature of astropy
tables is that they can readily be turned into pandas DataFrames
, which can in turn easily be turned into a sklearn X
array with NumPy
. For example:
X = np.array(SDSSgals.to_pandas())
And you are ready to go.
Challenge Problem Using the SDSS dataset above, identify interesting clusters within the data [this is intentionally very open ended, if you uncover anything especially exciting you'll have a chance to share it with the group]. Feel free to use the algorithms discussed above, or any other packages available via sklearn
. Can you make sense of the clusters in the context of galaxy evolution?
Hint - don't fret if you know nothing about galaxy evolution (neither do I!). Just take a critical look at the clusters that are identified
In [ ]:
# complete
Note - the above solution seems to separate out elliptical galaxies from blue star forming galaxies, however, the results are highly, highly dependent upon the tuning parameters.
Supervised machine learning, on the other hand, aims to predict a target class or produce a regression result based on the location of labelled sources (i.e. the training set) in the multidimensional feature space. The "supervised" comes from the fact that we are specifying the allowed outputs from the model. As there are labels available for the training set, it is possible to estimate the accuracy of the model (though there are generally important caveats about generalization, which we will explore in further detail later).
The details and machinations of supervised learning will be explored further during the following break-out session. Here, we will simply introduce some of the basics as a point of comparison to unsupervised machine learning.
We will begin with a simple, but nevertheless, elegant algorithm for classification and regression: $k$-nearest-neighbors ($k$NN). In brief, the classification or regression output is determined by examining the $k$ nearest neighbors in the training set, where $k$ is a user defined number. Typically, though not always, distances between sources are Euclidean, and the final classification is assigned to whichever class has a plurality within the $k$ nearest neighbors (in the case of regression, the average of the $k$ neighbors is the output from the model). We will experiment with the steps necessary to optimize $k$, and other tuning parameters, in the detailed break-out problem.
In scikit-learn
the KNeighborsClassifer
algorithm is implemented as part of the sklearn.neighbors
module.
Problem 3a Fit two different $k$NN models to the iris data, one with 3 neighbors and one with 10 neighbors. Plot the resulting class predictions in the sepal length-sepal width plane (same plot as above). How do the results compare to the true classifications? Is there any reason to be suspect of this procedure?
Hint - after you have constructed the model, it is possible to obtain model predictions using the .predict()
method, which requires a feature array, same features and order as the training set, as input.
Hint that isn't essential, but is worth thinking about - should the features be re-scaled in any way?
In [ ]:
from sklearn.neighbors import KNeighborsClassifier
KNNclf = KNeighborsClassifier( # complete
preds = # complete
plt.figure()
plt.scatter( # complete
KNNclf = KNeighborsClassifier(# complete
preds = # complete
plt.figure()
plt.scatter( # complete
These results are almost identical to the training classifications. However, we have cheated! In this case we are evaluating the accuracy of the model (98% in this case) using the same data that defines the model. Thus, what we have really evaluated here is the training error. The relevant parameter, however, is the generalization error: how accurate are the model predictions on new data?
Without going into too much detail, we will test this using cross validation (CV), which will be explored in more detail later. In brief, CV provides predictions on the training set using a subset of the data to generate a model that predicts the class of the remaining sources. Using cross_val_predict
, we can get a better sense of the model accuracy. Predictions from cross_val_predict
are produced in the following manner:
from sklearn.cross_validation import cross_val_predict
CVpreds = cross_val_predict(sklearn.model(), X, y)
where sklearn.model()
is the desired model, X
is the feature array, and y
is the label array.
Problem 3b Produce cross-validation predictions for the iris dataset and a $k$NN with 5 neighbors. Plot the resulting classifications, as above, and estimate the accuracy of the model as applied to new data. How does this accuracy compare to a $k$NN with 50 neighbors?
In [ ]:
from sklearn.cross_validation import cross_val_predict
CVpreds = cross_val_predict( # complete
plt.figure()
plt.scatter( # complete
print("The accuracy of the kNN = 5 model is ~{:.4}".format( # complete
CVpreds50 = cross_val_predict( # complete
print("The accuracy of the kNN = 50 model is ~{:.4}".format( # complete
While it is useful to understand the overall accuracy of the model, it is even more useful to understand the nature of the misclassifications that occur.
Problem 3c Calculate the accuracy for each class in the iris set, as determined via CV for the $k$NN = 50 model.
In [ ]:
# complete
We just found that the classifier does a much better job classifying setosa and versicolor than it does for virginica. The main reason for this is some viginica flowers lie far outside the main virginica locus, and within predominantly versicolor "neighborhoods". In addition to knowing the accuracy for the individual classes, it is also useful to know class predictions for the misclassified sources, or in other words where there is "confusion" for the classifier. The best way to summarize this information is with a confusion matrix. In a confusion matrix, one axis shows the true class and the other shows the predicted class. For a perfect classifier all of the power will be along the diagonal, while confusion is represented by off-diagonal signal.
Like almost everything else we have encountered during this exercise, scikit-learn
makes it easy to compute a confusion matrix. This can be accomplished with the following:
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_prep)
Problem 3d Calculate the confusion matrix for the iris training set and the $k$NN = 50 model.
In [ ]:
from sklearn.metrics import confusion_matrix
cm = confusion_matrix( # complete
print(cm)
From this representation, we see right away that most of the virginica that are being misclassifed are being scattered into the versicolor class. However, this representation could still be improved: it'd be helpful to normalize each value relative to the total number of sources in each class, and better still, it'd be good to have a visual representation of the confusion matrix. This visual representation will be readily digestible. Now let's normalize the confusion matrix.
Problem 3e Calculate the normalized confusion matrix. Be careful, you have to sum along one axis, and then divide along the other.
Anti-hint: This operation is actually straightforward using some array manipulation that we have not covered up to this point. Thus, we have performed the necessary operations for you below. If you have extra time, you should try to develop an alternate way to arrive at the same normalization.
In [ ]:
normalized_cm = cm.astype('float')/cm.sum(axis = 1)[:,np.newaxis]
normalized_cm
The normalization makes it easier to compare the classes, since each class has a different number of sources. Now we can procede with a visual representation of the confusion matrix. This is best done using imshow()
within pyplot. You will also need to plot a colorbar, and labeling the axes will also be helpful.
Problem C3 Plot the confusion matrix. Be sure to label each of the axeses.
Hint - you might find the sklearn
confusion matrix tutorial helpful for making a nice plot.
In [ ]:
plt.imshow( # complete
Now it is straight-forward to see that virginica and versicolor flowers are the most likely to be confused, which we could intuit from the very first plot in this notebook, but this exercise becomes far more important for large data sets with many, many classes.
Thus concludes our introduction to scikit-learn
and supervised and unsupervised learning.
In [ ]: