prompt
stringlengths
105
4.73k
reference_code
stringlengths
11
774
metadata
dict
code_context
stringlengths
746
120k
Problem: Is there a convenient way to calculate percentiles for a sequence or single-dimensional numpy array? I am looking for something similar to Excel's percentile function. I looked in NumPy's statistics reference, and couldn't find this. All I could find is the median (50th percentile), but not something more spec...
result = np.percentile(a, p)
{ "problem_id": 300, "library_problem_id": 9, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 9 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([1, 2, 3, 4, 5]) p = 25 elif test_case_id == 2: np.random.seed(42) a = np.random.rand(20)...
Problem: I want to convert a 1-dimensional array into a 2-dimensional array by specifying the number of columns in the 2D array. Something that would work like this: > import numpy as np > A = np.array([1,2,3,4,5,6]) > B = vec2matrix(A,ncol=2) > B array([[1, 2], [3, 4], [5, 6]]) Does numpy have a function...
B = np.reshape(A, (-1, ncol))
{ "problem_id": 301, "library_problem_id": 10, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 10 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: A = np.array([1, 2, 3, 4, 5, 6]) ncol = 2 elif test_case_id == 2: np.random.seed(42) A = np.random.ran...
Problem: I want to convert a 1-dimensional array into a 2-dimensional array by specifying the number of rows in the 2D array. Something that would work like this: > import numpy as np > A = np.array([1,2,3,4,5,6]) > B = vec2matrix(A,nrow=3) > B array([[1, 2], [3, 4], [5, 6]]) Does numpy have a function th...
B = np.reshape(A, (nrow, -1))
{ "problem_id": 302, "library_problem_id": 11, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 10 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: A = np.array([1, 2, 3, 4, 5, 6]) nrow = 2 elif test_case_id == 2: np.random.seed(42) A = np.random.ran...
Problem: I want to convert a 1-dimensional array into a 2-dimensional array by specifying the number of columns in the 2D array. Something that would work like this: > import numpy as np > A = np.array([1,2,3,4,5,6,7]) > B = vec2matrix(A,ncol=2) > B array([[1, 2], [3, 4], [5, 6]]) Note that when A cannot ...
col = ( A.shape[0] // ncol) * ncol B = A[:col] B= np.reshape(B, (-1, ncol))
{ "problem_id": 303, "library_problem_id": 12, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 10 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: A = np.array([1, 2, 3, 4, 5, 6, 7]) ncol = 2 elif test_case_id == 2: np.random.seed(42) A = np.random....
Problem: I want to reverse & convert a 1-dimensional array into a 2-dimensional array by specifying the number of columns in the 2D array. Something that would work like this: > import numpy as np > A = np.array([1,2,3,4,5,6,7]) > B = vec2matrix(A,ncol=2) > B array([[7, 6], [5, 4], [3, 2]]) Note that when...
col = ( A.shape[0] // ncol) * ncol B = A[len(A)-col:][::-1] B = np.reshape(B, (-1, ncol))
{ "problem_id": 304, "library_problem_id": 13, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 10 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: A = np.array([1, 2, 3, 4, 5, 6, 7]) ncol = 2 elif test_case_id == 2: np.random.seed(42) A = np.random....
Origin Problem: Following-up from this question years ago, is there a canonical "shift" function in numpy? I don't see anything from the documentation. Using this is like: In [76]: xs Out[76]: array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]) In [77]: shift(xs, 3) Out[77]: array([ nan, nan, nan, 0., 1., ...
def solution(xs, n): e = np.empty_like(xs) if n >= 0: e[:n] = np.nan e[n:] = xs[:-n] else: e[n:] = np.nan e[:n] = xs[-n:] return e result = solution(a, shift)
{ "problem_id": 305, "library_problem_id": 14, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 14 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]) shift = 3 elif test_case_id == 2: np.random.seed(...
Problem: Following-up from this question years ago, is there a canonical "shift" function in numpy? Ideally it can be applied to 2-dimensional arrays. Example: In [76]: xs Out[76]: array([[ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.], [ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]]) In [77]: shift(xs, 3) Ou...
def solution(xs, n): e = np.empty_like(xs) if n >= 0: e[:,:n] = np.nan e[:,n:] = xs[:,:-n] else: e[:,n:] = np.nan e[:,:n] = xs[:,-n:] return e result = solution(a, shift)
{ "problem_id": 306, "library_problem_id": 15, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 14 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array( [ [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], [1.0, 2.0, 3.0, 4.0, 5...
Problem: Following-up from this question years ago, is there a "shift" function in numpy? Ideally it can be applied to 2-dimensional arrays, and the numbers of shift are different among rows. Example: In [76]: xs Out[76]: array([[ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.], [ 1., 2., 3., 4., 5., 6., 7., ...
def solution(xs, shift): e = np.empty_like(xs) for i, n in enumerate(shift): if n >= 0: e[i,:n] = np.nan e[i,n:] = xs[i,:-n] else: e[i,n:] = np.nan e[i,:n] = xs[i,-n:] return e result = solution(a, shift)
{ "problem_id": 307, "library_problem_id": 16, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 14 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array( [ [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], [1.0, 2.0, 3.0, 4.0, 5...
Problem: I am waiting for another developer to finish a piece of code that will return an np array of shape (100,2000) with values of either -1,0, or 1. In the meantime, I want to randomly create an array of the same characteristics so I can get a head start on my development and testing. The thing is that I want this ...
np.random.seed(0) r_old = np.random.randint(3, size=(100, 2000)) - 1 np.random.seed(0) r_new = np.random.randint(3, size=(100, 2000)) - 1
{ "problem_id": 308, "library_problem_id": 17, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Origin", "perturbation_origin_id": 17 }
import numpy as np import pandas as pd import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: return None def generate_ans(data): none_input = data np.random.seed(0) r_old = np.random.randint...
Problem: How can I get get the position (indices) of the largest value in a multi-dimensional NumPy array `a`? Note that I want to get the raveled index of it, in C order. A: <code> import numpy as np a = np.array([[10,50,30],[60,20,40]]) </code> result = ... # put solution in this variable BEGIN SOLUTION <code>
result = a.argmax()
{ "problem_id": 309, "library_problem_id": 18, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 18 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([[10, 50, 30], [60, 20, 40]]) elif test_case_id == 2: np.random.seed(42) a = np.random.rand(np.random.randint(5, 10), np....
Problem: How can I get get the position (indices) of the smallest value in a multi-dimensional NumPy array `a`? Note that I want to get the raveled index of it, in C order. A: <code> import numpy as np a = np.array([[10,50,30],[60,20,40]]) </code> result = ... # put solution in this variable BEGIN SOLUTION <code>
result = a.argmin()
{ "problem_id": 310, "library_problem_id": 19, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 18 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([[10, 50, 30], [60, 20, 40]]) elif test_case_id == 2: np.random.seed(42) a = np.random.rand(np.random.randint(5, 10), np....
Problem: How can I get get the indices of the largest value in a multi-dimensional NumPy array `a`? Note that I want to get the unraveled index of it, in Fortran order. A: <code> import numpy as np a = np.array([[10,50,30],[60,20,40]]) </code> result = ... # put solution in this variable BEGIN SOLUTION <code>
result = np.unravel_index(a.argmax(), a.shape, order = 'F')
{ "problem_id": 311, "library_problem_id": 20, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 18 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([[10, 50, 30], [60, 20, 40]]) elif test_case_id == 2: np.random.seed(42) a = np.random.rand(np.random.randint(5, 10), np....
Problem: How can I get get the indices of the largest value in a multi-dimensional NumPy array `a`? Note that I want to get the unraveled index of it, in C order. A: <code> import numpy as np a = np.array([[10,50,30],[60,20,40]]) </code> result = ... # put solution in this variable BEGIN SOLUTION <code>
result = np.unravel_index(a.argmax(), a.shape)
{ "problem_id": 312, "library_problem_id": 21, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 18 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([[10, 50, 30], [60, 20, 40]]) elif test_case_id == 2: np.random.seed(42) a = np.random.rand(np.random.randint(5, 10), np....
Problem: How can I get get the position (indices) of the largest value in a multi-dimensional NumPy array `a`? Note that I want to get the raveled index of it, in C order. A: <code> import numpy as np example_a = np.array([[10,50,30],[60,20,40]]) def f(a = example_a): # return the solution in this function # re...
result = a.argmax() return result
{ "problem_id": 313, "library_problem_id": 22, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Surface", "perturbation_origin_id": 18 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([[10, 50, 30], [60, 20, 40]]) elif test_case_id == 2: np.random.seed(42) a = np.random.rand(np.random.randint(5, 10), np....
Problem: How can I get get the position (indices) of the second largest value in a multi-dimensional NumPy array `a`? All elements in a are positive for sure. Note that I want to get the unraveled index of it, in C order. A: <code> import numpy as np a = np.array([[10,50,30],[60,20,40]]) </code> result = ... # put solu...
idx = np.unravel_index(a.argmax(), a.shape) a[idx] = a.min() result = np.unravel_index(a.argmax(), a.shape)
{ "problem_id": 314, "library_problem_id": 23, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 18 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([[10, 50, 30], [60, 20, 40]]) elif test_case_id == 2: np.random.seed(42) a = np.random.rand(np.random.randint(5, 10), np....
Problem: I would like to delete selected columns in a numpy.array . This is what I do: n [397]: a = array([[ NaN, 2., 3., NaN], .....: [ 1., 2., 3., 9]]) #can be another array In [398]: print a [[ NaN 2. 3. NaN] [ 1. 2. 3. 9.]] In [399]: z = any(isnan(a), axis=0) In [400]: print z [ Tru...
z = np.any(np.isnan(a), axis = 0) a = a[:, ~z]
{ "problem_id": 315, "library_problem_id": 24, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 24 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([[np.nan, 2.0, 3.0, np.nan], [1.0, 2.0, 3.0, 9]]) elif test_case_id == 2: np.random.seed(42) a = np.random.rand(np.random...
Problem: I would like to delete selected rows in a numpy.array . n [397]: a = array([[ NaN, 2., 3., NaN], .....: [ 1., 2., 3., 9]]) #can be another array In [398]: print a [[ NaN 2. 3. NaN] [ 1. 2. 3. 9.]] In this example my goal is to delete all the rows that contain NaN. I expect the...
z = np.any(np.isnan(a), axis = 1) a = a[~z, :]
{ "problem_id": 316, "library_problem_id": 25, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 24 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([[np.nan, 2.0, 3.0, np.nan], [1.0, 2.0, 3.0, 9]]) elif test_case_id == 2: np.random.seed(42) a = np.random.rand(np.random...
Problem: I have a 2D list something like a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] and I want to convert it to a 2d numpy array. Can we do it without allocating memory like numpy.zeros((3,3)) and then storing values to it? A: <code> import numpy as np a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] </code> result = ... # put solut...
result = np.array(a)
{ "problem_id": 317, "library_problem_id": 26, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 26 }
import numpy as np import pandas as pd import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] elif test_case_id == 2: np.random.seed(42) a = np.random...
Problem: Is there a way to change the order of the columns in a numpy 2D array to a new and arbitrary order? For example, I have an array `a`: array([[10, 20, 30, 40, 50], [ 6, 7, 8, 9, 10]]) and I want to change it into, say array([[10, 30, 50, 40, 20], [ 6, 8, 10, 9, 7]]) by applying the permutati...
c = np.empty_like(permutation) c[permutation] = np.arange(len(permutation)) a = a[:, c]
{ "problem_id": 318, "library_problem_id": 27, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 27 }
import numpy as np import pandas as pd import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([[10, 20, 30, 40, 50], [6, 7, 8, 9, 10]]) permutation = [0, 4, 1, 3, 2] elif test_case_id == ...
Problem: Is there a way to change the order of the matrices in a numpy 3D array to a new and arbitrary order? For example, I have an array `a`: array([[[10, 20], [30, 40]], [[6, 7], [8, 9]], [[10, 11], [12, 13]]]) and I want to change it into, say array([[[6, 7], [8, 9]], [[10, 20...
c = np.empty_like(permutation) c[permutation] = np.arange(len(permutation)) result = a[c, :, :]
{ "problem_id": 319, "library_problem_id": 28, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 27 }
import numpy as np import pandas as pd import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([[[10, 20], [30, 40]], [[6, 7], [8, 9]], [[10, 11], [12, 13]]]) permutation = [1, 0, 2] elif ...
Problem: How can I know the (row, column) index of the minimum of a numpy array/matrix? For example, if A = array([[1, 2], [3, 0]]), I want to get (1, 1) Thanks! A: <code> import numpy as np a = np.array([[1, 2], [3, 0]]) </code> result = ... # put solution in this variable BEGIN SOLUTION <code>
result = np.unravel_index(a.argmin(), a.shape)
{ "problem_id": 320, "library_problem_id": 29, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 29 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([[1, 2], [3, 0]]) elif test_case_id == 2: np.random.seed(42) a = np.random.rand(5, 6) return a def generate_...
Problem: How can I know the (row, column) index of the maximum of a numpy array/matrix? For example, if A = array([[1, 2], [3, 0]]), I want to get (1, 0) Thanks! A: <code> import numpy as np a = np.array([[1, 2], [3, 0]]) </code> result = ... # put solution in this variable BEGIN SOLUTION <code>
result = np.unravel_index(a.argmax(), a.shape)
{ "problem_id": 321, "library_problem_id": 30, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 29 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([[1, 2], [3, 0]]) elif test_case_id == 2: np.random.seed(42) a = np.random.rand(5, 6) return a def generate_...
Problem: How can I know the (row, column) index of the minimum(might not be single) of a numpy array/matrix? For example, if A = array([[1, 0], [0, 2]]), I want to get [[0, 1], [1, 0]] In other words, the resulting indices should be ordered by the first axis first, the second axis next. Thanks! A: <code> import numpy ...
result = np.argwhere(a == np.min(a))
{ "problem_id": 322, "library_problem_id": 31, "library": "Numpy", "test_case_cnt": 3, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 29 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([[1, 2], [0, 0]]) elif test_case_id == 2: np.random.seed(42) a = np.random.randint(1, 5, (5, 6)) elif test_case_i...
Problem: I'm working on a problem that has to do with calculating angles of refraction and what not. However, it seems that I'm unable to use the numpy.sin() function in degrees. I have tried to use numpy.degrees() and numpy.rad2deg(). degree = 90 numpy.sin(degree) numpy.degrees(numpy.sin(degree)) Both return ~ 0.894 a...
result = np.sin(np.deg2rad(degree))
{ "problem_id": 323, "library_problem_id": 32, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 32 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: degree = 90 elif test_case_id == 2: np.random.seed(42) degree = np.random.randint(0, 360) return degree def generate_ans(...
Problem: I'm working on a problem that has to do with calculating angles of refraction and what not. However, it seems that I'm unable to use the numpy.cos() function in degrees. I have tried to use numpy.degrees() and numpy.rad2deg(). degree = 90 numpy.cos(degree) numpy.degrees(numpy.cos(degree)) But with no help. Ho...
result = np.cos(np.deg2rad(degree))
{ "problem_id": 324, "library_problem_id": 33, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 32 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: degree = 90 elif test_case_id == 2: np.random.seed(42) degree = np.random.randint(0, 360) return degree def generate_ans(...
Problem: Here is an interesting problem: whether a number is degree or radian depends on values of np.sin(). For instance, if sine value is bigger when the number is regarded as degree, then it is degree, otherwise it is radian. Your task is to help me confirm whether the number is a degree or a radian. The result is a...
deg = np.sin(np.deg2rad(number)) rad = np.sin(number) result = int(rad > deg)
{ "problem_id": 325, "library_problem_id": 34, "library": "Numpy", "test_case_cnt": 3, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 32 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: number = 4 elif test_case_id == 2: np.random.seed(43) number = np.random.randint(0, 360) elif test_case_id == 3: n...
Problem: I'm working on a problem that has to do with calculating angles of refraction and what not. What my trouble is, given a value of sine function, I want to find corresponding degree(ranging from -90 to 90) e.g. converting 1.0 to 90(degrees). Thanks for your help. A: <code> import numpy as np value = 1.0 </code> ...
result = np.degrees(np.arcsin(value))
{ "problem_id": 326, "library_problem_id": 35, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 32 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: value = 1.0 elif test_case_id == 2: np.random.seed(42) value = (np.random.rand() - 0.5) * 2 return value def generate_ans...
Problem: What's the more pythonic way to pad an array with zeros at the end? def pad(A, length): ... A = np.array([1,2,3,4,5]) pad(A, 8) # expected : [1,2,3,4,5,0,0,0] In my real use case, in fact I want to pad an array to the closest multiple of 1024. Ex: 1342 => 2048, 3000 => 3072, so I want non-loop solutio...
result = np.pad(A, (0, length-A.shape[0]), 'constant')
{ "problem_id": 327, "library_problem_id": 36, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 36 }
import numpy as np import pandas as pd import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: A = np.array([1, 2, 3, 4, 5]) length = 8 elif test_case_id == 2: np.random.seed(42) ...
Problem: What's the more pythonic way to pad an array with zeros at the end? def pad(A, length): ... A = np.array([1,2,3,4,5]) pad(A, 8) # expected : [1,2,3,4,5,0,0,0] pad(A, 3) # expected : [1,2,3,0,0] In my real use case, in fact I want to pad an array to the closest multiple of 1024. Ex: 1342 => 2048, 3...
if length > A.shape[0]: result = np.pad(A, (0, length-A.shape[0]), 'constant') else: result = A.copy() result[length:] = 0
{ "problem_id": 328, "library_problem_id": 37, "library": "Numpy", "test_case_cnt": 3, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 36 }
import numpy as np import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: A = np.array([1, 2, 3, 4, 5]) length = 8 elif test_case_id == 2: np.random.seed(42) A = np.random.rand...
Problem: I need to square a 2D numpy array (elementwise) and I have tried the following code: import numpy as np a = np.arange(4).reshape(2, 2) print(a^2, '\n') print(a*a) that yields: [[2 3] [0 1]] [[0 1] [4 9]] Clearly, the notation a*a gives me the result I want and not a^2. I would like to know if another notation ...
a = a ** power
{ "problem_id": 329, "library_problem_id": 38, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 38 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.arange(4).reshape(2, 2) power = 5 elif test_case_id == 2: np.random.seed(42) a = np.random.rand(np.random.randint(5...
Problem: I need to square a 2D numpy array (elementwise) and I have tried the following code: import numpy as np a = np.arange(4).reshape(2, 2) print(a^2, '\n') print(a*a) that yields: [[2 3] [0 1]] [[0 1] [4 9]] Clearly, the notation a*a gives me the result I want and not a^2. I would like to know if another notation ...
result = a ** power return result
{ "problem_id": 330, "library_problem_id": 39, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Surface", "perturbation_origin_id": 38 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.arange(4).reshape(2, 2) power = 5 elif test_case_id == 2: np.random.seed(42) a = np.random.rand(np.random.randint(5...
Problem: Does Python have a function to reduce fractions? For example, when I calculate 98/42 I want to get 7/3, not 2.3333333, is there a function for that using Python or Numpy? The result should be a tuple, namely (7, 3), the first for numerator and the second for denominator. A: <code> import numpy as np numerator ...
gcd = np.gcd(numerator, denominator) result = (numerator//gcd, denominator//gcd)
{ "problem_id": 331, "library_problem_id": 40, "library": "Numpy", "test_case_cnt": 3, "perturbation_type": "Origin", "perturbation_origin_id": 40 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: numerator = 98 denominator = 42 elif test_case_id == 2: np.random.seed(42) numerator = np.random.randint(2, 10) ...
Problem: Does Python have a function to reduce fractions? For example, when I calculate 98/42 I want to get 7/3, not 2.3333333, is there a function for that using Python or Numpy? The result should be a tuple, namely (7, 3), the first for numerator and the second for denominator. A: <code> import numpy as np def f(nume...
gcd = np.gcd(numerator, denominator) result = (numerator//gcd, denominator//gcd) return result
{ "problem_id": 332, "library_problem_id": 41, "library": "Numpy", "test_case_cnt": 3, "perturbation_type": "Surface", "perturbation_origin_id": 40 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: numerator = 98 denominator = 42 elif test_case_id == 2: np.random.seed(42) numerator = np.random.randint(2, 10) ...
Problem: Does Python have a function to reduce fractions? For example, when I calculate 98/42 I want to get 7/3, not 2.3333333, is there a function for that using Python or Numpy? The result should be a tuple, namely (7, 3), the first for numerator and the second for denominator. IF the dominator is zero, result should...
if denominator == 0: result = (np.nan, np.nan) else: gcd = np.gcd(numerator, denominator) result = (numerator//gcd, denominator//gcd)
{ "problem_id": 333, "library_problem_id": 42, "library": "Numpy", "test_case_cnt": 4, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 40 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: numerator = 98 denominator = 42 elif test_case_id == 2: np.random.seed(42) numerator = np.random.randint(2, 10) ...
Problem: I'd like to calculate element-wise average of numpy ndarrays. For example In [56]: a = np.array([10, 20, 30]) In [57]: b = np.array([30, 20, 20]) In [58]: c = np.array([50, 20, 40]) What I want: [30, 20, 30] A: <code> import numpy as np a = np.array([10, 20, 30]) b = np.array([30, 20, 20]) c = np.array([50, 20...
result = np.mean([a, b, c], axis=0)
{ "problem_id": 334, "library_problem_id": 43, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 43 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([10, 20, 30]) b = np.array([30, 20, 20]) c = np.array([50, 20, 40]) elif test_case_id == 2: ...
Problem: I'd like to calculate element-wise maximum of numpy ndarrays. For example In [56]: a = np.array([10, 20, 30]) In [57]: b = np.array([30, 20, 20]) In [58]: c = np.array([50, 20, 40]) What I want: [50, 20, 40] A: <code> import numpy as np a = np.array([10, 20, 30]) b = np.array([30, 20, 20]) c = np.array([50, 20...
result = np.max([a, b, c], axis=0)
{ "problem_id": 335, "library_problem_id": 44, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 43 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([10, 20, 30]) b = np.array([30, 20, 20]) c = np.array([50, 20, 40]) elif test_case_id == 2: ...
Problem: So in numpy arrays there is the built in function for getting the diagonal indices, but I can't seem to figure out how to get the diagonal starting from the top right rather than top left. This is the normal code to get starting from the top left, assuming processing on 5x5 array: >>> import numpy as np >>> a ...
result = np.diag(np.fliplr(a))
{ "problem_id": 336, "library_problem_id": 45, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 45 }
import numpy as np import pandas as pd import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array( [ [0, 1, 2, 3, 4], [5, 6, 7, 8, 9], ...
Problem: So in numpy arrays there is the built in function for getting the diagonal indices, but I can't seem to figure out how to get the diagonal starting from the top right rather than top left. This is the normal code to get starting from the top left, assuming processing on 5x6 array: >>> import numpy as np >>> a ...
result = np.diag(np.fliplr(a))
{ "problem_id": 337, "library_problem_id": 46, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Surface", "perturbation_origin_id": 45 }
import numpy as np import pandas as pd import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array( [ [0, 1, 2, 3, 4, 5], [5, 6, 7, 8, 9, 10], ...
Problem: So in numpy arrays there is the built in function for getting the diagonal indices, but I can't seem to figure out how to get the diagonal starting from the top right rather than top left. This is the normal code to get starting from the top left, assuming processing on 5x5 array: >>> import numpy as np >>> a ...
result = np.vstack((np.diag(a), np.diag(np.fliplr(a))))
{ "problem_id": 338, "library_problem_id": 47, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 45 }
import numpy as np import pandas as pd import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array( [ [0, 1, 2, 3, 4], [5, 6, 7, 8, 9], ...
Problem: So in numpy arrays there is the built in function for getting the diagonal indices, but I can't seem to figure out how to get the diagonal ending at bottom left rather than botton right(might not on the corner for non-square matrix). This is the normal code to get starting from the top left, assuming processin...
dim = min(a.shape) b = a[:dim,:dim] result = np.vstack((np.diag(b), np.diag(np.fliplr(b))))
{ "problem_id": 339, "library_problem_id": 48, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 45 }
import numpy as np import pandas as pd import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array( [ [0, 1, 2, 3, 4, 5], [5, 6, 7, 8, 9, 10], ...
Problem: I have created a multidimensional array in Python like this: self.cells = np.empty((r,c),dtype=np.object) Now I want to iterate through all elements of my two-dimensional array `X` and store element at each moment in result (an 1D list). I do not care about the order. How do I achieve this? A: <code> import nu...
result = [] for value in X.flat: result.append(value)
{ "problem_id": 340, "library_problem_id": 49, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Origin", "perturbation_origin_id": 49 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: np.random.seed(42) X = np.random.randint(2, 10, (5, 6)) return X def generate_ans(data): _a = data X = _a...
Problem: I have created a multidimensional array in Python like this: self.cells = np.empty((r,c),dtype=np.object) Now I want to iterate through all elements of my two-dimensional array `X` and store element at each moment in result (an 1D list), in 'C' order. How do I achieve this? A: <code> import numpy as np X = np....
result = [] for value in X.flat: result.append(value)
{ "problem_id": 341, "library_problem_id": 50, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Semantic", "perturbation_origin_id": 49 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: np.random.seed(42) X = np.random.randint(2, 10, (5, 6)) return X def generate_ans(data): _a = data X = _a...
Problem: I have created a multidimensional array in Python like this: self.cells = np.empty((r,c),dtype=np.object) Now I want to iterate through all elements of my two-dimensional array `X` and store element at each moment in result (an 1D list). I do not care about the order. How do I achieve this? A: <code> import nu...
result = [] for value in X.flat: result.append(value) return result
{ "problem_id": 342, "library_problem_id": 51, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Surface", "perturbation_origin_id": 49 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: np.random.seed(42) X = np.random.randint(2, 10, (5, 6)) return X def generate_ans(data): _a = data X = _a...
Problem: I have created a multidimensional array in Python like this: self.cells = np.empty((r,c),dtype=np.object) Now I want to iterate through all elements of my two-dimensional array `X` and store element at each moment in result (an 1D list), in 'Fortran' order. How do I achieve this? A: <code> import numpy as np X...
result = [] for value in X.T.flat: result.append(value)
{ "problem_id": 343, "library_problem_id": 52, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 49 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: np.random.seed(42) X = np.random.randint(2, 10, (5, 6)) return X def generate_ans(data): _a = data X = _a...
Problem: Example Input: mystr = "100110" Desired output numpy array(of integers): result == np.array([1, 0, 0, 1, 1, 0]) I have tried: np.fromstring(mystr, dtype=int, sep='') but the problem is I can't split my string to every digit of it, so numpy takes it as an one number. Any idea how to convert my string to numpy a...
result = np.array(list(mystr), dtype = int)
{ "problem_id": 344, "library_problem_id": 53, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 53 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: mystr = "100110" elif test_case_id == 2: mystr = "987543" return mystr def generate_ans(data): _a = data ...
Problem: I need to do some analysis on a large dataset from a hydrolgeology field work. I am using NumPy. I want to know how I can: 1. multiply e.g. the col-th column of my array by a number (e.g. 5.2). And then 2. calculate the cumulative sum of the numbers in that column. As I mentioned I only want to work on a speci...
a[:, col-1] *= multiply_number result = np.cumsum(a[:, col-1])
{ "problem_id": 345, "library_problem_id": 54, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 54 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: np.random.seed(42) a = np.random.rand(8, 5) col = 2 const = 5.2 elif test_case_id == 2: np.random.seed(42) ...
Problem: I need to do some analysis on a large dataset from a hydrolgeology field work. I am using NumPy. I want to know how I can: 1. multiply e.g. the row-th row of my array by a number (e.g. 5.2). And then 2. calculate the cumulative sum of the numbers in that row. As I mentioned I only want to work on a specific ro...
a[row-1, :] *= multiply_number result = np.cumsum(a[row-1, :])
{ "problem_id": 346, "library_problem_id": 55, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 54 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: np.random.seed(42) a = np.random.rand(8, 5) row = 2 const = 5.2 elif test_case_id == 2: np.random.seed(42) ...
Problem: I need to do some analysis on a large dataset from a hydrolgeology field work. I am using NumPy. I want to know how I can: 1. divide e.g. the row-th row of my array by a number (e.g. 5.2). And then 2. calculate the multiplication of the numbers in that row. As I mentioned I only want to work on a specific row ...
a[row-1, :] /= divide_number result = np.multiply.reduce(a[row-1, :])
{ "problem_id": 347, "library_problem_id": 56, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 54 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: np.random.seed(42) a = np.random.rand(8, 5) row = 2 const = 5.2 elif test_case_id == 2: np.random.seed(42) ...
Problem: How to get one maximal set of linearly independent vectors of a given matrix `a`? For example, [[0 1 0 0], [0 0 1 0], [1 0 0 1]] in [[0 1 0 0], [0 0 1 0], [0 1 1 0], [1 0 0 1]] A: <code> import numpy as np a = np.array([[0,1,0,0], [0,0,1,0], [0,1,1,0], [1,0,0,1]]) </code> result = ... # put solution in this va...
def LI_vecs(M): dim = M.shape[0] LI=[M[0]] for i in range(dim): tmp=[] for r in LI: tmp.append(r) tmp.append(M[i]) #set tmp=LI+[M[i]] if np.linalg.matrix_rank(tmp)>len(LI): #test if M[i] is linearly independent from all (row) vectors in LI ...
{ "problem_id": 348, "library_problem_id": 57, "library": "Numpy", "test_case_cnt": 3, "perturbation_type": "Origin", "perturbation_origin_id": 57 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([[0, 1, 0, 0], [0, 0, 1, 0], [0, 1, 1, 0], [1, 0, 0, 1]]) elif test_case_id == 2: a = np.array( [...
Problem: How do i get the length of the row in a 2D array? example, i have a nD array called a. when i print a.shape, it returns (1,21). I want to do a for loop, in the range of the row size (21) of the array a. How do i get the value of row size as result? A: <code> import numpy as np a = np.random.rand(np.random.rand...
result = a.shape[1]
{ "problem_id": 349, "library_problem_id": 58, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Origin", "perturbation_origin_id": 58 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: np.random.seed(42) a = np.random.rand(np.random.randint(5, 10), np.random.randint(6, 10)) return a def generate_ans(data)...
Problem: I have data of sample 1 and sample 2 (`a` and `b`) – size is different for sample 1 and sample 2. I want to do a weighted (take n into account) two-tailed t-test. I tried using the scipy.stat module by creating my numbers with np.random.normal, since it only takes data and not stat values like mean and std dev...
_, p_value = scipy.stats.ttest_ind(a, b, equal_var = False)
{ "problem_id": 350, "library_problem_id": 59, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Origin", "perturbation_origin_id": 59 }
import numpy as np import copy import scipy.stats def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: np.random.seed(42) a = np.random.randn(40) b = 4 * np.random.randn(50) return a, b def generate_ans(data): ...
Problem: I have data of sample 1 and sample 2 (`a` and `b`) – size is different for sample 1 and sample 2. I want to do a weighted (take n into account) two-tailed t-test. I tried using the scipy.stat module by creating my numbers with np.random.normal, since it only takes data and not stat values like mean and std dev...
_, p_value = scipy.stats.ttest_ind(a, b, equal_var = False, nan_policy = 'omit')
{ "problem_id": 351, "library_problem_id": 60, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 59 }
import numpy as np import copy import scipy.stats def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: np.random.seed(42) a = np.random.randn(40) b = 4 * np.random.randn(50) elif test_case_id == 2: np.rando...
Problem: I have only the summary statistics of sample 1 and sample 2, namely mean, variance, nobs(number of observations). I want to do a weighted (take n into account) two-tailed t-test. Any help on how to get the p-value would be highly appreciated. A: <code> import numpy as np import scipy.stats amean = -0.0896 avar...
_, p_value = scipy.stats.ttest_ind_from_stats(amean, np.sqrt(avar), anobs, bmean, np.sqrt(bvar), bnobs, equal_var=False)
{ "problem_id": 352, "library_problem_id": 61, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 59 }
import numpy as np import copy import scipy.stats def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: amean = -0.0896 avar = 0.954 anobs = 40 bmean = 0.719 bvar = 11.87 bnobs = 50 r...
Problem: Say I have these 2D arrays A and B. How can I remove elements from A that are in B. (Complement in set theory: A-B) Example: A=np.asarray([[1,1,1], [1,1,2], [1,1,3], [1,1,4]]) B=np.asarray([[0,0,0], [1,0,2], [1,0,3], [1,0,4], [1,1,0], [1,1,1], [1,1,4]]) #in original order #output = [[1,1,2], [1,1,3]] A: <code...
dims = np.maximum(B.max(0),A.max(0))+1 output = A[~np.in1d(np.ravel_multi_index(A.T,dims),np.ravel_multi_index(B.T,dims))]
{ "problem_id": 353, "library_problem_id": 62, "library": "Numpy", "test_case_cnt": 3, "perturbation_type": "Origin", "perturbation_origin_id": 62 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: A = np.asarray([[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 1, 4]]) B = np.asarray( [ [0, 0, 0], [1, 0, 2...
Problem: Say I have these 2D arrays A and B. How can I get elements from A that are not in B, and those from B that are not in A? (Symmetric difference in set theory: A△B) Example: A=np.asarray([[1,1,1], [1,1,2], [1,1,3], [1,1,4]]) B=np.asarray([[0,0,0], [1,0,2], [1,0,3], [1,0,4], [1,1,0], [1,1,1], [1,1,4]]) #elements ...
dims = np.maximum(B.max(0),A.max(0))+1 result = A[~np.in1d(np.ravel_multi_index(A.T,dims),np.ravel_multi_index(B.T,dims))] output = np.append(result, B[~np.in1d(np.ravel_multi_index(B.T,dims),np.ravel_multi_index(A.T,dims))], axis = 0)
{ "problem_id": 354, "library_problem_id": 63, "library": "Numpy", "test_case_cnt": 3, "perturbation_type": "Semantic", "perturbation_origin_id": 62 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: A = np.asarray([[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 1, 4]]) B = np.asarray( [ [0, 0, 0], ...
Problem: Similar to this answer, I have a pair of 3D numpy arrays, a and b, and I want to sort the entries of b by the values of a. Unlike this answer, I want to sort only along one axis of the arrays. My naive reading of the numpy.argsort() documentation: Returns ------- index_array : ndarray, int Array of indices...
sort_indices = np.argsort(a, axis=0) static_indices = np.indices(a.shape) c = b[sort_indices, static_indices[1], static_indices[2]]
{ "problem_id": 355, "library_problem_id": 64, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Origin", "perturbation_origin_id": 64 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: np.random.seed(42) a = np.random.rand(3, 3, 3) b = np.arange(3 * 3 * 3).reshape((3, 3, 3)) return a, b def generate_ans(data): ...
Problem: Similar to this answer, I have a pair of 3D numpy arrays, a and b, and I want to sort the entries of b by the values of a. Unlike this answer, I want to sort only along one axis of the arrays. My naive reading of the numpy.argsort() documentation: Returns ------- index_array : ndarray, int Array of indices...
sort_indices = np.argsort(a, axis=0) static_indices = np.indices(a.shape) c = b[sort_indices, static_indices[1], static_indices[2]]
{ "problem_id": 356, "library_problem_id": 65, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Surface", "perturbation_origin_id": 64 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: np.random.seed(42) a = np.random.rand(3, 3, 3) b = np.arange(3 * 3 * 3).reshape((3, 3, 3)) return a, b def generate_ans(data): ...
Problem: Similar to this answer, I have a pair of 3D numpy arrays, a and b, and I want to sort the entries of b by the values of a. Unlike this answer, I want to sort only along one axis of the arrays, in decreasing order. My naive reading of the numpy.argsort() documentation: Returns ------- index_array : ndarray, int...
sort_indices = np.argsort(a, axis=0)[::-1, :, :] static_indices = np.indices(a.shape) c = b[sort_indices, static_indices[1], static_indices[2]]
{ "problem_id": 357, "library_problem_id": 66, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Semantic", "perturbation_origin_id": 64 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: np.random.seed(42) a = np.random.rand(3, 3, 3) b = np.arange(3 * 3 * 3).reshape((3, 3, 3)) return a, b def generate_ans(data): ...
Problem: Similar to this answer, I have a pair of 3D numpy arrays, a and b, and I want to sort the matrices of b by the values of a. Unlike this answer, I want to sort the matrices according to their sum. My naive reading of the numpy.argsort() documentation: Returns ------- index_array : ndarray, int Array of indi...
index = np.argsort(a.sum(axis = (1, 2))) result = b[index, :, :]
{ "problem_id": 358, "library_problem_id": 67, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 64 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: np.random.seed(42) a = np.random.rand(3, 3, 3) b = np.arange(3 * 3 * 3).reshape((3, 3, 3)) return a, b def generate_ans(data): ...
Problem: >>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) >>> arr array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]]) I am deleting the 3rd column array([[ 1, 2, 4], [ 5, 6, 8], [ 9, 10, 12]]) Are there any good way ? Please consider this to be a novice question. A: <...
a = np.delete(a, 2, axis = 1)
{ "problem_id": 359, "library_problem_id": 68, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 68 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.arange(12).reshape(3, 4) elif test_case_id == 2: a = np.ones((3, 3)) return a def generate_ans(data): _a = data ...
Problem: >>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) >>> arr array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]]) I am deleting the 3rd row array([[ 1, 2, 3, 4], [ 5, 6, 7, 8]]) Are there any good way ? Please consider this to be a novice question. A: <code> import n...
a = np.delete(a, 2, axis = 0)
{ "problem_id": 360, "library_problem_id": 69, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 68 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.arange(12).reshape(3, 4) elif test_case_id == 2: a = np.ones((4, 4)) return a def generate_ans(data): _a = data ...
Problem: >>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) >>> arr array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]]) I am deleting the 1st and 3rd column array([[ 2, 4], [ 6, 8], [ 10, 12]]) Are there any good way ? Please consider this to be a novice question. A: <code...
temp = np.array([0, 2]) a = np.delete(a, temp, axis = 1)
{ "problem_id": 361, "library_problem_id": 70, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 68 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.arange(12).reshape(3, 4) elif test_case_id == 2: a = np.ones((6, 6)) return a def generate_ans(data): _a = data ...
Problem: >>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) >>> del_col = [1, 2, 4, 5] >>> arr array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]]) I am deleting some columns(in this example, 1st, 2nd and 4th) def_col = np.array([1, 2, 4, 5]) array([[ 3], [ 7], [ 11]]) Note t...
mask = (del_col <= a.shape[1]) del_col = del_col[mask] - 1 result = np.delete(a, del_col, axis=1)
{ "problem_id": 362, "library_problem_id": 71, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 68 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.arange(12).reshape(3, 4) del_col = np.array([1, 2, 4, 5]) elif test_case_id == 2: np.random.seed(42) a = np.random....
Problem: Lists have a very simple method to insert elements: a = [1,2,3,4] a.insert(2,66) print a [1, 2, 66, 3, 4] For a numpy array I could do: a = np.asarray([1,2,3,4]) a_l = a.tolist() a_l.insert(2,66) a = np.asarray(a_l) print a [1 2 66 3 4] but this is very convoluted. Is there an insert equivalent for numpy array...
a = np.insert(a, pos, element)
{ "problem_id": 363, "library_problem_id": 72, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 72 }
import numpy as np import pandas as pd import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.asarray([1, 2, 3, 4]) pos = 2 element = 66 elif test_case_id == 2: np.rando...
Problem: Lists have a very simple method to insert elements: a = [1,2,3,4] a.insert(2,66) print a [1, 2, 66, 3, 4] However, I’m confused about how to insert a row into an 2-dimensional array. e.g. changing array([[1,2],[3,4]]) into array([[1,2],[3,5],[3,4]]) A: <code> import numpy as np a = np.array([[1,2],[3,4]]) pos...
a = np.insert(a, pos, element, axis = 0)
{ "problem_id": 364, "library_problem_id": 73, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 72 }
import numpy as np import pandas as pd import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([[1, 2], [3, 4]]) pos = 1 element = [3, 5] elif test_case_id == 2: np...
Problem: Lists have a very simple method to insert elements: a = [1,2,3,4] a.insert(2,66) print a [1, 2, 66, 3, 4] For a numpy array I could do: a = np.asarray([1,2,3,4]) a_l = a.tolist() a_l.insert(2,66) a = np.asarray(a_l) print a [1 2 66 3 4] but this is very convoluted. Is there an insert equivalent for numpy array...
a = np.insert(a, pos, element) return a
{ "problem_id": 365, "library_problem_id": 74, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Surface", "perturbation_origin_id": 72 }
import numpy as np import pandas as pd import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.asarray([1, 2, 3, 4]) pos = 2 element = 66 elif test_case_id == 2: np.rando...
Problem: Lists have a very simple method to insert elements: a = [1,2,3,4] a.insert(2,66) print a [1, 2, 66, 3, 4] However, I’m confused about how to insert multiple rows into an 2-dimensional array. Meanwhile, I want the inserted rows located in given indices in a. e.g. a = array([[1,2],[3,4]]) element = array([[3, 5...
pos = np.array(pos) - np.arange(len(element)) a = np.insert(a, pos, element, axis=0)
{ "problem_id": 366, "library_problem_id": 75, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 72 }
import numpy as np import pandas as pd import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([[1, 2], [3, 4]]) pos = [1, 2] element = np.array([[3, 5], [6, 6]]) elif test_cas...
Problem: I have a numpy array of different numpy arrays and I want to make a deep copy of the arrays. I found out the following: import numpy as np pairs = [(2, 3), (3, 4), (4, 5)] array_of_arrays = np.array([np.arange(a*b).reshape(a,b) for (a, b) in pairs]) a = array_of_arrays[:] # Does not work b = array_of_arrays[:]...
import copy result = copy.deepcopy(array_of_arrays)
{ "problem_id": 367, "library_problem_id": 76, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Origin", "perturbation_origin_id": 76 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: pairs = [(2, 3), (3, 4), (4, 5)] array_of_arrays = np.array( [np.arange(a * b).reshape(a, b) for (a, b) in pairs], dty...
Problem: In numpy, is there a nice idiomatic way of testing if all rows are equal in a 2d array? I can do something like np.all([np.array_equal(a[0], a[i]) for i in xrange(1,len(a))]) This seems to mix python lists with numpy arrays which is ugly and presumably also slow. Is there a nicer/neater way? A: <code> import n...
result = np.isclose(a, a[0], atol=0).all()
{ "problem_id": 368, "library_problem_id": 77, "library": "Numpy", "test_case_cnt": 5, "perturbation_type": "Origin", "perturbation_origin_id": 77 }
import numpy as np import pandas as pd import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.repeat(np.arange(1, 6).reshape(1, -1), 3, axis=0) elif test_case_id == 2: np.random.seed(42) ...
Problem: In numpy, is there a nice idiomatic way of testing if all columns are equal in a 2d array? I can do something like np.all([np.array_equal(a[0], a[i]) for i in xrange(1,len(a))]) This seems to mix python lists with numpy arrays which is ugly and presumably also slow. Is there a nicer/neater way? A: <code> impor...
result =np.isclose(a, a[:, 0].reshape(-1, 1), atol=0).all()
{ "problem_id": 369, "library_problem_id": 78, "library": "Numpy", "test_case_cnt": 5, "perturbation_type": "Semantic", "perturbation_origin_id": 77 }
import numpy as np import pandas as pd import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.repeat(np.arange(1, 6).reshape(1, -1), 3, axis=0) elif test_case_id == 2: np.random.seed(42) ...
Problem: In numpy, is there a nice idiomatic way of testing if all rows are equal in a 2d array? I can do something like np.all([np.array_equal(a[0], a[i]) for i in xrange(1,len(a))]) This seems to mix python lists with numpy arrays which is ugly and presumably also slow. Is there a nicer/neater way? A: <code> import n...
result = np.isclose(a, a[0], atol=0).all() return result
{ "problem_id": 370, "library_problem_id": 79, "library": "Numpy", "test_case_cnt": 5, "perturbation_type": "Surface", "perturbation_origin_id": 77 }
import numpy as np import pandas as pd import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.repeat(np.arange(1, 6).reshape(1, -1), 3, axis=0) elif test_case_id == 2: np.random.seed(42) ...
Problem: SciPy has three methods for doing 1D integrals over samples (trapz, simps, and romb) and one way to do a 2D integral over a function (dblquad), but it doesn't seem to have methods for doing a 2D integral over samples -- even ones on a rectangular grid. The closest thing I see is scipy.interpolate.RectBivariate...
from scipy.integrate import simpson z = np.cos(x[:,None])**4 + np.sin(y)**2 result = simpson(simpson(z, y), x)
{ "problem_id": 371, "library_problem_id": 80, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 80 }
import numpy as np import copy import scipy from scipy.integrate import simpson def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: x = np.linspace(0, 1, 20) y = np.linspace(0, 1, 30) elif test_case_id == 2: x = np.l...
Problem: SciPy has three methods for doing 1D integrals over samples (trapz, simps, and romb) and one way to do a 2D integral over a function (dblquad), but it doesn't seem to have methods for doing a 2D integral over samples -- even ones on a rectangular grid. The closest thing I see is scipy.interpolate.RectBivariate...
from scipy.integrate import simpson z = np.cos(x[:,None])**4 + np.sin(y)**2 result = simpson(simpson(z, y), x) return result
{ "problem_id": 372, "library_problem_id": 81, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Surface", "perturbation_origin_id": 80 }
import numpy as np import copy import scipy from scipy.integrate import simpson def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: x = np.linspace(0, 1, 20) y = np.linspace(0, 1, 30) elif test_case_id == 2: x = np.l...
Problem: What is the equivalent of R's ecdf(x)(x) function in Python, in either numpy or scipy? Is ecdf(x)(x) basically the same as: import numpy as np def ecdf(x): # normalize X to sum to 1 x = x / np.sum(x) return np.cumsum(x) or is something else required? By default R's ecdf will return function values of el...
def ecdf_result(x): xs = np.sort(x) ys = np.arange(1, len(xs)+1)/float(len(xs)) return ys result = ecdf_result(grades)
{ "problem_id": 373, "library_problem_id": 82, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 82 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: grades = np.array( ( 93.5, 93, 60.8, 94.5, ...
Problem: What is the equivalent of R's ecdf(x)(x) function in Python, in either numpy or scipy? Is ecdf(x)(x) basically the same as: import numpy as np def ecdf(x): # normalize X to sum to 1 x = x / np.sum(x) return np.cumsum(x) or is something else required? What I want to do is to apply the generated ECDF func...
def ecdf_result(x): xs = np.sort(x) ys = np.arange(1, len(xs)+1)/float(len(xs)) return xs, ys resultx, resulty = ecdf_result(grades) result = np.zeros_like(eval, dtype=float) for i, element in enumerate(eval): if element < resultx[0]: result[i] = 0 elif element >= resultx[-1]: result...
{ "problem_id": 374, "library_problem_id": 83, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 82 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: grades = np.array( ( 93.5, 93, 60.8, 94.5, ...
Problem: What is the equivalent of R's ecdf(x)(x) function in Python, in either numpy or scipy? Is ecdf(x)(x) basically the same as: import numpy as np def ecdf(x): # normalize X to sum to 1 x = x / np.sum(x) return np.cumsum(x) or is something else required? Further, I want to compute the longest interval [low,...
def ecdf_result(x): xs = np.sort(x) ys = np.arange(1, len(xs)+1)/float(len(xs)) return xs, ys resultx, resulty = ecdf_result(grades) t = (resulty > threshold).argmax() low = resultx[0] high = resultx[t]
{ "problem_id": 375, "library_problem_id": 84, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 82 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: grades = np.array( ( 93.5, 93, 60.8, 94.5, ...
Problem: I want to generate a random array of size N which only contains 0 and 1, I want my array to have some ratio between 0 and 1. For example, 90% of the array be 1 and the remaining 10% be 0 (I want this 90% to be random along with the whole array). right now I have: randomLabel = np.random.randint(2, size=numbers...
nums = np.ones(size) nums[:int(size*(1-one_ratio))] = 0 np.random.shuffle(nums)
{ "problem_id": 376, "library_problem_id": 85, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 85 }
import numpy as np import pandas as pd import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: one_ratio = 0.9 size = 1000 elif test_case_id == 2: size = 100 one_ratio = 0.8 ...
Problem: How do I convert a torch tensor to numpy? A: <code> import torch import numpy as np a = torch.ones(5) </code> a_np = ... # put solution in this variable BEGIN SOLUTION <code>
a_np = a.numpy()
{ "problem_id": 377, "library_problem_id": 86, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 86 }
import numpy as np import pandas as pd import torch import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = torch.ones(5) elif test_case_id == 2: a = torch.tensor([1, 1, 4, 5, 1, 4]) return a def generate...
Problem: How do I convert a numpy array to pytorch tensor? A: <code> import torch import numpy as np a = np.ones(5) </code> a_pt = ... # put solution in this variable BEGIN SOLUTION <code>
a_pt = torch.Tensor(a)
{ "problem_id": 378, "library_problem_id": 87, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 86 }
import numpy as np import pandas as pd import torch import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.ones(5) elif test_case_id == 2: a = np.array([1, 1, 4, 5, 1, 4]) return a def generate_ans(da...
Problem: How do I convert a tensorflow tensor to numpy? A: <code> import tensorflow as tf import numpy as np a = tf.ones([2,3,4]) </code> a_np = ... # put solution in this variable BEGIN SOLUTION <code>
a_np = a.numpy()
{ "problem_id": 379, "library_problem_id": 88, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 88 }
import numpy as np import pandas as pd import tensorflow as tf import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = tf.ones([2, 3, 4]) elif test_case_id == 2: a = tf.zeros([3, 4]) return a def generate...
Problem: How do I convert a numpy array to tensorflow tensor? A: <code> import tensorflow as tf import numpy as np a = np.ones([2,3,4]) </code> a_tf = ... # put solution in this variable BEGIN SOLUTION <code>
a_tf = tf.convert_to_tensor(a)
{ "problem_id": 380, "library_problem_id": 89, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 88 }
import numpy as np import pandas as pd import tensorflow as tf import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.ones([2, 3, 4]) elif test_case_id == 2: a = np.array([1, 1, 4, 5, 1, 4]) return a ...
Problem: I'm sorry in advance if this is a duplicated question, I looked for this information but still couldn't find it. Is it possible to get a numpy array (or python list) filled with the indexes of the elements in decreasing order? For instance, the array: a = array([4, 1, 0, 8, 5, 2]) The indexes of the elements i...
result = np.argsort(a)[::-1][:len(a)]
{ "problem_id": 381, "library_problem_id": 90, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 90 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([4, 1, 0, 8, 5, 2]) elif test_case_id == 2: np.random.seed(42) a = (np.random.rand(100) - 0.5) * 100 return a ...
Problem: I'm sorry in advance if this is a duplicated question, I looked for this information but still couldn't find it. Is it possible to get a numpy array (or python list) filled with the indexes of the elements in increasing order? For instance, the array: a = array([4, 1, 0, 8, 5, 2]) The indexes of the elements i...
result = np.argsort(a)
{ "problem_id": 382, "library_problem_id": 91, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 90 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([4, 1, 0, 8, 5, 2]) elif test_case_id == 2: np.random.seed(42) a = (np.random.rand(100) - 0.5) * 100 return a ...
Problem: I'm sorry in advance if this is a duplicated question, I looked for this information but still couldn't find it. Is it possible to get a numpy array (or python list) filled with the indexes of the N biggest elements in decreasing order? For instance, the array: a = array([4, 1, 0, 8, 5, 2]) The indexes of the ...
result = np.argsort(a)[::-1][:N]
{ "problem_id": 383, "library_problem_id": 92, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 90 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array([4, 1, 0, 8, 5, 2]) N = 3 elif test_case_id == 2: np.random.seed(42) a = (np.random.rand(100) - 0.5) * 100 ...
Problem: I want to raise a 2-dimensional numpy array, let's call it A, to the power of some number n, but I have thus far failed to find the function or operator to do that. I'm aware that I could cast it to the matrix type and use the fact that then (similar to what would be the behaviour in Matlab), A**n does just w...
result = np.linalg.matrix_power(A, n)
{ "problem_id": 384, "library_problem_id": 93, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 93 }
import numpy as np import pandas as pd import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: A = np.arange(16).reshape(4, 4) n = 5 elif test_case_id == 2: np.random.seed(42) d...
Problem: I have a 2-d numpy array as follows: a = np.array([[1,5,9,13], [2,6,10,14], [3,7,11,15], [4,8,12,16]] I want to extract it into patches of 2 by 2 sizes with out repeating the elements. The answer should exactly be the same. This can be 3-d array or list with the same o...
result = a.reshape(a.shape[0]//2, 2, a.shape[1]//2, 2).swapaxes(1, 2).transpose(1, 0, 2, 3).reshape(-1, 2, 2)
{ "problem_id": 385, "library_problem_id": 94, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 94 }
import numpy as np import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array( [[1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]] ) elif test_case_id == 2: ...
Problem: I have a 2-d numpy array as follows: a = np.array([[1,5,9,13], [2,6,10,14], [3,7,11,15], [4,8,12,16]] I want to extract it into patches of 2 by 2 sizes like sliding window. The answer should exactly be the same. This can be 3-d array or list with the same order of elem...
result = np.lib.stride_tricks.sliding_window_view(a, window_shape=(2,2)).reshape(-1, 2, 2)
{ "problem_id": 386, "library_problem_id": 95, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 94 }
import numpy as np import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array( [[1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]] ) elif test_case_id == 2: ...
Problem: I have a 2-d numpy array as follows: a = np.array([[1,5,9,13], [2,6,10,14], [3,7,11,15], [4,8,12,16]] I want to extract it into patches of 2 by 2 sizes with out repeating the elements. The answer should exactly be the same. This can be 3-d array or list with the same o...
result = a.reshape(a.shape[0]//2, 2, a.shape[1]//2, 2).swapaxes(1, 2).reshape(-1, 2, 2)
{ "problem_id": 387, "library_problem_id": 96, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 94 }
import numpy as np import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array( [[1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]] ) elif test_case_id == 2: ...
Problem: I have a 2-d numpy array as follows: a = np.array([[1,5,9,13,17], [2,6,10,14,18], [3,7,11,15,19], [4,8,12,16,20]] I want to extract it into patches of 2 by 2 sizes with out repeating the elements. Pay attention that if the shape is indivisible by patch size, we would j...
x = a[:a.shape[0] // patch_size * patch_size, :a.shape[1] // patch_size * patch_size] result = x.reshape(x.shape[0]//patch_size, patch_size, x.shape[1]// patch_size, patch_size).swapaxes(1, 2). reshape(-1, patch_size, patch_size)
{ "problem_id": 388, "library_problem_id": 97, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 94 }
import numpy as np import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array( [ [1, 5, 9, 13, 17], [2, 6, 10, 14, 18], [3, 7, 11, 15, ...
Problem: I'm looking for a generic method to from the original big array from small arrays: array([[[ 0, 1, 2], [ 6, 7, 8]], [[ 3, 4, 5], [ 9, 10, 11]], [[12, 13, 14], [18, 19, 20]], [[15, 16, 17], [21, 22, 23]]]) -> # result array's shape: (h = 4, w =...
n, nrows, ncols = a.shape result = a.reshape(h//nrows, -1, nrows, ncols).swapaxes(1,2).reshape(h, w)
{ "problem_id": 389, "library_problem_id": 98, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 94 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array( [ [[0, 1, 2], [6, 7, 8]], [[3, 4, 5], [9, 10, 11]], [[12, 13, 14], [18, 19, ...
Problem: I have a 2-d numpy array as follows: a = np.array([[1,5,9,13,17], [2,6,10,14,18], [3,7,11,15,19], [4,8,12,16,20]] I want to extract it into patches of 2 by 2 sizes with out repeating the elements. Pay attention that if the shape is indivisible by patch size, we would j...
x = a[:a.shape[0] // patch_size * patch_size, :a.shape[1] // patch_size * patch_size] result = x.reshape(x.shape[0]//patch_size, patch_size, x.shape[1]// patch_size, patch_size).swapaxes(1, 2).transpose(1, 0, 2, 3).reshape(-1, patch_size, patch_size)
{ "problem_id": 390, "library_problem_id": 99, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 94 }
import numpy as np import copy import tokenize, io def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array( [ [1, 5, 9, 13, 17], [2, 6, 10, 14, 18], [3, 7, 11, 15, ...
Problem: I have an array : a = np.array([[ 0, 1, 2, 3, 5, 6, 7, 8], [ 4, 5, 6, 7, 5, 3, 2, 5], [ 8, 9, 10, 11, 4, 5, 3, 5]]) I want to extract array by its columns in RANGE, if I want to take column in range 1 until 5, It will return a = np.array([[ 1, 2, 3, 5, ], [ 5,...
result = a[:, low:high]
{ "problem_id": 391, "library_problem_id": 100, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 100 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array( [ [0, 1, 2, 3, 5, 6, 7, 8], [4, 5, 6, 7, 5, 3, 2, 5], [8...
Problem: I have an array : a = np.array([[ 0, 1, 2, 3, 5, 6, 7, 8], [ 4, 5, 6, 7, 5, 3, 2, 5], [ 8, 9, 10, 11, 4, 5, 3, 5]]) I want to extract array by its rows in RANGE, if I want to take rows in range 0 until 2, It will return a = np.array([[ 0, 1, 2, 3, 5, 6, 7, 8], ...
result = a[low:high, :]
{ "problem_id": 392, "library_problem_id": 101, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 100 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array( [ [0, 1, 2, 3, 5, 6, 7, 8], [4, 5, 6, 7, 5, 3, 2, 5], [8...
Problem: I have an array : a = np.array([[ 0, 1, 2, 3, 5, 6, 7, 8], [ 4, 5, 6, 7, 5, 3, 2, 5], [ 8, 9, 10, 11, 4, 5, 3, 5]]) I want to extract array by its columns in RANGE, if I want to take column in range 1 until 10, It will return a = np.array([[ 1, 2, 3, 5, 6, 7, 8], ...
high = min(high, a.shape[1]) result = a[:, low:high]
{ "problem_id": 393, "library_problem_id": 102, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 100 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: a = np.array( [ [0, 1, 2, 3, 5, 6, 7, 8], [4, 5, 6, 7, 5, 3, 2, 5], [8...
Problem: How can I read a Numpy array from a string? Take a string like: "[[ 0.5544 0.4456], [ 0.8811 0.1189]]" and convert it to an array: a = from_string("[[ 0.5544 0.4456], [ 0.8811 0.1189]]") where a becomes the object: np.array([[0.5544, 0.4456], [0.8811, 0.1189]]). There's nothing I can find in the NumPy docs...
a = np.array(np.matrix(string.replace(',', ';')))
{ "problem_id": 394, "library_problem_id": 103, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 103 }
import numpy as np import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: string = "[[ 0.5544 0.4456], [ 0.8811 0.1189]]" elif test_case_id == 2: np.random.seed(42) a = np.random.rand(5, 6) stri...
Problem: I could not find a built-in function in Python to generate a log uniform distribution given a min and max value (the R equivalent is here), something like: loguni[n, min, max, base] that returns n log uniformly distributed in the range min and max. The closest I found though was numpy.random.uniform. That is, ...
import scipy.stats result = scipy.stats.loguniform.rvs(a = min, b = max, size = n)
{ "problem_id": 395, "library_problem_id": 104, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Origin", "perturbation_origin_id": 104 }
import numpy as np import copy import tokenize, io import scipy from scipy.stats import ks_2samp def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: low = 1 high = np.e size = 10000 return low, high, size def ge...
Problem: I could not find a built-in function in Python to generate a log uniform distribution given a min and max value (the R equivalent is here), something like: loguni[n, exp(min), exp(max), base] that returns n log uniformly distributed in the range exp(min) and exp(max). The closest I found though was numpy.rando...
import scipy.stats result = scipy.stats.loguniform.rvs(a = np.exp(min), b = np.exp(max), size = n)
{ "problem_id": 396, "library_problem_id": 105, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Semantic", "perturbation_origin_id": 104 }
import numpy as np import copy import tokenize, io import scipy from scipy.stats import ks_2samp def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: low = 0 high = 1 size = 10000 return low, high, size def gener...
Problem: I could not find a built-in function in Python to generate a log uniform distribution given a min and max value (the R equivalent is here), something like: loguni[n, min, max, base] that returns n log uniformly distributed in the range min and max. The closest I found though was numpy.random.uniform. That is, ...
import scipy.stats result = scipy.stats.loguniform.rvs(a = min, b = max, size = n) return result
{ "problem_id": 397, "library_problem_id": 106, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Surface", "perturbation_origin_id": 104 }
import numpy as np import copy import tokenize, io import scipy from scipy.stats import ks_2samp def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: low = 1 high = np.e size = 10000 return low, high, size def ge...
Problem: I have a time-series A holding several values. I need to obtain a series B that is defined algebraically as follows: B[0] = a*A[0] B[t] = a * A[t] + b * B[t-1] where we can assume a and b are real numbers. Is there any way to do this type of recursive computation in Pandas or numpy? As an example of input: > A...
B = np.empty(len(A)) for k in range(0, len(B)): if k == 0: B[k] = a*A[k] else: B[k] = a*A[k] + b*B[k-1]
{ "problem_id": 398, "library_problem_id": 107, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 107 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: np.random.seed(42) A = pd.Series( np.random.randn( 10, ) ) ...
Problem: I have a time-series A holding several values. I need to obtain a series B that is defined algebraically as follows: B[0] = a*A[0] B[1] = a*A[1]+b*B[0] B[t] = a * A[t] + b * B[t-1] + c * B[t-2] where we can assume a and b are real numbers. Is there any way to do this type of recursive computation in Pandas or ...
B = np.empty(len(A)) for k in range(0, len(B)): if k == 0: B[k] = a*A[k] elif k == 1: B[k] = a*A[k] + b*B[k-1] else: B[k] = a*A[k] + b*B[k-1] + c*B[k-2]
{ "problem_id": 399, "library_problem_id": 108, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 107 }
import numpy as np import pandas as pd import copy def generate_test_case(test_case_id): def define_test_input(test_case_id): if test_case_id == 1: np.random.seed(42) A = pd.Series( np.random.randn( 10, ) ) ...