markdown stringlengths 0 37k | code stringlengths 1 33.3k | path stringlengths 8 215 | repo_name stringlengths 6 77 | license stringclasses 15
values |
|---|---|---|---|---|
This displays the call that produced the object fit and a three-column matrix with columns Df (the number of nonzero coefficients), %dev (the percent deviance explained) and Lambda (the corresponding value of $\lambda$).
(Note that the digits option can used to specify significant digits in the printout.)
Here the actu... | glmnetPlot(fit, xvar = 'lambda', label = True); | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
Now when we plot against %deviance we get a very different picture. This is percent deviance explained on the training data. What we see here is that toward the end of the path this value are not changing much, but the coefficients are "blowing up" a bit. This lets us focus attention on the parts of the fit that matter... | glmnetPlot(fit, xvar = 'dev', label = True); | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
We can extract the coefficients and make predictions at certain values of $\lambda$. Two commonly used options are:
s specifies the value(s) of $\lambda$ at which extraction is made.
exact indicates whether the exact values of coefficients are desired or not. That is, if exact = TRUE, and predictions are to be made... | any(fit['lambdau'] == 0.5)
glmnetCoef(fit, s = scipy.float64([0.5]), exact = False) | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
The output is for False.(TBD) The exact = 'True' option is not yet implemented.
Users can make predictions from the fitted object. In addition to the options in coef, the primary argument is newx, a matrix of new values for x. The type option allows users to choose the type of prediction:
* "link" gives the fitted va... | fc = glmnetPredict(fit, x[0:5,:], ptype = 'response', \
s = scipy.float64([0.05]))
print(fc) | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
gives the fitted values for the first 5 observations at $\lambda = 0.05$. If multiple values of s are supplied, a matrix of predictions is produced.
Users can customize K-fold cross-validation. In addition to all the glmnet parameters, cvglmnet has its special parameters including nfolds (the number of folds), foldid (... | warnings.filterwarnings('ignore')
cvfit = cvglmnet(x = x.copy(), y = y.copy(), ptype = 'mse', nfolds = 20)
warnings.filterwarnings('default') | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
does 20-fold cross-validation, based on mean squared error criterion (default though).
Parallel computing is also supported by cvglmnet. Parallel processing is turned off by default. It can be turned on using parallel=True in the cvglmnet call.
Parallel computing can significantly speed up the computation process, esp... | cvfit['lambda_min']
cvglmnetCoef(cvfit, s = 'lambda_min')
cvglmnetPredict(cvfit, newx = x[0:5,], s='lambda_min') | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
Users can control the folds used. Here we use the same folds so we can also select a value for $\alpha$. | foldid = scipy.random.choice(10, size = y.shape[0], replace = True)
cv1=cvglmnet(x = x.copy(),y = y.copy(),foldid=foldid,alpha=1)
cv0p5=cvglmnet(x = x.copy(),y = y.copy(),foldid=foldid,alpha=0.5)
cv0=cvglmnet(x = x.copy(),y = y.copy(),foldid=foldid,alpha=0) | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
There are no built-in plot functions to put them all on the same plot, so we are on our own here: | f = plt.figure()
f.add_subplot(2,2,1)
cvglmnetPlot(cv1)
f.add_subplot(2,2,2)
cvglmnetPlot(cv0p5)
f.add_subplot(2,2,3)
cvglmnetPlot(cv0)
f.add_subplot(2,2,4)
plt.plot( scipy.log(cv1['lambdau']), cv1['cvm'], 'r.')
plt.hold(True)
plt.plot( scipy.log(cv0p5['lambdau']), cv0p5['cvm'], 'g.')
plt.plot( scipy.log(cv0['lambdau']... | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
We see that lasso (alpha=1) does about the best here. We also see that the range of lambdas used differs with alpha.
Coefficient upper and lower bounds
These are recently added features that enhance the scope of the models. Suppose we want to fit our model, but limit the coefficients to be bigger than -0.7 and less tha... | cl = scipy.array([[-0.7], [0.5]], dtype = scipy.float64)
tfit=glmnet(x = x.copy(),y= y.copy(), cl = cl)
glmnetPlot(tfit); | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
These are rather arbitrary limits; often we want the coefficients to be positive, so we can set only lower.limit to be 0.
(Note, the lower limit must be no bigger than zero, and the upper limit no smaller than zero.)
These bounds can be a vector, with different values for each coefficient. If given as a scalar, the sam... | pfac = scipy.ones([1, 20])
pfac[0, 4] = 0; pfac[0, 9] = 0; pfac[0, 14] = 0
pfit = glmnet(x = x.copy(), y = y.copy(), penalty_factor = pfac)
glmnetPlot(pfit, label = True); | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
We see from the labels that the three variables with 0 penalty factors always stay in the model, while the others follow typical regularization paths and shrunken to 0 eventually.
Some other useful arguments. exclude allows one to block certain variables from being the model at all. Of course, one could simply subset t... | scipy.random.seed(101)
x = scipy.random.rand(100,10)
y = scipy.random.rand(100,1)
fit = glmnet(x = x, y = y)
glmnetPlot(fit); | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
We wish to label the curves with the variable names. Here's a simple way to do this, using the matplotlib library in python (and a little research into how to customize it). We need to have the positions of the coefficients at the end of the path. | %%capture
# Output from this sample code has been suppressed due to (possible) Jupyter limitations
# The code works just fine from ipython (tested on spyder)
c = glmnetCoef(fit)
c = c[1:, -1] # remove intercept and get the coefficients at the end of the path
h = glmnetPlot(fit)
ax1 = h['ax1']
xloc = plt.xlim()
xloc = ... | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
We have done nothing here to avoid overwriting of labels, in the event that they are close together. This would be a bit more work, but perhaps best left alone, anyway.
Linear Regression - Multiresponse Gaussian Family
The multiresponse Gaussian family is obtained using family = "mgaussian" option in glmnet. It is very... | # Import relevant modules and setup for calling glmnet
%reset -f
%matplotlib inline
import sys
sys.path.append('../test')
sys.path.append('../lib')
import scipy, importlib, pprint, matplotlib.pyplot as plt, warnings
from glmnet import glmnet; from glmnetPlot import glmnetPlot
from glmnetPrint import glmnetPrint; from... | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
We fit the data, with an object "mfit" returned. | mfit = glmnet(x = x.copy(), y = y.copy(), family = 'mgaussian') | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
For multiresponse Gaussian, the options in glmnet are almost the same as the single-response case, such as alpha, weights, nlambda, standardize. A exception to be noticed is that standardize.response is only for mgaussian family. The default value is FALSE. If standardize.response = TRUE, it standardizes the response v... | glmnetPlot(mfit, xvar = 'lambda', label = True, ptype = '2norm'); | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
Note that we set type.coef = "2norm". Under this setting, a single curve is plotted per variable, with value equal to the $\ell_2$ norm. The default setting is type.coef = "coef", where a coefficient plot is created for each response (multiple figures).
xvar and label are two other options besides ordinary graphical pa... | f = glmnetPredict(mfit, x[0:5,:], s = scipy.float64([0.1, 0.01]))
print(f[:,:,0], '\n')
print(f[:,:,1]) | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
The prediction result is saved in a three-dimensional array with the first two dimensions being the prediction matrix for each response variable and the third indicating the response variables.
We can also do k-fold cross-validation. The options are almost the same as the ordinary Gaussian family and we do not expand h... | warnings.filterwarnings('ignore')
cvmfit = cvglmnet(x = x.copy(), y = y.copy(), family = "mgaussian")
warnings.filterwarnings('default') | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
We plot the resulting cv.glmnet object "cvmfit". | cvglmnetPlot(cvmfit) | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
To show explicitly the selected optimal values of $\lambda$, type | cvmfit['lambda_min']
cvmfit['lambda_1se'] | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
As before, the first one is the value at which the minimal mean squared error is achieved and the second is for the most regularized model whose mean squared error is within one standard error of the minimal.
Prediction for cvglmnet object works almost the same as for glmnet object. We omit the details here.
Logistic R... | # Import relevant modules and setup for calling glmnet
%reset -f
%matplotlib inline
import sys
sys.path.append('../test')
sys.path.append('../lib')
import scipy, importlib, pprint, matplotlib.pyplot as plt, warnings
from glmnet import glmnet; from glmnetPlot import glmnetPlot
from glmnetPrint import glmnetPrint; from... | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
The input matrix $x$ is the same as other families. For binomial logistic regression, the response variable $y$ should be either a factor with two levels, or a two-column matrix of counts or proportions.
Other optional arguments of glmnet for binomial regression are almost same as those for Gaussian family. Don't forge... | fit = glmnet(x = x.copy(), y = y.copy(), family = 'binomial') | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
Like before, we can print and plot the fitted object, extract the coefficients at specific $\lambda$'s and also make predictions. For plotting, the optional arguments such as xvar and label are similar to the Gaussian. We plot against the deviance explained and show the labels. | glmnetPlot(fit, xvar = 'dev', label = True); | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
Prediction is a little different for logistic from Gaussian, mainly in the option type. "link" and "response" are never equivalent and "class" is only available for logistic regression. In summary,
* "link" gives the linear predictors
"response" gives the fitted probabilities
"class" produces the class label corres... | glmnetPredict(fit, newx = x[0:5,], ptype='class', s = scipy.array([0.05, 0.01])) | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
For logistic regression, cvglmnet has similar arguments and usage as Gaussian. nfolds, weights, lambda, parallel are all available to users. There are some differences in ptype: "deviance" and "mse" do not both mean squared loss and "class" is enabled. Hence,
* "mse" uses squared loss.
"deviance" uses actual deviance... | warnings.filterwarnings('ignore')
cvfit = cvglmnet(x = x.copy(), y = y.copy(), family = 'binomial', ptype = 'class')
warnings.filterwarnings('default') | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
It uses misclassification error as the criterion for 10-fold cross-validation.
We plot the object and show the optimal values of $\lambda$. | cvglmnetPlot(cvfit)
cvfit['lambda_min']
cvfit['lambda_1se'] | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
coef and predict are simliar to the Gaussian case and we omit the details. We review by some examples. | cvglmnetCoef(cvfit, s = 'lambda_min') | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
As mentioned previously, the results returned here are only for the second level of the factor response. | cvglmnetPredict(cvfit, newx = x[0:10, ], s = 'lambda_min', ptype = 'class') | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
Like other GLMs, glmnet allows for an "offset". This is a fixed vector of N numbers that is added into the linear predictor.
For example, you may have fitted some other logistic regression using other variables (and data), and now you want to see if the present variables can add anything. So you use the predicted logit... | # Import relevant modules and setup for calling glmnet
%reset -f
%matplotlib inline
import sys
sys.path.append('../test')
sys.path.append('../lib')
import scipy, importlib, pprint, matplotlib.pyplot as plt, warnings
from glmnet import glmnet; from glmnetPlot import glmnetPlot
from glmnetPrint import glmnetPrint; from... | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
The optional arguments in glmnet for multinomial logistic regression are mostly similar to binomial regression except for a few cases.
The response variable can be a nc >= 2 level factor, or a nc-column matrix of counts or proportions.
Internally glmnet will make the rows of this matrix sum to 1, and absorb the tota... | fit = glmnet(x = x.copy(), y = y.copy(), family = 'multinomial', mtype = 'grouped') | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
We plot the resulting object "fit". | glmnetPlot(fit, xvar = 'lambda', label = True, ptype = '2norm'); | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
The options are xvar, label and ptype, in addition to other ordinary graphical parameters.
xvar and label are the same as other families while ptype is only for multinomial regression and multiresponse Gaussian model. It can produce a figure of coefficients for each response variable if ptype = "coef" or a figure showi... | warnings.filterwarnings('ignore')
cvfit=cvglmnet(x = x.copy(), y = y.copy(), family='multinomial', mtype = 'grouped');
warnings.filterwarnings('default')
cvglmnetPlot(cvfit) | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
Note that although mtype is not a typical argument in cvglmnet, in fact any argument that can be passed to glmnet is valid in the argument list of cvglmnet. We also use parallel computing to accelerate the calculation.
Users may wish to predict at the optimally selected $\lambda$: | cvglmnetPredict(cvfit, newx = x[0:10, :], s = 'lambda_min', ptype = 'class') | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
Poisson Models
Poisson regression is used to model count data under the assumption of Poisson error, or otherwise non-negative data where the mean and variance are proportional. Like the Gaussian and binomial model, the Poisson is a member of the exponential family of distributions. We usually model its positive mean o... | # Import relevant modules and setup for calling glmnet
%reset -f
%matplotlib inline
import sys
sys.path.append('../test')
sys.path.append('../lib')
import scipy, importlib, pprint, matplotlib.pyplot as plt, warnings
from glmnet import glmnet; from glmnetPlot import glmnetPlot
from glmnetPrint import glmnetPrint; from... | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
We apply the function glmnet with the "poisson" option. | fit = glmnet(x = x.copy(), y = y.copy(), family = 'poisson') | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
The optional input arguments of glmnet for "poisson" family are similar to those for others.
offset is a useful argument particularly in Poisson models.
When dealing with rate data in Poisson models, the counts collected are often based on different exposures, such as length of time observed, area and years. A poisson... | glmnetPlot(fit); | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
Like before, we can extract the coefficients and make predictions at certain $\lambda$'s by using coef and predict respectively. The optional input arguments are similar to those for other families. In function predict, the option type, which is the type of prediction required, has its own specialties for Poisson famil... | glmnetCoef(fit, s = scipy.float64([1.0]))
glmnetPredict(fit, x[0:5,:], ptype = 'response', s = scipy.float64([0.1, 0.01])) | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
We may also use cross-validation to find the optimal $\lambda$'s and thus make inferences. | warnings.filterwarnings('ignore')
cvfit = cvglmnet(x.copy(), y.copy(), family = 'poisson')
warnings.filterwarnings('default') | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
Options are almost the same as the Gaussian family except that for type.measure,
* "deviance" (default) gives the deviance
* "mse" stands for mean squared error
* "mae" is for mean absolute error.
We can plot the cvglmnet object. | cvglmnetPlot(cvfit) | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
We can also show the optimal $\lambda$'s and the corresponding coefficients. | optlam = scipy.array([cvfit['lambda_min'], cvfit['lambda_1se']]).reshape([2,])
cvglmnetCoef(cvfit, s = optlam) | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
The predict method is similar and we do not repeat it here.
Cox Models
The Cox proportional hazards model is commonly used for the study of the relationship beteween predictor variables and survival time. In the usual survival analysis framework, we have data of the form $(y_1, x_1, \delta_1), \ldots, (y_n, x_n, \delta... | # Import relevant modules and setup for calling glmnet
%reset -f
%matplotlib inline
import sys
sys.path.append('../test')
sys.path.append('../lib')
import scipy, importlib, pprint, matplotlib.pyplot as plt, warnings
from glmnet import glmnet; from glmnetPlot import glmnetPlot
from glmnetPrint import glmnetPrint; from... | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
The Surv function in the package survival can create such a matrix. Note, however, that the coxph and related linear models can handle interval and other fors of censoring, while glmnet can only handle right censoring in its present form.
We apply the glmnet function to compute the solution path under default settings. | fit = glmnet(x = x.copy(), y = y.copy(), family = 'cox') | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
All the standard options are available such as alpha, weights, nlambda and standardize. Their usage is similar as in the Gaussian case and we omit the details here. Users can also refer to the help file help(glmnet).
We can plot the coefficients. | glmnetPlot(fit); | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
As before, we can extract the coefficients at certain values of $\lambda$. | glmnetCoef(fit, s = scipy.float64([0.05])) | docs/glmnet_vignette.ipynb | bbalasub1/glmnet_python | gpl-3.0 |
Basic Data Types
Python has many data types, e.g.
* numeric: int, float, complex
* string
* boolean values, i.e. true and false
* sequences: list, tuple
* dict
Variables are declared via assignment:
python
x = 5 | # scratch area | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Numeric Types
Python numeric types are similar to those in other languages such as C/C++.
python
x = 5 # int
x = 10**100 # long (2.7) or int (3.5)
x = 3.141592 # float
x = 1.0j # complex
Note: ordinary machine types can be accessed/manipulated through the ctypes module. | # scratch area | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Arithmetic Operations
python
3 + 2 # addition
3 - 2 # subtraction
3 * 2 # multiplication
3 ** 2 # exponentiation
3 / 2 # division (warning: int (2.7) or float (3.5))
3 % 2 # modulus | # scratch area | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Exercise
Use the Python interpreter to perform some basic arithemetic.
Strings
python
x = "hello" # string enclosed with double quotes
y = 'world' # string enclosed with single quotes
x + ' ' + y # string concatenation via +
"{} + {} = {}".format(5 , 6, 5+6) # string formatting | # scratch area | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Lists
python
x = [1, 2, 3] # initialize list
x[1] = 0 # modify element
x.append(4) # append to end
x.extend([5, 6]) # extend
x[3:5] # slice | # scratch area | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Tuples
Tuples are similar to lists, but are immutable:
python
x = (1, 2, 3) # initialize a tuple with ()
x[0] = 4 # will result in error | # scratch area | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
List Comprehension
Comprehension provides a convenient way to create new lists:
python
[ i for i in range (5) ] # result: [0, 1, 2, 3, 4]
[ i**2 for i in range (5) ] # result: [0, 1, 4, 9, 16]
the_list = [5, 2, 6, 1]
[ i**2 for i in the_list ] # result [25, 4, 36, 1] | # scratch area | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Exercise
Create a list of floating point numbers and then create a second list which contains the squares of the entries of teh fist list
Boolean Values and Comparisons
Boolean types take the values True or False. The result of a comparison operator is boolean.
python
5 < 6 # evalutes to True
5 >= 6 # evalua... | # scratch area | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Functions
Functions are defined with def:
python
def hello():
print 'hello, world'
Note: Python uses indentation to denote blocks of code, rather than braces {} as in many other languages. It is common to use either 4 spaces or 2 spaces to indent. It doesn't matter, as long as you are consistent.
Use the return key... | # scratch area | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Loops and Flow Control
For loop: | for i in range(10):
print(i**2) | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
It is also possible to use for..in to iterate through elements of a list: | for i in ['hello', 'world']:
print(i) | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
While loops have the form while condition: | i = 0
while i < 10:
print(i**2)
i = i + 1 | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
The keywords break and continue can be used for flow control inside a loop
* continue: skip to the next iteration of the loop
* break: jump out of the loop entirely | for i in range(10):
if i == 3:
continue
if i == 7:
break
print(i) | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Use the keywords if, elif, else for branching
python
if 5 > 6:
# never reached
pass
elif 1 > 2:
# reached
pass
else:
# never reached
pass | # scratch area | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Exercise
Write a function fib(n) which returns the nth Fibonacci number. The Fibonacci numbers are defined by
* fib(0) = fib(1) = 1
* fib(n) = fib(n-1) + fib(n-2) for n >= 2.
Exercise
”Write a program that prints the numbers from 1 to 100. But for
multiples of three print Fizz instead of the number and for the
mul... | # scratch area | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Modules
Load external modules (built-in or user-defined) via import: | import math
print(math.pi)
print(math.sin(math.pi/2.0)) | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Rename modules with as: | import math as m
print(m.pi) | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Load specific functions or submodules: | from math import pi, sin
print(sin(pi/2.0))
# scratch area | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
User-defined Modules
Any code written in a separate file (with .py extension) can be imported as a module. Suppose we have a script my_module.py which defines a function do_something(). Then we can call it as | import my_module
my_module.do_something() | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Exercise
Implement your FizzBuzz solution as a function called FizzBuzz() in a module called fizzbuzz. Check that it works by importing it and calling FizzBuzz() in a separate script. | # scratch area | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
numpy
numpy is a module used for numerical calculation. The main data type is numpy.array, which is a multidimensional array of numbers (integer, float, complex). | import numpy as np
x = np.array([1, 2, 3, 4])
print(x.sum())
print(x.mean()) | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
The basic arithmetic operations work elementwise on numpy arrays: | x = np.array([1, 2, 3, 4])
y = np.array([5, 6, 7, 8])
print(x + y)
print(x * y)
print(x / y) | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
It is also possible to call functions on numpy arrays: | x = np.array([1, 2, 3, 4])
print(np.sin(x))
print(np.log(x))
# scratch area | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Generating numpy Arrays
numpy arrays can be generated with zeros, ones, linspace, and rand: | print(np.zeros(4))
print(np.ones(3))
print(np.linspace(-1, 1, num=4))
print(np.random.rand(2))
# scratch area | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Plotting with matplotlib
We use matplotlib.pyplot for plotting: | import numpy as np
from matplotlib import pyplot as plt
x = np.linspace(-3.14, 3.14, num=100)
y = np.sin(x)
plt.plot(x, y)
plt.xlabel('x values')
plt.ylabel('y')
plt.title('y=sin(x)')
plt.show() | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Exercise
Create plots the following functions
* f(x) = log(x)
* f(x) = sqrt(x)
* f(x) = x**2
* f(x) = log(1 + x**2)
* anything else you might find interesting or challenging
Combining Plots
Plots can be combined using addition: | x = np.linspace(-10, 10, num=100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.arctan(x)
plt.plot(x, y1, x, y2, x, y3)
plt.show() | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
todo
array manipulation routines
numpy.flipud, fliplr, transpose, rot90, flatten, ravel
Colormap Plots
Plot color maps with pcolormesh: | x = np.linspace (-1, 1, num =100)
y = np.linspace (-1, 1, num =100)
xx, yy = np.meshgrid (x, y)
z = np.sin(xx**2 + yy**2 + yy)
plt.pcolormesh(x, y, z, shading = 'gouraud')
plt.show() | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Or with imshow: | plt.imshow(z, aspect='auto')
plt.show() | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Note that the image is flipped because images start from top left and go to bottom right. We can fix this with flipud: | plt.imshow(np.flipud(z), aspect='auto')
plt.show()
# scratch area | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
3D Plots | from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
%matplotlib inline
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(xx, yy, z, rstride=5, cstride=5, cmap=cm.coolwarm, linewidth=1, antialiased=True)
plt.show() | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
3D Wireframe Plot | %matplotlib inline
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_wireframe(xx, yy, z, rstride=5, cstride=5, antialiased=True)
plt.show() | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Gallery of matplotlib Plots
See http://matplotlib.org/gallery.html
Plotting Exercise
Consider the function f(x, y) = exp(x + 1.0j*y) for −4 ≤ x, y ≤ 4. Create colormap and 3d plots of the magnitude, real,
and imaginary parts of f. | # scratch | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Plotting Images | x = np.linspace(-2, 2, num=100)
y = np.linspace(-2, 2, num=100)
result = np.flipud(np.array([[u*v for u in x] for v in y]))
fig = plt.figure()
plt.imshow(result, extent=[x.min(), x.max(), y.min(), y.max()], aspect='auto')
plt.show() | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Classes
Classes can be used to package data and methods together: | class SomeClass:
def __init__ (self, x):
self.x = x
def doSomething(self):
print("my x value is {}".format(self.x))
obj = SomeClass(5)
obj.doSomething()
# scratch area | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Inheritance
Classes can be derived from others: | class SomeOtherClass (SomeClass):
def __init__ (self, x, y):
SomeClass.__init__ (self, x)
self.y = y
def doSomethingElse(self):
print("my y value is {}".format(self.y))
other_obj = SomeOtherClass(5, 6)
other_obj.doSomething()
other_obj.doSomethingElse() | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Polymorphism
An instance of a derived class is automatically an instance of its base class: | print('The type of obj is {}'.format(type(obj)))
print('The type of other_obj is {}'.format(type(other_obj)))
print('obj is instance of SomeClass? {}'.format(isinstance(obj, SomeClass)))
print('obj is instance of SomeOtherClass? {}'.format(isinstance(obj, SomeOtherClass)))
print('other_obj is instance of SomeClass? {... | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Exercise
todo | # todo | old/python/tutorial.ipynb | waltervh/BornAgain-tutorial | gpl-3.0 |
Detectors
Note: LRISb has employed different detectors. We may need to
make PYPIT backwards compatible.
FITS file | fil = '/Users/xavier/PYPIT/LRIS_blue/Raw/b150910_2033.fits.gz'
hdu = fits.open(fil)
hdu.info()
head0['OBSTYPE']
head0 = hdu[0].header
head0
#head0['DATE']
plt.clf()
plt.imshow(hdu[1].data)
plt.show() | doc/nb/LRIS_blue_notes.ipynb | PYPIT/PYPIT | gpl-3.0 |
Display Raw LRIS image in Ginga | ### Need to port readmhdufits
head0
reload(pyp_ario)
img, head = pyp_ario.read_lris('/Users/xavier/PYPIT/LRIS_blue/Raw/b150910_2070.fits',TRIM=True)
xdb.ximshow(img)
import subprocess
subprocess.call(["touch", "dum.fil"])
b = 'as'
'{1:s}'.format(b)
range(1,5)
tmp = np.ones((10,20))
tmp[0:1,:].shape | doc/nb/LRIS_blue_notes.ipynb | PYPIT/PYPIT | gpl-3.0 |
Floating point numbers
Floating point numbers, or decimal numbers are just that: any number with a decimal place in it such as 4.566642 and -156.986714. Pandas stores these as a float64. They could also be stored in scientific notation like this: 4.509013e+14. This means "4.509013 times 10 raised to the +14". These are... | print("Float Values")
print(sampledata['FloatCol'].values) | Class03/Class03.ipynb | madsenmj/ml-introduction-course | apache-2.0 |
Before we move on, I'd like to take a quick look at the data graphically. | sampledata.plot(kind='scatter', x='IntCol',y='FloatCol') | Class03/Class03.ipynb | madsenmj/ml-introduction-course | apache-2.0 |
Because this is "fake" data, I put in a functional dependence here. The float column looks like it is some function of the integer column. It is almost always a good idea to visualize your data early on to see what it looks like graphically!
Text
Pandas can store text in its columns. Because there are a number of diffe... | print("Text Values")
print(sampledata['TextCol'].values) | Class03/Class03.ipynb | madsenmj/ml-introduction-course | apache-2.0 |
Categorical
A categorical data type is a finite set of different objects. These objects are represented internally as integers but may be displayed as text or other generic objects. To make things simple, we'll start with a categorical object that has three possible values: "yes", "no", and "maybe". Internally, pandas ... | print("Categorical Values")
print(sampledata['CatCol'].values) | Class03/Class03.ipynb | madsenmj/ml-introduction-course | apache-2.0 |
When we loaded the data, it actually loaded this column as an object, which means it doesn't know that it is supposed to be a categorical column. We will tell pandas to do that. We will use the astype() command that will tell pandas to change the data type of that column. We check to make sure it worked, too. Note that... | sampledata["CatCol2"] = sampledata["CatCol"].astype('category')
sampledata.dtypes | Class03/Class03.ipynb | madsenmj/ml-introduction-course | apache-2.0 |
We can now look at how the data are stored as categorical data. We can get thi internal codes for each of the entries like this: | sampledata["CatCol2"].cat.codes | Class03/Class03.ipynb | madsenmj/ml-introduction-course | apache-2.0 |
We can also get a list of the categories that pandas found when converting the column. These are in order- the first entry corresponds to 0, the second to 1, etc. | sampledata["CatCol2"].cat.categories | Class03/Class03.ipynb | madsenmj/ml-introduction-course | apache-2.0 |
We may encounter situations where we want to plot the data and visualize each category as its own color. We saw how to do this back in Class01. | import seaborn as sns
sns.set_style('white')
sns.lmplot(x='IntCol', y='FloatCol', data=sampledata, hue='CatCol2', fit_reg=False) | Class03/Class03.ipynb | madsenmj/ml-introduction-course | apache-2.0 |
Date/Times
We will frequently encounter date/time values in working with data. There are many different ways that these values get stored, but mostly we'll find that they start as a text object. We need to know how they are stored (in what order are the year-month-day-hour-minute-second values are stored). There are ut... | print("Date/Time Values")
print(sampledata['DateCol'].values) | Class03/Class03.ipynb | madsenmj/ml-introduction-course | apache-2.0 |
They are currently stored as objects, not as datetimes. We need to convert this column as well, but we'll use a special pandas function to do that. Take a quick look at the reference page for this function to see what else it can do. Note that the new column has type datetime64[ns]. That means that the date format is c... | sampledata["DateCol2"] = pd.to_datetime(sampledata["DateCol"])
sampledata.dtypes
#We print out the column to see what it looks like
sampledata["DateCol2"] | Class03/Class03.ipynb | madsenmj/ml-introduction-course | apache-2.0 |
Now that we have the datetime column, I'd like to plot the data as a function of date. This is often a useful thing to do with time series data. We'll need to import the matplotlib library and use a trick to format the data by date. Here's the code that makes it work. | import matplotlib.pyplot as plt
%matplotlib inline
# We will plot the data values and set the linestyle to 'None' which will not plot the line. We also want to show the individual data points, so we set the marker.
plt.plot(sampledata['DateCol2'].values, sampledata['FloatCol'].values, linestyle='None', marker='o')
# au... | Class03/Class03.ipynb | madsenmj/ml-introduction-course | apache-2.0 |
Geographical
Although this is not typically a single data type, you may encounter geographical data. These are typically in a Latitude-Longitude format where both Latitude and Longitude are floating point numbers like this: (32.1545, -138.5532). There are a number of tools we can use to work with and plot this type of ... | print("Latitude Values")
print(sampledata['LatCol'].values)
print("Longitude Values")
print(sampledata['LonCol'].values) | Class03/Class03.ipynb | madsenmj/ml-introduction-course | apache-2.0 |
It is also useful to plot the geographical data. There are python libraries that make this easy to do. | from mpl_toolkits.basemap import Basemap
import numpy as np
# Draw the base map of the world
m = Basemap(projection='robin',lon_0=0,resolution='c')
# Draw the continent coast lines
m.drawcoastlines()
# Color in the water and the land masses
m.fillcontinents(color='red',lake_color='aqua')
# draw parallels and meridians... | Class03/Class03.ipynb | madsenmj/ml-introduction-course | apache-2.0 |
Column-wise processing
Now that we have data columns, we've already seen a couple of examples of column-wise processing. When we created the categorical column and the datetime column we took the data from one column and operated on it all at the same time creating the new columns with the different data types. There a... | sampledata['GTfour'] = sampledata['FloatCol'].apply(lambda x: x > 4.0)
print(sampledata[['FloatCol','GTfour']]) | Class03/Class03.ipynb | madsenmj/ml-introduction-course | apache-2.0 |
Common functions
There are a number of common functions that we could use inside the apply. For example, if we wanted to get the square root of each entry, this is what it would look like. We are using the function np.sqrt from the numpy library. We already imported this library, but if we didn't, we'd need to import n... | sampledata['FloatSQRT'] = sampledata['FloatCol'].apply(np.sqrt)
print(sampledata[['FloatCol','FloatSQRT']]) | Class03/Class03.ipynb | madsenmj/ml-introduction-course | apache-2.0 |
Another useful function is adding up columns. Note that we need to tell pandas to run through each row by adding the argument axis=1 to the apply function. Otherwise it tries to add up each column. This might be something you might want to do, too, though the easiest way to do that is to use the pandas sum function for... | sampledata['IntSUM'] = sampledata[['IntCol','FloatCol']].apply(np.sum,axis=1)
print(sampledata[['IntCol','FloatCol','IntSUM']])
sampledata['IntCol'].sum() | Class03/Class03.ipynb | madsenmj/ml-introduction-course | apache-2.0 |
Custom functions
We will now create our first custom function and use it to process the data. We will make a short function that will look to see if a value in the TextCol feature matches an item on a list we create. |
# We first tell the computer that we are writing a function by starting with "def"
# The next text is the name of the function. We name this one "isMammal" meaning it will tell us if an animal is in our list of mammals
# The final text in the parenthesis is an input to the function. This is another "dummy" variable - ... | Class03/Class03.ipynb | madsenmj/ml-introduction-course | apache-2.0 |
Feature extraction
We can often pull additional features from what we currently have. This involves doing a column-wise processing step, but with the additional component of doing a transformation or extraction from the data. We'll look at a couple of techniques to do this.
Date/day/week features
We already saw how to ... | # Get the day of the week for each of the data features. We can get either a numerical value (0-6) or the names
sampledata['DayofWeek'] = sampledata['DateCol2'].apply(lambda x: x.weekday_name)
# Or the week number in the year
sampledata['WeekofYear'] = sampledata['DateCol2'].apply(lambda x: x.week)
print(sampledata[['... | Class03/Class03.ipynb | madsenmj/ml-introduction-course | apache-2.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.