Element extraction using the Conditional and Boolean operators
Apart from using the numerical indexes we can use the conditions and Boolean operators to selectively extract the elements in an array. In the following example we'll select all values that are less than 0.5 in a matrix containing random numbers between 0 and 1. See the program below:
import numpy as np
arr1 = np.random.random((3, 3))
print(arr1)
print('\n')
print(arr1<0.5)
print('\n')
print(arr1[arr1<0.5])
The output of the program is:
[[0.08995485 0.45831529 0.59991243]
[0.99256889 0.31161011 0.24247865]
[0.8438997 0.20548216 0.15781144]]
[[ True True False]
[False True True]
[False True True]]
[0.08995485 0.45831529 0.31161011 0.24247865 0.20548216 0.15781144]
------------------
(program exited with code: 0)
Press any key to continue . . .
In the program we first defined a matrix of random numbers and printed the matrix. Next we applied an operator condition and received as a return value, a Boolean array containing true values in the positions in which the condition arr1<0.5 is satisfied. But we are interested in the extraction of elements smaller than 0.5 and the Boolean arrays are used implicitly for making selections of parts of
arrays, thus we inserted our condition directly inside the square brackets and extract all elements smaller than 0.5, so as to obtain a new array as shown in the third output.
Changing shapes of array
1. reshape()
Using this function it is possible to convert a one-dimensional array into a matrix. We have used this function in previous programs and the following program also shows how to use it:
import numpy as np
arr1 = np.random.random(9)
print(arr1)
print('\n')
print(arr1.reshape(3,3))
The output of the program is:
[0.29559686 0.78175579 0.68994419 0.15964917 0.60401985 0.50051105
0.5012908 0.13863332 0.72451119]
[[0.29559686 0.78175579 0.68994419]
[0.15964917 0.60401985 0.50051105]
[0.5012908 0.13863332 0.72451119]]
------------------
(program exited with code: 0)
Press any key to continue . . .
The reshape() function returns a new array and can thus create new objects.
2. Modifying the shape attribute
It is also possible to modify the object by modifying it's shape attribute. This is done by assigning a
tuple containing the new dimensions directly to its shape attribute as show in the following program:
import numpy as np
arr1 = np.random.random(12)
print(arr1)
print('\n')
arr1.shape = (3,4)
print(arr1)
The output of the program is:
[0.00256283 0.22191957 0.05906521 0.97575381 0.34349577 0.8302148
0.263992 0.70762576 0.89476555 0.95956978 0.5096924 0.01297329]
[[0.00256283 0.22191957 0.05906521 0.97575381]
[0.34349577 0.8302148 0.263992 0.70762576]
[0.89476555 0.95956978 0.5096924 0.01297329]]
------------------
(program exited with code: 0)
Press any key to continue . . .
In this case no object is returned as the original array changes it's shape.
3. The ravel() function
To convert a two-dimensional array into a one-dimensional array, use the ravel() function as shown in the following program:
import numpy as np
arr1 = np.random.random(12).reshape(3,4)
print('The original array\n')
print(arr1)
print('\nThe converted array\n')
arr1 = arr1.ravel()
print(arr1)
The output of the program is:
The original array
[[0.09085803 0.78986751 0.81665587 0.98706791]
[0.75774707 0.75674414 0.17217261 0.28350861]
[0.8894444 0.70953447 0.90111085 0.68554993]]
The converted array
[0.09085803 0.78986751 0.81665587 0.98706791 0.75774707 0.75674414
0.17217261 0.28350861 0.8894444 0.70953447 0.90111085 0.68554993]
------------------
(program exited with code: 0)
Press any key to continue . . .
Other way to convert the array is using the shape attribute as shown in the program:
import numpy as np
arr1 = np.random.random(12).reshape(3,4)
print('The original array\n')
print(arr1)
print('\nThe converted array\n')
arr1.shape =(12)
print(arr1)
The output of the program is:
The original array
[[0.93676251 0.87865295 0.64477846 0.50678966]
[0.07506221 0.44684496 0.06758759 0.70553111]
[0.25710731 0.42245553 0.5651178 0.82364247]]
The converted array
[0.93676251 0.87865295 0.64477846 0.50678966 0.07506221 0.44684496
0.06758759 0.70553111 0.25710731 0.42245553 0.5651178 0.82364247]
------------------
(program exited with code: 0)
Press any key to continue . . .
4. The transpose() function
This function is used in transposing a matrix, which is basically inverting the columns with the rows. See the following program:
import numpy as np
arr1 = np.random.random(9).reshape(3,3)
print('The original Matrix is \n')
print(arr1)
print('\nThe transposed Matrix is\n')
print(arr1.transpose())
The output of the program is:
The original Matrix is
[[5.46213281e-04 8.42534325e-01 8.56678343e-01]
[3.34582626e-01 3.34379643e-01 4.54937957e-01]
[2.46151757e-01 4.48300885e-01 7.83050542e-01]]
The transposed Matrix is
[[5.46213281e-04 3.34582626e-01 2.46151757e-01]
[8.42534325e-01 3.34379643e-01 4.48300885e-01]
[8.56678343e-01 4.54937957e-01 7.83050542e-01]]
------------------
(program exited with code: 0)
Press any key to continue . . .
Here I am ending today's post. In the next post we shall further explore NumPy library and discuss about Array manipulation. Till we meet next keep practicing and learning Python as Python is easy to learn!
Apart from using the numerical indexes we can use the conditions and Boolean operators to selectively extract the elements in an array. In the following example we'll select all values that are less than 0.5 in a matrix containing random numbers between 0 and 1. See the program below:
import numpy as np
arr1 = np.random.random((3, 3))
print(arr1)
print('\n')
print(arr1<0.5)
print('\n')
print(arr1[arr1<0.5])
The output of the program is:
[[0.08995485 0.45831529 0.59991243]
[0.99256889 0.31161011 0.24247865]
[0.8438997 0.20548216 0.15781144]]
[[ True True False]
[False True True]
[False True True]]
[0.08995485 0.45831529 0.31161011 0.24247865 0.20548216 0.15781144]
------------------
(program exited with code: 0)
Press any key to continue . . .
In the program we first defined a matrix of random numbers and printed the matrix. Next we applied an operator condition and received as a return value, a Boolean array containing true values in the positions in which the condition arr1<0.5 is satisfied. But we are interested in the extraction of elements smaller than 0.5 and the Boolean arrays are used implicitly for making selections of parts of
arrays, thus we inserted our condition directly inside the square brackets and extract all elements smaller than 0.5, so as to obtain a new array as shown in the third output.
Changing shapes of array
1. reshape()
Using this function it is possible to convert a one-dimensional array into a matrix. We have used this function in previous programs and the following program also shows how to use it:
import numpy as np
arr1 = np.random.random(9)
print(arr1)
print('\n')
print(arr1.reshape(3,3))
The output of the program is:
[0.29559686 0.78175579 0.68994419 0.15964917 0.60401985 0.50051105
0.5012908 0.13863332 0.72451119]
[[0.29559686 0.78175579 0.68994419]
[0.15964917 0.60401985 0.50051105]
[0.5012908 0.13863332 0.72451119]]
------------------
(program exited with code: 0)
Press any key to continue . . .
The reshape() function returns a new array and can thus create new objects.
2. Modifying the shape attribute
It is also possible to modify the object by modifying it's shape attribute. This is done by assigning a
tuple containing the new dimensions directly to its shape attribute as show in the following program:
import numpy as np
arr1 = np.random.random(12)
print(arr1)
print('\n')
arr1.shape = (3,4)
print(arr1)
The output of the program is:
[0.00256283 0.22191957 0.05906521 0.97575381 0.34349577 0.8302148
0.263992 0.70762576 0.89476555 0.95956978 0.5096924 0.01297329]
[[0.00256283 0.22191957 0.05906521 0.97575381]
[0.34349577 0.8302148 0.263992 0.70762576]
[0.89476555 0.95956978 0.5096924 0.01297329]]
------------------
(program exited with code: 0)
Press any key to continue . . .
In this case no object is returned as the original array changes it's shape.
3. The ravel() function
To convert a two-dimensional array into a one-dimensional array, use the ravel() function as shown in the following program:
import numpy as np
arr1 = np.random.random(12).reshape(3,4)
print('The original array\n')
print(arr1)
print('\nThe converted array\n')
arr1 = arr1.ravel()
print(arr1)
The output of the program is:
The original array
[[0.09085803 0.78986751 0.81665587 0.98706791]
[0.75774707 0.75674414 0.17217261 0.28350861]
[0.8894444 0.70953447 0.90111085 0.68554993]]
The converted array
[0.09085803 0.78986751 0.81665587 0.98706791 0.75774707 0.75674414
0.17217261 0.28350861 0.8894444 0.70953447 0.90111085 0.68554993]
------------------
(program exited with code: 0)
Press any key to continue . . .
Other way to convert the array is using the shape attribute as shown in the program:
import numpy as np
arr1 = np.random.random(12).reshape(3,4)
print('The original array\n')
print(arr1)
print('\nThe converted array\n')
arr1.shape =(12)
print(arr1)
The output of the program is:
The original array
[[0.93676251 0.87865295 0.64477846 0.50678966]
[0.07506221 0.44684496 0.06758759 0.70553111]
[0.25710731 0.42245553 0.5651178 0.82364247]]
The converted array
[0.93676251 0.87865295 0.64477846 0.50678966 0.07506221 0.44684496
0.06758759 0.70553111 0.25710731 0.42245553 0.5651178 0.82364247]
------------------
(program exited with code: 0)
Press any key to continue . . .
4. The transpose() function
This function is used in transposing a matrix, which is basically inverting the columns with the rows. See the following program:
import numpy as np
arr1 = np.random.random(9).reshape(3,3)
print('The original Matrix is \n')
print(arr1)
print('\nThe transposed Matrix is\n')
print(arr1.transpose())
The output of the program is:
The original Matrix is
[[5.46213281e-04 8.42534325e-01 8.56678343e-01]
[3.34582626e-01 3.34379643e-01 4.54937957e-01]
[2.46151757e-01 4.48300885e-01 7.83050542e-01]]
The transposed Matrix is
[[5.46213281e-04 3.34582626e-01 2.46151757e-01]
[8.42534325e-01 3.34379643e-01 4.48300885e-01]
[8.56678343e-01 4.54937957e-01 7.83050542e-01]]
------------------
(program exited with code: 0)
Press any key to continue . . .
Here I am ending today's post. In the next post we shall further explore NumPy library and discuss about Array manipulation. Till we meet next keep practicing and learning Python as Python is easy to learn!
0 comments:
Post a Comment