Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab 0: Python and NumPy Bootcamp

This lab checks the computing skills needed in Chapter 1. Complete it in a fresh notebook. You may consult the chapter and official documentation, but write and explain your own code.

Estimated time: 90–120 minutes

Learning objectives

In this lab, you will:

Submission standard

Your notebook must:

  1. include your name, date, and a descriptive title in a Markdown cell;

  2. import packages once near the beginning;

  3. run from top to bottom after a kernel restart;

  4. show requested output;

  5. include a written response wherever interpretation is requested; and

  6. avoid unexplained errors and unused code.

Setup

Part A: Python function

A1. Weighted score

Write a function named weighted_score(homework, exams, project) that returns a final score using these weights:

Include a one-line docstring. Test the function with homework score 88, exam score 79, and project score 94.

Self-check: The result for the supplied values is 84.25.

A2. Letter category

Extend the task by writing score_category(score), which returns:

Test the function at the boundary values 70, 80, and 90.

Part B: Arrays and data selection

The rows of student_data represent students. The columns contain hours studied, homework average, and exam average.

B1. Inspect the array

Display:

  1. the shape of student_data;

  2. the number of dimensions;

  3. the data type; and

  4. the total number of entries.

Then explain in one sentence what each row and column represents.

B2. Select columns

Create one-dimensional arrays named hours, homework, and exams by selecting the corresponding columns. Display each array and its shape.

B3. Filter rows

Use a Boolean mask to select students who studied at least 6 hours. Store all three columns for those students in selected_students.

Self-check: selected_students should have shape (3, 3).

B4. Center the score columns

Select the homework and exam columns together as a two-dimensional array named scores. Subtract each column’s mean using broadcasting and store the result in centered_scores.

Verify numerically that each centered column has mean zero, allowing for floating-point rounding.

In a Markdown cell, explain which shapes participate in the broadcasting operation.

Part C: Matrix calculations

The matrix below assigns weights to homework and exams for two different grading plans:

Each row is one plan, and the columns correspond to homework and exams.

C1. All plan scores

Use matrix multiplication to compute both plan scores for all students. Your result should have one row per student and one column per grading plan.

State the shapes of the two input arrays and the output array. Explain why the inner dimensions are compatible.

C2. Compare operations

Explain why scores * weights does not answer the same question as your matrix multiplication in C1. Mention both meaning and shape.

Part D: Solve a system

A small shop sells notebooks and pens. Three notebooks and two pens cost 13.Twonotebooksandfivepenscost13. Two notebooks and five pens cost 16. Let nn be the price of one notebook and pp be the price of one pen.

D1. Model

Write the two equations in a Markdown cell. Then define a coefficient matrix A and right-hand side b so the system is Ax=bAx=b.

D2. Solve and verify

Solve the system with np.linalg.solve. Verify it with both:

  1. np.allclose(A @ x, b); and

  2. the norm of the residual.

Interpret both entries of the solution in a complete sentence with units.

Part E: Visualization

Create a scatter plot of study hours on the horizontal axis and exam average on the vertical axis.

Your plot must include:

Below the figure, write two or three sentences describing the relationship visible in this small dataset. Do not claim that study time causes the scores.

Part F: Reflection

Answer each question in complete sentences:

  1. What is one difference between a Python list and a NumPy array?

  2. What is one difference between * and @ for arrays?

  3. Why do we check a linear-system solution with a residual?

  4. Which Chapter 0 skill needs more practice before Chapter 1?

Readiness checklist

Before submitting, confirm:

Optional challenge

Write a function standardize_columns(X) that returns a new array whose columns have mean 0 and standard deviation 1. Raise a ValueError if any column has standard deviation 0. Test the function on scores and verify its output.