Why is numpy faster?
Python lists hold references to the values they hold (instead of containing the values themselves, they contain locations in memory where the data is stored). That gives us heterogeneity, but that also means we have no guarantee that all (or, at least, enough) of our values are copied to the cache in one operation.
NumPy arrays are homogeneous and store data in sequential chunks of memory*. That means several values can be copied from RAM to the CPU cache at once.
NumPy also supports vectorized operations on the data. That means we don’t need to explicitly loop over elements; get results of our computations faster.
What is vectorization?
vectorization refers to performing operation on several values at once (SIMD)
several values need to be present at once in CPU registers to perform vectorized operations
vanilla Python does not provide vectorization capabilities
NumPy ndarrays store data in contiguous chunks of memory and implement optimizations to perform vectorized operations
Can one ndarray hold different datatypes at once?
No?
Wha happen?
array([0, 0, 0], dtype=uint8)
UFF
How do you check the values of np datatypes?
np.iinfo(arr.dtype/np.int16/…)
array(['andrea', 'dmitri'], dtype='<U6’)
NumPy uses the maximum length of strings present in an array as the upper limit on all values. Be careful!
Use object dtype to store str of arbitrary length
change with arr.astype()
Double Bam
Give the commands for the id matrix, random array and random int array all 3x3.
How do you create ndarrays from iterables?
np.eye(3)
np.random.random((3, 3))
np.random.randint(0, 99, (3, 3))
use np.fromiter()
Create a mask that only selects even numbers
and return a new array with only even numbers
even = (numbers % 2 == 0)
numbers[even]
What is fancy indexing and give an return in a list the diagonal numbers of the matrix below.
numbers =
A “fancy” way to access values in arrays.
[numbers[0,0], numbers[1,1], numbers[2,2]]
How do you find elements in the given arr which are bigger than 15?
arr[np.where(arr > 15)]
What chruch is better?
Matmul & @
or
Dot
matmuuuul & @
What is broadcasting?
Broadcasting allows us to use a smaller array several times together with a larger array according to the following rules:
if the arrays do not have same number of dimensions, prepend 1 to the shape of the smaller one until they do;
arrays are compatible in a dimension if they have the same size in a given dimension OR if the smaller array has size 1;
a smaller array acts as if it was copied along those dimensions where its size is 1.
Does slicing return a copy or a view?
What about reshaping and transposing?
view!
A view is a new array object that refers to the same data
Be careful with reshaping and transposing! This will be a new view object if possible; otherwise, it will be a copy.
BAAAM
Cpt Data
BMW 3x4m hoch
Hayjiaaa
Im loosing iiiit.
Concatenate to concate along exisiting axis
stack to concat along new axis
np.inf BAM
Name the 3 ufuncs we “need” to know.
reduce
accumulate
outer
Krrrrr
Wha happens?
pd.Series has a copy parameter with a default argument set to False. When we pass a list that doesn’t matter, since the values have to be copied anyway. On the other hand, when we pass a NumPy array, we might want to set copy to True to avoid undesirable changes
Missing values can turn our integers into floats
in the dtype specidifcation
django mango
can use .astype to cast
importante
Last changed2 years ago