# Numpy Flashcards

## Taken from https://jakevdp.github.io/PythonDataScienceHandbook/02.02-the-basics-of-numpy-arrays.html

What is common acronym for numpy when importing?

import numpy as np

What is numpy short for?

Numerical Python

What does numpy provide?

An efficient interface to store and operate on dense data buffers.

How are numpy arrays better than Python lists - 1?

NumPy arrays are like Python’s built-in list type, but NumPy arrays provide much more efficient storage and data operations as the arrays grow larger in size.

What is the difference between a dynamic-type list and a fixed-type (numpy style) array?

At the implementation level, the array essentially contains a single pointer to one contiguous block of data. The Python list, on the other hand, contains a pointer to a block of pointers, each of which in turn points to a full Python object

Can numpy arrays contain elements of mixed type?

No. All elements must be the same.

How do you create a fixed-type array in Python?

import array

L = list(range(10))

A = array.array(‘i’, L)

returns: array(‘i’, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

where ‘i’ is type code indicating integers

How do you create an numpy array from Python list?

np.array([1, 4, 2, 5, 3])

What happens when creating numpy arrays which are not of the same type?

Numpy will attempt to upcast if possible. ints upcast to floats

How do we explicitly set the data type of an numpy array?

np.array([1, 2, 3, 4], dtype=’float32’)

Is it more efficient to create a numpy array from scratch or from existing list?

From scratch - especially for larger arrays

Create numpy array all zeros

np.zeros(10, dtype=int) # 10 indicates 10 values

Create numpy array all ones

np.ones(10, dtype=int) # 10 indicates 10 values

Create numpy array with 1 specific value

np.full(10, 3.14, dtype=int) # 10 indicates 10 values, 3.14 is the value to include

Create multi-dimensional numpy array

np.zeros((3,5), dtype=float) # create a 3x5 array poulated with zeros in float type

What is syntax for numpy array filled with a linear sequence?

np.arange(0, 20, 2) # values from 0 up to, not including 20, step by 2

What is syntax for numpy array of x values equally spaced btw two values?

np.linspace(0, 1, 5)

where 0 and 1 are bounding values, and 5 = x

How do you create a numpy array of 3x3 random ints between 0 and 10?

np.random.randint(0, 10, (3, 3))

What are the attributes of an numpy array, where array = x3.

x3. ndim = number of dimensions

x3. shape = shape of the array

x3. size = number of elements in array

x3. dtype = datatype

How do you access the i-th element in a 1 dimensional numpy array x?

x[i] - same as for Python lists

How do you access the elements in a multi-dimensional numpy array x?

x[2,0] # will return 1 for below array

array([[3, 5, 2, 4],

[7, 6, 8, 8],

[1, 6, 7, 7]])