Dataset Viewer
Auto-converted to Parquet Duplicate
id
stringlengths
7
11
category
stringclasses
13 values
difficulty
stringclasses
3 values
prompt
stringlengths
61
181
solution
stringlengths
12
165
input_data
stringlengths
16
95
test_cases
stringlengths
52
190
array_001
array_creation
easy
Create a 3x3 identity matrix using NumPy. Store the result in a variable called `result`.
result = np.eye(3)
{"type": "none"}
[{"type": "shape_check", "expected": [3, 3]}, {"type": "value_check", "expected": [[1, 0, 0], [0, 1, 0], [0, 0, 1]], "rtol": 1e-10}]
array_002
array_creation
easy
Create an array of 5 evenly spaced values from 0 to 2 (inclusive) using np.linspace. Store the result in `result`.
result = np.linspace(0, 2, 5)
{"type": "none"}
[{"type": "shape_check", "expected": [5]}, {"type": "value_check", "expected": [0.0, 0.5, 1.0, 1.5, 2.0], "rtol": 1e-10}]
array_003
array_creation
easy
Create a 2x4 array filled with zeros. Store the result in `result`.
result = np.zeros((2, 4))
{"type": "none"}
[{"type": "shape_check", "expected": [2, 4]}, {"type": "all_zeros", "expected": true}]
array_004
array_creation
easy
Create an array containing values from 0 to 10 (exclusive) with step size 2 using np.arange. Store the result in `result`.
result = np.arange(0, 10, 2)
{"type": "none"}
[{"type": "value_check", "expected": [0, 2, 4, 6, 8], "rtol": 1e-10}]
array_005
array_creation
medium
Given the 1D array `arr` containing values 1 through 12, reshape it into a 3x4 matrix. Store the result in `result`.
result = arr.reshape(3, 4)
{"type": "array", "arr": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]}
[{"type": "shape_check", "expected": [3, 4]}, {"type": "value_check", "expected": [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], "rtol": 1e-10}]
array_006
array_creation
medium
Given arrays `a` and `b`, stack them vertically using np.vstack. Store the result in `result`.
result = np.vstack([a, b])
{"type": "multi_arrays", "a": [1, 2, 3], "b": [4, 5, 6]}
[{"type": "shape_check", "expected": [2, 3]}, {"type": "value_check", "expected": [[1, 2, 3], [4, 5, 6]], "rtol": 1e-10}]
array_007
array_creation
medium
Given array `arr`, extract all elements greater than 5. Store the result in `result`.
result = arr[arr > 5]
{"type": "array", "arr": [1, 7, 3, 9, 2, 8, 4, 6]}
[{"type": "value_check", "expected": [7, 9, 8, 6], "rtol": 1e-10}]
array_008
array_creation
medium
Given 2D arrays `a` (2x3) and `b` (2x3), concatenate them horizontally (along axis=1). Store the result in `result`.
result = np.concatenate([a, b], axis=1)
{"type": "multi_arrays", "a": [[1, 2, 3], [4, 5, 6]], "b": [[7, 8, 9], [10, 11, 12]]}
[{"type": "shape_check", "expected": [2, 6]}, {"type": "value_check", "expected": [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]], "rtol": 1e-10}]
array_009
array_creation
hard
Given a column vector `col` (shape 3x1) and a row vector `row` (shape 1x3), compute their sum using broadcasting to get a 3x3 matrix. Store the result in `result`.
result = col + row
{"type": "multi_arrays", "col": [[1], [2], [3]], "row": [[10, 20, 30]]}
[{"type": "shape_check", "expected": [3, 3]}, {"type": "value_check", "expected": [[11, 21, 31], [12, 22, 32], [13, 23, 33]], "rtol": 1e-10}]
array_010
array_creation
hard
Given a 4x4 matrix `mat`, extract the diagonal elements and the anti-diagonal elements, then concatenate them into a single 1D array. Store the result in `result`.
diag = np.diag(mat) anti_diag = np.diag(np.fliplr(mat)) result = np.concatenate([diag, anti_diag])
{"type": "array", "mat": [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]}
[{"type": "shape_check", "expected": [8]}, {"type": "value_check", "expected": [1, 6, 11, 16, 4, 7, 10, 13], "rtol": 1e-10}]
vec_001
vectorized_math
easy
Given array `x`, compute the sine of each element. Store the result in `result`.
result = np.sin(x)
{"type": "array", "x": [0, 1.5707963267948966, 3.141592653589793]}
[{"type": "shape_check", "expected": [3]}, {"type": "value_check", "expected": [0.0, 1.0, 0.0], "rtol": 1e-05}]
vec_002
vectorized_math
easy
Given array `x`, compute e^x (exponential) for each element. Store the result in `result`.
result = np.exp(x)
{"type": "array", "x": [0, 1, 2]}
[{"type": "value_check", "expected": [1.0, 2.718281828459045, 7.38905609893065], "rtol": 1e-05}]
vec_003
vectorized_math
easy
Given array `x`, compute the natural logarithm of each element. Store the result in `result`.
result = np.log(x)
{"type": "array", "x": [1, 2.718281828459045, 7.38905609893065]}
[{"type": "value_check", "expected": [0.0, 1.0, 2.0], "rtol": 1e-05}]
vec_004
vectorized_math
medium
Given array `x`, use np.where to replace all negative values with 0 and keep positive values unchanged. Store the result in `result`.
result = np.where(x < 0, 0, x)
{"type": "array", "x": [-3, 2, -1, 5, -4, 0, 3]}
[{"type": "value_check", "expected": [0, 2, 0, 5, 0, 0, 3], "rtol": 1e-10}]
vec_005
vectorized_math
medium
Given vector `v`, compute its L2 (Euclidean) norm. Store the result in `result`.
result = np.linalg.norm(v)
{"type": "array", "v": [3, 4]}
[{"type": "close_to", "expected": 5.0, "rtol": 1e-10}]
vec_006
vectorized_math
medium
Given arrays `a` and `b`, compute (a^2 + b^2) element-wise. Store the result in `result`.
result = a**2 + b**2
{"type": "multi_arrays", "a": [1, 2, 3], "b": [4, 5, 6]}
[{"type": "value_check", "expected": [17, 29, 45], "rtol": 1e-10}]
vec_007
vectorized_math
medium
Evaluate the polynomial p(x) = 2x^2 + 3x + 1 at points x = [0, 1, 2, 3]. Use np.polyval with coefficients [2, 3, 1]. Store the result in `result`.
result = np.polyval([2, 3, 1], x)
{"type": "array", "x": [0, 1, 2, 3]}
[{"type": "value_check", "expected": [1, 6, 15, 28], "rtol": 1e-10}]
vec_008
vectorized_math
hard
Implement the softmax function for array `x`: softmax(x_i) = exp(x_i) / sum(exp(x)). For numerical stability, subtract max(x) from all elements first. Store the result in `result`.
x_shifted = x - np.max(x) exp_x = np.exp(x_shifted) result = exp_x / np.sum(exp_x)
{"type": "array", "x": [1.0, 2.0, 3.0]}
[{"type": "shape_check", "expected": [3]}, {"type": "sum_close_to", "expected": 1.0, "rtol": 1e-10}, {"type": "value_check", "expected": [0.09003057, 0.24472847, 0.66524096], "rtol": 1e-05}]
linalg_001
linear_algebra
easy
Given matrices `A` (2x3) and `B` (3x2), compute their matrix product A @ B. Store the result in `result`.
result = A @ B
{"type": "multi_arrays", "A": [[1, 2, 3], [4, 5, 6]], "B": [[7, 8], [9, 10], [11, 12]]}
[{"type": "shape_check", "expected": [2, 2]}, {"type": "value_check", "expected": [[58, 64], [139, 154]], "rtol": 1e-10}]
linalg_002
linear_algebra
easy
Given vectors `a` and `b`, compute their dot product. Store the result in `result`.
result = np.dot(a, b)
{"type": "multi_arrays", "a": [1, 2, 3], "b": [4, 5, 6]}
[{"type": "close_to", "expected": 32, "rtol": 1e-10}]
linalg_003
linear_algebra
medium
Solve the linear system Ax = b where A = [[2, 1], [1, 3]] and b = [4, 5]. Store the solution vector in `result`.
result = np.linalg.solve(A, b)
{"type": "multi_arrays", "A": [[2, 1], [1, 3]], "b": [4, 5]}
[{"type": "shape_check", "expected": [2]}, {"type": "value_check", "expected": [1.4, 1.2], "rtol": 1e-05}]
linalg_004
linear_algebra
medium
Compute the determinant of matrix `A`. Store the result in `result`.
result = np.linalg.det(A)
{"type": "array", "A": [[4, 2], [3, 1]]}
[{"type": "close_to", "expected": -2.0, "rtol": 1e-05}]
linalg_005
linear_algebra
medium
Compute the inverse of matrix `A`. Store the result in `result`.
result = np.linalg.inv(A)
{"type": "array", "A": [[1, 2], [3, 4]]}
[{"type": "shape_check", "expected": [2, 2]}, {"type": "value_check", "expected": [[-2.0, 1.0], [1.5, -0.5]], "rtol": 1e-05}]
linalg_006
linear_algebra
medium
Compute the transpose of matrix `A`. Store the result in `result`.
result = A.T
{"type": "array", "A": [[1, 2, 3], [4, 5, 6]]}
[{"type": "shape_check", "expected": [3, 2]}, {"type": "value_check", "expected": [[1, 4], [2, 5], [3, 6]], "rtol": 1e-10}]
linalg_007
linear_algebra
medium
Compute the rank of matrix `A`. Store the result in `result`.
result = np.linalg.matrix_rank(A)
{"type": "array", "A": [[1, 2, 3], [4, 5, 6], [7, 8, 9]]}
[{"type": "close_to", "expected": 2, "rtol": 1e-10}]
linalg_008
linear_algebra
hard
Compute the eigenvalues of matrix `A`. Store the eigenvalues array in `result`.
result = np.linalg.eigvals(A)
{"type": "array", "A": [[4, 2], [1, 3]]}
[{"type": "shape_check", "expected": [2]}, {"type": "eigenvalue_check", "expected": [5.0, 2.0], "rtol": 1e-05}]
linalg_009
linear_algebra
hard
Compute the singular values of matrix `A` using SVD. Store only the singular values (s) in `result`.
U, s, Vh = np.linalg.svd(A) result = s
{"type": "array", "A": [[1, 2], [3, 4], [5, 6]]}
[{"type": "shape_check", "expected": [2]}, {"type": "value_check", "expected": [9.52551809, 0.51430058], "rtol": 1e-05}]
linalg_010
linear_algebra
hard
Compute the QR decomposition of matrix `A`. Store the R matrix in `result`.
Q, R = np.linalg.qr(A) result = R
{"type": "array", "A": [[1, 2], [3, 4], [5, 6]]}
[{"type": "shape_check", "expected": [2, 2]}, {"type": "upper_triangular", "expected": true}]
calc_001
calculus
easy
Given arrays `x` and `y` representing points on a curve, compute the integral using the trapezoidal rule (np.trapz). Store the result in `result`.
result = np.trapz(y, x)
{"type": "multi_arrays", "x": [0, 1, 2, 3, 4], "y": [0, 1, 4, 9, 16]}
[{"type": "close_to", "expected": 22.0, "rtol": 1e-05}]
calc_002
calculus
medium
Use scipy.integrate.quad to compute the integral of sin(x) from 0 to pi. Store the integral value (not the error) in `result`.
from scipy import integrate result, _ = integrate.quad(np.sin, 0, np.pi)
{"type": "none"}
[{"type": "close_to", "expected": 2.0, "rtol": 1e-05}]
calc_003
calculus
medium
Use scipy.integrate.quad to compute the integral of x^2 from 0 to 3. Store the integral value in `result`.
from scipy import integrate result, _ = integrate.quad(lambda x: x**2, 0, 3)
{"type": "none"}
[{"type": "close_to", "expected": 9.0, "rtol": 1e-05}]
calc_004
calculus
medium
Given arrays `x` and `y`, compute the integral using Simpson's rule (scipy.integrate.simpson). Store the result in `result`.
from scipy import integrate result = integrate.simpson(y, x=x)
{"type": "multi_arrays", "x": [0, 0.5, 1.0, 1.5, 2.0], "y": [0, 0.25, 1.0, 2.25, 4.0]}
[{"type": "close_to", "expected": 2.666666666666667, "rtol": 1e-05}]
calc_005
calculus
hard
Use scipy.integrate.dblquad to compute the double integral of x*y over the region x=[0,1], y=[0,2]. Store the integral value in `result`.
from scipy import integrate result, _ = integrate.dblquad(lambda y, x: x * y, 0, 1, 0, 2)
{"type": "none"}
[{"type": "close_to", "expected": 1.0, "rtol": 1e-05}]
opt_001
optimization
medium
Use scipy.optimize.minimize to find the minimum of f(x) = (x - 3)^2. Start from x0 = 0. Store the optimal x value (result.x[0]) in `result`.
from scipy import optimize res = optimize.minimize(lambda x: (x - 3)**2, x0=0) result = res.x[0]
{"type": "none"}
[{"type": "close_to", "expected": 3.0, "rtol": 0.001}]
opt_002
optimization
medium
Use scipy.optimize.brentq to find the root of f(x) = x^2 - 4 in the interval [0, 3]. Store the root in `result`.
from scipy import optimize result = optimize.brentq(lambda x: x**2 - 4, 0, 3)
{"type": "none"}
[{"type": "close_to", "expected": 2.0, "rtol": 1e-05}]
opt_003
optimization
medium
Use scipy.optimize.fsolve to find the root of f(x) = cos(x) - x starting from x0 = 1. Store the root in `result`.
from scipy import optimize result = optimize.fsolve(lambda x: np.cos(x) - x, 1.0)[0]
{"type": "none"}
[{"type": "close_to", "expected": 0.7390851332151607, "rtol": 1e-05}]
opt_004
optimization
hard
Use scipy.optimize.minimize to find the minimum of the Rosenbrock function f(x,y) = (1-x)^2 + 100*(y-x^2)^2. Start from [0, 0]. Store the optimal point as a 1D array in `result`.
from scipy import optimize def rosenbrock(xy): x, y = xy return (1 - x)**2 + 100 * (y - x**2)**2 res = optimize.minimize(rosenbrock, [0, 0]) result = res.x
{"type": "none"}
[{"type": "shape_check", "expected": [2]}, {"type": "value_check", "expected": [1.0, 1.0], "rtol": 0.001}]
opt_005
optimization
hard
Use scipy.optimize.curve_fit to fit the function f(x) = a*x + b to the data points. Store the fitted parameters [a, b] in `result`.
from scipy import optimize def linear(x, a, b): return a * x + b popt, _ = optimize.curve_fit(linear, x_data, y_data) result = popt
{"type": "multi_arrays", "x_data": [0, 1, 2, 3, 4], "y_data": [1, 3, 5, 7, 9]}
[{"type": "shape_check", "expected": [2]}, {"type": "value_check", "expected": [2.0, 1.0], "rtol": 0.001}]
stats_001
statistics
easy
Given array `data`, compute the mean. Store the result in `result`.
result = np.mean(data)
{"type": "array", "data": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}
[{"type": "close_to", "expected": 5.5, "rtol": 1e-10}]
stats_002
statistics
easy
Given array `data`, compute the standard deviation (population, ddof=0). Store the result in `result`.
result = np.std(data)
{"type": "array", "data": [2, 4, 4, 4, 5, 5, 7, 9]}
[{"type": "close_to", "expected": 2.0, "rtol": 1e-05}]
stats_003
statistics
medium
Compute the probability density function (PDF) of the standard normal distribution at x = 0. Store the result in `result`.
from scipy import stats result = stats.norm.pdf(0)
{"type": "none"}
[{"type": "close_to", "expected": 0.3989422804014327, "rtol": 1e-05}]
stats_004
statistics
medium
Compute the cumulative distribution function (CDF) of the standard normal distribution at x = 0. Store the result in `result`.
from scipy import stats result = stats.norm.cdf(0)
{"type": "none"}
[{"type": "close_to", "expected": 0.5, "rtol": 1e-05}]
stats_005
statistics
medium
Find the 95th percentile (quantile) of the standard normal distribution. Store the result in `result`.
from scipy import stats result = stats.norm.ppf(0.95)
{"type": "none"}
[{"type": "close_to", "expected": 1.6448536269514722, "rtol": 1e-05}]
stats_006
statistics
medium
Compute the probability mass function (PMF) of a Poisson distribution with lambda=3 at k=2. Store the result in `result`.
from scipy import stats result = stats.poisson.pmf(2, mu=3)
{"type": "none"}
[{"type": "close_to", "expected": 0.22404180765538775, "rtol": 1e-05}]
stats_007
statistics
hard
Perform a one-sample t-test to test if the mean of `data` is significantly different from 5. Store the p-value in `result`.
from scipy import stats _, result = stats.ttest_1samp(data, 5.0)
{"type": "array", "data": [4.8, 5.1, 4.9, 5.2, 4.7, 5.0, 5.3, 4.8, 5.1, 4.9]}
[{"type": "close_to", "expected": 0.7509058687700305, "rtol": 0.1}]
stats_008
statistics
hard
Compute the Pearson correlation coefficient between arrays `x` and `y`. Store only the correlation coefficient (not the p-value) in `result`.
from scipy import stats result, _ = stats.pearsonr(x, y)
{"type": "multi_arrays", "x": [1, 2, 3, 4, 5], "y": [2, 4, 5, 4, 5]}
[{"type": "close_to", "expected": 0.7745966692414834, "rtol": 1e-05}]
random_001
random
easy
Set the random seed to 42, then generate an array of 5 random floats between 0 and 1 using np.random.random. Store the result in `result`.
np.random.seed(42) result = np.random.random(5)
{"type": "none"}
[{"type": "shape_check", "expected": [5]}, {"type": "value_check", "expected": [0.37454012, 0.95071431, 0.73199394, 0.59865848, 0.15601864], "rtol": 1e-05}]
random_002
random
easy
Set the random seed to 0, then generate an array of 6 random integers from 1 to 10 (inclusive) using np.random.randint. Store the result in `result`.
np.random.seed(0) result = np.random.randint(1, 11, size=6)
{"type": "none"}
[{"type": "shape_check", "expected": [6]}, {"type": "value_check", "expected": [6, 1, 4, 4, 8, 10], "rtol": 1e-10}]
random_003
random
medium
Set the random seed to 123, then generate 1000 samples from a normal distribution with mean=50 and std=10. Store the mean of the samples in `result`.
np.random.seed(123) samples = np.random.normal(50, 10, 1000) result = np.mean(samples)
{"type": "none"}
[{"type": "close_to", "expected": 49.95, "rtol": 0.05}]
random_004
random
medium
Set the random seed to 7, then randomly choose 4 elements from array `arr` without replacement. Store the result in `result`.
np.random.seed(7) result = np.random.choice(arr, size=4, replace=False)
{"type": "array", "arr": [10, 20, 30, 40, 50, 60, 70, 80]}
[{"type": "shape_check", "expected": [4]}, {"type": "value_check", "expected": [30, 60, 10, 70], "rtol": 1e-10}]
fft_001
fft
medium
Compute the FFT of the array `signal`. Store the result in `result`.
result = np.fft.fft(signal)
{"type": "array", "signal": [1, 1, 1, 1]}
[{"type": "shape_check", "expected": [4]}, {"type": "value_check", "expected": [4, 0, 0, 0], "rtol": 1e-10}]
fft_002
fft
medium
Compute the FFT of `signal` and return the magnitude (absolute value) of the FFT coefficients. Store the result in `result`.
result = np.abs(np.fft.fft(signal))
{"type": "array", "signal": [1, 0, 1, 0]}
[{"type": "shape_check", "expected": [4]}, {"type": "value_check", "expected": [2, 0, 2, 0], "rtol": 1e-10}]
fft_003
fft
medium
Given FFT coefficients `fft_coefs`, compute the inverse FFT to recover the original signal. Store the real part of the result in `result`.
result = np.real(np.fft.ifft(fft_coefs))
{"type": "array", "fft_coefs": [10, -2, 2, -2]}
[{"type": "shape_check", "expected": [4]}, {"type": "value_check", "expected": [2, 3, 4, 1], "rtol": 1e-05}]
fft_004
fft
hard
Given a signal sampled at 100 Hz with 100 samples, compute the FFT frequencies using np.fft.fftfreq. Store the result in `result`.
result = np.fft.fftfreq(n_samples, d=1/sample_rate)
{"type": "params", "n_samples": 100, "sample_rate": 100}
[{"type": "shape_check", "expected": [100]}, {"type": "first_value", "expected": 0.0, "rtol": 1e-10}, {"type": "max_value", "expected": 49.0, "rtol": 1e-10}]
signal_001
signal_processing
medium
Convolve the signal `x` with kernel `h` using mode='same'. Store the result in `result`.
result = np.convolve(x, h, mode='same')
{"type": "multi_arrays", "x": [1, 2, 3, 4, 5], "h": [1, 0, -1]}
[{"type": "shape_check", "expected": [5]}, {"type": "value_check", "expected": [-2, -2, -2, -2, 4], "rtol": 1e-10}]
signal_002
signal_processing
medium
Compute the cross-correlation of `x` and `y` using mode='full'. Store the result in `result`.
result = np.correlate(x, y, mode='full')
{"type": "multi_arrays", "x": [1, 2, 3], "y": [0, 1, 0.5]}
[{"type": "shape_check", "expected": [5]}, {"type": "value_check", "expected": [0.5, 2.0, 3.5, 3.0, 0.0], "rtol": 1e-10}]
signal_003
signal_processing
hard
Design a 2nd order Butterworth lowpass filter with cutoff frequency 0.3 (normalized). Return the filter coefficients b. Store b in `result`.
from scipy import signal as sig b, a = sig.butter(2, 0.3, btype='low') result = b
{"type": "none"}
[{"type": "shape_check", "expected": [3]}, {"type": "sum_close_to", "expected": 0.16666666666666669, "rtol": 0.001}]
signal_004
signal_processing
hard
Apply a simple moving average filter of size 3 to the signal `x` using np.convolve. Use mode='valid'. Store the result in `result`.
kernel = np.ones(3) / 3 result = np.convolve(x, kernel, mode='valid')
{"type": "array", "x": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}
[{"type": "shape_check", "expected": [8]}, {"type": "value_check", "expected": [2, 3, 4, 5, 6, 7, 8, 9], "rtol": 1e-05}]
interp_001
interpolation
easy
Use np.interp to linearly interpolate the value at x=1.5 given the points (x_pts, y_pts). Store the result in `result`.
result = np.interp(1.5, x_pts, y_pts)
{"type": "multi_arrays", "x_pts": [0, 1, 2, 3], "y_pts": [0, 2, 4, 6]}
[{"type": "close_to", "expected": 3.0, "rtol": 1e-10}]
interp_002
interpolation
medium
Use scipy.interpolate.interp1d to create a linear interpolation function from (x_pts, y_pts). Evaluate it at x=[0.5, 1.5, 2.5]. Store the result in `result`.
from scipy import interpolate f = interpolate.interp1d(x_pts, y_pts) result = f([0.5, 1.5, 2.5])
{"type": "multi_arrays", "x_pts": [0, 1, 2, 3], "y_pts": [0, 1, 4, 9]}
[{"type": "shape_check", "expected": [3]}, {"type": "value_check", "expected": [0.5, 2.5, 6.5], "rtol": 1e-05}]
interp_003
interpolation
hard
Use scipy.interpolate.CubicSpline to fit a cubic spline to (x_pts, y_pts). Evaluate the spline at x=1.5. Store the result in `result`.
from scipy import interpolate cs = interpolate.CubicSpline(x_pts, y_pts) result = float(cs(1.5))
{"type": "multi_arrays", "x_pts": [0, 1, 2, 3, 4], "y_pts": [0, 1, 4, 9, 16]}
[{"type": "close_to", "expected": 2.25, "rtol": 0.001}]
spatial_001
spatial
medium
Compute the Euclidean distance between points `p1` and `p2` using scipy.spatial.distance.euclidean. Store the result in `result`.
from scipy.spatial import distance result = distance.euclidean(p1, p2)
{"type": "multi_arrays", "p1": [0, 0], "p2": [3, 4]}
[{"type": "close_to", "expected": 5.0, "rtol": 1e-10}]
spatial_002
spatial
medium
Compute the pairwise distance matrix for the points in `points` using scipy.spatial.distance.cdist. Store the result in `result`.
from scipy.spatial import distance result = distance.cdist(points, points)
{"type": "array", "points": [[0, 0], [1, 0], [0, 1]]}
[{"type": "shape_check", "expected": [3, 3]}, {"type": "diagonal_zeros", "expected": true}]
spatial_003
spatial
hard
Build a KD-Tree from `points` and find the index of the nearest neighbor to `query_point`. Store the index in `result`.
from scipy.spatial import KDTree tree = KDTree(points) dist, result = tree.query(query_point)
{"type": "multi_arrays", "points": [[0, 0], [1, 1], [2, 2], [3, 3]], "query_point": [1.1, 0.9]}
[{"type": "close_to", "expected": 1, "rtol": 1e-10}]
spatial_004
spatial
hard
Compute the convex hull of `points` and return the area of the convex hull. Store the result in `result`.
from scipy.spatial import ConvexHull hull = ConvexHull(points) result = hull.volume
{"type": "array", "points": [[0, 0], [1, 0], [1, 1], [0, 1], [0.5, 0.5]]}
[{"type": "close_to", "expected": 1.0, "rtol": 1e-05}]
sparse_001
sparse
medium
Convert the dense matrix `A` to a CSR sparse matrix and count the number of non-zero elements. Store the count in `result`.
from scipy import sparse sp = sparse.csr_matrix(A) result = sp.nnz
{"type": "array", "A": [[1, 0, 0], [0, 2, 0], [0, 0, 3]]}
[{"type": "close_to", "expected": 3, "rtol": 1e-10}]
sparse_002
sparse
medium
Convert matrix `A` to sparse CSR format and multiply it by vector `v`. Store the result (as a dense array) in `result`.
from scipy import sparse sp = sparse.csr_matrix(A) result = sp.dot(v)
{"type": "multi_arrays", "A": [[1, 0, 2], [0, 3, 0], [4, 0, 5]], "v": [1, 2, 3]}
[{"type": "shape_check", "expected": [3]}, {"type": "value_check", "expected": [7, 6, 19], "rtol": 1e-10}]
sparse_003
sparse
hard
Create a 4x4 sparse identity matrix using scipy.sparse.eye, then add 2 to the diagonal. Return the diagonal elements as a dense array. Store in `result`.
from scipy import sparse I = sparse.eye(4, format='csr') I_plus = I + sparse.diags([2, 2, 2, 2], 0) result = I_plus.diagonal()
{"type": "none"}
[{"type": "shape_check", "expected": [4]}, {"type": "value_check", "expected": [3, 3, 3, 3], "rtol": 1e-10}]
ode_001
ode
hard
Solve the ODE dy/dt = y with initial condition y(0) = 1 from t=0 to t=1 using scipy.integrate.solve_ivp. Store y(1) (the final value) in `result`.
from scipy import integrate sol = integrate.solve_ivp(lambda t, y: y, [0, 1], [1], t_eval=[1]) result = sol.y[0, 0]
{"type": "none"}
[{"type": "close_to", "expected": 2.718281828459045, "rtol": 0.001}]
ode_002
ode
hard
Solve the harmonic oscillator ODE: d²x/dt² = -x, converted to system: dy1/dt = y2, dy2/dt = -y1. Initial conditions: x(0)=1, v(0)=0. Solve from t=0 to t=pi. Store x(pi) in `result`.
from scipy import integrate def harmonic(t, y): return [y[1], -y[0]] sol = integrate.solve_ivp(harmonic, [0, np.pi], [1, 0], t_eval=[np.pi]) result = sol.y[0, 0]
{"type": "none"}
[{"type": "close_to", "expected": -1.0, "rtol": 0.001}]
README.md exists but content is empty.
Downloads last month
7