<jupyter_start><jupyter_text>The BasicsNumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of non-negative integers. In NumPy dimensions are called axes.For example, the coordinates of a point in 3D space `[1, 2, 1]` has one axis. That axis has 3 elements in it, so we say it has a length of 3. In the example pictured below, the array has 2 axes. The first axis has a length of 2, the second axis has a length of 3.<jupyter_code>[[ 1., 0., 0.],
 [ 0., 1., 2.]]<jupyter_output><empty_output><jupyter_text>NumPy’s array class is called `ndarray`. It is also known by the alias `array`. Note that `numpy.array` is not the same as the Standard Python Library class `array.array`, which only handles one-dimensional arrays and offers less functionality. The more important attributes of an `ndarray` object are:<jupyter_code>import numpy as np
my_array = np.arange(15).reshape(3, 5)

print(my_array)
print(type(my_array))<jupyter_output><empty_output><jupyter_text>ndarray.ndimthe number of axes (dimensions) of the array.<jupyter_code>print(my_array.ndim)<jupyter_output><empty_output><jupyter_text>ndarray.shapethe dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix with n rows and m columns, `shape` will be `(n,m)`. The length of the `shape` tuple is therefore the number of axes, `ndim`.<jupyter_code>print(my_array.shape)<jupyter_output><empty_output><jupyter_text>ndarray.sizethe total number of elements of the array. This is equal to the product of the elements of `shape`.<jupyter_code>print(my_array.size)<jupyter_output><empty_output><jupyter_text>ndarray.dtypean object describing the type of the elements in the array. One can create or specify dtype’s using standard Python types. Additionally NumPy provides types of its own. numpy.int32, numpy.int16, and numpy.float64 are some examples.<jupyter_code>print(my_array.dtype.name)<jupyter_output><empty_output><jupyter_text>ndarray.itemsizethe size in bytes of each element of the array. For example, an array of elements of type `float64` has `itemsize` 8 (=64/8), while one of type `complex32` has `itemsize` 4 (=32/8). It is equivalent to `ndarray.dtype.itemsize`.<jupyter_code>print(my_array.itemsize)<jupyter_output><empty_output><jupyter_text>ndarray.datathe buffer containing the actual elements of the array. Normally, we won’t need to use this attribute because we will access the elements in an array using indexing facilities.<jupyter_code>print(my_array.data)<jupyter_output><empty_output><jupyter_text>Array CreationThere are several ways to create arrays.For example, you can create an array from a regular Python list or tuple using the `array` function. The type of the resulting array is deduced from the type of the elements in the sequences.<jupyter_code>my_int_array = np.array([2,3,4])

print(my_int_array)

print(my_int_array.dtype)
my_float_array = np.array([1.2, 3.5, 5.1])

print(my_float_array)

print(my_float_array.dtype)<jupyter_output><empty_output><jupyter_text>> A frequent error consists in calling `array` with multiple arguments, rather than providing a single sequence as an argument.<jupyter_code># wrong_array = np.array(1,2,3,4)    # WRONG

right_array = np.array([1,2,3,4])  # RIGHT<jupyter_output><empty_output><jupyter_text>> `array` transforms sequences of sequences into two-dimensional arrays, sequences of sequences of sequences into three-dimensional arrays, and so on.<jupyter_code>my_array = np.array([(1.5,2,3), (4,5,6)])

print(my_array)<jupyter_output><empty_output><jupyter_text>> The type of the array can also be explicitly specified at creation time:<jupyter_code>my_array = np.array( [ [1,2], [3,4] ], dtype=complex )

print(my_array)

print(my_array.dtype)<jupyter_output><empty_output><jupyter_text>np.zeros(), np.ones(), np.empty()Often, the elements of an array are originally unknown, but its size is known. Hence, NumPy offers several functions to create arrays with initial placeholder content. These minimize the necessity of growing arrays, an expensive operation.> The function `zeros` creates an array full of zeros, the function `ones` creates an array full of ones, and the function `empty` creates an array whose initial content is random and depends on the state of the memory. By default, the dtype of the created array is float64.<jupyter_code>my_zeros_array = np.zeros((3, 4))

print(my_zeros_array)
my_ones_array = np.ones((2,3,4), dtype=np.int16)                # dtype can also be specified

print(my_ones_array)
my_empty_array = np.empty((2,3))                                 # uninitialized

print(my_empty_array)<jupyter_output><empty_output><jupyter_text>np.arange()To create sequences of numbers, NumPy provides the `arange` function which is analogous to the Python built-in `range`, but returns an array.<jupyter_code>my_array = np.arange(10, 30)

