<jupyter_start><jupyter_text>DataScience Course $\circ$ Numpy $\circ$ Vectorized Operations & BroadcastingBy Dr <NAME>     Topics- Vectorized operations- Broadcasting- Selection- Reduce operations<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

sns.set(rc={'figure.figsize':(14, 5)})
rs = np.random.RandomState(0)

M1 = rs.randint(1, 100, 15).reshape(3, 5)
M2 = rs.randint(1, 100, 5)
M3 = np.arange(1, 4)

r = np.arange(10) + 1
def plot_r():
    plt.figure(figsize=(10,5))
    pd.DataFrame(dict(
        circumfirence = r_circ,
        area = r_area
    ), index = r
                ).plot() ;<jupyter_output><empty_output><jupyter_text>Exercises- Run provided codes prior to the exercise question- Add a **new cell** to try your code- Type in Python code in the box to get the results as displayed right below__Warning__: results will be replaced, so make sure you add a new cell to try your code **$\Rightarrow$ Print the shape of each of the numpy arrays `M1`, `M2`, and `M3`**<jupyter_code><jupyter_output>(3, 5)
(5,)
(3,)<jupyter_text>**$\Rightarrow$ Perform the addition between `M1` and `M2` with broadcasting.What is the shape of the resulting array?** **$\Rightarrow$ Perform the multiplication between `M2` and `M3` with broadcasting.Why is there an error?** **$\Rightarrow$Try reshaping `M2` to `(5, 1)` and do the multiplication again. What is the shape of the resulting array?** **$\Rightarrow$Use vectorize operations (and broadcasting) to compute the circumference and area of circles with radius specified in `r`**<jupyter_code>r_circ = None
r_area = None
#plot_r()<jupyter_output><empty_output><jupyter_text>**$\Rightarrow$Access the element at row index 2, column index 1 of `M1`** **$\Rightarrow$Slice the array `M1` below to get the required sub-array**```array([[45, 48, 65, 68, 68],       [10, 84, 22, 37, 88],       [71, 89, 89, 13, 59]])``` ** OPTIONAL $\Rightarrow$Use the jupyter magic command `%timeit` to compare the efficiency of calculating the sum in three ways**1. a for loop2. the Python built-in `sum()`3. the Numpy vectorized `np.sum()`<jupyter_code>arr = rs.randint(0, 100, 1000000)<jupyter_output>1000 loops, best of 3: 439 µs per loop
{'fraction_non_alphanumeric': 0.12666666666666668, 'fraction_numerical': 0.043555555555555556, 'mean_word_length': 4.727735368956743, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '69815', 'license': 'MIT', 'n_tokens_mistral': 829, 'n_tokens_neox': 730, 'n_words': 275, 'path': 'pynotes/e12_Numpy_Operations.ipynb', 'repo_name': 'meng234/ds_python_1', 'size': '2270'}