NumPy’s core object is the ndarray
(n - dimensional array). It is a homogeneous array, meaning all elements in the array must have the same data type. You can create an ndarray
in several ways, such as from a Python list:
import numpy as np
# Create a 1 - D array from a list
arr = np.array([1, 2, 3, 4, 5])
print(arr)
NumPy provides a large number of mathematical functions that can be applied element - wise to arrays. For example, you can add a scalar value to every element in an array:
new_arr = arr + 1
print(new_arr)
Streamlit applications are essentially Python scripts. You can start by importing the streamlit
library and using its functions to build the user interface. For example, to display text on the app:
import streamlit as st
st.title("My First Streamlit App")
st.write("Welcome to this simple data app!")
Streamlit uses a reactive programming model. When a user interacts with an input widget (e.g., a slider, a dropdown), Streamlit reruns the script from top - to - bottom, updating the output based on the new input values.
Interactive data exploration is one of the most common use cases. You can create an app where users can select different columns of a dataset, filter the data based on certain criteria, and view summary statistics. For example, in a dataset of customer information, users could filter customers by age range and view the average purchase amount.
If you have trained a machine learning model, you can use Streamlit to create an interactive visualization of the model’s predictions. For instance, in a regression model, users can adjust the input features and see how the predicted output changes in real - time.
import numpy as np
import streamlit as st
# Generate a random 2 - D array
data = np.random.rand(10, 5)
# Create a title for the app
st.title("Interactive Data Viewer")
# Create a slider to select the number of rows to display
rows = st.slider("Number of rows to display", 1, 10, 5)
# Display the selected rows of the data
st.write(data[:rows])
import numpy as np
import streamlit as st
import matplotlib.pyplot as plt
# Generate some sample data for a linear regression model
x = np.linspace(0, 10, 100)
y = 2 * x + 1 + np.random.randn(100)
# Create a title for the app
st.title("Linear Regression Model Visualization")
# Create a slider to adjust the slope of the line
slope = st.slider("Slope", 0.0, 5.0, 2.0)
# Create a slider to adjust the intercept of the line
intercept = st.slider("Intercept", - 5.0, 5.0, 1.0)
# Calculate the predicted values
y_pred = slope * x + intercept
# Plot the data and the predicted line
fig, ax = plt.subplots()
ax.scatter(x, y, label='Data')
ax.plot(x, y_pred, color='red', label='Predicted Line')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.legend()
# Display the plot in the app
st.pyplot(fig)
NumPy arrays can consume a large amount of memory, especially when dealing with large datasets. If you create intermediate arrays during your data processing, make sure to release the memory when they are no longer needed. For example, instead of creating multiple copies of large arrays, try to perform in - place operations whenever possible.
If your app has complex computations or large datasets, it may become slow. Streamlit reruns the entire script every time there is a user interaction. You can use caching mechanisms provided by Streamlit to avoid recomputing expensive functions if the input parameters have not changed. For example, you can use the @st.cache
decorator:
import streamlit as st
import numpy as np
@st.cache
def expensive_function():
data = np.random.rand(1000, 1000)
result = np.sum(data)
return result
st.write(expensive_function())
Keep your code well - organized. Separate the data processing code from the Streamlit UI code. You can create functions for different parts of the app, such as data generation, data processing, and plotting. This makes the code easier to read, maintain, and debug.
Add proper error handling in your code. For example, if the user enters an invalid input in a widget, your app should display an appropriate error message instead of crashing. You can use Python’s try - except
blocks to handle exceptions.
Combining NumPy and Streamlit is a powerful way to create interactive data applications. NumPy provides the necessary data processing capabilities, while Streamlit simplifies the process of building web - based user interfaces. By understanding the core concepts, typical usage scenarios, common pitfalls, and best practices, you can develop high - quality interactive data applications that are both useful and user - friendly.