Datasets:
Tasks:
Text2Text Generation
Modalities:
Text
Formats:
json
Languages:
code
Size:
1K - 10K
Tags:
code-generation
License:
fix 400, 401
Browse files- test.jsonl +2 -2
test.jsonl
CHANGED
@@ -398,8 +398,8 @@
|
|
398 |
{"prompt": "Problem:\nI 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.\nThe closest I found though was numpy.random.uniform.\nThat is, given range of x, I want to get samples of given size (n) that suit log-uniform distribution. \nAny help would be appreciated!\nA:\n<code>\nimport numpy as np\ndef f(min=1, max=np.e, n=10000):\n # return the solution in this function\n # result = f(min=1, max=np.e, n=10000)\n ### BEGIN SOLUTION", "reference_code": " import scipy.stats\n result = scipy.stats.loguniform.rvs(a = min, b = max, size = n)\n \n\n return result\n", "metadata": {"problem_id": 397, "library_problem_id": 106, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Surface", "perturbation_origin_id": 104}, "code_context": "import numpy as np\nimport copy\nimport tokenize, io\nimport scipy\nfrom scipy.stats import ks_2samp\n\n\ndef generate_test_case(test_case_id):\n\n def define_test_input(test_case_id):\n if test_case_id == 1:\n low = 1\n high = np.e\n size = 10000\n return low, high, size\n\n def generate_ans(data):\n _a = data\n min, max, n = _a\n result = scipy.stats.loguniform.rvs(a=min, b=max, size=n)\n return result\n\n test_input = define_test_input(test_case_id)\n expected_result = generate_ans(copy.deepcopy(test_input))\n\n return test_input, expected_result\n\n\ndef exec_test(result, ans):\n\n np.testing.assert_array_equal(result.shape, ans.shape)\n assert ks_2samp(result, ans)[0] <= 0.1\n\n return 1\n\n\nexec_context = r\"\"\"\nimport numpy as np\nmin, max, n = test_input\ndef f(min=1, max=np.e, n=10000):\n[insert]\nresult = f(min, max, n)\n\"\"\"\n\n\ndef test_execution(solution: str):\n code = exec_context.replace(\"[insert]\", solution)\n for i in range(1):\n test_input, expected_result = generate_test_case(i + 1)\n test_env = {\"test_input\": test_input}\n exec(code, test_env)\n assert exec_test(test_env[\"result\"], expected_result)\n\n\ndef test_string(solution: str):\n tokens = []\n for token in tokenize.tokenize(io.BytesIO(solution.encode(\"utf-8\")).readline):\n tokens.append(token.string)\n assert \"while\" not in tokens and \"for\" not in tokens\n"}
|
399 |
{"prompt": "Problem:\nI have a time-series A holding several values. I need to obtain a series B that is defined algebraically as follows:\nB[0] = a*A[0]\nB[t] = a * A[t] + b * B[t-1]\nwhere we can assume a and b are real numbers.\nIs there any way to do this type of recursive computation in Pandas or numpy?\nAs an example of input:\n> A = pd.Series(np.random.randn(10,))\n0 -0.310354\n1 -0.739515\n2 -0.065390\n3 0.214966\n4 -0.605490\n5 1.293448\n6 -3.068725\n7 -0.208818\n8 0.930881\n9 1.669210\nA:\n<code>\nimport numpy as np\nimport pandas as pd\nA = pd.Series(np.random.randn(10,))\na = 2\nb = 3\n</code>\nB = ... # put solution in this variable\nBEGIN SOLUTION\n<code>\n", "reference_code": "B = np.empty(len(A))\nfor k in range(0, len(B)):\n if k == 0:\n B[k] = a*A[k]\n else:\n B[k] = a*A[k] + b*B[k-1]\n", "metadata": {"problem_id": 398, "library_problem_id": 107, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 107}, "code_context": "import numpy as np\nimport pandas as pd\nimport copy\n\n\ndef generate_test_case(test_case_id):\n def define_test_input(test_case_id):\n if test_case_id == 1:\n np.random.seed(42)\n A = pd.Series(\n np.random.randn(\n 10,\n )\n )\n a = 2\n b = 3\n elif test_case_id == 2:\n np.random.seed(42)\n A = pd.Series(\n np.random.randn(\n 30,\n )\n )\n a, b = np.random.randint(2, 10, (2,))\n return A, a, b\n\n def generate_ans(data):\n _a = data\n A, a, b = _a\n B = np.empty(len(A))\n for k in range(0, len(B)):\n if k == 0:\n B[k] = a * A[k]\n else:\n B[k] = a * A[k] + b * B[k - 1]\n return B\n\n test_input = define_test_input(test_case_id)\n expected_result = generate_ans(copy.deepcopy(test_input))\n return test_input, expected_result\n\n\ndef exec_test(result, ans):\n np.testing.assert_array_equal(result, ans)\n return 1\n\n\nexec_context = r\"\"\"\nimport numpy as np\nimport pandas as pd\nA, a, b = test_input\n[insert]\nresult = B\n\"\"\"\n\n\ndef test_execution(solution: str):\n code = exec_context.replace(\"[insert]\", solution)\n for i in range(2):\n test_input, expected_result = generate_test_case(i + 1)\n test_env = {\"test_input\": test_input}\n exec(code, test_env)\n assert exec_test(test_env[\"result\"], expected_result)\n"}
|
400 |
{"prompt": "Problem:\nI have a time-series A holding several values. I need to obtain a series B that is defined algebraically as follows:\nB[0] = a*A[0]\nB[1] = a*A[1]+b*B[0]\nB[t] = a * A[t] + b * B[t-1] + c * B[t-2]\nwhere we can assume a and b are real numbers.\nIs there any way to do this type of recursive computation in Pandas or numpy?\nAs an example of input:\n> A = pd.Series(np.random.randn(10,))\n0 -0.310354\n1 -0.739515\n2 -0.065390\n3 0.214966\n4 -0.605490\n5 1.293448\n6 -3.068725\n7 -0.208818\n8 0.930881\n9 1.669210\nA:\n<code>\nimport numpy as np\nimport pandas as pd\nA = pd.Series(np.random.randn(10,))\na = 2\nb = 3\nc = 4\n</code>\nB = ... # put solution in this variable\nBEGIN SOLUTION\n<code>\n", "reference_code": "B = np.empty(len(A))\nfor k in range(0, len(B)):\n if k == 0:\n B[k] = a*A[k]\n elif k == 1:\n B[k] = a*A[k] + b*B[k-1]\n else:\n B[k] = a*A[k] + b*B[k-1] + c*B[k-2]\n\n", "metadata": {"problem_id": 399, "library_problem_id": 108, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 107}, "code_context": "import numpy as np\nimport pandas as pd\nimport copy\n\n\ndef generate_test_case(test_case_id):\n def define_test_input(test_case_id):\n if test_case_id == 1:\n np.random.seed(42)\n A = pd.Series(\n np.random.randn(\n 10,\n )\n )\n a = 2\n b = 3\n c = 4\n elif test_case_id == 2:\n np.random.seed(42)\n A = pd.Series(\n np.random.randn(\n 30,\n )\n )\n a, b, c = np.random.randint(2, 10, (3,))\n return A, a, b, c\n\n def generate_ans(data):\n _a = data\n A, a, b, c = _a\n B = np.empty(len(A))\n for k in range(0, len(B)):\n if k == 0:\n B[k] = a * A[k]\n elif k == 1:\n B[k] = a * A[k] + b * B[k - 1]\n else:\n B[k] = a * A[k] + b * B[k - 1] + c * B[k - 2]\n return B\n\n test_input = define_test_input(test_case_id)\n expected_result = generate_ans(copy.deepcopy(test_input))\n return test_input, expected_result\n\n\ndef exec_test(result, ans):\n np.testing.assert_array_equal(result, ans)\n return 1\n\n\nexec_context = r\"\"\"\nimport numpy as np\nimport pandas as pd\nA, a, b, c = test_input\n[insert]\nresult = B\n\"\"\"\n\n\ndef test_execution(solution: str):\n code = exec_context.replace(\"[insert]\", solution)\n for i in range(2):\n test_input, expected_result = generate_test_case(i + 1)\n test_env = {\"test_input\": test_input}\n exec(code, test_env)\n assert exec_test(test_env[\"result\"], expected_result)\n"}
|
401 |
-
{"prompt": "Problem:\n\nI am trying to convert a MATLAB code in Python. I don't know how to initialize an empty matrix in Python.\nMATLAB Code:\ndemod4(1) = [];\nI want to create an empty numpy array, with shape = (0,)\n\nA:\n<code>\nimport numpy as np\n</code>\nresult = ... # put solution in this variable\nBEGIN SOLUTION\n<code>\n", "reference_code": "result = np.array([])\n", "metadata": {"problem_id": 400, "library_problem_id": 109, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Origin", "perturbation_origin_id": 109}, "code_context": "import numpy as np\nimport pandas as pd\nimport copy\n\n\ndef generate_test_case(test_case_id):\n def define_test_input(test_case_id):\n if test_case_id == 1:\n return None\n\n def generate_ans(data):\n none_input = data\n return np.array([])\n\n test_input = define_test_input(test_case_id)\n expected_result = generate_ans(copy.deepcopy(test_input))\n return test_input, expected_result\n\n\ndef exec_test(result, ans):\n np.testing.assert_array_equal(result, ans)\n return 1\n\n\nexec_context = r\"\"\"\nimport numpy as np\n[insert]\n\"\"\"\n\n\ndef test_execution(solution: str):\n code = exec_context.replace(\"[insert]\", solution)\n for i in range(1):\n test_input, expected_result = generate_test_case(i + 1)\n test_env = {\"test_input\": test_input}\n exec(code, test_env)\n assert exec_test(test_env[\"result\"], expected_result)\n"}
|
402 |
-
{"prompt": "Problem:\nI am trying to convert a MATLAB code in Python. I don't know how to initialize an empty matrix in Python.\nMATLAB Code:\ndemod4(1) = [];\nI want to create an empty numpy array, with shape = (3,0)\n\nA:\n<code>\nimport numpy as np\n</code>\nresult = ... # put solution in this variable\nBEGIN SOLUTION\n<code>\n", "reference_code": "result = np.array([[], [], []])\n", "metadata": {"problem_id": 401, "library_problem_id": 110, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Semantic", "perturbation_origin_id": 109}, "code_context": "import numpy as np\nimport pandas as pd\nimport copy\n\n\ndef generate_test_case(test_case_id):\n def define_test_input(test_case_id):\n if test_case_id == 1:\n return None\n\n def generate_ans(data):\n none_input = data\n return np.array([[], [], []])\n\n test_input = define_test_input(test_case_id)\n expected_result = generate_ans(copy.deepcopy(test_input))\n return test_input, expected_result\n\n\ndef exec_test(result, ans):\n np.testing.assert_array_equal(result, ans)\n return 1\n\n\nexec_context = r\"\"\"\nimport numpy as np\n[insert]\n\"\"\"\n\n\ndef test_execution(solution: str):\n code = exec_context.replace(\"[insert]\", solution)\n for i in range(1):\n test_input, expected_result = generate_test_case(i + 1)\n test_env = {\"test_input\": test_input}\n exec(code, test_env)\n assert exec_test(test_env[\"result\"], expected_result)\n"}
|
403 |
{"prompt": "Problem:\nMatlab offers the function sub2ind which \"returns the linear index equivalents to the row and column subscripts ... for a matrix... .\" Additionally, the index is in Fortran order.\nI need this sub2ind function or something similar, but I did not find any similar Python or Numpy function. How can I get this functionality?\nThis is an example from the matlab documentation (same page as above):\nExample 1\nThis example converts the subscripts (2, 1, 2) for three-dimensional array A \nto a single linear index. Start by creating a 3-by-4-by-2 array A:\nrng(0,'twister'); % Initialize random number generator.\nA = rand(3, 4, 2)\nA(:,:,1) =\n 0.8147 0.9134 0.2785 0.9649\n 0.9058 0.6324 0.5469 0.1576\n 0.1270 0.0975 0.9575 0.9706\nA(:,:,2) =\n 0.9572 0.1419 0.7922 0.0357\n 0.4854 0.4218 0.9595 0.8491\n 0.8003 0.9157 0.6557 0.9340\nFind the linear index corresponding to (2, 1, 2):\nlinearInd = sub2ind(size(A), 2, 1, 2)\nlinearInd =\n 14\nMake sure that these agree:\nA(2, 1, 2) A(14)\nans = and =\n 0.4854 0.4854\nNote that the desired result of such function in python can be 14 - 1 = 13(due to the difference of Python and Matlab indices). \nA:\n<code>\nimport numpy as np\ndims = (3, 4, 2)\na = np.random.rand(*dims)\nindex = (1, 0, 1)\n</code>\nresult = ... # put solution in this variable\nBEGIN SOLUTION\n<code>\n", "reference_code": "result = np.ravel_multi_index(index, dims=dims, order='F')\n", "metadata": {"problem_id": 402, "library_problem_id": 111, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 111}, "code_context": "import numpy as np\nimport copy\n\n\ndef generate_test_case(test_case_id):\n def define_test_input(test_case_id):\n if test_case_id == 1:\n dims = (3, 4, 2)\n np.random.seed(42)\n a = np.random.rand(*dims)\n index = (1, 0, 1)\n elif test_case_id == 2:\n np.random.seed(42)\n dims = np.random.randint(8, 10, (5,))\n a = np.random.rand(*dims)\n index = np.random.randint(0, 7, (5,))\n return dims, a, index\n\n def generate_ans(data):\n _a = data\n dims, a, index = _a\n result = np.ravel_multi_index(index, dims=dims, order=\"F\")\n return result\n\n test_input = define_test_input(test_case_id)\n expected_result = generate_ans(copy.deepcopy(test_input))\n return test_input, expected_result\n\n\ndef exec_test(result, ans):\n np.testing.assert_array_equal(result, ans)\n return 1\n\n\nexec_context = r\"\"\"\nimport numpy as np\ndims, a, index = test_input\n[insert]\n\"\"\"\n\n\ndef test_execution(solution: str):\n code = exec_context.replace(\"[insert]\", solution)\n for i in range(2):\n test_input, expected_result = generate_test_case(i + 1)\n test_env = {\"test_input\": test_input}\n exec(code, test_env)\n assert exec_test(test_env[\"result\"], expected_result)\n"}
|
404 |
{"prompt": "Problem:\nMatlab offers the function sub2ind which \"returns the linear index equivalents to the row and column subscripts ... for a matrix... .\" \nI need this sub2ind function or something similar, but I did not find any similar Python or Numpy function. Briefly speaking, given subscripts like (1, 0, 1) for a (3, 4, 2) array, the function can compute the corresponding single linear index 9.\nHow can I get this functionality? The index should be in C order.\nA:\n<code>\nimport numpy as np\ndims = (3, 4, 2)\na = np.random.rand(*dims)\nindex = (1, 0, 1)\n</code>\nresult = ... # put solution in this variable\nBEGIN SOLUTION\n<code>\n", "reference_code": "result = np.ravel_multi_index(index, dims=dims, order='C')\n", "metadata": {"problem_id": 403, "library_problem_id": 112, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 111}, "code_context": "import numpy as np\nimport copy\n\n\ndef generate_test_case(test_case_id):\n def define_test_input(test_case_id):\n if test_case_id == 1:\n dims = (3, 4, 2)\n np.random.seed(42)\n a = np.random.rand(*dims)\n index = (1, 0, 1)\n elif test_case_id == 2:\n np.random.seed(42)\n dims = np.random.randint(8, 10, (5,))\n a = np.random.rand(*dims)\n index = np.random.randint(0, 7, (5,))\n return dims, a, index\n\n def generate_ans(data):\n _a = data\n dims, a, index = _a\n result = np.ravel_multi_index(index, dims=dims, order=\"C\")\n return result\n\n test_input = define_test_input(test_case_id)\n expected_result = generate_ans(copy.deepcopy(test_input))\n return test_input, expected_result\n\n\ndef exec_test(result, ans):\n np.testing.assert_array_equal(result, ans)\n return 1\n\n\nexec_context = r\"\"\"\nimport numpy as np\ndims, a, index = test_input\n[insert]\n\"\"\"\n\n\ndef test_execution(solution: str):\n code = exec_context.replace(\"[insert]\", solution)\n for i in range(2):\n test_input, expected_result = generate_test_case(i + 1)\n test_env = {\"test_input\": test_input}\n exec(code, test_env)\n assert exec_test(test_env[\"result\"], expected_result)\n"}
|
405 |
{"prompt": "Problem:\nI want to create a pandas dataframe with default values of zero, but first column of integers and the other of floats. I am able to create a numpy array with the correct types, see the values variable below. However, when I pass that into the dataframe constructor, it only returns NaN values (see df below). I have include the untyped code that returns an array of floats(see df2)\nimport pandas as pd\nimport numpy as np\nvalues = np.zeros((2,3), dtype='int32,float32')\nindex = ['x', 'y']\ncolumns = ['a','b','c']\ndf = pd.DataFrame(data=values, index=index, columns=columns)\ndf.values.dtype\nvalues2 = np.zeros((2,3))\ndf2 = pd.DataFrame(data=values2, index=index, columns=columns)\ndf2.values.dtype\nAny suggestions on how to construct the dataframe?\nA:\n<code>\nimport numpy as np\nimport pandas as pd\nindex = ['x', 'y']\ncolumns = ['a','b','c']\n</code>\ndf = ... # put solution in this variable\nBEGIN SOLUTION\n<code>\n", "reference_code": "dtype = [('a','int32'), ('b','float32'), ('c','float32')]\nvalues = np.zeros(2, dtype=dtype)\ndf = pd.DataFrame(values, index=index)\n\n", "metadata": {"problem_id": 404, "library_problem_id": 113, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Origin", "perturbation_origin_id": 113}, "code_context": "import numpy as np\nimport pandas as pd\nimport copy\n\n\ndef generate_test_case(test_case_id):\n def define_test_input(test_case_id):\n if test_case_id == 1:\n index = [\"x\", \"y\"]\n columns = [\"a\", \"b\", \"c\"]\n return index, columns\n\n def generate_ans(data):\n _a = data\n index, columns = _a\n dtype = [(\"a\", \"int32\"), (\"b\", \"float32\"), (\"c\", \"float32\")]\n values = np.zeros(2, dtype=dtype)\n df = pd.DataFrame(values, index=index)\n return df\n\n test_input = define_test_input(test_case_id)\n expected_result = generate_ans(copy.deepcopy(test_input))\n return test_input, expected_result\n\n\ndef exec_test(result, ans):\n pd.testing.assert_frame_equal(result, ans)\n return 1\n\n\nexec_context = r\"\"\"\nimport numpy as np\nimport pandas as pd\nindex, columns = test_input\n[insert]\nresult = df\n\"\"\"\n\n\ndef test_execution(solution: str):\n code = exec_context.replace(\"[insert]\", solution)\n for i in range(1):\n test_input, expected_result = generate_test_case(i + 1)\n test_env = {\"test_input\": test_input}\n exec(code, test_env)\n assert exec_test(test_env[\"result\"], expected_result)\n"}
|
|
|
398 |
{"prompt": "Problem:\nI 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.\nThe closest I found though was numpy.random.uniform.\nThat is, given range of x, I want to get samples of given size (n) that suit log-uniform distribution. \nAny help would be appreciated!\nA:\n<code>\nimport numpy as np\ndef f(min=1, max=np.e, n=10000):\n # return the solution in this function\n # result = f(min=1, max=np.e, n=10000)\n ### BEGIN SOLUTION", "reference_code": " import scipy.stats\n result = scipy.stats.loguniform.rvs(a = min, b = max, size = n)\n \n\n return result\n", "metadata": {"problem_id": 397, "library_problem_id": 106, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Surface", "perturbation_origin_id": 104}, "code_context": "import numpy as np\nimport copy\nimport tokenize, io\nimport scipy\nfrom scipy.stats import ks_2samp\n\n\ndef generate_test_case(test_case_id):\n\n def define_test_input(test_case_id):\n if test_case_id == 1:\n low = 1\n high = np.e\n size = 10000\n return low, high, size\n\n def generate_ans(data):\n _a = data\n min, max, n = _a\n result = scipy.stats.loguniform.rvs(a=min, b=max, size=n)\n return result\n\n test_input = define_test_input(test_case_id)\n expected_result = generate_ans(copy.deepcopy(test_input))\n\n return test_input, expected_result\n\n\ndef exec_test(result, ans):\n\n np.testing.assert_array_equal(result.shape, ans.shape)\n assert ks_2samp(result, ans)[0] <= 0.1\n\n return 1\n\n\nexec_context = r\"\"\"\nimport numpy as np\nmin, max, n = test_input\ndef f(min=1, max=np.e, n=10000):\n[insert]\nresult = f(min, max, n)\n\"\"\"\n\n\ndef test_execution(solution: str):\n code = exec_context.replace(\"[insert]\", solution)\n for i in range(1):\n test_input, expected_result = generate_test_case(i + 1)\n test_env = {\"test_input\": test_input}\n exec(code, test_env)\n assert exec_test(test_env[\"result\"], expected_result)\n\n\ndef test_string(solution: str):\n tokens = []\n for token in tokenize.tokenize(io.BytesIO(solution.encode(\"utf-8\")).readline):\n tokens.append(token.string)\n assert \"while\" not in tokens and \"for\" not in tokens\n"}
|
399 |
{"prompt": "Problem:\nI have a time-series A holding several values. I need to obtain a series B that is defined algebraically as follows:\nB[0] = a*A[0]\nB[t] = a * A[t] + b * B[t-1]\nwhere we can assume a and b are real numbers.\nIs there any way to do this type of recursive computation in Pandas or numpy?\nAs an example of input:\n> A = pd.Series(np.random.randn(10,))\n0 -0.310354\n1 -0.739515\n2 -0.065390\n3 0.214966\n4 -0.605490\n5 1.293448\n6 -3.068725\n7 -0.208818\n8 0.930881\n9 1.669210\nA:\n<code>\nimport numpy as np\nimport pandas as pd\nA = pd.Series(np.random.randn(10,))\na = 2\nb = 3\n</code>\nB = ... # put solution in this variable\nBEGIN SOLUTION\n<code>\n", "reference_code": "B = np.empty(len(A))\nfor k in range(0, len(B)):\n if k == 0:\n B[k] = a*A[k]\n else:\n B[k] = a*A[k] + b*B[k-1]\n", "metadata": {"problem_id": 398, "library_problem_id": 107, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 107}, "code_context": "import numpy as np\nimport pandas as pd\nimport copy\n\n\ndef generate_test_case(test_case_id):\n def define_test_input(test_case_id):\n if test_case_id == 1:\n np.random.seed(42)\n A = pd.Series(\n np.random.randn(\n 10,\n )\n )\n a = 2\n b = 3\n elif test_case_id == 2:\n np.random.seed(42)\n A = pd.Series(\n np.random.randn(\n 30,\n )\n )\n a, b = np.random.randint(2, 10, (2,))\n return A, a, b\n\n def generate_ans(data):\n _a = data\n A, a, b = _a\n B = np.empty(len(A))\n for k in range(0, len(B)):\n if k == 0:\n B[k] = a * A[k]\n else:\n B[k] = a * A[k] + b * B[k - 1]\n return B\n\n test_input = define_test_input(test_case_id)\n expected_result = generate_ans(copy.deepcopy(test_input))\n return test_input, expected_result\n\n\ndef exec_test(result, ans):\n np.testing.assert_array_equal(result, ans)\n return 1\n\n\nexec_context = r\"\"\"\nimport numpy as np\nimport pandas as pd\nA, a, b = test_input\n[insert]\nresult = B\n\"\"\"\n\n\ndef test_execution(solution: str):\n code = exec_context.replace(\"[insert]\", solution)\n for i in range(2):\n test_input, expected_result = generate_test_case(i + 1)\n test_env = {\"test_input\": test_input}\n exec(code, test_env)\n assert exec_test(test_env[\"result\"], expected_result)\n"}
|
400 |
{"prompt": "Problem:\nI have a time-series A holding several values. I need to obtain a series B that is defined algebraically as follows:\nB[0] = a*A[0]\nB[1] = a*A[1]+b*B[0]\nB[t] = a * A[t] + b * B[t-1] + c * B[t-2]\nwhere we can assume a and b are real numbers.\nIs there any way to do this type of recursive computation in Pandas or numpy?\nAs an example of input:\n> A = pd.Series(np.random.randn(10,))\n0 -0.310354\n1 -0.739515\n2 -0.065390\n3 0.214966\n4 -0.605490\n5 1.293448\n6 -3.068725\n7 -0.208818\n8 0.930881\n9 1.669210\nA:\n<code>\nimport numpy as np\nimport pandas as pd\nA = pd.Series(np.random.randn(10,))\na = 2\nb = 3\nc = 4\n</code>\nB = ... # put solution in this variable\nBEGIN SOLUTION\n<code>\n", "reference_code": "B = np.empty(len(A))\nfor k in range(0, len(B)):\n if k == 0:\n B[k] = a*A[k]\n elif k == 1:\n B[k] = a*A[k] + b*B[k-1]\n else:\n B[k] = a*A[k] + b*B[k-1] + c*B[k-2]\n\n", "metadata": {"problem_id": 399, "library_problem_id": 108, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Difficult-Rewrite", "perturbation_origin_id": 107}, "code_context": "import numpy as np\nimport pandas as pd\nimport copy\n\n\ndef generate_test_case(test_case_id):\n def define_test_input(test_case_id):\n if test_case_id == 1:\n np.random.seed(42)\n A = pd.Series(\n np.random.randn(\n 10,\n )\n )\n a = 2\n b = 3\n c = 4\n elif test_case_id == 2:\n np.random.seed(42)\n A = pd.Series(\n np.random.randn(\n 30,\n )\n )\n a, b, c = np.random.randint(2, 10, (3,))\n return A, a, b, c\n\n def generate_ans(data):\n _a = data\n A, a, b, c = _a\n B = np.empty(len(A))\n for k in range(0, len(B)):\n if k == 0:\n B[k] = a * A[k]\n elif k == 1:\n B[k] = a * A[k] + b * B[k - 1]\n else:\n B[k] = a * A[k] + b * B[k - 1] + c * B[k - 2]\n return B\n\n test_input = define_test_input(test_case_id)\n expected_result = generate_ans(copy.deepcopy(test_input))\n return test_input, expected_result\n\n\ndef exec_test(result, ans):\n np.testing.assert_array_equal(result, ans)\n return 1\n\n\nexec_context = r\"\"\"\nimport numpy as np\nimport pandas as pd\nA, a, b, c = test_input\n[insert]\nresult = B\n\"\"\"\n\n\ndef test_execution(solution: str):\n code = exec_context.replace(\"[insert]\", solution)\n for i in range(2):\n test_input, expected_result = generate_test_case(i + 1)\n test_env = {\"test_input\": test_input}\n exec(code, test_env)\n assert exec_test(test_env[\"result\"], expected_result)\n"}
|
401 |
+
{"prompt": "Problem:\n\nI am trying to convert a MATLAB code in Python. I don't know how to initialize an empty matrix in Python.\nMATLAB Code:\ndemod4(1) = [];\nI want to create an empty numpy array, with shape = (0,)\n\nA:\n<code>\nimport numpy as np\n</code>\nresult = ... # put solution in this variable\nBEGIN SOLUTION\n<code>\n", "reference_code": "result = np.array([])\n", "metadata": {"problem_id": 400, "library_problem_id": 109, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Origin", "perturbation_origin_id": 109}, "code_context": "import numpy as np\nimport pandas as pd\nimport copy\n\n\ndef generate_test_case(test_case_id):\n def define_test_input(test_case_id):\n if test_case_id == 1:\n return None\n\n def generate_ans(data):\n none_input = data\n return np.array([])\n\n test_input = define_test_input(test_case_id)\n expected_result = generate_ans(copy.deepcopy(test_input))\n return test_input, expected_result\n\n\ndef exec_test(result, ans):\n assert result is not None\n np.testing.assert_array_equal(result, ans)\n return 1\n\n\nexec_context = r\"\"\"\nimport numpy as np\n[insert]\n\"\"\"\n\n\ndef test_execution(solution: str):\n code = exec_context.replace(\"[insert]\", solution)\n for i in range(1):\n test_input, expected_result = generate_test_case(i + 1)\n test_env = {\"test_input\": test_input}\n exec(code, test_env)\n assert exec_test(test_env[\"result\"], expected_result)\n"}
|
402 |
+
{"prompt": "Problem:\nI am trying to convert a MATLAB code in Python. I don't know how to initialize an empty matrix in Python.\nMATLAB Code:\ndemod4(1) = [];\nI want to create an empty numpy array, with shape = (3,0)\n\nA:\n<code>\nimport numpy as np\n</code>\nresult = ... # put solution in this variable\nBEGIN SOLUTION\n<code>\n", "reference_code": "result = np.array([[], [], []])\n", "metadata": {"problem_id": 401, "library_problem_id": 110, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Semantic", "perturbation_origin_id": 109}, "code_context": "import numpy as np\nimport pandas as pd\nimport copy\n\n\ndef generate_test_case(test_case_id):\n def define_test_input(test_case_id):\n if test_case_id == 1:\n return None\n\n def generate_ans(data):\n none_input = data\n return np.array([[], [], []])\n\n test_input = define_test_input(test_case_id)\n expected_result = generate_ans(copy.deepcopy(test_input))\n return test_input, expected_result\n\n\ndef exec_test(result, ans):\n assert result is not None\n np.testing.assert_array_equal(result, ans)\n return 1\n\n\nexec_context = r\"\"\"\nimport numpy as np\n[insert]\n\"\"\"\n\n\ndef test_execution(solution: str):\n code = exec_context.replace(\"[insert]\", solution)\n for i in range(1):\n test_input, expected_result = generate_test_case(i + 1)\n test_env = {\"test_input\": test_input}\n exec(code, test_env)\n assert exec_test(test_env[\"result\"], expected_result)\n"}
|
403 |
{"prompt": "Problem:\nMatlab offers the function sub2ind which \"returns the linear index equivalents to the row and column subscripts ... for a matrix... .\" Additionally, the index is in Fortran order.\nI need this sub2ind function or something similar, but I did not find any similar Python or Numpy function. How can I get this functionality?\nThis is an example from the matlab documentation (same page as above):\nExample 1\nThis example converts the subscripts (2, 1, 2) for three-dimensional array A \nto a single linear index. Start by creating a 3-by-4-by-2 array A:\nrng(0,'twister'); % Initialize random number generator.\nA = rand(3, 4, 2)\nA(:,:,1) =\n 0.8147 0.9134 0.2785 0.9649\n 0.9058 0.6324 0.5469 0.1576\n 0.1270 0.0975 0.9575 0.9706\nA(:,:,2) =\n 0.9572 0.1419 0.7922 0.0357\n 0.4854 0.4218 0.9595 0.8491\n 0.8003 0.9157 0.6557 0.9340\nFind the linear index corresponding to (2, 1, 2):\nlinearInd = sub2ind(size(A), 2, 1, 2)\nlinearInd =\n 14\nMake sure that these agree:\nA(2, 1, 2) A(14)\nans = and =\n 0.4854 0.4854\nNote that the desired result of such function in python can be 14 - 1 = 13(due to the difference of Python and Matlab indices). \nA:\n<code>\nimport numpy as np\ndims = (3, 4, 2)\na = np.random.rand(*dims)\nindex = (1, 0, 1)\n</code>\nresult = ... # put solution in this variable\nBEGIN SOLUTION\n<code>\n", "reference_code": "result = np.ravel_multi_index(index, dims=dims, order='F')\n", "metadata": {"problem_id": 402, "library_problem_id": 111, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Origin", "perturbation_origin_id": 111}, "code_context": "import numpy as np\nimport copy\n\n\ndef generate_test_case(test_case_id):\n def define_test_input(test_case_id):\n if test_case_id == 1:\n dims = (3, 4, 2)\n np.random.seed(42)\n a = np.random.rand(*dims)\n index = (1, 0, 1)\n elif test_case_id == 2:\n np.random.seed(42)\n dims = np.random.randint(8, 10, (5,))\n a = np.random.rand(*dims)\n index = np.random.randint(0, 7, (5,))\n return dims, a, index\n\n def generate_ans(data):\n _a = data\n dims, a, index = _a\n result = np.ravel_multi_index(index, dims=dims, order=\"F\")\n return result\n\n test_input = define_test_input(test_case_id)\n expected_result = generate_ans(copy.deepcopy(test_input))\n return test_input, expected_result\n\n\ndef exec_test(result, ans):\n np.testing.assert_array_equal(result, ans)\n return 1\n\n\nexec_context = r\"\"\"\nimport numpy as np\ndims, a, index = test_input\n[insert]\n\"\"\"\n\n\ndef test_execution(solution: str):\n code = exec_context.replace(\"[insert]\", solution)\n for i in range(2):\n test_input, expected_result = generate_test_case(i + 1)\n test_env = {\"test_input\": test_input}\n exec(code, test_env)\n assert exec_test(test_env[\"result\"], expected_result)\n"}
|
404 |
{"prompt": "Problem:\nMatlab offers the function sub2ind which \"returns the linear index equivalents to the row and column subscripts ... for a matrix... .\" \nI need this sub2ind function or something similar, but I did not find any similar Python or Numpy function. Briefly speaking, given subscripts like (1, 0, 1) for a (3, 4, 2) array, the function can compute the corresponding single linear index 9.\nHow can I get this functionality? The index should be in C order.\nA:\n<code>\nimport numpy as np\ndims = (3, 4, 2)\na = np.random.rand(*dims)\nindex = (1, 0, 1)\n</code>\nresult = ... # put solution in this variable\nBEGIN SOLUTION\n<code>\n", "reference_code": "result = np.ravel_multi_index(index, dims=dims, order='C')\n", "metadata": {"problem_id": 403, "library_problem_id": 112, "library": "Numpy", "test_case_cnt": 2, "perturbation_type": "Semantic", "perturbation_origin_id": 111}, "code_context": "import numpy as np\nimport copy\n\n\ndef generate_test_case(test_case_id):\n def define_test_input(test_case_id):\n if test_case_id == 1:\n dims = (3, 4, 2)\n np.random.seed(42)\n a = np.random.rand(*dims)\n index = (1, 0, 1)\n elif test_case_id == 2:\n np.random.seed(42)\n dims = np.random.randint(8, 10, (5,))\n a = np.random.rand(*dims)\n index = np.random.randint(0, 7, (5,))\n return dims, a, index\n\n def generate_ans(data):\n _a = data\n dims, a, index = _a\n result = np.ravel_multi_index(index, dims=dims, order=\"C\")\n return result\n\n test_input = define_test_input(test_case_id)\n expected_result = generate_ans(copy.deepcopy(test_input))\n return test_input, expected_result\n\n\ndef exec_test(result, ans):\n np.testing.assert_array_equal(result, ans)\n return 1\n\n\nexec_context = r\"\"\"\nimport numpy as np\ndims, a, index = test_input\n[insert]\n\"\"\"\n\n\ndef test_execution(solution: str):\n code = exec_context.replace(\"[insert]\", solution)\n for i in range(2):\n test_input, expected_result = generate_test_case(i + 1)\n test_env = {\"test_input\": test_input}\n exec(code, test_env)\n assert exec_test(test_env[\"result\"], expected_result)\n"}
|
405 |
{"prompt": "Problem:\nI want to create a pandas dataframe with default values of zero, but first column of integers and the other of floats. I am able to create a numpy array with the correct types, see the values variable below. However, when I pass that into the dataframe constructor, it only returns NaN values (see df below). I have include the untyped code that returns an array of floats(see df2)\nimport pandas as pd\nimport numpy as np\nvalues = np.zeros((2,3), dtype='int32,float32')\nindex = ['x', 'y']\ncolumns = ['a','b','c']\ndf = pd.DataFrame(data=values, index=index, columns=columns)\ndf.values.dtype\nvalues2 = np.zeros((2,3))\ndf2 = pd.DataFrame(data=values2, index=index, columns=columns)\ndf2.values.dtype\nAny suggestions on how to construct the dataframe?\nA:\n<code>\nimport numpy as np\nimport pandas as pd\nindex = ['x', 'y']\ncolumns = ['a','b','c']\n</code>\ndf = ... # put solution in this variable\nBEGIN SOLUTION\n<code>\n", "reference_code": "dtype = [('a','int32'), ('b','float32'), ('c','float32')]\nvalues = np.zeros(2, dtype=dtype)\ndf = pd.DataFrame(values, index=index)\n\n", "metadata": {"problem_id": 404, "library_problem_id": 113, "library": "Numpy", "test_case_cnt": 1, "perturbation_type": "Origin", "perturbation_origin_id": 113}, "code_context": "import numpy as np\nimport pandas as pd\nimport copy\n\n\ndef generate_test_case(test_case_id):\n def define_test_input(test_case_id):\n if test_case_id == 1:\n index = [\"x\", \"y\"]\n columns = [\"a\", \"b\", \"c\"]\n return index, columns\n\n def generate_ans(data):\n _a = data\n index, columns = _a\n dtype = [(\"a\", \"int32\"), (\"b\", \"float32\"), (\"c\", \"float32\")]\n values = np.zeros(2, dtype=dtype)\n df = pd.DataFrame(values, index=index)\n return df\n\n test_input = define_test_input(test_case_id)\n expected_result = generate_ans(copy.deepcopy(test_input))\n return test_input, expected_result\n\n\ndef exec_test(result, ans):\n pd.testing.assert_frame_equal(result, ans)\n return 1\n\n\nexec_context = r\"\"\"\nimport numpy as np\nimport pandas as pd\nindex, columns = test_input\n[insert]\nresult = df\n\"\"\"\n\n\ndef test_execution(solution: str):\n code = exec_context.replace(\"[insert]\", solution)\n for i in range(1):\n test_input, expected_result = generate_test_case(i + 1)\n test_env = {\"test_input\": test_input}\n exec(code, test_env)\n assert exec_test(test_env[\"result\"], expected_result)\n"}
|