(Time 3 hours 15 minutes)
print
is a keyword (in Python 2, not in 3)python
in your terminalipython
in your terminalipython notebook
in your terminalprint "Hello"
weight_kg = 55
weight_kg
print weight_kg
print "Weight in pounds is ", 2.2 * weight_kg
weight_lb = 2.2 * weight_kg
print weight_lb
weight_kg = 100
print weight_kg
print weight_lb
Numpy is a library for some math in Python
import numpy
We creating a new name (data
) to store the result of calling a function in the numpy module that reads text files (numpy.loadtxt
)
data = numpy.loadtxt(fname="data/inflammation-01.csv", delimiter=',')
print data
type
is a Python built-in function (is loaded by default when you start any interpreter) which tells you the kind of object referred by a name.
Object is the generic concept for "things" in Python, everything is an object. Objects represent how the "thing" is stored in the computer memory, for example they have instructions on how your computer reads the object from the memory, so you may get an error if you are trying to process an object as the wrong kind. More importantly for you, objects have functions associated to them (called methods). Each type of object may have its own methods.
print type(data)
print type(weight_lb)
data.shape
weight_lb.shape
data[3:4, 20]
data[:4, 30:]
data[30, 20]
sample = data[4:8, 1:3]
print sample
### Operators and methods for Numpy arrays
double_sample = 2 * sample
print double_sample
data.mean()
data.max()
data.min()
Pause and reflect on the amount of work that your computer has done when you called any of this methods. This is called high-level programming language and means you don't need to know much about how your computer does its thing.
Slicing a numpy array gives you another numpy array (nice, we still have all methods available)
patient_0 = data[0, :]
print patient_0
type(patient_0)
patient_0.shape
patient_0.mean()
data.mean(axis=1)
You can import parts of a module using from
, in this case the pyplot
part of matplotlib
.
from matplotlib import pyplot
%matplotlib inline
image = pyplot.imshow(data)
pyplot.show(image)
ave_inflammation = data.mean(axis=0)
ave_plot = pyplot.plot(ave_inflammation)
pyplot.show(ave_plot)
max_plot = pyplot.plot(data.max(axis=0))
pyplot.show(max_plot)
import numpy as np
from matplotlib import pyplot as plt
data = np.loadtxt(fname="data/inflammation-01.csv", delimiter=",")
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('max')
axes2.plot(data.max(axis=0))
axes3.set_ylabel('min')
axes3.plot(data.min(axis=0))
fig.tight_layout()
plt.show(fig)
word = "lead"
word
print word[0]
print word[1]
print word[2]
print word[3]
length = 0
for char in word:
length = length + 1
print char
print "The number of letters ", length
print char
print length
len(word)
range(2, 8, 2)
for number in range(1,4):
print number
result = 1
for times in range(3):
result *= 5
print result
l = range(5)
type(l)
odd = [1, 3, 5]
type(odd)
print odd
odd[1:]
odd[-2]
names = ["Newton", "Darwin", "Tuinrg"]
names[-2]
darwin = names[-2]
print darwin
names[-1] = "Turing"
names
darwin
darwin[0]
darwin[0] = 'd'
odd
odd.append(7)
odd
odd.reverse()
odd
odd.append('john')
Lists can have different types of objects
odd
type(odd[0])
type(odd[-1])
Sorta ls
in the shell, but storing the result into a Python list (nice!)
import glob
print glob.glob('*.ipynb')
print glob.glob('./data/*.csv')
filenames = glob.glob("data/*csv")
print filenames
for f in filenames[:3]:
print "Analyzing file: ", f
analyze_inflammation(f)
def analyze(filename):
"""Analyze inflammation data and make plots
"""
data = np.loadtxt(fname=filename, delimiter=",")
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('max')
axes2.plot(data.max(axis=0))
axes3.set_ylabel('min')
axes3.plot(data.min(axis=0))
fig.tight_layout()
plt.show(fig)
weight_kg = 20
if weight_kg > 80:
print "You're over 80 kg"
else:
print "You're under 80 kg"
number = -3
if (number >= 0):
print "Positive"
else:
print "Negative"
(80 - 32) * 5/9 + 273.15
def fahr_to_kelvin(temp):
return (temp - 32) * 5.0/9 + 273.15
def kelvin_to_celsius(temp):
'''Converts temperatures in Kelvin to Celsius
More stuff
'''
result = temp - 273.15
return result
def farh_to_celsius(temp):
kelvin = fahr_to_kelvin(temp)
result = kelvin_to_celsius(kelvin)
return result
kelvin_to_celsius(fahr_to_kelvin(0))
fahr_to_kelvin(80)
temp_in_kelvin = fahr_to_kelvin(100)
temp_in_kelvin
Be careful with dividing numbers
10/9
10.0/9
help(analyze)
help(np.loadtxt)