print(my_array)
my_array = np.arange(0, 2, 0.3)                 # it accepts float arguments

print(my_array)<jupyter_output><empty_output><jupyter_text>np.linspace()When `arange` is used with floating point arguments, it is generally not possible to predict the number of elements obtained, due to the finite floating point precision. For this reason, it is usually better to use the function `linspace` that receives as an argument the number of elements that we want, instead of the step:<jupyter_code>my_array = np.linspace(0, 2, 9)                 # 9 numbers from 0 to 2
np.linspace()
print(my_array)
my_array = np.linspace( 0, 2*np.pi, 100 )        # useful to evaluate function at lots of points

f = np.sin(my_array)

print(f)<jupyter_output><empty_output><jupyter_text>Printing ArraysWhen you print an array, NumPy displays it in a similar way to nested lists, but with the following layout:* the last axis is printed from left to right,* the second-to-last is printed from top to bottom,* the rest are also printed from top to bottom, with each slice separated from the next by an empty line.One-dimensional arrays are then printed as rows, bidimensionals as matrices and tridimensionals as lists of matrices.<jupyter_code>my_array = np.arange(6)                         # 1d array

print(my_array)
my_array = np.arange(12).reshape(4,3)           # 2d array

print(my_array)
my_array = np.arange(24).reshape(2,3,4)         # 3d array

print(my_array)<jupyter_output><empty_output><jupyter_text>> If an array is too large to be printed, NumPy automatically skips the central part of the array and only prints the corners:<jupyter_code>my_array = np.arange(10000).reshape(100,100)

print(my_array)<jupyter_output><empty_output><jupyter_text>> To disable this behaviour and force NumPy to print the entire array, you can change the printing options using `set_printoptions`.<jupyter_code>import sys
np.set_printoptions(threshold=sys.maxsize)       # sys module should be imported

my_array = np.arange(10000).reshape(100,100)

print(my_array)<jupyter_output><empty_output><jupyter_text>Basic OperationsArithmetic operators on arrays apply elementwise. A new array is created and filled with the result.<jupyter_code>my_array00 = np.array([[20,30,40,50],
                       [60,70,80,90]])

my_array01 = np.arange(1,9).reshape((2,4))

print(my_array00, my_array01, sep='\n\n')
sum_result = my_array00 + my_array01

print('sum is:', sum_result,'\n', sep='\n')
minus_result = my_array00 - my_array01

print('minus is:', minus_result, sep='\n')
double_result = my_array00 * 2

print('double is:', double_result,'\n', sep='\n')
sin_result = np.sin(my_array00)

print('sin is:', sin_result,'\n', sep='\n')
comparison_result = my_array00 > 30

print('comparsion is:', comparison_result, sep='\n')<jupyter_output><empty_output><jupyter_text>> Unlike in many matrix languages, the product operator `*` operates elementwise in NumPy arrays. The matrix product can be performed using the `@` operator (in python >=3.5) or the `dot` function or method:<jupyter_code>my_array00 = np.array([[1,1],
                       [0,1]])

my_array01 = np.array([[2,0],
                       [3,4]])
elementwise_product_result = my_array00 * my_array01                  # elementwise product

print('elementwise product:', elementwise_product_result,'\n', sep='\n')
matrix_product_result00 = my_array00 @ my_array01                       # matrix product

print('matrix product:', matrix_product_result00,'\n', sep='\n')
matrix_product_result01 = my_array00.dot(my_array01)                    # another matrix product

print('matrix product:', matrix_product_result01, sep='\n')<jupyter_output><empty_output><jupyter_text>> Some operations, such as `+=` and `*=`, act in place to modify an existing array rather than create a new one.<jupyter_code>my_array00 = np.ones((2,3), dtype=float)

my_array01 = np.random.randint(10,size=(2,3))

print('my_array00 dtype is: ',my_array00.dtype,'\n')

print('my_array01 dtype is: ',my_array01.dtype,'\n')

print(my_array00,my_array01, sep='\n\n')
my_array00 += my_array01

print('\nmy_array00 is:', my_array00, sep='\n')<jupyter_output><empty_output><jupyter_text>> When operating with arrays of different types, the type of the resulting array corresponds to the more general or precise one (a behavior known as upcasting).<jupyter_code>my_array01 += my_array00                            # b is not automatically converted to integer type

print('my_array01 is:', my_array00, sep='\n')<jupyter_output><empty_output><jupyter_text>unary operationsMany unary operations, such as computing the sum of all the elements in the array, are implemented as methods of the `ndarray` class.<jupyter_code>my_array = np.random.random((2,3))

print(my_array)
sum_of_array_elements = my_array.sum()

