Exercise 1 (with Gemini): Ask Gemini to “create a 2D numpy array with 3 rows and 4 columns”
Exercise 2 (on your own): Type import numpy as np then arr = np.array([1, 2, 3]) then print(arr) and run it.
Slicing Arrays
# slice a 1-dimensional array (like slicing a list)print('Extract a single element:', arr_1d[1])print('Extract a range:', arr_1d[:2])# slice a 2-dimensional array - index is [row, column]print('\nExtract element from 2d array:', arr_2d[0, 1])print('Extract range from 2d array:')print(arr_2d[:, :2])
Extract a single element: 1.4
Extract a range: [1.1 1.4]
Extract element from 2d array: 1.4
Extract range from 2d array:
[[ 1.1 1.4 ]
[-3.2 5.603]]
Practice: Slicing Arrays
Exercise 1 (with Gemini): Ask Gemini to “extract the first row from a 2D numpy array”
Exercise 2 (on your own): Type arr = np.array([[1, 2], [3, 4]]) then print(arr[0, 1]) and run it.
Array Operations
# array addition (sum element by element)arr_1d_second = np.array([7, 4.3, 1.1])print('Sum of arrays:', arr_1d + arr_1d_second)# array multiplication (multiply element by element)print('Product of arrays:', arr_1d * arr_1d_second)# broadcasting: multiply by scalarprint('\nBroadcast (multiply by 2):')print(2* arr_2d)
Sum of arrays: [8.1 5.7 3.7]
Product of arrays: [7.7 6.02 2.86]
Broadcast (multiply by 2):
[[ 2.2 2.8 5.2 ]
[-6.4 11.206 8. ]]
Practice: Array Operations
Exercise 1 (with Gemini): Ask Gemini to “create two numpy arrays and add them together element-wise”
Exercise 2 (on your own): Type import numpy as np then arr = np.array([1, 2, 3]) then print(arr * 2) and run it.
Array Methods
# sum methodprint('Array:', arr_1d)print('Sum:', arr_1d.sum())# standard deviation methodprint('\nStandard deviation:', arr_1d.std())# other useful methods: mean(), min(), max()print('Mean:', arr_1d.mean())
Exercise 1 (with Gemini): Ask Gemini to “calculate the sum and mean of a numpy array”
Exercise 2 (on your own): Type arr = np.array([10, 20, 30]) then print(arr.max()) and run it.
Random Number Generation
import pandas as pdimport matplotlib.pyplot as plt# Set random seed for reproducible resultsnp.random.seed(42)# Roll a six-sided die 1000 timesnum_rolls =1000dice_rolls = np.random.randint(1, 7, num_rolls)print(f"First 10 rolls: {dice_rolls[:10]}")print(f"Average: {dice_rolls.mean():.2f} (expected: 3.5)")
# Business question: What's the probability of meeting target?target_sales =300000prob_meeting_target = np.mean(all_annual_totals >= target_sales) *100print(f"Probability of meeting ${target_sales:,} target: {prob_meeting_target:.1f}%")# Show different targetsfor target in [280000, 300000, 320000]: prob = np.mean(all_annual_totals >= target) *100print(f"Target ${target:,}: {prob:.1f}% probability")
Probability of meeting $300,000 target: 79.8%
Target $280,000: 97.2% probability
Target $300,000: 79.8% probability
Target $320,000: 37.0% probability
Summary
Numpy arrays: Efficient numerical operations
Random numbers: np.random.randint(), np.random.normal()
Monte Carlo: Simulate many scenarios to understand distributions
Applications: Risk analysis, forecasting, decision making
Key insight: Run thousands of simulations to estimate probabilities