NumPy - Array Manipulation
NumPy Arrays
The learning objectives of this section are:
- Manipulate arrays
- Reshape and Resize arrays
- Stack arrays
- Transposing
- Swapaxis
- Perform operations on arrays
- Perform basic mathematical operations
- Broadcast Numpy
- Apply built-in functions
- Expressing Conditional Logic
Reshaping of an array, changing the shape of an array
import numpy as np
arr = np.array([[1,2,3,4],[5,6,7,8], [9,10,11,12]])
print(arr)
OUTPUT:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
print(arr.shape)
OUTPUT:
(3,4)
sh_arr=arr.reshape(12,1)
sh_arr
OUTPUT:
array([[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10],
[11],
[12]])
numpy.resize, this function returns a new array with the specified size.
import numpy as np
arr = np.array([[1,2,3,4],[5,6,7,8]])
arr
OUTPUT:
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
arr.resize(6,6,refcheck=False)
arr
OUTPUT:
array([[1, 2, 3, 4, 5, 6],
[7, 8, 0, 0, 0,0],
[0, 0, 0, 0, 0,0],
[0, 0, 0, 0, 0,0],
[0, 0, 0, 0, 0,0],
[0, 0, 0, 0, 0,0]])
Stacking Arrays: np.hstack()
and n.vstack()
Stacking is done using the np.hstack()
and np.vstack()
methods. For horizontal stacking, the number of rows should be the same, while for vertical stacking, the number of columns should be the same.
a = np.array([1, 2, 3,4,5])
b = np.array([4, 5, 6,7,8])
c = np.array([4, 5, 6])
np.hstack([a,b,c])
OUTPUT:
array([1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 4, 5, 6])
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.array([4, 5, 6])
np.vstack((a,b,c))
OUTPUT:
array([[1, 2, 3],
[4, 5, 6],
[4, 5, 6]])
Flatten an array, returns a copy of the array collapsed into one dimension
a=np.array([[1,2,3,4],[5,6,7,8]])
a
OUTPUT:
array([[1, 2, 3, 4], [5, 6, 7, 8]])
a.shape
OUTPUT:
(2,4)
flat_array = a.flatten()
flat_array
OUTPUT:
array([1, 2, 3, 4, 5, 6, 7, 8])
flat_array.shape
OUTPUT:
(8,)
Transpose of an array,the transpose of a matrix is obtained by moving the rows data to the column and columns data to the rows.
Transpose of 2-D an array
arr = np.array([[1,2,3,4],[5,6,7,8]])
arr
OUTPUT:
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
arr.shape
OUTPUT:
(2,4)
arr1= arr.transpose(1,0)
arr1.shape
OUTPUT:
(4,2)
arr1
OUTPUT:
array([[1, 5],
[2, 6],
[3, 7],
[4, 8]])
Mathematical Operations
arr = np.arange(10).reshape((5,2))
arr
OUTPUT:
array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
np.sum(arr)
OUTPUT:
45
print(np.sum(arr,axis =0)) ## rows
OUTPUT:
[20 25]
print(np.sum(arr,axis =1))
OUTPUT:
[ 1 5 9 13 17]
Expressing conditional Logic as array operation
a = np.arange(10)
a
OUTPUT:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# condition , yes , no
b = np.where(a > 5, a, 10*a)
b
OUTPUT:
array([ 0, 10, 20, 30, 40, 50, 6, 7, 8, 9])
Sorting an array
arr = np.array([3,4,1,5,9,6,2])
print(arr)
arr_sorted = sorted(arr,reverse=False)
print(arr_sorted)
OUTPUT:
[3 4 1 5 9 6 2]
[1, 2, 3, 4, 5, 6, 9]
(Arithmetic Operations) Broadcasting
array1 = np.array([10,20,30,40,50])
array2 = np.array([2])
print(array1/array2)
OUTPUT:
[ 5. 10. 15. 20. 25.]
array1 = np.array([[10,20,30,40,50],[10,20,30,40,50]])
array2 = np.array([1,2,3,4,5])
print(array1+array2)
OUTPUT:
[[11 22 33 44 55]
[11 22 33 44 55]]
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)
Sign up to get access to our code lab and run this code.
Try Now for Free