Numpy arrays are a great tool to handle and analyze numerical data in Python. In the last article, we discussed different ways to create a numpy array. In this article, we will discuss various numpy array operations with which you can analyze numerical data in Python.
- Arithmetic Operations on a Numpy Array in Python
- Comparison Operations With a Numpy Array in Python
- Find the Minimum and Maximum Element in a Numpy Array
- Find the Index of Minimum and Maximum Element in a Numpy Array
- Sort a Numpy Array in Python
- Slice a Numpy Array in Python
- Slice a 2-D Numpy Array in Python
- Reshape a Numpy Array in Python
- Copy a Numpy Array in Python
- Conclusion
Arithmetic Operations on a Numpy Array in Python
We can perform arithmetic operations on numpy arrays in Python in the easiest manner. If you want to add a number to all the elements of a numpy array, you can simply add the number to the array itself. The python interpreter broadcasts the arithmetic operation to all the elements in the array and the given number is added to all the array elements. Remember that the original array isn’t modified during the arithmetic operation. Hence, you need to store the result of the arithmetic operation in the desired variable. You can observe this in the following example.
import numpy as np
arr1=np.array([1,2,3,4,5,6,7])
print("The original array is:",arr1)
number=10
arr=arr1+10
print("The modified array is:",arr)
Output:
The original array is: [1 2 3 4 5 6 7]
The modified array is: [11 12 13 14 15 16 17]
Here, you can see that we have added the number 10 to the array. After execution of the addition statement, the result is broadcasted to each element and the values increase by 10.
You can also perform subtraction, multiplication, and division of array elements by a number using the same syntax.
import numpy as np
arr1=np.array([1,2,3,4,5,6,7])
print("The original array is:",arr1)
number=10
arr=arr1*10
print("The modified array is:",arr)
Output:
The original array is: [1 2 3 4 5 6 7]
The modified array is: [10 20 30 40 50 60 70]
Numpy also allows you to perform arithmetic operations between elements of two arrays. You can perform arithmetic operations on the elements of one array with the elements of another array as if you are performing the same operations on single elements.
For instance, if you want to add the elements of an array to elements of another array, you can do it as follows.
import numpy as np
arr1=np.array([1,2,3,4,5,6,7])
print("The first array is:",arr1)
arr2=np.array([8,9,10,11,12,13,14])
print("The second array is:",arr2)
arr=arr1+arr2
print("The output array is:",arr)
Output:
The first array is: [1 2 3 4 5 6 7]
The second array is: [ 8 9 10 11 12 13 14]
The output array is: [ 9 11 13 15 17 19 21]
Here, we have added two arrays. After addition, the elements of one array are added to the element at the same position in another array.
If there are different number of elements in the arrays that are being added, the program might run into error. Hence, you need to make sure that the arrays have the same shape. Otherwise, the program will run into a ValueError exception as shown below.
import numpy as np
arr1=np.array([1,2,3,4,5,6,7])
print("The first array is:",arr1)
arr2=np.array([8,9,10,11,12,13])
print("The second array is:",arr2)
arr=arr1+arr2
print("The output array is:",arr)
Output:
The first array is: [1 2 3 4 5 6 7]
The second array is: [ 8 9 10 11 12 13]
ValueError: operands could not be broadcast together with shapes (7,) (6,)
Just like addition, you can perform subtraction, multiplication, and division between elements of two numpy arrays as shown below.
import numpy as np
arr1=np.array([1,2,3,4,5,6,7])
print("The first array is:",arr1)
arr2=np.array([8,9,10,11,12,13,14])
print("The second array is:",arr2)
arr=arr1*arr2
print("The output array is:",arr)
Output:
The first array is: [1 2 3 4 5 6 7]
The second array is: [ 8 9 10 11 12 13 14]
The output array is: [ 8 18 30 44 60 78 98]
Here, we have performed multiplication between two arrays. You can also perform other arithmetic operation as per your requirement.
Comparison Operations With a Numpy Array in Python
We can compare elements of a numpy array with another element in one go. This looks as simple as comparing two numbers. For instance, you can compare if array elements of a numpy array are greater than a number or not as shown below.
import numpy as np
arr1=np.array([1,2,3,4,5,6,7])
print("The array is:",arr1)
number=5
arr=arr1>number
print("The output array is:",arr)
Output:
The array is: [1 2 3 4 5 6 7]
The output array is: [False False False False False True True]
Here, we get a numpy array of boolean values after the comparison. First, the elements of the input array are compared with the given element. For each element in the numpy array, the output of the comparison operation is stored in the output numpy array. For the elements that are greater than 5, we get True in the output array. Elements in the output array corresponding to the elements in the original array that are less than or equal to 5 are False.
You can also compare two numpy arrays element-wise using the comparison operators. In this case, the elements in an array are compared to the element at the same position in another array. The output of the comparison is returned in another array as shown below.
import numpy as np
arr1=np.array([1,2,3,4,5,6,7])
print("The first array is:",arr1)
arr2=np.array([8,9,10,11,12,13,14])
print("The second array is:",arr2)
arr=arr1>arr2
print("The output array is:",arr)
Output:
The first array is: [1 2 3 4 5 6 7]
The second array is: [ 8 9 10 11 12 13 14]
The output array is: [False False False False False False False]
Here, each element in the first array is compared to the corresponding element in second array. The result is then stored in the output array.
Again, you need to make sure that the arrays should have equal length. Otherwise, the program will run into ValueError exception.
You can also perform arithmetic and comparison operations on 2-D numpy arrays in a similar manner to the 1-D arrays.
When we perform arithmetic or comparison operations on a 2-D numpy array with a number, the result is broadcasted to each element.
When we perform element wise numpy array operations on 2-D arrays, the operations are performed element wise. Here, you should make sure that the shape of the arrays should be same while performing element wise arithmetic or comparison operations on 2-D numpy arrays.
Find the Minimum and Maximum Element in a Numpy Array
To find the maximum element in a numpy array, you can use the max() method. The max() method, when invoked on a numpy array, returns the maximum element of the array. You can observe this in the following example.
import numpy as np
arr1=np.array([1,2,3,4,5,6,7])
print("The array is:",arr1)
max_element=arr1.max()
print("The maximum element is:",max_element)
Output:
The array is: [1 2 3 4 5 6 7]
The maximum element is: 7
Here, the max() method returns the largest element in the array. You can also use the max() method to find the maximum element in a 2-D numpy array as shown below.
import numpy as np
arr1=np.array([[1,2,3,4,5,6,7],[8,9,10,11,12,13,14]])
print("The array is:",arr1)
max_element=arr1.max()
print("The maximum element is:",max_element)
Output:
The array is: [[ 1 2 3 4 5 6 7]
[ 8 9 10 11 12 13 14]]
The maximum element is: 14
To find the minimum element in a numpy array, you can use the min() method. The min() method, when invoked on a numpy array, returns the minimum element as shown in the following example.
import numpy as np
arr1=np.array([1,2,3,4,5,6,7])
print("The array is:",arr1)
min_element=arr1.min()
print("The minimum element is:",min_element)
Output:
The array is: [1 2 3 4 5 6 7]
The minimum element is: 1
Instead of obtaining the minimum and maximum values in the numpy array, you can obtain the position of minimum and maximum elements. For this, you can use the argmax() and the argmin() method.
Find the Index of Minimum and Maximum Element in a Numpy Array
The argmax() method, when invoked on a numpy array, returns the position of the maximum element in the array as shown below.
import numpy as np
arr1=np.array([1,2,3,4,5,6,7])
print("The array is:",arr1)
max_element=arr1.argmax()
print("The index of maximum element is:",max_element)
Output:
The array is: [1 2 3 4 5 6 7]
The index of maximum element is: 6
Here, you can observe that the largest element is 7 which is at index 6. Hence, the argmax() method returns the value 6.
We can find the index of the minimum element in a numpy array using the argmin() method. The argmin() method, when invoked on a numpy array, returns the position of the minimum element in the array as shown below.
import numpy as np
arr1=np.array([1,2,3,4,5,6,7])
print("The array is:",arr1)
min_element=arr1.argmin()
print("The index of minimum element is:",min_element)
Output:
The array is: [1 2 3 4 5 6 7]
The index of minimum element is: 0
Here, the smallest number is 1 which is at index 0. Hence, the argmin() method returns 0.
For a 2-D array, the argmax() and argmin() methods do not return the exact position of the largest element. Instead, they return the index of the largest element if the 2-D array would have been flattened in row major order.
For instance, 14 is the largest element in the array shown in the following example. Its position is (0, 6). However, the argmax() method returns the value 6. This is due to the reason that the argmax() method returns the index of the element if it were in a flattened array. The argmin() method works in a similar manner to the argmax() method.
import numpy as np
arr1=np.array([[8,9,10,11,12,13,14],[1,2,3,4,5,6,7]])
print("The array is:",arr1)
max_element=arr1.argmax()
print("The index of maximum element is:",max_element)
Output:
The array is: [[ 8 9 10 11 12 13 14]
[ 1 2 3 4 5 6 7]]
The index of maximum element is: 6
Sort a Numpy Array in Python
You can sort a numpy array using the sort() method. The sort() method, when invoked on a numpy array, sorts the elements in the numpy array as shown in the following example.
import numpy as np
arr1=np.array([1,2,17,4,21,6,7])
print("The original array is:",arr1)
arr1.sort()
print("The sorted array is:",arr1)
Output:
The original array is: [ 1 2 17 4 21 6 7]
The sorted array is: [ 1 2 4 6 7 17 21]
Here, you can see that the original array has been sorted. The sort() method returns the value None after execution.
If we use the sort() method on a 2-D numpy array, all the internal arrays are sorted in increasing order as shown below.
import numpy as np
arr1=np.array([[8,9,12,11,27,34,14],[55,2,15,4,22,6,7]])
print("The original array is:",arr1)
arr1.sort()
print("The sorted array is:",arr1)
Output:
The original array is: [[ 8 9 12 11 27 34 14]
[55 2 15 4 22 6 7]]
The sorted array is: [[ 8 9 11 12 14 27 34]
[ 2 4 6 7 15 22 55]]
In the above example, you can observe that the internal 1-D arrays are sorted in ascending order when we invoke the sort() method on a 2-D numpy array.
Slice a Numpy Array in Python
Slicing a numpy array is similar to slicing a list in Python. You can perform slicing operations on a numpy array using the indexing operator. The syntax for slicing a numpy array is as follows.
mySlice=myArray[start_index:end_index:interval]
Here,
- myArray is the existing numpy array.
- mySlice is the newly created slice of myArray.
- The start_index is the index of myArray from which we have to select the element.
- The end_index is the index of myArray till which we have to select the element.
- The interval is the interval between two consecutive elements of myArray that have to be included in mySlice.
You can slice a numpy array using the above syntax as shown below.
import numpy as np
myArr=np.array([1,2,17,4,21,6,7,12,13,14])
print("The original array is:",myArr)
mySlice=myArr[2:6:1]
print("The slice is:",mySlice)
Output:
The original array is: [ 1 2 17 4 21 6 7 12 13 14]
The slice is: [17 4 21 6]
In the above example, we have sliced elements from index 2 to 5 from the original array into mySlice. When you are slicing consecutive elements, you can also omit the last colon and interval from the syntax. The result will be the same. You can observe this in the following example.
import numpy as np
myArr=np.array([1,2,17,4,21,6,7,12,13,14])
print("The original array is:",myArr)
mySlice=myArr[2:6]
print("The slice is:",mySlice)
Output:
The original array is: [ 1 2 17 4 21 6 7 12 13 14]
The slice is: [17 4 21 6]
In the above example, we have omitted the last colon and interval from the slicing syntax. However, the result is similar to the previous example.
If you want to select elements from the beginning to a specific index, you can leave the start value empty as shown below.
import numpy as np
myArr=np.array([1,2,17,4,21,6,7,12,13,14])
print("The original array is:",myArr)
mySlice=myArr[:6:1]
print("The slice is:",mySlice)
Output:
The original array is: [ 1 2 17 4 21 6 7 12 13 14]
The slice is: [ 1 2 17 4 21 6]
In the above example, we have left the start index empty. Due to this, the output slice contains elements from index 0 till the index 5.
To select elements of the array from a specific index till the end, you can leave the end index empty as shown below.
import numpy as np
myArr=np.array([1,2,17,4,21,6,7,12,13,14])
print("The original array is:",myArr)
mySlice=myArr[2::1]
print("The slice is:",mySlice)
Output:
The original array is: [ 1 2 17 4 21 6 7 12 13 14]
The slice is: [17 4 21 6 7 12 13 14]
Here, we have left end index empty. Hence,the output slice contains element from index 2 till last.
In the above examples, the elements are selected from continuous indices. However, you can select elements at certain intervals using the interval value as shown below.
import numpy as np
myArr=np.array([1,2,17,4,21,6,7,12,13,14])
print("The original array is:",myArr)
mySlice=myArr[2::2]
print("The slice is:",mySlice)
Output:
The original array is: [ 1 2 17 4 21 6 7 12 13 14]
The slice is: [17 21 7 13]
In the above example, we have passed the value of interval as 2. Hence, the output slice contains alternate elements from the input array.
Slice a 2-D Numpy Array in Python
You can also perform slicing operations on a 2-D numpy array using the indexing operator. For slicing a 2-D numpy array, we use the following syntax.
mySlice=myArray[start_row:end_row:row_interval,start_column:end_column:column_interval]
Here, the terms start_row, end_row, and row_interval denote the index of the starting row to be included in the slice, the index of the last row to be included in the slice and the interval between two consecutive rows of myArray that are to be included in the slice respectively.
Similarly, the terms start_column, end_column, and column_interval denote the index of the starting column to be included in the slice, the index of the last column to be included in the slice, and the interval between two consecutive columns of myArray that are to be included in the slice respectively.
For instance, you can slice out the first row to the third row and the second column to 4th column of an array as shown below.
import numpy as np
myArr=np.arange(100).reshape((10,10))
print("The original array is:")
print(myArr)
mySlice=myArr[0:4:1,1:5:1]
print("The slice is:")
print(mySlice)
Output:
The original array is:
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[20 21 22 23 24 25 26 27 28 29]
[30 31 32 33 34 35 36 37 38 39]
[40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59]
[60 61 62 63 64 65 66 67 68 69]
[70 71 72 73 74 75 76 77 78 79]
[80 81 82 83 84 85 86 87 88 89]
[90 91 92 93 94 95 96 97 98 99]]
The slice is:
[[ 1 2 3 4]
[11 12 13 14]
[21 22 23 24]
[31 32 33 34]]
In the above example, we have sliced rows from index 0 to 3 and columns from index 1 to 4. Hence, we have got a 4×4 array out of 10×10 array.
If you want to include all the rows but choose only certain columns in the slice, you can leave the start_row and end_row values empty.
import numpy as np
myArr=np.arange(100).reshape((10,10))
print("The original array is:")
print(myArr)
mySlice=myArr[::1,1:5:1]
print("The slice is:")
print(mySlice)
Output:
The original array is:
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[20 21 22 23 24 25 26 27 28 29]
[30 31 32 33 34 35 36 37 38 39]
[40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59]
[60 61 62 63 64 65 66 67 68 69]
[70 71 72 73 74 75 76 77 78 79]
[80 81 82 83 84 85 86 87 88 89]
[90 91 92 93 94 95 96 97 98 99]]
The slice is:
[[ 1 2 3 4]
[11 12 13 14]
[21 22 23 24]
[31 32 33 34]
[41 42 43 44]
[51 52 53 54]
[61 62 63 64]
[71 72 73 74]
[81 82 83 84]
[91 92 93 94]]
In this example, we have selected all the rows but columns have been selected from index 1 to 4. For this, we have left the start and end row indices empty in the slicing syntax.
Similarly, if you want to include all the columns but choose certain rows in the slice, you can leave the start_column and end_column values empty as shown below.
import numpy as np
myArr=np.arange(100).reshape((10,10))
print("The original array is:")
print(myArr)
mySlice=myArr[0:4:1,::1]
print("The slice is:")
print(mySlice)
Output:
The original array is:
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[20 21 22 23 24 25 26 27 28 29]
[30 31 32 33 34 35 36 37 38 39]
[40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59]
[60 61 62 63 64 65 66 67 68 69]
[70 71 72 73 74 75 76 77 78 79]
[80 81 82 83 84 85 86 87 88 89]
[90 91 92 93 94 95 96 97 98 99]]
The slice is:
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[20 21 22 23 24 25 26 27 28 29]
[30 31 32 33 34 35 36 37 38 39]]
In this example, we have selected all the columns but rows have been selected from index 0 to 3. For this, we have left the start and end column indices empty in the slicing syntax.
If you want to include the rows from the beginning to a certain row index in the slice, you can leave start_row empty.
import numpy as np
myArr=np.arange(100).reshape((10,10))
print("The original array is:")
print(myArr)
mySlice=myArr[:4:1,2:5:1]
print("The slice is:")
print(mySlice)
Output:
The original array is:
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[20 21 22 23 24 25 26 27 28 29]
[30 31 32 33 34 35 36 37 38 39]
[40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59]
[60 61 62 63 64 65 66 67 68 69]
[70 71 72 73 74 75 76 77 78 79]
[80 81 82 83 84 85 86 87 88 89]
[90 91 92 93 94 95 96 97 98 99]]
The slice is:
[[ 2 3 4]
[12 13 14]
[22 23 24]
[32 33 34]]
If you want to include rows from a certain row index till last, you can leave end_row empty as shown below.
import numpy as np
myArr=np.arange(100).reshape((10,10))
print("The original array is:")
print(myArr)
mySlice=myArr[3::1,2:5:1]
print("The slice is:")
print(mySlice)
Output:
The original array is:
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[20 21 22 23 24 25 26 27 28 29]
[30 31 32 33 34 35 36 37 38 39]
[40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59]
[60 61 62 63 64 65 66 67 68 69]
[70 71 72 73 74 75 76 77 78 79]
[80 81 82 83 84 85 86 87 88 89]
[90 91 92 93 94 95 96 97 98 99]]
The slice is:
[[32 33 34]
[42 43 44]
[52 53 54]
[62 63 64]
[72 73 74]
[82 83 84]
[92 93 94]]
If you want to include the columns from the beginning to a certain column index in the slice, you can leave start_column empty.
import numpy as np
myArr=np.arange(100).reshape((10,10))
print("The original array is:")
print(myArr)
mySlice=myArr[1:4:1,:5:1]
print("The slice is:")
print(mySlice)
Output:
The original array is:
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[20 21 22 23 24 25 26 27 28 29]
[30 31 32 33 34 35 36 37 38 39]
[40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59]
[60 61 62 63 64 65 66 67 68 69]
[70 71 72 73 74 75 76 77 78 79]
[80 81 82 83 84 85 86 87 88 89]
[90 91 92 93 94 95 96 97 98 99]]
The slice is:
[[10 11 12 13 14]
[20 21 22 23 24]
[30 31 32 33 34]]
If you want to include columns from a certain column index till last, you can leave end_column empty as shown below.
import numpy as np
myArr=np.arange(100).reshape((10,10))
print("The original array is:")
print(myArr)
mySlice=myArr[3::1,4::1]
print("The slice is:")
print(mySlice)
Output:
The original array is:
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[20 21 22 23 24 25 26 27 28 29]
[30 31 32 33 34 35 36 37 38 39]
[40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59]
[60 61 62 63 64 65 66 67 68 69]
[70 71 72 73 74 75 76 77 78 79]
[80 81 82 83 84 85 86 87 88 89]
[90 91 92 93 94 95 96 97 98 99]]
The slice is:
[[34 35 36 37 38 39]
[44 45 46 47 48 49]
[54 55 56 57 58 59]
[64 65 66 67 68 69]
[74 75 76 77 78 79]
[84 85 86 87 88 89]
[94 95 96 97 98 99]]
You can also introduce intervals between selected rows and columns as shown below.
import numpy as np
myArr=np.arange(100).reshape((10,10))
print("The original array is:")
print(myArr)
mySlice=myArr[3::2,4::3]
print("The slice is:")
print(mySlice)
Output:
The original array is:
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[20 21 22 23 24 25 26 27 28 29]
[30 31 32 33 34 35 36 37 38 39]
[40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59]
[60 61 62 63 64 65 66 67 68 69]
[70 71 72 73 74 75 76 77 78 79]
[80 81 82 83 84 85 86 87 88 89]
[90 91 92 93 94 95 96 97 98 99]]
The slice is:
[[34 37]
[54 57]
[74 77]
[94 97]]
Slices are views of the original numpy arrays. Hence, any changes made to the slices are also reflected in the original array. For instance, look at the following example.
import numpy as np
myArr=np.arange(100).reshape((10,10))
print("The original array is:")
print(myArr)
mySlice=myArr[3::2,4::3]
print("The slice is:")
print(mySlice)
mySlice+=100
print("The original array is:")
print(myArr)
Output:
The original array is:
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[20 21 22 23 24 25 26 27 28 29]
[30 31 32 33 34 35 36 37 38 39]
[40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59]
[60 61 62 63 64 65 66 67 68 69]
[70 71 72 73 74 75 76 77 78 79]
[80 81 82 83 84 85 86 87 88 89]
[90 91 92 93 94 95 96 97 98 99]]
The slice is:
[[34 37]
[54 57]
[74 77]
[94 97]]
The original array is:
[[ 0 1 2 3 4 5 6 7 8 9]
[ 10 11 12 13 14 15 16 17 18 19]
[ 20 21 22 23 24 25 26 27 28 29]
[ 30 31 32 33 134 35 36 137 38 39]
[ 40 41 42 43 44 45 46 47 48 49]
[ 50 51 52 53 154 55 56 157 58 59]
[ 60 61 62 63 64 65 66 67 68 69]
[ 70 71 72 73 174 75 76 177 78 79]
[ 80 81 82 83 84 85 86 87 88 89]
[ 90 91 92 93 194 95 96 197 98 99]]
Here, we have made changes to the slice only. However, the changes are also reflected in the original array. This shows that the slices are only views of the original array. Hence, if you want to make any changes to the slices, you should first copy it to an another variable using the copy() method.
Reshape a Numpy Array in Python
You can reshape a numpy array using the reshape() method. The reshape() method can be used in two ways.
First, you can invoke the reshape() method on an array that you want to reshape. The reshape() method, when invoked on a numpy array, takes the number of rows and number of columns in the output array as its first and second input arguments. After execution, it returns the reshaped array as shown in the following example.
import numpy as np
arr1=np.arange(20)
print("The first array is:")
print(arr1)
arr2=arr1.reshape((4,5))
print("The reshaped array is:")
print(arr2)
Output:
The first array is:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
The reshaped array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
In the above example, we have reshaped a 1-D array having 20 elements to a 2-D array having the shape (4, 5).
You can also pass the original array to the reshape() method. In this approach, the reshape() method takes the original array as its first input argument and a tuple containing the number of rows and number of columns as its second input argument. After execution, it returns the reshaped array as shown below.
import numpy as np
arr1=np.arange(20)
print("The first array is:")
print(arr1)
arr2=np.reshape(arr1,(4,5))
print("The reshaped array is:")
print(arr2)
Output:
The first array is:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
The reshaped array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
As long as the total number of elements in the original array is equal to the number of elements in the desired reshaped array, the reshape() method works well. However, if the total number of elements in the original array is less than or greater than the number of elements in the desired output array, the program runs into a ValueError exception. You can observe this in the following example.
import numpy as np
arr1=np.arange(20)
print("The first array is:")
print(arr1)
arr2=np.reshape(arr1,(4,7))
print("The reshaped array is:")
print(arr2)
Output:
ValueError: cannot reshape array of size 20 into shape (4,7)
The array returned by the reshape() method is a view of the original array. Hence, any change made to the new array will result in a modification of the original array. You can observe this in the following example.
import numpy as np
arr1=np.arange(20)
print("The first array is:")
print(arr1)
arr2=np.reshape(arr1,(4,5))
print("The reshaped array is:")
print(arr2)
arr2+=100
print("The first array is:")
print(arr1)
Output:
The first array is:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
The reshaped array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
The first array is:
[100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
118 119]
Here, you can observe that the changes made in the reshaped 2-D array are also visible in the original 1-D array. This proves that the reshaped arrays are just views of the original array. Hence, if you want to make changed to the reshaped array, you should first consider copying it to a new variable if you don’t want to make changes to the original array.
Flatten a Numpy Array in Python
To flatten a numpy array, you can pass the value -1 as a dimension to the reshape() method as shown below.
import numpy as np
arr1=np.arange(20).reshape(4,5)
print("The first array is:")
print(arr1)
arr2=np.reshape(arr1,-1)
print("The flattened array is:")
print(arr2)
Output:
The first array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
The flattened array is:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
Instead of using the reshape() method, you can use the flatten() method to flatten a numpy array. The flatten() method, when invoked on a 2-D numpy array, returns a flattened 1-D view of the array. You can observe this in the following example.
import numpy as np
arr1=np.arange(20).reshape(4,5)
print("The first array is:")
print(arr1)
arr2=arr1.flatten()
print("The flattened array is:")
print(arr2)
Output:
The first array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
The flattened array is:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
The flatten() method returns a copy and not a view. To understand this, look at the following example.
import numpy as np
arr1=np.arange(20).reshape(4,5)
print("The first array is:")
print(arr1)
arr2=arr1.flatten()
print("The flattened array is:")
print(arr2)
arr2+=50
print("The first array is:")
print(arr1)
Output:
The first array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
The flattened array is:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
The first array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
Here, you can see that the changes made to the flattened array are not reflected in the original array. This doesn’t happen in case of the reshape() method.
The reshape() method returns a view of the original array and any changes made to the reshaped array is visible in the original array. You can observe this in the following example.
import numpy as np
arr1=np.arange(20).reshape(4,5)
print("The first array is:")
print(arr1)
arr2=arr1.reshape(-1)
print("The flattened array is:")
print(arr2)
arr2+=50
print("The first array is:")
print(arr1)
Output:
The first array is:
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
The flattened array is:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
The first array is:
[[50 51 52 53 54]
[55 56 57 58 59]
[60 61 62 63 64]
[65 66 67 68 69]]
Hence, you should use the flatten() method instead of the reshape() method to flatten numpy arrays if you want to keep the original array unchanged while modifying the flattened array.
Suggested Reading: If you are into machine learning, you can read this article on regression in machine learning. You might also like this article on k-means clustering with numerical example.
Copy a Numpy Array in Python
As the reshape() method and the flatten() method return a view of the original NumPy array, you cannot modify the views if you don’t want to change the original array. In such a case, you can copy the view to another array using the copy() method.
The copy() method, when invoked on a numpy array, returns a copy of the original array. If you make any changes to the copy array, the changes are not reflected in the original array. You can observe this in the following example.
import numpy as np
arr1=np.arange(20)
print("The first array is:")
print(arr1)
arr2=arr1.copy()
print("The copied array is:")
print(arr2)
Output:
The first array is:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
The copied array is:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
Conclusion
In this article, we have discussed different numpy array operations in Python. To know more about python programming, you can read this article on how to create a pandas dataframe. You might also like this article on string manipulation in Python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.