NumPy Flashcards
What is NumPy and why is it useful?
- A powerful Python library for scientific computing
- It provides a multidimensional array object, and allows fast operations on arrays including mathematical, logical, array manipulation, sorting and selecting
- Mathematical operations are up to 50x faster on ndarray objects and require fewer lines of code than Python lists.
- It contains built-in functions for working with arrays and maths, such as linear algebra and array transformations.
What are Numpy arrays?
Numpy arrays are the central data structure of the NumPy library. They are a grid of values which may have one or more dimensions:
Vectors: one dimension arrays
Matrices: two-dimensional arrays (2D) represent matrices.
Tensors: higher dimensional arrays
NumPy array creation functions
range, array, copy, empty, eye, linespace, ones, zeros
NumPy conversions
astype, mat
NumPy manipulations
array_split, column_stack, concatenate, hsplit, hstack, reshape, vsplit, vstack
NumPy questions
all, any, nonzero, where
NumPy ordering
argmax, argmin, max, min, sort
NumPy Operators
ndarray.fill, prod, sum
NumPy Statistics
mean, std, var
Get help on Numpy
np.lookfor(‘search’)
Array shape, dimensions, and size in bytes
arr.shape
arr.ndim
arr.itemsize
2.Create a 5X2 integer array from a range between 100 to 200, such that the difference between each element is 10
arr = np.arange(100,200,10).reshape((5, 2))
sample_array = np.array([[11 ,22, 33], [44, 55, 66], [77, 88, 99]])
Return array of items by taking the third column from all rows
sample_array[:,2]
4.Create a result array by adding the following two NumPy arrays.
# Next, modify the result array by calculating the square of each element.
GIVEN
arrayOne = np.array([[5, 6, 9], [21 ,18, 27]])
arrayTwo = np.array([[15 ,33, 24], [4 ,7, 1]])
arrayThree = arrayOne + arrayTwo
arraySquared = arrayThree ** 2
Split an array into four equal-sized sub-arrays
arr = np.arange(24).reshape(8,3)
new_arr = np.array_split(arr,4)