<jupyter_start><jupyter_text>Introduction to NumPy<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>ArraysA NumPy array is a collection of values *of the same type*, arranged along one or more dimensions, and indexed by a tuple of non-negative integers.  One-dimensional arrays (vectors)<jupyter_code>a = np.array([0, 1, 1, 2, 3, 5, 8, 13, 21, 34])
print(a)
a.shape
a.dtype
a[3]
a[3] = 10
print(a)<jupyter_output>[ 0  1  1 10  3  5  8 13 21 34]<jupyter_text>Two-dimensional arrays (matrices)<jupyter_code>b = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float)
print(b)
b.shape
b.dtype
b[1, 0]
b[1, 0] = 10
print(b)<jupyter_output>[[ 1.  2.  3.]
 [10.  5.  6.]]<jupyter_text>Slicing and indexing SlicingNumPy arrays can be sliced using a syntax similar to the one used for Python lists.<jupyter_code>a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a)
b = a[1:, 1::2]
print(b)
b[0, 0] = 100  # b[0, 0] is the same as a[1, 1]
print(a)
print(b)<jupyter_output>[[  1   2   3   4]
 [  5 100   7   8]
 [  9  10  11  12]]
[[100   8]
 [ 10  12]]<jupyter_text>Integer indexingSpecific elements of a NumPy array can be accessed using lists of integer indices.<jupyter_code>a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a)
a[[0, 2], [1, 2]]<jupyter_output><empty_output><jupyter_text>Boolean indexingAlternatively, elements that satisfy certain logical conditions can be selected using Boolean arrays.<jupyter_code>a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a)
a > 5
a[a > 5]<jupyter_output><empty_output><jupyter_text>Functions that create arrays<jupyter_code>np.arange(5)
np.zeros((3, 5))
np.ones((3, 5))
np.full((3, 5), 7)
np.random.random((3, 5))<jupyter_output><empty_output><jupyter_text>Array operatorsBasic mathematical operators such as `+` and `*` operate _element-wise_ on NumPy arrays.<jupyter_code>a = [1, 2, 3]
b = [4, 5, 6]
print(a + b)
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b)
3*a + b**2<jupyter_output><empty_output><jupyter_text>Other array functionsMost functions defined in NumPy operate on arrays.<jupyter_code>a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a)
np.sum(a)
np.mean(a)
np.sum(a, axis=0)
np.sum(a, axis=1)<jupyter_output><empty_output><jupyter_text>BroadcastingBroadcasting allows NumPy to operate on arrays of different shapes when performing some operation.This is particularly useful when we want to use a smaller array multiple times to perform some operation on a larger array.<jupyter_code>a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a)
b = np.arange(a.shape[1])
print(b)
a - b
b = np.arange(a.shape[0])
print(b)
b[:, np.newaxis]
a - b[:, np.newaxis]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16745368688703233, 'fraction_numerical': 0.07119505993461678, 'mean_word_length': 5.0394736842105265, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '352972', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1278, 'n_tokens_neox': 1140, 'n_words': 342, 'path': '01-python-refresher/notebooks/03-numpy.ipynb', 'repo_name': 'estimand/python-for-data-science', 'size': '2753'}