<jupyter_start><jupyter_text>Breakout Session 1  Exercise 1 - Slicing stringsA section of an array is called a _slice_. We can take slices of character strings as well:<jupyter_code>element = "oxygen"
print('first three characters:', element[0:3])
print('last three characters:', element[3:6])<jupyter_output>first three characters: oxy
last three characters: gen<jupyter_text>What is the value of `element[4]`? What about `element[4:]`? Or `element[:]`?What is `element[-1]`? What is `element[-3:]`?  Solution<jupyter_code>element = "oxygen"
print('fifth character:', element[4])
print('characters fifth until end:', element[4:])
print('all elements:', element[:])
print('last element:', element[-1])
print('last three characters:', element[-3:])<jupyter_output>fifth character: e
characters fifth until end: en
all elements: oxygen
last element: n
last three characters: gen<jupyter_text>Exercise 2 - Slicing lists with stepsSo far we’ve seen how to use slicing to take single blocks of successive entries from a sequence. But what if we want to take a subset of entries that aren’t next to each other in the sequence? You can achieve this by providing a third argument to the range within the brackets, called the step size. The full sytax for creating slices is `[begin:end:step]`, although you most often find a short-hand notation as we've seen in Exercise 1. The example below shows how you can take every third entry in a list:<jupyter_code>primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
subset = primes[0:12:3]
print('subset', subset)<jupyter_output>subset [2, 7, 17, 29]<jupyter_text>Given the following list of months:<jupyter_code>months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']<jupyter_output><empty_output><jupyter_text>Questions1. What slice of `months` will produce the following output `['jan', 'mar', 'may', 'jul', 'sep', nov']`?1. Given the short-hand notation we used for the character string in Exercise 1 (i.e. `element[:2] == 'element[0:2]`), can you find the short-hand notation for question 1? What do you find easier to read?1. Using the step size parameter, can you think of a way to reverse the list?  Solution<jupyter_code># Verbose solution
print('verbose:', months[0:12:2])

# Short-hand solution
print('short-hand:', months[::2])

# List reversal
print('reversed list:', months[::-1]) # verbose: months[12:0:-1]<jupyter_output>verbose: ['jan', 'mar', 'may', 'jul', 'sep', 'nov']
short-hand: ['jan', 'mar', 'may', 'jul', 'sep', 'nov']
reversed list: ['dec', 'nov', 'oct', 'sep', 'aug', 'jul', 'jun', 'may', 'apr', 'mar', 'feb', 'jan']<jupyter_text>Breakout Session 2  Exercise 3 - Change in inflammationThe patient data is longitudinal in the sense that each row represents a series of observations relating to one individual. This means that the change in inflammation over time is a meaningful concept.The `numpy.diff()` function takes an array and returns the differences between two successive values. Let’s use it to examine the changes each day across the first week of patient 3 from our inflammation dataset:<jupyter_code># Load data
import numpy
data = numpy.loadtxt(fname='data/inflammation-01.csv', delimiter=',')
patient3_week1 = data[3, :7]
print(patient3_week1)<jupyter_output>[0. 0. 2. 0. 4. 2. 2.]<jupyter_text>Calling `numpy.diff(patient3_week1)` would do the following calculations<jupyter_code>numpy.diff(patient3_week1)<jupyter_output><empty_output><jupyter_text>Questions1. When calling `numpy.diff()` with a multi-dimensional array, an axis argument may be passed to the function to specify which axis to process. When applying `numpy.diff()` to our 2D inflammation array data, which axis would we specify?1. If the shape of an individual data file is `(60, 40)` (60 rows and 40 columns), what would the `shape` of the array be after you run the `diff()` function and why?1. How would you find the **largest change** in inflammation for each patient?  Solution 1) Using axis=0, means it will calculate the consecutive difference between patients (considering all days). Using axis=1 means it will calculate the consecutive difference between days (considering all patients). Since it is more useful to see what happens to all patients as they go through the clinical trial, it makes more sense to see overall how the number of inflammation episodes change for the patients between consecutive days.<jupyter_code>numpy.diff(data, axis=1)<jupyter_output><empty_output><jupyter_text>2) The shape will be `(60, 39)` because there is one fewer difference between columns than there are columns in the data.3) By using the `numpy.max()` function after you apply the `numpy.diff()` function, you will get the largest difference between days.<jupyter_code>numpy.max(numpy.diff(data, axis=1), axis=1)<jupyter_output><empty_output><jupyter_text>Alternatively, `numpy.diff(data, axis=1).max(axis=1)` also works. Furhtermore, it can be extended with `.shape` to check the size: `numpy.diff(data, axis=1).max(axis=1).shape`  Exercise 4 - Plotting differencesPlot the difference between the average inflammations reported in the first and second datasets (stored in `inflammation-01.csv` and `inflammation-02.csv`, correspondingly), i.e., the difference between the leftmost plots of the first two figures we have plotted so far. Steps:1. Import libraries1. Import data1. Calculate difference1. Create and annotate figure  Solution<jupyter_code># Import libraries
import glob
import numpy
import matplotlib.pyplot as plt

# Import data
filenames = sorted(glob.glob('data/inflammation*.csv'))

# Calculate difference
data0 = numpy.loadtxt(fname=filenames[0], delimiter=',')
data1 = numpy.loadtxt(fname=filenames[1], delimiter=',')
difference = numpy.mean(data0, axis=0) - numpy.mean(data1, axis=0)

# Create and annotate figure
fig = plt.figure(figsize=(10.0, 3.0))

plt.xlabel('Day')
plt.ylabel('Difference in average')
plt.plot(difference)

fig.tight_layout()
plt.show()
# THIS SHOULD BE THE LAST CELL

# Convert notebook
!jupyter nbconvert python_exercises_1.ipynb --to slides<jupyter_output>[NbConvertApp] Converting notebook python_exercises_1.ipynb to slides
[NbConvertApp] Writing 618508 bytes to python_exercises_1.slides.html
{'fraction_non_alphanumeric': 0.11518072289156626, 'fraction_numerical': 0.023293172690763052, 'mean_word_length': 5.9720044792833145, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '312608', 'license': 'CC0-1.0', 'n_tokens_mistral': 2027, 'n_tokens_neox': 1882, 'n_words': 752, 'path': 'Notes_for_lesson/slides/python_exercises_1.ipynb', 'repo_name': 'jurra/software_carpentries', 'size': '6225'}