Back to Cheatsheets

numpy

Table of Contents

1. GETTING STARTED

2. ARRAYS

3. STATISTICS

4. PLOTS

GETTING STARTED

Import

import numpy as np

Read CSV

np.genfromtxt('file_name.csv', delimiter=',')

ARRAYS

Create

np.array([#1, #2, #3])
OR 
np.array(list_name)

Element-wise operations

np_array_name + #

Combine

new_np_array = np_array_1 + np_array_2 + np_array_3

2D

np.array([np_array_1, 
         np_array_2,
         np_array_3])

Select

Select from 1D

np_array_name[index]

Select from 2D

np_array_name[row,column]

Logical Operations

array_name[array_name > 5]
array_name[(array_name > 5) | (array_name < 2)]

STATISTICS

MEAN

Average

np.mean(survey_array)
OR
np.average(array_nums)

Logical statements

np.mean(array_name op #)

2D Arrays

np.mean(2D_array_name, axis=#)

VARIATION

Variance

np.var(dataset)

Standard Deviation

np.std(dataset)

Range

np.amin(dataset)
np.amax(dataset)

Outliers

np.sort(datset)

QUANTILES

np.quantile(dataset, [list of quantiles])
Quantiles Split Arg
2-quantile (median) 2 groups 0.5
quartiles 4 groups [0,25, 0.5, 0.75]
quintiles 5 groups [0.2, 0.4, 0.6, 0.8]
deciles 10 [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
percentiles 100 [0.01, 0.02,….]

Median

np.median(array_nums)

PERCENTILES

np.percentile(patrons, 30)

QUARTILES

np.quantile(dataset, Q#)
OR 
np.percentile(dataset, %#)

HISTOGRAM

np.histogram(array_name, range= (min, max), bins = #)

STATISTICAL DISTRIBUTIONS

Normal Distribution

np.random.normal(mean, std, size=#_of_random)