Posts Tagged ‘python’

Articles

Install python stack on Ubuntu 16.04

In howto on May 22, 2017 by theoryl Tagged:

Note to self:

# First, install pip
sudo apt install python-pip

# Upgrade pip
sudo -H pip install --upgrade pip

# Install python stack using pip
sudo -H pip install setuptools
sudo -H pip install ipython
sudo -H pip install numpy scipy
sudo -H pip install matplotlib pandas
sudo -H pip install Theano
sudo -H pip install keras
Advertisements

Articles

Install IPython on Ubuntu

In howto on Aug 3, 2016 by theoryl Tagged: , , , ,

IPython is a much more powerful shell than the usual Python shell. As Ubuntu and Linux Mint are already shipped with python and pip, installing IPython is trivial:

sudo pip install ipython

Voilà!

Articles

Solve weighted least squares with NumPy

In python on Aug 3, 2016 by theoryl Tagged: , , , ,

As posted on StackOverflow: http://stackoverflow.com/questions/27128688/how-to-use-least-squares-with-weight-matrix-in-python

import numpy as np

# Solve for X in AX = B
A = np.array([[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,0,0]])
B = np.array([1,1,1,1,1])
W = np.array([1,2,3,4,5])
Aw = A * np.sqrt(W[:,np.newaxis])
Bw = B * np.sqrt(W)
X = np.linalg.lstsq(Aw, Bw)[0]

Articles

Solve system of linear equations

In python on Jul 27, 2016 by theoryl Tagged: , , ,

Solving system of linear equations m x = y with NumPy

import numpy as np
from numpy import linalg as LA

m = np.array([
[  0.68,  0.597,  -0.33,],
[-0.211,  0.823,  0.536,],
[ 0.566, -0.605, -0.444,],
])

y = np.array([
[  0.108,   -0.27,   0.832,],
[-0.0452,  0.0268,   0.271,],
[  0.258,   0.904,   0.435,],
])

# Using QR decomposition
# http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.qr.html
q, r = LA.qr(m)
soln = np.dot(LA.inv(r), np.dot(q.T, y))

# Using least squares
# http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.solve.html
soln2 = LA.solve(m, y)

>>> print m
[[ 0.68   0.597 -0.33 ]
 [-0.211  0.823  0.536]
 [ 0.566 -0.605 -0.444]]
>>> print y
[[ 0.108  -0.27    0.832 ]
 [-0.0452  0.0268  0.271 ]
 [ 0.258   0.904   0.435 ]]
>>> print q
[[-0.74762435 -0.58417508 -0.31590713]
 [ 0.23198344 -0.67542676  0.69998741]
 [-0.62228733  0.45004241  0.64048444]]
>>> print r
[[-0.90954769  0.12107447  0.64735473]
 [-0.         -1.1769044  -0.3690698 ]
 [ 0.         -0.          0.19506751]]
>>> print soln
[[ 0.60907309  2.67396101  1.67163769]
 [-0.2309286  -1.56240961  0.07184063]
 [ 0.51001615  3.50162105  1.05334088]]
>>> print np.dot(m, soln)
[[ 0.108  -0.27    0.832 ]
 [-0.0452  0.0268  0.271 ]
 [ 0.258   0.904   0.435 ]]
>>> print soln2
[[ 0.60907309  2.67396101  1.67163769]
 [-0.2309286  -1.56240961  0.07184063]
 [ 0.51001615  3.50162105  1.05334088]]
>>> print np.dot(m, soln2)
[[ 0.108  -0.27    0.832 ]
 [-0.0452  0.0268  0.271 ]
 [ 0.258   0.904   0.435 ]]

Articles

Loop over all histograms in pyROOT

In howto on Jun 19, 2016 by theoryl Tagged: , , ,

Here’s a code snippet to loop over all histograms (and get their names) in pyROOT:

from ROOT import TFile

fname = 'histos.root'
hnames = []

tfile = TFile.Open(fname)
for key in tfile.GetListOfKeys():
    h = key.ReadObj()
    if h.ClassName() == 'TH1F' or h.ClassName() == 'TH2F':
        hnames.append(h.GetName())

Articles

Install Jupyter Notebook on Linux Mint 17.3

In howto on Dec 30, 2015 by theoryl Tagged: , , , , ,

To install Jupyter Notebook on Linux Mint 17.3, you can either use Anaconda or Pip. The instructions for both package managers can be found on http://jupyter.readthedocs.org/en/latest/install.html.

I decided to build from source. The instructions are listed on https://github.com/jupyter/notebook (look for “Ubuntu Trusty”). Basically, do:

sudo apt-get install nodejs-legacy npm javascript-common python-pip python-virtualenv python-dev
# Use a virtual environment
virtualenv jupyter-venv
source jupyter-venv/bin/activate
# ensure setuptools/pip are up-to-date
pip install --upgrade setuptools pip
git clone git://github.com/jupyter/notebook.git
cd notebook
pip install --pre -e .

jupyter --version
# Output: 4.0.6

jupyter notebook

Note: Jupyter notebook used to be called IPython notebook.

Articles

TGraph in pyROOT

In howto on Nov 22, 2015 by theoryl Tagged: , ,

pyROOT is a python binding of ROOT, which allows you to do a lot of pythonic things. The example below shows how to make a TGraph in pyROOT:

from ROOT import TGraph, gPad
from array import array
from math import exp

def func(x):
    y = 100*exp(-x)
    return y


# Method 1
if True:
    n = 100
    gr1 = TGraph(n)

    for i in range(n):
        x = 0.01*i
        gr1.SetPoint(i, x, func(x))

    gr1.SetMarkerStyle(20)
    gr1.SetMarkerSize(1)
    gr1.Draw("AP")


# Method 2
if False:
    n = 100
    xarr = []
    yarr = []

    for i in range(n):
        x = 0.01*i
        y = func(x)
        xarr.append(x)
        yarr.append(y)

    gr2 = TGraph(len(xarr), array('d', xarr), array('d', yarr))

    gr2.SetMarkerStyle(20)
    gr2.SetMarkerSize(1)
    gr2.Draw("AP")