Indexing and Slicing NumPy Arrays
1-D Array
Accessing 1-D Array (Indexing and Slicing), we can have both positive and negative indexing
import numpy as np
colors = np.array(["red","blue","yellow","orange"])
colors
OUTPUT:
array(['red', 'blue', 'yellow', 'orange'], dtype='<U6')
colors[-3]
OUTPUT:
'blue'
-5 -4 -3 -2 -1
0 1 2 3 4
a, b, c, d, e
arr = np.arange(10,20)
arr
OUTPUT:
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
print("FIRST Element is ",arr[0])
OUTPUT:
FIRST Element is 10
arr[0:4]
OUTPUT:
array([10, 11, 12, 13])
print("5th to 8th elements are",arr[5:8])
OUTPUT:
5th to 8th elements are [15 16 17]
arr = np.arange(10,20)
arr
OUTPUT:
array([10,11,12,13,14,15,16,17,18,19])
print("with step 2 :",arr[0:9:2])
OUTPUT:
with step 2 : [10 12 14 16 18]
array[start : end : jump ]
print("Last element:",arr[-1])
OUTPUT:
Last element: 19
print("All the elements except the last one ",arr[0:-2])
OUTPUT:
All the elements except the last one [10 11 12 13 14 15 16 17]
Changing the values of an array
arr=np.array([1,2,3,4,5,6,7,8,9,10])
arr[0]= 99
arr
OUTPUT:
array([99, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Copying one array to another using simple assignment
arr1 = np.array([1, 2, 3, 4])
print("First array:",arr1)
arr2 = arr1
print("After copying ,second array:",arr2)
arr1[0] =500
print("After changing a value in arr1")
print("arr1:",arr1)
print("arr2:",arr2)
OUTPUT:
First array: [1 2 3 4]
After copying ,second array: [1 2 3 4]
After changing a value in arr1
arr1: [500 2 3 4]
arr2: [500 2 3 4]
Copy one array to another using copy function
import numpy as np
arr1 = np.array([1, 2, 3, 4])
print("First array:",arr1)
arr2 = arr1.copy()
print("After copying ,second array:",arr2)
arr2[0] = 100
print("After changing a value in arr1")
print("arr1:",arr1)
print("arr2:",arr2)
OUTPUT:
First array: [1 2 3 4]After copying ,second array: [1 2 3 4]After changing a value in arr1arr1: [1 2 3 4]arr2: [100 2 3 4]
Filtering data
score= np.array([34,56,78,94,29,56,79])
passing_score = score > 80
score[passing_score]
OUTPUT:
array([94])
Functions in Numpy
import numpy as np
A=np.array([4,16,36,49])
A
OUTPUT:
array([ 4, 16, 36, 49])
np.std(A)
OUTPUT:
17.41228014936585
arr = np.array([3.4, 5.6, 2.1, 6.9])
print(np.ceil(arr))
print(np.floor(arr))
OUTPUT:
[4. 6. 3. 7.]
[3. 5. 2. 6.]
import numpy as np
A=np.array([4,7,3,4,2,8])
print("Maximum Value:",np.max(A))
print("Mainimum Value:",np.min(A))
print("Average:",np.mean(A))
print("Square root:",np.sqrt(A))
print("Median:",np.median(A))
print("Standard Deviation:",np.std(A))
OUTPUT:
Maximum Value: 8
Mainimum Value: 2
Average: 4.666666666666667
Square root: [2. 2.64575131 1.73205081 2. 1.41421356 2.82842712]
Median: 4.0
Standard Deviation: 2.134374745810949
arr = np.array([3.4,5.6,2.1,6.9])
print("floor function")
print (np.floor(arr))
OUTPUT:
floor function[3. 5. 2. 6.]
Lesson Assignment
Challenge yourself with our lab assignment and put your skills to test.
# Python Program to find the area of triangle
a = 5
b = 6
c = 7
# Uncomment below to take inputs from the user
# a = float(input('Enter first side: '))
# b = float(input('Enter second side: '))
# c = float(input('Enter third side: '))
# calculate the semi-perimeter
s = (a + b + c) / 2
# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)