<jupyter_start><jupyter_text>How`numpy` is a large library with incredible functionality all of which will not belisted here. Instead some specific tools will be described so as to give asufficient understanding of the capabilities of the library. How to create an arrayTo create a `numpy` array we use the `numpy.array` tool which takes any iterable.````{tip}```numpy.array(iterable)```````For example:<jupyter_code>import numpy as np

array = np.array((0, 12, 3, 1))
array<jupyter_output><empty_output><jupyter_text>How to create a given number of values between two bounds`numpy` has a popular tool to create a linear space: `numpy.linspace` which takea lower bound, an upper bounds and a number. It returns the given number of pointsuniformly spaced between the bounds.````{tip}```numpy.linspace(lower_bound, upper_bound, number)```````For example:<jupyter_code>import numpy as np

np.linspace(10, 400, 4)<jupyter_output><empty_output><jupyter_text>How to create an array of zerosTo create an array of zeros we can use the `numpy.zeros` tool. It takes anargument for the dimension of the array. This can either be a single number inwhich case a single dimensional array is created or a tuple with multipledimensions.````{tip}```numpy.zeros(size)```````For example:<jupyter_code>import numpy as np

np.zeros(4)<jupyter_output><empty_output><jupyter_text>Or:<jupyter_code>np.zeros((3, 5))<jupyter_output><empty_output><jupyter_text>How to generate random arrays`numpy` has a powerful random number generator. It can be accessed from`numpy.random` and has multiple tools. The simplest of which is`numpy.random.random`. This takes an optional argument that is the dimension ofthe array.````{tip}```numpy.random.random(size)```````For example:<jupyter_code>import numpy as np

np.random.random()<jupyter_output><empty_output><jupyter_text>Or:<jupyter_code>import numpy as np

np.random.random((3, 5))<jupyter_output><empty_output><jupyter_text>How to index arraysMultiple dimensional arrays can be indexed in a natural mathematical way using`array[position]`.````{tip}```array[position]```````For example:<jupyter_code>import numpy as np

array = np.array(((1, 2, 3), (4, 5, 6)))
array[1, 2]<jupyter_output><empty_output><jupyter_text>How to do arithmetic on arraysIt is possible to directly do arithmetic on arrays in a mathematical way.````{tip}```array1 + array2```````For example to do element wise multiplication:<jupyter_code>array1 = np.array((1, 2, 3, 4))
array2 = np.array((2, 0, -1, 0.5))
array1 * array2<jupyter_output><empty_output><jupyter_text>How to invert a matrix`numpy` can invert a matrix using `np.linalg.inv`. There are numerous otherlinear algebraic tools available in `np.linalg`.````{tip}```np.linalg.inv(array)```````For example:<jupyter_code>import numpy as np

matrix = np.array(((1, 2), (3, 1)))
matrix_inverse = np.linalg.inv(matrix)
matrix_inverse<jupyter_output><empty_output><jupyter_text>We can confirm the expected result:<jupyter_code>matrix @ matrix_inverse<jupyter_output><empty_output><jupyter_text>How to raise a matrix to a powerUsing the usual operator for exponentiation `**` with a `numpy` array carriesout element wise exponentiation. To raise a matrix to a power we use`np.linalg.matrix_power`. This takes an array and the exponent.````{tip}```np.linalg.ing(array, exponent)```````For example:<jupyter_code>import numpy as np

matrix = np.array(((1, 2), (3, 1)))
np.linalg.matrix_power(matrix, 3)<jupyter_output><empty_output><jupyter_text>How to fit a line of best fit`numpy` can be used to fit polynomials to a points. This is done with the`numpy.polyfit` tool. This takes an array of x values, an array of y values anda degree. It returns the coefficients of a polynomial of given degree that bestapproximates $f(x)=y$.````{tip}```np.polyfit(x, y, degree)```````For example, the code below creates `y` using a quadratic and recovers thecoefficients of the quadratic:<jupyter_code>x = np.array((1, 2, 3, 4))
y = 2 * x ** 2 + 3 * x + 1
a, b, c = np.polyfit(x, y, 2)
a, b, c<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12549407114624506, 'fraction_numerical': 0.01408102766798419, 'mean_word_length': 6.56822429906542, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '55419', 'license': 'MIT', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1301, 'n_words': 442, 'path': 'book/further-information/01-numpy/how/.main.md.bcp.ipynb', 'repo_name': 'daffidwilde/pfm', 'size': '4048'}