Programming with Python · free preview

Analyzing Patient Data

Explain what a library is and what libraries are used for. Import a Python library and use the functions it contains. Read tabular data from a file into a program. Select individual values and subsections from data. Perform operations on arrays of data.

1. Overview

Overview

2. Loading data into Python

Loading data into Python

3. Data Type

Data Type

4. In the Corner

In the Corner

5. Slicing data

Slicing data

6. Analyzing data

Analyzing data

7. Not All Functions Have Input

Not All Functions Have Input

8. Mystery Functions in IPython

Mystery Functions in IPython

9. Slicing Strings

Slicing Strings

10. Thin Slices

Thin Slices

11. Stacking Arrays

Stacking Arrays

12. Change In Inflammation

Change In Inflammation

13. Predict a slice shape

For a (60, 40) NumPy array, what is the shape of data[:2, 1:4]? Answer as a tuple.

14. Trace, load, shape, slice, and axis-aware maxima

Run this complete NumPy program where the inflammation CSV is available, predict each output, then compare the shapes before the sliced block.

Example. Trace, load, shape, slice, and axis-aware maxima

Problem. Run this complete NumPy program where the inflammation CSV is available, predict each output, then compare the shapes before the sliced block.

Correct solution.

Complete program.```python import numpy

data = numpy.loadtxt(fname='data/inflammation-01.csv', delimiter=',') print('shape:', data.shape) print('dtype:', data.dtype)

print('first four patients, first ten days:')

print(data[0:4, 0:10])

print('patient 0 maximum:', numpy.max(data, axis=1)[0]) print('daily average first three:', numpy.mean(data, axis=0)[:3])

code

Expected output.```output
shape: (60, 40)
dtype: float64
first four patients, first ten days:
[[ 0. 0. 1. 3. 1. 2. 4. 7. 8. 3.]
 [ 0. 1. 2. 1. 2. 1. 3. 2. 2. 6.]
 [ 0. 1. 1. 3. 3. 2. 6. 2. 5. 9.]
 [ 0. 0. 2. 0. 4. 2. 2. 1. 6. 7.]]
patient 0 maximum: 18.0
daily average first three: [0. 0.45 1.11666667]

关键提醒。

loadtxt returns a two-dimensional float array with 60 patient rows and 40 day columns. axis=1 computes one maximum per patient; axis=0 computes one mean per day.

15. Retrieval, file loading and a column slice

From memory, write the numpy.loadtxt call that loads data/inflammation-01.csv and the exact slice that returns rows 0 through 3 and columns 36 through 39. Give the output shape and the first row.

Write the call and the slice exactly enough that a reader could paste them into Python.

16. Mastery, axis-aware results for a new array

Use the new input A = numpy.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14]]). Write the two operations that return one maximum for each row and one mean for each column, and give the exact outputs.

Name the axis argument for each operation and state why the chosen axis collapses that direction.

Practice this interactively

Free account · instant grading · spaced review that schedules itself.

Start this course for free