print('sum of array elements is {}'.format(sum_of_array_elements))
min_of_array_elements = my_array.min()

print('min of array elements is {}'.format(min_of_array_elements))
max_of_array_elements = my_array.max()

print('max of array elements is {}'.format(max_of_array_elements))<jupyter_output><empty_output><jupyter_text>> By default, these operations apply to the array as though it were a list of numbers, regardless of its shape. However, by specifying the axis parameter you can apply an operation along the specified `axis` of an array:<jupyter_code>my_array = np.arange(12).reshape(3,4)

print(my_array)
columns_sum = my_array.sum(axis=0)                            # sum of each column

print('columns sum is: {}'.format(columns_sum))
row_cumulative_sum = my_array.cumsum(axis=1)                         # cumulative sum along each row

print('cumulative sum along each row:\n {}'.format(row_cumulative_sum))<jupyter_output><empty_output><jupyter_text>Universal FunctionsNumPy provides familiar mathematical functions such as sin, cos, and exp. In NumPy, these are called “universal functions”(`ufunc`). Within NumPy, these functions operate elementwise on an array, producing an array as output.<jupyter_code>my_array00 = np.arange(5)

print('array00 is: ', my_array00,'\n')

my_array01 = np.array([2., -1., 4., 3., 6.])

print('array01 is: ', my_array01,'\n')
my_array_exp = np.exp(my_array00)

print('array exp is: ', my_array_exp,'\n')
my_array_sqrt = np.sqrt(my_array00)

print('array sqrt is: ', my_array_sqrt,'\n')
array_add = np.add(my_array00, my_array01)

print('sum of arrays is: ', array_add,'\n')<jupyter_output><empty_output><jupyter_text>Indexing, Slicing and Iterating**One-dimensional** arrays can be indexed, sliced and iterated over, much like lists and other Python sequences.<jupyter_code>my_array = np.arange(10)**3

print(my_array)
print(my_array[3])
print(my_array[2:5])
my_array[:6:2] = 1000

print(my_array)
print(my_array[::-1])
for i in my_array:
    print(i)<jupyter_output><empty_output><jupyter_text>**Multidimensional** arrays can have one index per axis. These indices are given in a tuple separated by commas:<jupyter_code>def f(x,y):
    return 10*x+y
my_array = np.fromfunction(f,(5,4),dtype=int)

print(my_array)
print(my_array[0,2])
print(my_array[0:5, 1])                       # each row in the second column of b
print(my_array[ : ,1])                        # equivalent to the previous example
print(my_array[1:3, : ])                      # each column in the second and third row of b<jupyter_output><empty_output><jupyter_text>> When fewer indices are provided than the number of axes, the missing indices are considered complete slices:<jupyter_code>print(my_array[-1])                                  # the last row. Equivalent to b[-1,:]<jupyter_output><empty_output><jupyter_text>The expression within brackets in `b[i]` is treated as an `i` followed by as many instances of `:` as needed to represent the remaining axes. NumPy also allows you to write this using dots as `b[i,...]`.The dots (`...`) represent as many colons as needed to produce a complete indexing tuple. For example, if x is an array with 5 axes, then* `x[1,2,...]` is equivalent to `x[1,2,:,:,:]`,* `x[...,3]` to `x[:,:,:,:,3]` and* `x[4,...,5,:]` to `x[4,:,:,5,:]`.<jupyter_code>my_array = np.array( [[[  0,  1,  2],               # a 3D array (two stacked 2D arrays)
                       [ 10, 12, 13]],
                      [[100,101,102],
                       [110,112,113]]])

print(my_array)
print(my_array.shape)
print(my_array[1,...])                                   # same as c[1,:,:] or c[1]
print(my_array[...,2])                                   # same as c[:,:,2]<jupyter_output><empty_output><jupyter_text>**Iterating** over multidimensional arrays is done with respect to the first axis:<jupyter_code>my_array = np.arange(12).reshape(3,4)
for row in my_array:
    print(row)<jupyter_output><empty_output><jupyter_text>However, if one wants to perform an operation on each element in the array, one can use the `flat` attribute which is an iterator over all the elements of the array:<jupyter_code>for i in my_array.flat:
    print(i)<jupyter_output><empty_output><jupyter_text>Shape Manipulation Changing the shape of an arrayAn array has a shape given by the number of elements along each axis:<jupyter_code>my_array = np.arange(12).reshape(3,4)

print(my_array)
print(my_array.shape)<jupyter_output><empty_output><jupyter_text>The shape of an array can be changed with various commands. Note that the following three commands all return a modified array, but do not change the original array:<jupyter_code>my_flated_array = my_array.ravel()  # returns the array, flattened

