<jupyter_start><jupyter_text>Review of Numpy  Overview:- **Teaching:** 5 min- **Exercises:** 15 min**Questions**- What is numpy?- How do I create and manipulate arrays with numpy?**Objectives**- Know that `numpy` provides array structures to help improve your code.- Understand that `numpy` contains a rich range of library to create and manipulate arrays.   Numpy Arrays* Standard Python Library provides lists and 1d arrays (array.array)  * Lists are general containers for objects  * Arrays are 1d containers for objects of the same type  * Limited functionality  * Some memory and performance overhead associated with these structures* NumPy provides multidimensional arrays (numpy.ndarray)  * Can store many elements of the same data type in multiple dimensions  * cf. Fortran/C/C++ arrays  * More functionality than core Python e.g. many conveninent methods for array manipulation  * Efficient storage and executionSee, e.g.,* http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.ndarray.html  Creating Arrays (by hand)We can create `numpy` arrays *by hand*, taking a list and and creating a numpy array from it:<jupyter_code>import numpy as np
a = np.array( [-1, 0, 1] )
b = np.array( a )
print(a)
print(b)<jupyter_output>[-1  0  1]
[-1  0  1]<jupyter_text>All NumPy arrays are of `type`, `ndarray`<jupyter_code>type(b)<jupyter_output><empty_output><jupyter_text>Creating arrays (helper functions)However often we want to create large array, that follow a specific sequence or of initial conditions:  Exercise: Create ArraysWhat arrays do the following do:```pythonw = np.arange( -2, 6, 2 )x = np.linspace(-10, 10, 5) y = np.zeros(3)z = np.ones(3)```[Solution]()  Solution+:<jupyter_code># arange for arrays (similar to range for lists)
w = np.arange( -2, 6, 2 )
print(w)
# linspace to create regular sample points in an interval
x = np.linspace(-10, 10, 5) 
print(x)
# Create an array of zeros
y = np.zeros(3)
print(y)
#Create an array of ones
z = np.ones(3)
print(z)<jupyter_output>[1. 1. 1.]<jupyter_text>:Solution+  Attributes of ArraysEvery `numpy` array has attributes that describe e.g. its *dimension* and *shape*<jupyter_code>print("Dimensions ", x.ndim)
print("Shape      ", x.shape)<jupyter_output>Shape       (5,)<jupyter_text>Exercise:What are the following attributes:```pythonx.sizex.dtype```What other attributes can you find for a numpy array (and how)?[Solution]()  Solution+:<jupyter_code>print("Size       ", a.size)
print("Data type  ", a.dtype)<jupyter_output>Data type   int64<jupyter_text>:Solution  Multi-dimensional arraysMany different ways to create N-dimensional arrays. A two-dimensional array or matrix can be created from, e.g., list of lists<jupyter_code>mat = np.array( [[1,2,3], [4,5,6]] )
print(mat)
print("Dimensions: ", mat.ndim)
print("Size:       ", mat.size)
print("Shape:      ", mat.shape)
mat0 = np.zeros( (3,3) )
print(mat0)
mat1 = np.ones( (3,3) )
print(mat0)<jupyter_output>[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
{'fraction_non_alphanumeric': 0.12533602150537634, 'fraction_numerical': 0.022513440860215055, 'mean_word_length': 4.825831702544031, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '494472', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1036, 'n_tokens_neox': 980, 'n_words': 340, 'path': 'nbplain/01_numpy.ipynb', 'repo_name': 'james-grant1/perf-python', 'size': '2976'}