<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Analyzing patient data
Words are useful, but what’s more useful are the sentences and stories we build with them.
A lot of powerful tools are built into languages like Python, even more live in the libraries they are used to build
We need to import a library called NumPy
Use this library to do fancy things with numbers (e.g. if you have matrices or arrays).
Step1:  Importing a library akin to getting lab equipment out of a locker and setting up on bench
Libraries provide additional functionality
With NumPy loaded we can read the CSV into python.
Step2:  numpy.loadtex() is a function call, runs loadtxt in numpy
uses dot notation to access thing.component
two parameters
Step3:  print above shows several things at once by separating with commas
variable as putting sticky note on value
means assigning a value to one variable does not chage the value of other variables.
Step4:  whos #ipython command to see what variables & mods you have
Step5:  What does the following program print out?
python
first, second = 'Grace', 'Hopper'
third, fourth = second, first
print(third, fourth)
Step6:  data refers to N-dimensional array 
data corres. to patients' inflammation
let's look at the shape of the data
Step7:  data has 60 rows and 40 columns 
when we created data with numpy it also creates members or attributes
extra info describes data like adjective does a noun
dot notation to access members
Step8:  programming languages like MATLAB and R start counting at 1
languages in C family (C++, Java, Perl & python)
we have MxN array in python, indices go from 0 to M-1 on the first axis and 0 to N-1 on second
indices are (row, column)
Step9:  slice 0
Step10:  dont' have to include uper and lower bound
python uses 0 by default if we don't include lower
no upper slice runs to the axis 
Step11:  A section of an array is called a slice. We can take slices of character strings as well
Step12:  operation on arrays is done on each individual element of the array
Step13:  we can also do arithmetic operation with another array of same shape (same dims)
Step14:  we can do more than simple arithmetic
let's take average inflammation for patients
Step15:  mean is a method of the array (function)
variables are nouns, methods are verbs - they are what the thing knows how to do
for mean we need empty () parense even if we aren't passing in parameters to tell python to go do something
data.shape doesn't need () because it's just a description 
NumPy arrays have lots of useful methods
Step16:  however, we are usually more interested in partial stats, e.g. max value per patient or the avg value per day
we can create a new subset array of the data we want
Step17:  let's visualize this data with matplotlib library
first we import the plyplot module from matplotlib
Step18:  nice, but ipython/jupyter proved us with 'magic' functions and one lets us display our plot inline
% indicates an ipython magic function 
what if we need max inflammation for all patients, or the average for each day?
most array methods let us specify the axis we want to work on
Step19:  now let's look at avg inflammation over days (columns)
Step20:  avg per day across all patients in the var day_avg_plot
matplotlib create and display a line graph of those values
<ASSISTANT_TASK:>
Python Code:
import numpy
Explanation: Analyzing patient data
Words are useful, but what’s more useful are the sentences and stories we build with them.
A lot of powerful tools are built into languages like Python, even more live in the libraries they are used to build
We need to import a library called NumPy
Use this library to do fancy things with numbers (e.g. if you have matrices or arrays).
End of explanation
#assuming the data file is in the data/ folder
numpy.loadtxt(fname='data/inflammation-01.csv', delimiter=',') 
Explanation: Importing a library akin to getting lab equipment out of a locker and setting up on bench
Libraries provide additional functionality
With NumPy loaded we can read the CSV into python.
End of explanation
data = numpy.loadtxt(fname='data/inflammation-01.csv', delimiter=',') 
print(data)
weight_kg = 55 #assigns value 55 to weight_kg
print(weight_kg) #we can print to the screen
print("weight in kg", weight_kg)
weight_kg = 70
print("weight in kg", weight_kg)
Explanation: numpy.loadtex() is a function call, runs loadtxt in numpy
uses dot notation to access thing.component
two parameters: filename and delimiter - both character strings (")
we didn't save in memory using a variable 
variables in python must start with letter & are case sensitive
assignment operator is =
let's look at assigning this inflammation data to a variable
End of explanation
weight_kg * 2
weight_lb = weight_kg * 2.2
print('weigh in lb:', weight_lb)
print("weight in lb:", weight_kg*2.2)
print(data)
Explanation: print above shows several things at once by separating with commas
variable as putting sticky note on value
means assigning a value to one variable does not chage the value of other variables.
End of explanation
whos
Explanation: whos #ipython command to see what variables & mods you have
End of explanation
print(data)
print(type(data)) #we can get type of object
Explanation: What does the following program print out?
python
first, second = 'Grace', 'Hopper'
third, fourth = second, first
print(third, fourth)
End of explanation
print(data.shape)
Explanation: data refers to N-dimensional array 
data corres. to patients' inflammation
let's look at the shape of the data
End of explanation
print('first value in data', data[0,0]) #use index in square brackets
print('4th value in data', data[0,3]) #use index in square brackets
print('first value in 3rd row data', data[3,0]) #use index in square brackets
!head -3 data/inflammation-01.csv
print('middle value in data', data[30,20]) # get the middle value - notice here i didn't use print
Explanation: data has 60 rows and 40 columns 
when we created data with numpy it also creates members or attributes
extra info describes data like adjective does a noun
dot notation to access members
End of explanation
data[0:4, 0:10] #select whole sections of matrix, 1st 10 days & 4 patients
Explanation: programming languages like MATLAB and R start counting at 1
languages in C family (C++, Java, Perl & python)
we have MxN array in python, indices go from 0 to M-1 on the first axis and 0 to N-1 on second
indices are (row, column)
End of explanation
data[5:10,0:10]
Explanation: slice 0:4 means start at 0 and go up to but not include 4
up-to-but-not-including takes a bit of getting used to
End of explanation
data[:3, 36:] 
Explanation: dont' have to include uper and lower bound
python uses 0 by default if we don't include lower
no upper slice runs to the axis 
: will include everything
End of explanation
element = 'oxygen'
print('first three characters:', element[0:3])
print('last three characters:', element[3:6])
print(element[:4])
print(element[4:])
print(:)
#oxygen
print(element[-1])
print(element[-2])
print(element[2:-1])
doubledata = data * 2.0 #we can perform math on array
Explanation: A section of an array is called a slice. We can take slices of character strings as well:
python
element = 'oxygen'
print('first three characters:', element[0:3])
print('last three characters:', element[3:6])
first three characters: oxy
last three characters: gen
What is the value of element[:4]? What about element[4:]? Or element[:]?
What is element[-1]? What is element[-2]? Given those answers, explain what element[1:-1] does.
End of explanation
doubledata
data[:3, 36:]
doubledata[:3, 36:]
Explanation: operation on arrays is done on each individual element of the array
End of explanation
tripledata = doubledata + data 
print('tripledata:')
print(tripledata[:3, 36:])
Explanation: we can also do arithmetic operation with another array of same shape (same dims)
End of explanation
print(data.mean())
Explanation: we can do more than simple arithmetic
let's take average inflammation for patients
End of explanation
print('maximum inflammation: ', data.max())
print('minimum inflammation: ', data.min())
print('standard deviation:', data.std())
Explanation: mean is a method of the array (function)
variables are nouns, methods are verbs - they are what the thing knows how to do
for mean we need empty () parense even if we aren't passing in parameters to tell python to go do something
data.shape doesn't need () because it's just a description 
NumPy arrays have lots of useful methods:
End of explanation
%matplotlib inline
import matplotlib.pyplot as plt
data
Explanation: however, we are usually more interested in partial stats, e.g. max value per patient or the avg value per day
we can create a new subset array of the data we want
End of explanation
plt.imshow(data)
image = plt.imshow(data)
plt.savefig('timsheatmap.png')
Explanation: let's visualize this data with matplotlib library
first we import the plyplot module from matplotlib
End of explanation
avg_inflam = data.mean(axis=0) #asix zero is by each day
print(data.mean(axis=0))
print(data.mean(axis=0).shape) #Nx1 vector of averages
print(data.mean(axis=1)) #avg inflam per patient across all days
print(data.mean(axis=1).shape)
Explanation: nice, but ipython/jupyter proved us with 'magic' functions and one lets us display our plot inline
% indicates an ipython magic function 
what if we need max inflammation for all patients, or the average for each day?
most array methods let us specify the axis we want to work on
End of explanation
print(avg_inflam)
day_avg_plot = plt.plot(avg_inflam)
Explanation: now let's look at avg inflammation over days (columns)
End of explanation
data.mean(axis=0).shape
data.shape
data.mean(axis=1).shape
max_plot = plt.plot(data.max(axis=0))
Explanation: avg per day across all patients in the var day_avg_plot
matplotlib create and display a line graph of those values
End of explanation
<END_TASK>