In [3]:
def fahr_to_kelvin(temperature):
    print temperature
    print 'hello'
In [4]:
fahr_to_kelvin(123)
123
hello

In [13]:
def fahr_to_kelvin(temperature):
    return ((temperature - 32) * (5/9)) + 273.15
In [14]:
print "The freezing point of water is:", fahr_to_kelvin(32)
print "The boiling point of water is:", fahr_to_kelvin(212)
The freezing point of water is: 273.15
The boiling point of water is: 273.15

In [15]:
5/9 # in python 2 it is 0
Out[15]:
0
In [16]:
5.0 / 9.0
Out[16]:
0.5555555555555556
In [17]:
def fahr_to_kelvin(temperature):
    return ((temperature - 32) * (5.0/9)) + 273.15
In [18]:
print "The freezing point of water is:", fahr_to_kelvin(32)
print "The boiling point of water is:", fahr_to_kelvin(212)
The freezing point of water is: 273.15
The boiling point of water is: 373.15

In [20]:
def kelvin_to_celsius(t):
    return t - 273.15
In [21]:
print 'absolute zero is Celsius:', kelvin_to_celsius(0)
absolute zero is Celsius: -273.15

In [22]:
def fahr_to_celsius(t_fahr):
    t_kelvin = fahr_to_kelvin(t_fahr)
    return kelvin_to_celsius(t_kelvin)
In [23]:
print 'freezing point of water in celsius:', fahr_to_celsius(32.0)
freezing point of water in celsius: 0.0

In [26]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

def plot_average_max_min(data):
    fig = plt.figure(figsize=(10.0, 3.0))
    
    axes1 = fig.add_subplot(1, 3, 1)
    axes2 = fig.add_subplot(1, 3, 2)
    axes3 = fig.add_subplot(1, 3, 3)
    
    axes1.set_ylabel('average')
    axes1.plot( data.mean(axis=0) )
    
    axes2.set_ylabel(r'$\frac{max}{min}$')
    axes2.plot( data.max(axis=0) )
    
    axes3.set_ylabel('min')
    axes3.plot( data.min(axis=0) )
    
    fig.tight_layout()
    plt.show(fig)


def analyze_all_files(filenames):
    for f in filenames:
        print f
        plot_average_max_min( np.loadtxt(f, delimiter=',') )

import glob
analyze_all_files(glob.glob('data/*.csv')[0:2])
analyze_all_files(glob.glob('data/*.csv')[5:6])
data/inflammation-04.csv

data/inflammation-10.csv

data/small-02.csv

In [34]:
def increment(a, inc=1):
    return a+inc
In [35]:
increment(10)
Out[35]:
11
In [36]:
increment(10, 20)
Out[36]:
30
In [39]:
def increment(a):
    """
    This function is pretty useless.
    """
    return a+1
In [40]:
print """hello
this is amazing
yeah"""
hello
this is amazing
yeah

In [41]:
print increment(10)
11

In [44]:
help(increment)
help(np.max)
Help on function increment in module __main__:

increment(a)
    This function is pretty useless.

Help on function amax in module numpy.core.fromnumeric:

amax(a, axis=None, out=None, keepdims=False)
    Return the maximum of an array or maximum along an axis.
    
    Parameters
    ----------
    a : array_like
        Input data.
    axis : int, optional
        Axis along which to operate.  By default, flattened input is used.
    out : ndarray, optional
        Alternative output array in which to place the result.  Must
        be of the same shape and buffer length as the expected output.
        See `doc.ufuncs` (Section "Output arguments") for more details.
    keepdims : bool, optional
        If this is set to True, the axes which are reduced are left
        in the result as dimensions with size one. With this option,
        the result will broadcast correctly against the original `arr`.
    
    Returns
    -------
    amax : ndarray or scalar
        Maximum of `a`. If `axis` is None, the result is a scalar value.
        If `axis` is given, the result is an array of dimension
        ``a.ndim - 1``.
    
    See Also
    --------
    amin :
        The minimum value of an array along a given axis, propagating any NaNs.
    nanmax :
        The maximum value of an array along a given axis, ignoring any NaNs.
    maximum :
        Element-wise maximum of two arrays, propagating any NaNs.
    fmax :
        Element-wise maximum of two arrays, ignoring any NaNs.
    argmax :
        Return the indices of the maximum values.
    
    nanmin, minimum, fmin
    
    Notes
    -----
    NaN values are propagated, that is if at least one item is NaN, the
    corresponding max value will be NaN as well. To ignore NaN values
    (MATLAB behavior), please use nanmax.
    
    Don't use `amax` for element-wise comparison of 2 arrays; when
    ``a.shape[0]`` is 2, ``maximum(a[0], a[1])`` is faster than
    ``amax(a, axis=0)``.
    
    Examples
    --------
    >>> a = np.arange(4).reshape((2,2))
    >>> a
    array([[0, 1],
           [2, 3]])
    >>> np.amax(a)           # Maximum of the flattened array
    3
    >>> np.amax(a, axis=0)   # Maxima along the first axis
    array([2, 3])
    >>> np.amax(a, axis=1)   # Maxima along the second axis
    array([1, 3])
    
    >>> b = np.arange(5, dtype=np.float)
    >>> b[2] = np.NaN
    >>> np.amax(b)
    nan
    >>> np.nanmax(b)
    4.0


