<jupyter_start><jupyter_text>Introduction to `numpy`  Overview- **Teaching:** 10 min- **Exercises:** 10 min**Questions*** * **Objectives*** * *  NumPy ('Numerical Python') is the defacto standard module for doing numerical work in Python. Its main feature is its array data type which allows very compact and efficient storage of homogenous (of the same type) data.A lot of the material in this section is based on [SciPy Lecture Notes](http://www.scipy-lectures.org/intro/numpy/array_object.html) ([CC-by 4.0](http://www.scipy-lectures.org/preface.htmllicense)).As you go through this material, you'll likely find it useful to refer to the [NumPy documentation](https://docs.scipy.org/doc/numpy/), particularly the [array objects](https://docs.scipy.org/doc/numpy/reference/arrays.html) section.As with `pandas` there is a standard convention for importing `numpy`, and that is as `np`:<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>Now that we have access to the `numpy` package we can start using its features. Creating arraysIn many ways a NumPy array can be treated like a standard Python `list` and much of the way you interact with it is identical. Given a list, you can create an array as follows:<jupyter_code>python_list = [1, 2, 3, 4, 5, 6, 7, 8]
numpy_array = np.array(python_list)
print(numpy_array)
# ndim give the number of dimensions
numpy_array.ndim
# the shape of an array is a tuple of its length in each dimension. In this case it is only 1-dimensional
numpy_array.shape
# as in standard Python, len() gives a sensible answer
len(numpy_array)
nested_list = [[1, 2, 3], [4, 5, 6]]
two_dim_array = np.array(nested_list)
print(two_dim_array)
two_dim_array.ndim
two_dim_array.shape<jupyter_output><empty_output><jupyter_text>It's very common when working with data to not have it already in a Python list but rather to want to create some data from scratch. `numpy` comes with a whole suite of functions for creating arrays. We will now run through some of the most commonly used. The first is `np.arange` (meaning "array range") which works in a vary similar fashion the the standard Python `range()` function, including how it defaults to starting from zero, doesn't include the number at the top of the range and how it allows you to specify a 'step:<jupyter_code>np.arange(10) #0 .. n-1  (!)
np.arange(1, 9, 2) # start, end (exclusive), step<jupyter_output><empty_output><jupyter_text>Next up is the `np.linspace` (meaning "linear space") which generates a given floating point numbers starting from the first argument up to the second argument. The third argument defines how many numbers to create:<jupyter_code>np.linspace(0, 1, 6)   # start, end, num-points<jupyter_output><empty_output><jupyter_text>Note how it included the end point unlike `arange()`. You can change this feature by using the `endpoint` argument:<jupyter_code>np.linspace(0, 1, 5, endpoint=False)<jupyter_output><empty_output><jupyter_text>`np.ones` creates an n-dimensional array filled with the value `1.0`. The argument you give to the function defines the shape of the array:<jupyter_code>np.ones((3, 3))  # reminder: (3, 3) is a tuple<jupyter_output><empty_output><jupyter_text>Likewise, you can create an array of any size filled with zeros:<jupyter_code>np.zeros((2, 2))<jupyter_output><empty_output><jupyter_text>The `np.eye` (referring to the matematical identity matrix, commonly labelled as `I`) creates a square matrix of a given size with `1.0` on the diagonal and `0.0` elsewhere:<jupyter_code>np.eye(3)<jupyter_output><empty_output><jupyter_text>The `np.diag` creates a square matrix with the given values on the diagonal and `0.0` elsewhere:<jupyter_code>np.diag([1, 2, 3, 4])<jupyter_output><empty_output><jupyter_text>Finally, you can fill an array with random numbers:<jupyter_code>np.random.rand(4)  # uniform in [0, 1]
np.random.randn(4)  # Gaussian<jupyter_output><empty_output><jupyter_text>Exercise: Name- Experiment with `arange`, `linspace`, `ones`, `zeros`, `eye` and `diag`.- Create different kinds of arrays with random numbers.- Look at the function `np.empty`. What does it do? When might this be useful?  Reshaping arraysBehind the scenes, a multi-dimensional NumPy `array` is just stored as a linear segment of memory. The fact that it is presented as having more than one dimension is simply a layer on top of that (sometimes called a *view*). This means that we can simply change that interpretive layer and change the shape of an array very quickly (i.e without NumPy having to copy any data around).This is mostly done with the `reshape()` method on the array object:<jupyter_code>my_array = np.arange(16)
my_array
my_array.shape
my_array.reshape((2, 8))
my_array.reshape((4, 4))<jupyter_output><empty_output><jupyter_text>Note that if you check, `my_array.shape` will still return `(16,)` as `reshaped` is simply a *view* on the original data, it hasn't actually *changed* it. If you want to edit the original object in-place then you can use the `resize()` method.You can also transpose an array using the `transpose()` method which mirrors the array along its diagonal:<jupyter_code>my_array.reshape((2, 8)).transpose()
my_array.reshape((4,4)).transpose()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09908536585365854, 'fraction_numerical': 0.013147865853658536, 'mean_word_length': 5.686624203821656, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '313160', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1613, 'n_words': 652, 'path': 'notebooks_plain/02_numpy_pt1.ipynb', 'repo_name': 'JDBetteridge/intro-data-plotting_start', 'size': '5248'}