This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
code:ridge_regression [2013/09/18 14:38] asa created |
code:ridge_regression [2016/08/09 10:25] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | === Ridge Regression === | + | ===== Ridge Regression ===== |
+ | |||
+ | Here's code for ridge regression: | ||
<file python ridge_regression.py> | <file python ridge_regression.py> | ||
Line 81: | Line 83: | ||
</file> | </file> | ||
+ | |||
+ | Now let's play with the code. | ||
+ | |||
+ | <code python> | ||
+ | from PyML import * | ||
+ | import ridge_regression | ||
+ | rr = ridge_regression.RidgeRegression(regression=True) | ||
+ | </code> | ||
+ | We are going to use ridge regression for regression, so we have to set the regression flag to True. | ||
+ | |||
+ | Next we will read in some data taken from the UCI machine learning repository. The task is to predict | ||
+ | where in the body a CT scan is obtained from. Here's a [[http://archive.ics.uci.edu/ml/datasets/Relative+location+of+CT+slices+on+axial+axis | link to the data]]. | ||
+ | |||
+ | <code python> | ||
+ | data = vectorDatasets.PyVectorDataSet('../data/slice_localization_data.csv', labelsColumn = -1, numericLabels=True) | ||
+ | </code> | ||
+ | Note that we had to tell PyML to interpret the labels as numeric. | ||
+ | |||
+ | Evaluating the classifier: | ||
+ | <code python> | ||
+ | results = rr.cv(data) | ||
+ | </code> | ||
+ | |||
+ | A couple of other things to do with the data: | ||
+ | |||
+ | <code python> | ||
+ | # how are the labels distributed? | ||
+ | from matplotlib import pyplot as plt | ||
+ | plt.hist(data.labels.Y, 50) | ||
+ | |||
+ | # Looking at the weight vectors | ||
+ | |||
+ | rr.train(data) | ||
+ | plt.hist(rr.w, 25) | ||
+ | </code> | ||
+ | |||
+ | Using ridge regression as a classifier: | ||
+ | <code python> | ||
+ | from PyML import * | ||
+ | data = vectorDatasets.PyVectorDataSet('../data/gisette_sample.data', labelsColumn = 0) | ||
+ | import ridge_regression | ||
+ | rr = ridge_regression.RidgeRegression() | ||
+ | rr.train(data) | ||
+ | |||
+ | from matplotlib import pyplot as plt | ||
+ | plt.hist(rr.w, 100) | ||
+ | |||
+ | import perceptron | ||
+ | p = perceptron.Perceptron() | ||
+ | p.train(data) | ||
+ | |||
+ | plt.hist(p.w, 100) | ||
+ | |||
+ | # compare accuracy of ridge regression and the perceptron | ||
+ | |||
+ | perceptron_results = p.stratifiedCV(data) | ||
+ | ridge_results = rr.stratifiedCV(data) | ||
+ | |||
+ | </code> | ||
+ |