In [49]:
help(10.0)
Help on float object:

class float(object)
 |  float(x) -> floating point number
 |  
 |  Convert a string or number to a floating point number, if possible.
 |  
 |  Methods defined here:
 |  
 |  __abs__(...)
 |      x.__abs__() <==> abs(x)
 |  
 |  __add__(...)
 |      x.__add__(y) <==> x+y
 |  
 |  __coerce__(...)
 |      x.__coerce__(y) <==> coerce(x, y)
 |  
 |  __div__(...)
 |      x.__div__(y) <==> x/y
 |  
 |  __divmod__(...)
 |      x.__divmod__(y) <==> divmod(x, y)
 |  
 |  __eq__(...)
 |      x.__eq__(y) <==> x==y
 |  
 |  __float__(...)
 |      x.__float__() <==> float(x)
 |  
 |  __floordiv__(...)
 |      x.__floordiv__(y) <==> x//y
 |  
 |  __format__(...)
 |      float.__format__(format_spec) -> string
 |      
 |      Formats the float according to format_spec.
 |  
 |  __ge__(...)
 |      x.__ge__(y) <==> x>=y
 |  
 |  __getattribute__(...)
 |      x.__getattribute__('name') <==> x.name
 |  
 |  __getformat__(...)
 |      float.__getformat__(typestr) -> string
 |      
 |      You probably don't want to use this function.  It exists mainly to be
 |      used in Python's test suite.
 |      
 |      typestr must be 'double' or 'float'.  This function returns whichever of
 |      'unknown', 'IEEE, big-endian' or 'IEEE, little-endian' best describes the
 |      format of floating point numbers used by the C type named by typestr.
 |  
 |  __getnewargs__(...)
 |  
 |  __gt__(...)
 |      x.__gt__(y) <==> x>y
 |  
 |  __hash__(...)
 |      x.__hash__() <==> hash(x)
 |  
 |  __int__(...)
 |      x.__int__() <==> int(x)
 |  
 |  __le__(...)
 |      x.__le__(y) <==> x<=y
 |  
 |  __long__(...)
 |      x.__long__() <==> long(x)
 |  
 |  __lt__(...)
 |      x.__lt__(y) <==> x<y
 |  
 |  __mod__(...)
 |      x.__mod__(y) <==> x%y
 |  
 |  __mul__(...)
 |      x.__mul__(y) <==> x*y
 |  
 |  __ne__(...)
 |      x.__ne__(y) <==> x!=y
 |  
 |  __neg__(...)
 |      x.__neg__() <==> -x
 |  
 |  __nonzero__(...)
 |      x.__nonzero__() <==> x != 0
 |  
 |  __pos__(...)
 |      x.__pos__() <==> +x
 |  
 |  __pow__(...)
 |      x.__pow__(y[, z]) <==> pow(x, y[, z])
 |  
 |  __radd__(...)
 |      x.__radd__(y) <==> y+x
 |  
 |  __rdiv__(...)
 |      x.__rdiv__(y) <==> y/x
 |  
 |  __rdivmod__(...)
 |      x.__rdivmod__(y) <==> divmod(y, x)
 |  
 |  __repr__(...)
 |      x.__repr__() <==> repr(x)
 |  
 |  __rfloordiv__(...)
 |      x.__rfloordiv__(y) <==> y//x
 |  
 |  __rmod__(...)
 |      x.__rmod__(y) <==> y%x
 |  
 |  __rmul__(...)
 |      x.__rmul__(y) <==> y*x
 |  
 |  __rpow__(...)
 |      y.__rpow__(x[, z]) <==> pow(x, y[, z])
 |  
 |  __rsub__(...)
 |      x.__rsub__(y) <==> y-x
 |  
 |  __rtruediv__(...)
 |      x.__rtruediv__(y) <==> y/x
 |  
 |  __setformat__(...)
 |      float.__setformat__(typestr, fmt) -> None
 |      
 |      You probably don't want to use this function.  It exists mainly to be
 |      used in Python's test suite.
 |      
 |      typestr must be 'double' or 'float'.  fmt must be one of 'unknown',
 |      'IEEE, big-endian' or 'IEEE, little-endian', and in addition can only be
 |      one of the latter two if it appears to match the underlying C reality.
 |      
 |      Override the automatic determination of C-level floating point type.
 |      This affects how floats are converted to and from binary strings.
 |  
 |  __str__(...)
 |      x.__str__() <==> str(x)
 |  
 |  __sub__(...)
 |      x.__sub__(y) <==> x-y
 |  
 |  __truediv__(...)
 |      x.__truediv__(y) <==> x/y
 |  
 |  __trunc__(...)
 |      Return the Integral closest to x between 0 and x.
 |  
 |  as_integer_ratio(...)
 |      float.as_integer_ratio() -> (int, int)
 |      
 |      Return a pair of integers, whose ratio is exactly equal to the original
 |      float and with a positive denominator.
 |      Raise OverflowError on infinities and a ValueError on NaNs.
 |      
 |      >>> (10.0).as_integer_ratio()
 |      (10, 1)
 |      >>> (0.0).as_integer_ratio()
 |      (0, 1)
 |      >>> (-.25).as_integer_ratio()
 |      (-1, 4)
 |  
 |  conjugate(...)
 |      Return self, the complex conjugate of any float.
 |  
 |  fromhex(...)
 |      float.fromhex(string) -> float
 |      
 |      Create a floating-point number from a hexadecimal string.
 |      >>> float.fromhex('0x1.ffffp10')
 |      2047.984375
 |      >>> float.fromhex('-0x1p-1074')
 |      -4.9406564584124654e-324
 |  
 |  hex(...)
 |      float.hex() -> string
 |      
 |      Return a hexadecimal representation of a floating-point number.
 |      >>> (-0.1).hex()
 |      '-0x1.999999999999ap-4'
 |      >>> 3.14159.hex()
 |      '0x1.921f9f01b866ep+1'
 |  
 |  is_integer(...)
 |      Return True if the float is an integer.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  imag
 |      the imaginary part of a complex number
 |  
 |  real
 |      the real part of a complex number
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T


