Numpy#
#
Introduction#
You’ll also need to import numpy to get started:
import numpy as np
Importing/exporting#
- |
- |
|---|---|
|
From a text file |
|
From a CSV file |
|
Writes to a text file |
|
Writes to a CSV file |
Creating Arrays#
- |
- |
|---|---|
|
One dimensional array |
|
Two dimensional array |
|
1D array of length 3 all values 0 |
|
3x4 array with all values 1 |
|
5x5 array of 0 with 1 on diagonal (Identity matrix) |
|
Array of 6 evenly divided values from 0 to 100 |
|
Array of values from 0 to less than 10 with step 3 (eg [0,3,6,9]) |
|
2x3 array with all values 8 |
|
4x5 array of random floats between 0–1 |
|
6x7 array of random floats between 0–100 |
|
2x3 array with random ints between 0–4 |
Inspecting Properties#
- |
- |
|---|---|
|
Returns number of elements in arr |
|
Returns dimensions of arr (rows,columns) |
|
Returns type of elements in arr |
|
Convert arr elements to type dtype |
|
Convert arr to a Python list |
|
View documentation for np.eye |
Copying/sorting/reshaping#
- |
- |
|---|---|
|
Copies arr to new memory |
|
Creates view of arr elements with type dtype |
|
Sorts arr |
|
Sorts specific axis of arr |
|
Flattens 2D array two_d_arr to 1D |
|
Transposes arr (rows become columns and vice versa) |
|
Reshapes arr to 3 rows, 4 columns without changing data |
|
Changes arr shape to 5x6 and fills new values with 0 |
Adding/removing Elements#
- |
- |
|---|---|
|
Appends values to end of arr |
|
Inserts values into arr before index 2 |
|
Deletes row on index 3 of arr |
|
Deletes column on index 4 of arr |
Combining/splitting#
- |
- |
|---|---|
|
Adds arr2 as rows to the end of arr1 |
|
Adds arr2 as columns to end of arr1 |
|
Splits arr into 3 sub-arrays |
|
Splits arr horizontally on the 5th index |
Indexing/slicing/subsetting#
- |
- |
|---|---|
|
Returns the element at index 5 |
|
Returns the 2D array element on index [2][5] |
|
Assigns array element on index 1 the value 4 |
|
Assigns array element on index [1][3] the value 10 |
|
Returns the elements at indices 0,1,2 (On a 2D array: returns rows 0,1,2) |
|
Returns the elements on rows 0,1,2 at column 4 |
|
Returns the elements at indices 0,1 (On a 2D array: returns rows 0,1) |
|
Returns the elements at index 1 on all rows |
|
Returns an array with boolean values |
|
Returns an array with boolean values |
|
Inverts a boolean array |
|
Returns array elements smaller than 5 |
Vector Math#
- |
- |
|---|---|
|
Elementwise add arr2 to arr1 |
|
Elementwise subtract arr2 from arr1 |
|
Elementwise multiply arr1 by arr2 |
|
Elementwise divide arr1 by arr2 |
|
Elementwise raise arr1 raised to the power of arr2 |
|
Returns True if the arrays have the same elements and shape |
|
Square root of each element in the array |
|
Sine of each element in the array |
|
Natural log of each element in the array |
|
Absolute value of each element in the array |
|
Rounds up to the nearest int |
|
Rounds down to the nearest int |
|
Rounds to the nearest int |
Scalar Math#
- |
- |
|---|---|
|
Add 1 to each array element |
|
Subtract 2 from each array element |
|
Multiply each array element by 3 |
|
Divide each array element by 4 (returns np.nan for division by zero) |
|
Raise each array element to the 5th power |
Statistics#
- |
- |
|---|---|
|
Returns mean along specific axis |
|
Returns sum of arr |
|
Returns minimum value of arr |
|
Returns maximum value of specific axis |
|
Returns the variance of array |
|
Returns the standard deviation of specific axis |
|
Returns correlation coefficient of array |