[Day-9]NumPy-The End
Hello everyone! I’m thrilled to announce that I’ve finished reading the beginner’s documentation of NumPy. To ensure thorough understanding, I also consulted multiple YouTube tutorials on NumPy to confirm I’ve covered all the topics they taught. It took me 9 days to complete, and while some might consider it a long time for such a topic, I believe in the value of daily incremental learning and ample practice. Here’s a summary of what I’ve learned today.
- I learnt how to Generate Random Numbers in NumPy.
- How to get unique items and counts using
np.unique()
and its parameters. - How to transpose and reshape a matrix using
arr.reshape()
,arr.transpose()
,arr.T
- How to reverse and array using
np.flip()
- How to reshape and flatten data using
np.flatten()
andnp.ravel()
Let’s Begin…
To Generate random numbers, we can use the random() function.
For example: random.randint(), random.integers() etc.
How to get unique items and counts
You can find the unique elements in an array easily with np.unique
.
For example, if you start with this array:
a = np.array([11, 11, 12, 13, 14, 15, 16, 17, 12, 13, 11, 14, 18, 19, 20])
you can use np.unique
to print the unique values in your array:
unique_values = np.unique(a)
print(unique_values)
#Output :
[11 12 13 14 15 16 17 18 19 20]
To get the indices of unique values in a NumPy array (an array of first index positions of unique values in the array), just pass the return_index
argument in np.unique()
as well as your array.
unique_values, indices_list = np.unique(a, return_index=True)
print(indices_list)
#Output :
[ 0 2 3 4 5 6 7 12 13 14]
You can pass the return_counts
argument in np.unique()
along with your array to get the frequency count of unique values in a NumPy array.
unique_values, occurrence_count = np.unique(a, return_counts=True)
print(occurrence_count)
#Output :
[3 2 2 2 1 1 1 1 1 1]
This also works with 2D arrays! If you start with this array:
a_2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [1, 2, 3, 4]])
If the axis argument isn’t passed, your 2D array will be flattened.
If you want to get the unique rows or columns, make sure to pass the axis
argument. To find the unique rows, specify axis=0
and for columns, specify axis=1
.
unique_rows = np.unique(a_2d, axis=0)
>>> print(unique_rows)
#Output :
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
To get the unique rows, index position, and occurrence count, you can use:
unique_rows, indices, occurrence_count = np.unique(a_2d, axis=0, return_counts=True, return_index=True)
print(unique_rows)
#Output :
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
print(indices)
#Output :
[0 1 2]
print(occurrence_count)
#Output :
[2 1 1]
Transposing and reshaping a matrix
This section covers arr.reshape()
, arr.transpose()
, arr.T
It’s common to need to transpose your matrices. NumPy arrays have the property T
that allows you to transpose a matrix.
You may also need to switch the dimensions of a matrix. This can happen when, for example, you have a model that expects a certain input shape that is different from your dataset. This is where the reshape
method can be useful. You simply need to pass in the new dimensions that you want for the matrix.
data.reshape(2, 3)
#Output :
array([[1, 2, 3],
[4, 5, 6]])
data.reshape(3, 2)
#Output :
array([[1, 2],
[3, 4],
[5, 6]])
You can also use .transpose()
to reverse or change the axes of an array according to the values you specify.
If you start with this array:
arr = np.arange(6).reshape((2, 3))
arr
#Output :
array([[0, 1, 2],
[3, 4, 5]])
You can transpose your array with arr.transpose()
.
arr.transpose()
#Output :
array([[0, 3],
[1, 4],
[2, 5]])
You can also use arr.T
:
arr.T
#Output :
array([[0, 3],
[1, 4],
[2, 5]])
How to reverse an array
This section covers np.flip()
NumPy’s np.flip()
function allows you to flip, or reverse, the contents ofan array along an axis. When using np.flip()
, specify the array you would like to reverse and the axis. If you don’t specify the axis, NumPy will reverse the contents along all of the axes of your input array.
Reversing a 1D array
If you begin with a 1D array like this one:
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
You can reverse it with:
reversed_arr = np.flip(arr)
If you want to print your reversed array, you can run:
print('Reversed Array: ', reversed_arr)
#Output :
Reversed Array: [8 7 6 5 4 3 2 1]
Reversing a 2D array
A 2D array works much the same way.
If you start with this array:
arr_2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
You can reverse the content in all of the rows and all of the columns with:
reversed_arr = np.flip(arr_2d)
print(reversed_arr)
#Output :
[[12 11 10 9]
[ 8 7 6 5]
[ 4 3 2 1]]
You can easily reverse only the rows with:
reversed_arr_rows = np.flip(arr_2d, axis=0)
print(reversed_arr_rows)
#Output :
[[ 9 10 11 12]
[ 5 6 7 8]
[ 1 2 3 4]]
Or reverse only the columns with:
reversed_arr_columns = np.flip(arr_2d, axis=1)
print(reversed_arr_columns)
#Output :
[[ 4 3 2 1]
[ 8 7 6 5]
[12 11 10 9]]
You can also reverse the contents of only one column or row. For example, you can reverse the contents of the row at index position 1 (the second row):
arr_2d[1] = np.flip(arr_2d[1])
print(arr_2d)
#Output :
[[ 1 2 3 4]
[ 8 7 6 5]
[ 9 10 11 12]]
You can also reverse the column at index position 1 (the second column):
arr_2d[:,1] = np.flip(arr_2d[:,1])
print(arr_2d)
#Output :
[[ 1 10 3 4]
[ 8 7 6 5]
[ 9 2 11 12]]
Reshaping and flattening multidimensional arrays
This section covers .flatten()
, ravel()
There are two popular ways to flatten an array: .flatten()
and .ravel()
. The primary difference between the two is that the new array created using ravel()
is actually a reference to the parent array (i.e., a “view”). This means that any changes to the new array will affect the parent array as well. Since ravel
does not create a copy, it’s memory efficient.
If you start with this array:
x = np.array([[1 , 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
You can use flatten
to flatten your array into a 1D array.
x.flatten()
#Output :
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
When you use flatten
, changes to your new array won’t change the parent array.
For example:
a1 = x.flatten()
a1[0] = 99
print(x) # Original array
#Output :
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
print(a1) # New array
#Output :
[99 2 3 4 5 6 7 8 9 10 11 12]
But when you use ravel
, the changes you make to the new array will affect the parent array.
For example:
a2 = x.ravel()
a2[0] = 98
print(x) # Original array
#Output :
[[98 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
print(a2) # New array
#Output :
[98 2 3 4 5 6 7 8 9 10 11 12]
This is all that I learned about the different numpy today. My learnings will be documented here on Medium for better understanding. Stay tuned for the next blog.
All The Best People☺️.
Follow me on Linkedin:
www.linkedin.com/in/advaitszone
To read my previous blog “[Day-8]NumPy-Matrices”:
https://medium.com/@advaitszone/day-8-numpy-matrices-53408cc2c102