up to now:
problem is
need a way to package our code for reuse
* Let's write function to convert from fahrenheit to kelvin
In [3]:
import numpy
import matplotlib.pyplot
In [1]:
def fahr_to_kelvin(temp):
return ((temp - 32) * (5/9)) + 273.15
def
followed by function name and parenthesized list of parameter namescall
a function and the values we pass it are assigned to those varialbes so we can use inside We can now try our function out!
In [2]:
print('freezing point of water:', fahr_to_kelvin(32))
print('boiling point of water:', fahr_to_kelvin(212))
In [3]:
def kelvin_to_celsius(temp_k):
return temp_k - 273.15
print('absolute zero in Celsius:', kelvin_to_celsius(0.0))
What about Fahrenheit to Celsius?
In [4]:
def fahr_to_celsius(temp_f):
temp_k = fahr_to_kelvin(temp_f)
result = kelvin_to_celsius(temp_k)
return result
print('freezing point of water in Celsius:', fahr_to_celsius(32.0))
first taste of how larger programs are built:
“Adding” two strings produces their concatenation: 'a' + 'b' is 'ab'. Write a function called fence that takes two parameters called original and wrapper and returns a new string that has the wrapper character at the beginning and end of the original. A call to your function should look like this:
print(fence('name', '*'))
In [2]:
def fence(original, wrapper):
return wrapper + original + wrapper
print(fence('name', '*'))
In [11]:
def analyze(filename):
data = numpy.loadtxt(fname=filename, delimiter=',')
fig = matplotlib.pyplot.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('max')
axes2.plot(data.max(axis=0))
axes3.set_ylabel('min')
axes3.plot(data.min(axis=0))
fig.tight_layout()
matplotlib.pyplot.show(fig)
add another function called detect_problems that checks for those systematics we noticed:
In [12]:
def detect_problems(filename):
data = numpy.loadtxt(fname=filename, delimiter=',')
if data.max(axis=0)[0] == 0 and data.max(axis=0)[20] == 20:
print('Suspicious looking maxima!')
elif data.min(axis=0).sum() == 0:
print('Minima add up to zero!')
else:
print('Seems OK!')
In [21]:
import glob
import numpy
import matplotlib.pyplot
%matplotlib inline
In [22]:
filenames = glob.glob('data/inflammation*.csv')
In [23]:
for f in filenames[:3]:
print(f)
analyze(f)
detect_problems(f)
If the variable s refers to a string, then s[0] is the string’s first character and s[-1] is its last. Write a function called outer that returns a string made up of just the first and last characters of its input. A call to your function should look like this:
print(outer('helium'))
In [4]:
def outer(input_string):
return input_string[0] + input_string[-1]
print(outer('helium'))
In [1]:
def center(data, desired):
return (data - data.mean()) + desired
In [4]:
z = numpy.zeros((2,2))
print(center(z, 3))
In [6]:
data = numpy.loadtxt(fname='data/inflammation-01.csv', delimiter=',')
print(center(data, 0))
In [7]:
print('original min, mean, and max are:', data.min(), data.mean(), data.max())
centered = center(data, 0)
print('min, mean, and and max of centered data are:', centered.min(), centered.mean(), centered.max())
In [8]:
print('std dev before and after:', data.std(), centered.std())
In [10]:
print('difference in standard deviations before and after:', data.std() - centered.std())
In [27]:
# center(data, desired): return a new array containing the original data centered around the desired value.
def center(data, desired):
return (data - data.mean()) + desired
In [11]:
def center(data, desired):
'''Return a new array containing the original data centered around the desired value.'''
return (data - data.mean()) + desired
better because we can now ask python's built-in help to show us the documentation
In [12]:
help(center)
In [13]:
def center(data, desired):
'''Return a new array containing the original data centered around the desired value.
Example: center([1, 2, 3], 0) => [-1, 0, 1]'''
return (data - data.mean()) + desired
help(center)
In [14]:
numpy.loadtxt('data/inflammation-01.csv', delimiter=',')
Out[14]:
but we still need the delimiter=
:
In [15]:
numpy.loadtxt('data/inflammation-01.csv', ',')
center
function like this:
In [33]:
def center(data, desired=0.0):
'''Return a new array containing the original data centered around the desired value (0 by default).
Example: center([1, 2, 3], 0) => [-1, 0, 1]'''
return (data - data.mean()) + desired
desired=0.0
In [16]:
test_data = numpy.zeros((2, 2))
print(center(test_data, 3))
In [35]:
more_data = 5 + numpy.zeros((2, 2))
print('data before centering:')
print(more_data)
print('centered data:')
print(center(more_data))
this is handy:
if we want a function to work one way, but occasionally need it to do something else, we can allow people to pass a diff parameter when they need to but provide a default
below example shows how python matches values to parameters:
In [17]:
def display(a=1, b=2, c=3):
print('a:', a, 'b:', b, 'c:', c)
print('no parameters:')
display()
print('one parameter:')
display(55)
print('two parameters:')
display(55, 66)
In [19]:
# we can override this behavior by naming value as we pass it in
print('only setting the value of c')
display(c=77)
Let's now look at numpy.loadtext:
In [20]:
help(numpy.loadtxt)
lots of info but look at this part:
loadtxt(fname, dtype=<type 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None,
unpack=False, ndmin=0)
loadtxt
has one parameter called fname
that doesn't have a default value, & 8 others that do
In [21]:
numpy.loadtxt('data/inflammation-01.csv', ',')
delimiter
for the second parameter since delimiter is not the 2nd in the list
In [ ]: