Spaces:
Runtime error
Runtime error
import streamlit as st | |
import numpy as np | |
st.sidebar.title("NumPy Demo") | |
# Array creation routines | |
st.sidebar.header("Array creation routines") | |
st.sidebar.write("np.zeros(5):", np.zeros(5)) | |
st.sidebar.write("np.ones((2, 3)):", np.ones((2, 3))) | |
st.sidebar.write("np.arange(0, 10, 2):", np.arange(0, 10, 2)) | |
st.sidebar.write("np.linspace(0, 1, 5):", np.linspace(0, 1, 5)) | |
st.sidebar.write("np.eye(3):", np.eye(3)) | |
# Array manipulation routines | |
st.sidebar.header("Array manipulation routines") | |
arr = np.array([[1, 2], [3, 4]]) | |
st.sidebar.write("arr.flatten():", arr.flatten()) | |
st.sidebar.write("np.transpose(arr):", np.transpose(arr)) | |
st.sidebar.write("np.rot90(arr):", np.rot90(arr)) | |
# Binary operations | |
st.sidebar.header("Binary operations") | |
x = np.array([1, 2, 3]) | |
y = np.array([4, 5, 6]) | |
st.sidebar.write("np.add(x, y):", np.add(x, y)) | |
st.sidebar.write("np.subtract(x, y):", np.subtract(x, y)) | |
st.sidebar.write("np.multiply(x, y):", np.multiply(x, y)) | |
# String operations | |
st.sidebar.header("String operations") | |
st.sidebar.write("np.char.add(['hello', 'world'], ['!', '?']):", np.char.add(['hello', 'world'], ['!', '?'])) | |
st.sidebar.write("np.char.upper('numpy'):", np.char.upper('numpy')) | |
st.sidebar.write("np.char.replace('numpy', 'py', 'ython'):", np.char.replace('numpy', 'py', 'ython')) | |
# C-Types Foreign Function Interface (numpy.ctypeslib) | |
st.sidebar.header("C-Types Foreign Function Interface (numpy.ctypeslib)") | |
# Omitted for simplicity | |
# Datetime Support Functions | |
st.sidebar.header("Datetime Support Functions") | |
st.sidebar.write("np.datetime64('2023-02-21'):", np.datetime64('2023-02-21')) | |
st.sidebar.write("np.datetime64('2023-02-21 12:00:00'):", np.datetime64('2023-02-21 12:00:00')) | |
# Data type routines | |
st.sidebar.header("Data type routines") | |
st.sidebar.write("np.dtype('float64'):", np.dtype('float64')) | |
st.sidebar.write("np.issubdtype(np.float64, np.number):", np.issubdtype(np.float64, np.number)) | |
# Optionally SciPy-accelerated routines (numpy.dual) | |
st.sidebar.header("Optionally SciPy-accelerated routines (numpy.dual)") | |
# Omitted for simplicity | |
# Mathematical functions with automatic domain | |
st.sidebar.header("Mathematical functions with automatic domain") | |
st.sidebar.write("np.sqrt(-1):", np.sqrt(-1)) | |
st.sidebar.write("np.log(0):", np.log(0)) | |
# Functional programming | |
st.sidebar.header("Functional programming") | |
st.sidebar.write("np.vectorize(np.square)([1, 2, 3]):", np.vectorize(np.square)([1, 2, 3])) | |
# NumPy-specific help functions | |
st.sidebar.header("NumPy-specific help functions") | |
st.sidebar.write("np.info(np.add):", np.info(np.add)) | |
# Linear algebra (numpy.linalg) | |
st.sidebar.header("Linear algebra (numpy.linalg)") | |
mat = np.array([[1, 2], [3, 4]]) | |
st.sidebar.write("np.linalg.inv(mat):", np.linalg.inv(mat)) | |
st.sidebar.write("np.linalg.eig(mat):", np.linalg.eig(mat)) | |
# Logic functions | |
st.sidebar.header("Logic functions") | |
x = np.array([1, 2, 3]) | |
y = np.array([2, 2, 2]) | |
st.sidebar.write("np.logical_and(x > 1, y < 3):", np.logical_and(x > 1, y < 3)) | |
st.sidebar.write("np.logical_or(x > 2, y < 2):", np.logical_or(x > 2, y < 2)) | |
st.sidebar.write("np.logical_not(x > 2):", np.logical_not(x > 2)) | |
# Mathematical functions | |
st.sidebar.header("Mathematical functions") | |
x = np.array([0, 1, 2]) | |
st.sidebar.write("np.exp(x):", np.exp(x)) | |
st.sidebar.write("np.sin(x):", np.sin(x)) | |
st.sidebar.write("np.arctan(x):", np.arctan(x)) | |
# Miscellaneous routines | |
st.sidebar.header("Miscellaneous routines") | |
st.sidebar.write("np.percentile([1, 2, 3, 4, 5], 50):", np.percentile([1, 2, 3, 4, 5], 50)) | |
st.sidebar.write("np.histogram([1, 2, 1], bins=[0, 1, 2, 3]):", np.histogram([1, 2, 1], bins=[0, 1, 2, 3])) | |
# Polynomials | |
st.sidebar.header("Polynomials") | |
st.sidebar.write("np.poly1d([1, 2, 3])(4):", np.poly1d([1, 2, 3])(4)) | |
# Random sampling (numpy.random) | |
st.sidebar.header("Random sampling (numpy.random)") | |
st.sidebar.write("np.random.rand(3, 2):", np.random.rand(3, 2)) | |
st.sidebar.write("np.random.normal(size=(2, 2)):", np.random.normal(size=(2, 2))) | |
#Set routines | |
st.sidebar.header("Set routines") | |
x = np.array([1, 2, 3, 4]) | |
y = np.array([3, 4, 5, 6]) | |
st.sidebar.write("np.intersect1d(x, y):", np.intersect1d(x, y)) | |
st.sidebar.write("np.union1d(x, y):", np.union1d(x, y)) | |
st.sidebar.write("np.setdiff1d(x, y):", np.setdiff1d(x, y)) | |
#Sorting, searching, and counting | |
st.sidebar.header("Sorting, searching, and counting") | |
x = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3]) | |
st.sidebar.write("np.sort(x):", np.sort(x)) | |
st.sidebar.write("np.argsort(x):", np.argsort(x)) | |
st.sidebar.write("np.where(x == 5):", np.where(x == 5)) | |
st.sidebar.write("np.count_nonzero(x > 3):", np.count_nonzero(x > 3)) | |
# Statistics | |
st.sidebar.header("Statistics") | |
x = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3]) | |
st.sidebar.write("np.mean(x):", np.mean(x)) | |
st.sidebar.write("np.std(x):", np.std(x)) | |
st.sidebar.write("np.median(x):", np.median(x)) | |