In [79]:
def magic(a, b=1):
    if a == 1:
        return b
    else:
        return magic(a-1, a*b)

print(magic(3))
print(magic(5))
6
120

In [81]:
def magic(a, b=1):
    if a == 1:
        return b
    else:
        return b*magic(a, a*b)

#print(magic(3, 0))
In [50]:
# load "data" from a csv file
# "normalize" the data: after normalization, it should have 0 mean
#                       and unit variance (or standard deviation)
In [54]:
import numpy as np
data = np.loadtxt('data/inflammation-01.csv', delimiter=',')
print data.shape
print data.var()
print data.std()
print data.var()**0.5
(60, 40)
21.2874567708
4.61383319712
4.61383319712

In [62]:
def normalize_data(d):
    mean_of_d = np.mean(d)
    standard_deviation_of_d = np.std(d)
    return (d - mean_of_d) / standard_deviation_of_d
In [63]:
ndata = normalize_data( np.loadtxt('data/inflammation-01.csv', delimiter=',') )
In [64]:
print ndata.mean()
6.36527867452e-17

In [65]:
print ndata.var()
1.0

In [66]:
print data.dtype
float64

In [69]:
a = np.array([1, 2, 4])
print a.dtype
int64

In [75]:
a = np.array([1, 2, 4], dtype=np.float128)
print a.dtype
float128

In [76]:
a = np.array(data, dtype=np.float128)
print a.dtype
float128

In [81]:
In []: