# numpy – INDEXING AND SELECTION, OPERATIONS

1. Grabbing one or more elements
3. Grabbing of 2D array elements
4. Conditional Selection of array elements
5. 1D Array (+-*/) 1D Array,
6. 2D Array -> Overall sum, rowwise sum, columnwise sum
7. Aabhar : Jose Portilla (Head of Data Science at Pierian Training) @Udemy
``````import numpy as np

# SELECTION OF ARRAY ELEMENTS
arr = np.arange(1,11)
print(arr)
# RES -> [ 1  2  3  4  5  6  7  8  9 10]
print(arr)
# RES ->6
print(arr[5:9])  #5th to 9th indexed value
# RES -> [6 7 8 9]
print(arr[5:]) # 5th till end
# RES -> [ 6  7  8  9 10]
print(arr[:5]) # From start till 5th
# RES -> [1 2 3 4 5]

arr[0:3] = 999    # All 3 elements will be now 999 - means values are broadcasted to all the elements which is not same as in list
print(arr)
# RES -> [999 999 999   4   5   6   7   8   9  10]

slicedArr = arr[0:5]
print(slicedArr)
# RES -> [999 999 999   4   5]
slicedArr[:]= np.arange(300,305)
print(slicedArr)
print(arr)
# RES -> [300 301 302 303 304]
#        [300 301 302 303 304   6   7   8   9  10]

sliceArr2 = arr.copy()
sliceArr2[:] = 111
print(sliceArr2)
print(arr)
# RES -> [111 111 111 111 111 111 111 111 111 111]
#        [300 301 302 303 304   6   7   8   9  10]

# -----  2D array selection and indexing
arr2d = np.array([ [5,10,15], [20,25,30], [35,40,45]])
print(arr2d)
# RES -> [[ 5 10 15]
#         [20 25 30]
#         [35 40 45]]
print(arr2d) # 1 row
# RES -> [ 5 10 15]
print(arr2d[0,2]) # 1st row last element - 15
# RES -> 15
print(arr2d[:2])  #Rows upto 2 excluding 2 means leave the 3rd row
# RES -> [[ 5 10 15]
#         [20 25 30]]
print(arr2d[:2, 1]) # Now form the extracted, 1st colum from all rows
# RES -> [10 25]
print(arr2d[:2, 1:]) # Now form the extracted, 1st and all the rest colum from all rows
# RES -> [[10 15]
#        [25 30]]

# CONDITIONAL SELECTION
x = np.arange(1,11)
boolX = x>8
print(boolX)
# RES -> [False False False False False False False False  True  True]
print(x[boolX])   # returns only those values > 8
# RES -> [ 9 10]
print(x[x<5]) # Shortcut way of writing
# RES -> [1 2 3 4]

# OPERATIONS
m = np.array([3,3,4])
print("m is : ", m)
# RES -> m is :  [3 3 4]
print(sum(m), max(m), min(m) , " ---", m.sum(), m.max(), m.min(), m.var(), m.std())
# RES -> 10 4 3  --- 10 4 3 0.22222222222222224 0.4714045207910317
print(m+5, m-m, m/2)
# RES -> [8 8 9] [0 0 0] [1.5 1.5 2. ]
print(np.sqrt(m), np.sin(m), np.log(m))
# RES -> [1.73205081 1.73205081 2.]
#        [ 0.14112001  0.14112001 -0.7568025 ]
#        [1.09861229 1.09861229 1.38629436]

# OPERATIONS on 2D array
m2d = np.arange(6).reshape(3,2)
print(m2d)
# RES -> [[0 1]
#         [2 3]
#         [4 5]]
print("Overall Sum : ", m2d.sum())
# RES -> 15
print("Column-wise Sum : ", m2d.sum(axis=0)) # 0 indicates rows -> Across the rows i.e downward
# RES -> [6 9]
print("Row-wise Sum : ", m2d.sum(axis=1)) # Across the columbs - horizontally
# RES -> [1 5 9]``````