# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [default]
#     language: python
#     name: python3
# ---

# # Numpy II
# ---

import numpy as np

help(np.reshape)

np.arange(12).shape

np.arange(12)

np.arange(12).reshape(3, 4)

# *reshapes one array to another with given dimension in row, column format*

np.arange(12).reshape(3, 2, 2)

arr = np.arange(12).reshape(3, 4)

arr

# ### Indexing

arr[0]

arr[0][-1]

# *Numpy has another syntax for accessing multidimensional array more elegantly*
#
# `arr[row, column]`

arr[0, -1]

# > *Which is same as `arr[0][-1]`*

arr[-1, -1]



# ### Slicing

# get all items from 3rd row
arr[2]

# get all items from 3rd row
arr[2, :]

# get all items from 3rd row except first
arr[2, 1:]

# get all items from 3rd row except last
arr[2, :-1]

arr

# get all items from 2nd column
arr[:, 1]

# get all items from 2nd column except first
arr[1:, 0]

# get all items from 2nd column except last
arr[:-1, 1]

# get 2nd and 3rd column items from 2nd and 3rd row
arr[1:3, 1:3]



# ### Assigning values to array

arr

arr[0, 0] = 20

arr

arr[1, 1:]

arr[1, 1:] = [55, 66, 77]

arr

arr[1:3, 1:3] = [[555, 666], [999, 100]]

arr

# **slices are references**

ab = arr[1, 2:]

ab

ab[0] = 667

arr

# > *`667` is reflected on `arr`, because `ab` is just a reference to slice of `arr`*



# ### Three dimensional array

arr3 = np.arange(24).reshape(2, 3, 4)
arr3

# > *`depth, rows, columns`*

# **Indexing**
#
# `arr3[depth, row, column]`

arr3[1]

arr3[1, 0]

arr3[1, 0, 3]

# **slicing**

arr3[1, 1:, :-1]

arr3[:, 1:, :-1]



# ### Fancy Indexing

arr

# select 2nd and 3rd row
arr[[1, 2]]

# select 1st, 3rd and 4th item of 2nd row
arr[1, [0, 2, 3]]

# select 1st and 4th item of 2nd and 3rd column respectively
arr[[0, -1], [0, -1]]

arr[::2, [0, -1]]



# ### Boolean operation and Masking

abc = np.arange(30)
abc

abc < 10

# > *gives us boolean array, with True on each element which satifies the condition*

# *Only select that satisfy condition*

abc[abc < 10]

abc[(abc < 10) | (abc > 20)]

# use __&__ for "_and_", __|__ for "_or_". Each condition must be enclosed in brackets*

# *each condition should be enclosed in brackets, to remove ambiguity created by operator precedence*

abc[abc % 2 == 0]



# ## Numpy Methods

abc

# sum of all elements
abc.sum()

# product
abc.prod()

abc[(abc > 1) & (abc < 10)].prod()

# mean
abc.mean()

# median
np.median(abc)

# maximum
abc.max()

# minimum
abc.min()

# standard deviation
abc.std()

# variance
abc.var()

# cumulative sum
abc.cumsum()

# index of minimum value
abc.argmin()

# index of maximum value
abc.argmax()

# **argmin/argmax in multidimensional array**

arr

# index of minimum value as if given array is flat
arr.argmin()

np.unravel_index(arr.argmin(), arr.shape)

help(np.unravel_index)

np.unravel_index(6, arr.shape)

np.unravel_index(6, (4, 2))

# > return tuple is index within multidimensional array, ie. 0th row and 1st column

# *sum across rows or across columns*

arr

# across rows
arr.sum(axis=0)

# across columns
arr.sum(axis=1)

# *can be used with other methods as well*

arr.mean(axis=1)



StarcoderdataJupyterScriptsDedupFiltered