0
0
NumPydata~15 mins

transpose() for swapping axes in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Transpose a NumPy Array to Swap Axes
📖 Scenario: Imagine you have a table of data representing sales numbers for different products across several months. The data is stored in a 2D array where rows are products and columns are months. Sometimes, you want to switch the rows and columns to analyze the data from a different perspective.
🎯 Goal: You will create a NumPy array with sales data, then use the transpose() function to swap its axes. Finally, you will print the transposed array to see the switched rows and columns.
📋 What You'll Learn
Create a 2D NumPy array named sales with the exact values [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
Create a variable named swapped_axes to hold the transposed array using sales.transpose()
Print the swapped_axes array to display the result
💡 Why This Matters
🌍 Real World
Swapping axes of data arrays is common in data science when you want to change the perspective of your data, such as switching rows and columns in tables or images.
💼 Career
Understanding how to manipulate array shapes and axes is essential for data cleaning, feature engineering, and preparing data for machine learning models.
Progress0 / 4 steps
1
Create the sales data array
Import NumPy as np and create a 2D NumPy array called sales with these exact values: [[10, 20, 30], [40, 50, 60], [70, 80, 90]].
NumPy
Need a hint?

Use np.array() to create the array with the exact nested list.

2
Create a variable for the transposed array
Create a variable called swapped_axes and assign it the transposed version of the sales array using sales.transpose().
NumPy
Need a hint?

Use the transpose() method on the sales array.

3
Print the transposed array
Print the variable swapped_axes to display the transposed array.
NumPy
Need a hint?

Use print(swapped_axes) to show the transposed array.

4
Verify the shape change after transpose
Print the shape of the original sales array and the shape of the swapped_axes array using print(sales.shape) and print(swapped_axes.shape).
NumPy
Need a hint?

Use print(sales.shape) and print(swapped_axes.shape) to see the shapes.