0
0
NumPydata~15 mins

np.newaxis for adding dimensions in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Using np.newaxis to Add Dimensions in NumPy Arrays
📖 Scenario: Imagine you are working with data from a weather station. You have temperature readings for a week stored as a simple list. To prepare this data for a machine learning model, you need to add extra dimensions to the data array.
🎯 Goal: You will learn how to use np.newaxis to add new dimensions to a NumPy array. This helps reshape data for analysis or modeling.
📋 What You'll Learn
Create a NumPy array with exact temperature values
Create a variable to add a new axis
Use np.newaxis to add a dimension to the array
Print the reshaped array to see the new shape
💡 Why This Matters
🌍 Real World
Adding dimensions to arrays is common when preparing data for machine learning models, which often expect inputs in specific shapes.
💼 Career
Data scientists and analysts frequently reshape data arrays to fit model requirements or to perform broadcasting operations in NumPy.
Progress0 / 4 steps
1
Create the temperature data array
Create a NumPy array called temps with these exact temperature values: [22, 24, 19, 23, 25, 20, 21].
NumPy
Need a hint?

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

2
Create a variable for the new axis
Create a variable called axis and set it to np.newaxis.
NumPy
Need a hint?

Assign np.newaxis to the variable axis.

3
Add a new dimension to the array
Create a new array called temps_reshaped by adding a new axis to temps using the variable axis. Use temps[:, axis] to add a new dimension as a column vector.
NumPy
Need a hint?

Use slicing with : and axis to add a new dimension.

4
Print the reshaped array
Print the variable temps_reshaped to see the array with the new dimension.
NumPy
Need a hint?

Use print(temps_reshaped) to display the reshaped array.