print(my_flated_array)
my_reshaped_array = my_array.reshape(6,2)  # returns the array with a modified shape

print(my_reshaped_array)
my_transposed_array = my_array.T  # returns the array, transposed

print(my_transposed_array)
print('shape of orginal array is: ',my_array.shape)
print('shape of transposed array is: ',my_transposed_array.shape)<jupyter_output><empty_output><jupyter_text>The order of the elements in the array resulting from ravel() is normally “C-style”, that is, the rightmost index “changes the fastest”, so the element after a[0,0] is a[0,1]. If the array is reshaped to some other shape, again the array is treated as “C-style”. NumPy normally creates arrays stored in this order, so ravel() will usually not need to copy its argument, but if the array was made by taking slices of another array or created with unusual options, it may need to be copied. The functions ravel() and reshape() can also be instructed, using an optional argument, to use FORTRAN-style arrays, in which the leftmost index changes the fastest. The `reshape` function returns its argument with a modified shape, whereas the `ndarray.resize` method modifies the array itself:<jupyter_code>my_array = np.arange(12).reshape(3,4)

print(my_array)<jupyter_output><empty_output><jupyter_text>If a dimension is given as -1 in a reshaping operation, the other dimensions are automatically calculated:<jupyter_code>my_array = my_array.reshape(4,-1)

print(my_array)
my_array.reshape(2,6)

print(my_array)
my_array.resize(2,6)

print(my_array)<jupyter_output><empty_output><jupyter_text>Stacking together different arraysSeveral arrays can be stacked together along different axes:<jupyter_code>my_array00 = np.arange(0,4).reshape(2,2)

print(my_array00)
my_array01 = np.arange(4,8).reshape(2,2)

print(my_array01)
vstacked_array = np.vstack((my_array00,my_array01))

print(vstacked_array)
hstacked_array = np.hstack((my_array00,my_array01))

print(hstacked_array)<jupyter_output><empty_output><jupyter_text>Copies and ViewsWhen operating and manipulating arrays, their data is sometimes copied into a new array and sometimes not. This is often a source of confusion for beginners. There are three cases:  No Copy at AllSimple assignments make no copy of objects or their data.<jupyter_code>my_array00 = np.array([[ 0,  1,  2,  3],
                       [ 4,  5,  6,  7],
                       [ 8,  9, 10, 11]])
my_array01 = my_array00            # no new object is created
print(my_array01 is my_array00)           # my_array00 and my_array01 are two names for the same ndarray object<jupyter_output><empty_output><jupyter_text>View or Shallow CopyDifferent array objects can share the same data. The `view` method creates a new array object that looks at the same data.<jupyter_code>my_array02 = my_array00.view()

print(my_array02)
print(my_array02 is my_array00)           # my_array00 and my_array02 are two names for the same ndarray object
my_array02.base is my_array00                        # my_array02 is a view of the data owned by my_array00
print(my_array02.flags.owndata)
my_array02 = my_array02.reshape((2, 6))                      # my_array00's shape doesn't change

print('shape of my_array00 is: ', my_array00.shape)

print('shape of my_array02 is: ', my_array02.shape)
my_array00[0,0] = 1000

print(my_array02)<jupyter_output><empty_output><jupyter_text>Deep CopyThe copy method makes a complete copy of the array and its data.<jupyter_code>my_array03 = my_array00.copy()

print(my_array03)

print(my_array03 is my_array00)           # my_array00 and my_array03 are two names for the same ndarray object
my_array03.base is my_array00                        # my_array03 is a view of the data owned by my_array00
my_array03[0,0] = 0

print(my_array00,'\n')

print(my_array03)<jupyter_output><empty_output><jupyter_text>Sometimes copy should be called after slicing if the original array is not required anymore. For example, suppose a is a huge intermediate result and the final result b only contains a small fraction of a, a deep copy should be made when constructing b with slicing:<jupyter_code>my_array00 = np.arange(int(1e8))
my_array01 = my_array00[:100].copy()
del my_array00  # the memory of ``my_array00`` can be released.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10167234767788666, 'fraction_numerical': 0.028014486578610994, 'mean_word_length': 4.838619402985074, 'pattern_counts': {'":': 0, '<': 161, '<?xml version=': 0, '>': 174, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '40', 'dir': 'jupyter-structured-clean-dedup', 'id': '496278', 'license': 'MIT', 'n_tokens_mistral': 6313, 'n_tokens_neox': 5968, 'n_words': 1937, 'path': 'Week04/Numpy.ipynb', 'repo_name': 'njiix/py4ds', 'size': '18776'}