create an app commented to explain this problem: I've encountered two different use cases of the axis argument where they are interpreted differently. Take this array: arr = np.reshape(np.arange(30, dtype="float"), (6, 5)) In this first case, we will sum all elements through the columns: np.sum(arr, axis=0) The result would be [75. 81. 87. 93. 99.] But if we were to delete the columns, we will have to use axis = 1, like this: print(np.delete(arr, (0, 1), axis=1)) And this is the result: [[ 2. 3. 4.] [ 7. 8. 9.] [12. 13. 14.] [17. 18. 19.] [22. 23. 24.] [27. 28. 29.]] Using axis=0 would have deleted the rows, anyone does have an explanation?
To upload files, please first save the app
import streamlit as st
import numpy as np
st.title("Understanding NumPy Axis Operations")
st.write("""
This app explains how the `axis` parameter works differently in various NumPy operations,
particularly focusing on `np.sum()` and `np.delete()`.
""")
# Create our example array
arr = np.reshape(np.arange(30, dtype="float"), (6, 5))
st.subheader("1. Original Array")
st.write("Let's start with a 6x5 array:")
st.write(arr)
st.write("""
Shape of the array: (6, 5) meaning:
- 6 rows (axis 0)
- 5 columns (axis 1)
""")
# Visualization of axes
st.subheader("2. Understanding Axes")
st.write("""
```
axis=0 (rows) ↓
[0 1 2 3 4]
[5 6 7 8 9]
[10 11 12 13 14] → axis=1 (columns)
[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]
```
""")
st.subheader("3. Sum Operation (np.sum)")
st.write("""
When using `np.sum(arr, axis=0)`, we're telling NumPy to:
- Sum along the vertical direction (down the columns)
- Collapse the rows into a single row
- Keep the columns dimension intact
""")
sum_result = np.sum(arr, axis=0)
st.write("Result of np.sum(arr, axis=0):")
st.write(sum_result)
st.subheader("4. Delete Operation (np.delete)")
st.write("""
When using `np.delete(arr, (0, 1), axis=1)`, we're telling NumPy to:
- Remove elements along axis 1 (columns)
- Specifically remove columns 0 and 1
- Keep all other columns intact
""")
delete_result = np.delete(arr, (0, 1), axis=1)
st.write("Result of np.delete(arr, (0, 1), axis=1):")
st.write(delete_result)
st.subheader("5. Key Differences Explained")
st.write("""
The key difference in interpretation comes from what the operations do:
1. **For sum (axis=0)**:
- Think "collapse along this axis"
- axis=0 means collapse rows, summing vertically
- The result has the same number of columns but only one row
2. **For delete (axis=1)**:
- Think "which axis to count positions on"
- axis=1 means count positions along columns
- Remove elements at those positions from each row
This is why they seem to work "opposite" to each other, but they're actually
just different types of operations with different interpretations of what
the axis parameter means in their context.
""")
# Interactive demonstration
st.subheader("6. Interactive Demo")
operation = st.radio(
"Choose operation:",
["Sum", "Delete"]
)
if operation == "Sum":
axis = st.radio("Choose axis for sum:", [0, 1])
result = np.sum(arr, axis=axis)
st.write(f"Result of np.sum(arr, axis={axis}):")
st.write(result)
else:
axis = st.radio("Choose axis for delete:", [0, 1])
indices = (0, 1) # Fixed for simplicity
result = np.delete(arr, indices, axis=axis)
st.write(f"Result of np.delete(arr, {indices}, axis={axis}):")
st.write(result)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?