Numpy
integers = np.array([[1, 2, 3], [4, 5, 6]]) floats = np.array([0.0, 0.1, 0.2, 0.3, 0.4]) input: integers.shape floats.shape
(2, 3) -> integers.shape, 2 rows 3 columns (5,) -> floats.shape, 5 elements in array
.size .itemsize
.size returns the number of elements in array (just count number of elements in array to get answer) .itemsize returns the number of bytes required to store each element
Calculation methods
.sum, .min, .maxm .std, .mean, .var These methods ignore the array's shape and use all the elements in the calculations. Each is a functional-style programming reduction
array([[ 87, 96, 70], [100, 87, 90], [ 94, 77, 90], [100, 81, 82]]) grades[0, 1], row zero column 1
96
Broadcasting
Arithmetic operations require as operands two arrays of the same size and shape. numbers * 2 is equivalent to numbers * [2, 2, 2, 2, 2] for a 5-element array. Applying the operation to every element is called broadcasting. Also can be applied between arrays of different sizes and shapes, enabling some concise and powerful manipulations.
Comparing arrays
Can compare arrays with individual values and with other arrays. Comparisons performed element-wise. Produce arrays of Boolean values in which each element's True or False value indicates the comparison result
Axis
Each 2D+ array has one axis per dimension In a 2D array
axis = 1
In a 2D array, axis=1 indicates calculations should be row-by-row
.reshape vs. .resize
Method reshape returns a view (shallow copy) of the original array with new dimensions. Does not modify the original array. Method resize modifies the original array's shape, this alters the original array.
array vs. list
Most array operations execute significantly faster than corresponding list operations
Trailing zeros?
Numpy does not display trailing zeros
One dimensional Indexing
One-dimensional arrays can be indexed and sliced like lists.
numbers = np.array([2, 3, 5, 7, 11]) Input: numbers
Output: array([ 2, 3, 5, 7, 11])
np.linspace
Produce evenly spaced floating-point ranges with NumPy's linspace function. Ending value is included in the array, unlike a range
Input: np.array([[1, 2, 3], [4, 5, 6]])
This is a multideminsional array: Output: array([[1, 2, 3], [4, 5, 6]])
id(array name here)
Use built-in id function to see that numbers and numbers2 are different objects
Shallow copies: .view
Views "see" the data in other objects, rather than having their own copies of the data Views are shallow copies array method *view** returns a new array object with a view of the original array object's data. AFFECTS ORINGINAL ARRAY
Displaying Large arrays
When displaying an array, if there are 1000 items or more, NumPy drops the middle rows, columns or both from the output
.reshape
array method reshape transforms an array into different number of dimensions. New shape must have the same number of elements as the original
numbers2 = np.linspace(1.1, 5.5, 5) array([1.1, 2.2, 3.3, 4.4, 5.5]) numbers = np.arange(1, 6) array([1, 2, 3, 4, 5]) numbers * numbers2
array([ 1.1, 4.4, 9.9, 17.6, 27.5])
Array Operations numbers = np.arange(1, 6) numbers array([1, 2, 3, 4, 5]) numbers *2 -> does not change numbers array, numbers array is still [1,2,3,4,5] numbers **3 -> does not change numers array numbers += 10 -> changes numbers array, assigns it new values numbers *= 2 -> this changes numbers array as well
array([ 2, 4, 6, 8, 10]) array([ 1, 8, 27, 64, 125], dtype=int32) array([11, 12, 13, 14, 15]) ** array([ 2, 4, 6, 8, 10])**
np.arange(5)
array([0, 1, 2, 3, 4])
np.arange(0, 10, 3)
array([0, 3, 6, 9])
np.linspace(0.0, 1.0, num=5)
array([0. , 0.25, 0.5 , 0.75, 1. ]), five elements in array
np.zeros(5)
array([0., 0., 0., 0., 0.])
np.arange(10, 1, -2)
array([10, 8, 6, 4, 2])
np.arange(5, 10)
array([5, 6, 7, 8, 9])
grades -> array([[ 87, 96, 70], [100, 87, 90], [ 94, 77, 90], [100, 81, 82]]) grades.mean(axis=0) grades.mean(axis=1)
array([95.25, 85.25, 83. ]) array([84.33333333, 92.33333333, 87. , 87.66666667])
numbers array([1, 2, 3, 4, 5]) numbers >= 13
array([False, False, False, False, False])
numbers -> array([1, 2, 3, 4, 5]) numbers2 -> array([1.1, 2.2, 3.3, 4.4, 5.5]) numbers2 < numbers numbers == numbers2 numbers == numbers
array([False, False, False, False, False]) array([False, False, False, False, False]) array([ True, True, True, True, True])
large array: np.arange(1, 100001).reshape(100, 1000)
array([[ 1, 2, 3, ..., 998, 999, 1000], [ 1001, 1002, 1003, ..., 1998, 1999, 2000], [ 2001, 2002, 2003, ..., 2998, 2999, 3000], ..., [ 97001, 97002, 97003, ..., 97998, 97999, 98000], [ 98001, 98002, 98003, ..., 98998, 98999, 99000], [ 99001, 99002, 99003, ..., 99998, 99999, 100000]]) *middle rows, colums, or both are dropped*
np.ones((2, 4), dtype=int)
array([[1, 1, 1, 1], [1, 1, 1, 1]]) - need to specify dtype = int to prevent the aray from filling with 1.'s
test = np.array ([1,2,3,4]) test.reshape(2,2)
array([[1, 2], [3, 4]])
np.full((3, 5), 13) .full allows you to fill array with any specified value, 0's, 1's, 23's, etc.
array([[13, 13, 13, 13, 13], [13, 13, 13, 13, 13], [13, 13, 13, 13, 13]]) - don't need dtype?
flatten vs. ravel
both will turn a multidimensional array into 1 dimensional. Flattened deep copies and does not modify original array. ravel alters hte original array since it shares the data and since it is view of the data.
.shape
contains a tuple specifying an array's dimensions
.ndim
contains an array's number of dimensions
Iterate though 1 dimensional array: floats = np.array([0.0, 0.1, 0.2, 0.3, 0.4])
for i in floats: print(i, end = ", ") output: 0.0, 0.1, 0.2, 0.3, 0.4, (trailing zeros seen here)
terate through a multidimensional array as if it were one-dimensional by using flat: integers = np.array([[1, 2, 3], [4, 5, 6]])
for i in integers.flat: print(i, end=' ') output: 1 2 3 4 5 6
Iterating through multi dimensional array: integers = np.array([[1, 2, 3], [4, 5, 6]])
for row in integers: for column in row: print(column, end=' ') print() output: 1 2 3 4 5 6
import numpy as np
imports numpy library
axis = 0
indicates calculations should be column-by-column, in 2d array
Input: floats = np.array([0.0, 0.1, 0.2, 0.3, 0.4])
no trailing zeros! output: array([0. , 0.1, 0.2, 0.3, 0.4])
np.arange(1, 21).reshape(4, 5)
np.arange(1,21) -> array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]) np.arange(1,21).reshape(4,5) -> array([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]])
create an array
numbers = np.array([2, 3, 5, 7, 11]) x = np.array([.....])
numbers = np.array([2, 3, 5, 7, 11]) type(numbers)
numpy.ndarray
Determining an array's element type: .dtype floats = np.array([0.0, 0.1, 0.2, 0.3, 0.4]) integers = np.array([[1, 2, 3], [4, 5, 6]]) inputs: floats.dtype integers.dtype
output: dtype('float64') and dtype('int32')
integers = np.array([[1, 2, 3], [4, 5, 6]]) input: integers.ndim
output: 2 this array is 2 dimensions
type(arrayNameHere)
shows type of whatever is put into parenthasis
.T
transposes array, flips columns to rows (3,2) goes to (2,3)
.copy vs .view
view returns a shallow copy of array, if you assign view to a variable, than any edits made to array eill affect other array. .copy returns a deep copy, does not affect other arrays