Prompt:
Problem:
I have a raster with a set of unique ID patches/regions which I've converted into a two-dimensional Python numpy array. I would like to calculate pairwise Manhattan distances between all regions to obtain the minimum distance separating the nearest edges of each raster patch.
I've experimented with the cdist function from scipy.spatial.distance as suggested in this answer to a related question, but so far I've been unable to solve my problem using the available documentation. As an end result I would ideally have a N*N array in the form of "from ID, to ID, distance", including distances between all possible combinations of regions.
Here's a sample dataset resembling my input data:
import numpy as np
import matplotlib.pyplot as plt
# Sample study area array
example_array = np.array([[0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0],
                          [0, 0, 2, 0, 2, 2, 0, 6, 0, 3, 3, 3],
                          [0, 0, 0, 0, 2, 2, 0, 0, 0, 3, 3, 3],
                          [0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 0],
                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3],
                          [1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3],
                          [1, 1, 1, 0, 0, 0, 3, 3, 3, 0, 0, 3],
                          [1, 1, 1, 0, 0, 0, 3, 3, 3, 0, 0, 0],
                          [1, 1, 1, 0, 0, 0, 3, 3, 3, 0, 0, 0],
                          [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                          [1, 0, 1, 0, 0, 0, 0, 5, 5, 0, 0, 0],
                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4]])
# Plot array
plt.imshow(example_array, cmap="spectral", interpolation='nearest')
A:
<code>
import numpy as np
import scipy.spatial.distance
example_array = np.array([[0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0],
                          [0, 0, 2, 0, 2, 2, 0, 6, 0, 3, 3, 3],
                          [0, 0, 0, 0, 2, 2, 0, 0, 0, 3, 3, 3],
                          [0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 0],
                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3],
                          [1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3],
                          [1, 1, 1, 0, 0, 0, 3, 3, 3, 0, 0, 3],
                          [1, 1, 1, 0, 0, 0, 3, 3, 3, 0, 0, 0],
                          [1, 1, 1, 0, 0, 0, 3, 3, 3, 0, 0, 0],
                          [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                          [1, 0, 1, 0, 0, 0, 0, 5, 5, 0, 0, 0],
                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4]])
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

import itertools
n = example_array.max()+1
indexes = []
for k in range(1, n):
    tmp = np.nonzero(example_array == k)
    tmp = np.asarray(tmp).T
    indexes.append(tmp)
result = np.zeros((n-1, n-1), dtype=float)
for i, j in itertools.combinations(range(n-1), 2):
    d2 = scipy.spatial.distance.cdist(indexes[i], indexes[j], metric='minkowski', p=1)
    result[i, j] = result[j, i] = d2.min()

import numpy as np
import itertools
import copy
import scipy.spatial.distance

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            example_array = np.array(
                [
                    [0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 2, 0, 2, 2, 0, 6, 0, 3, 3, 3],
                    [0, 0, 0, 0, 2, 2, 0, 0, 0, 3, 3, 3],
                    [0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3],
                    [1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3],
                    [1, 1, 1, 0, 0, 0, 3, 3, 3, 0, 0, 3],
                    [1, 1, 1, 0, 0, 0, 3, 3, 3, 0, 0, 0],
                    [1, 1, 1, 0, 0, 0, 3, 3, 3, 0, 0, 0],
                    [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [1, 0, 1, 0, 0, 0, 0, 5, 5, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4],
                ]
            )
        return example_array

    def generate_ans(data):
        _a = data
        example_array = _a
        n = example_array.max() + 1
        indexes = []
        for k in range(1, n):
            tmp = np.nonzero(example_array == k)
            tmp = np.asarray(tmp).T
            indexes.append(tmp)
        result = np.zeros((n - 1, n - 1), dtype=float)
        for i, j in itertools.combinations(range(n - 1), 2):
            d2 = scipy.spatial.distance.cdist(
                indexes[i], indexes[j], metric="minkowski", p=1
            )
            result[i, j] = result[j, i] = d2.min()
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    assert np.allclose(result, ans)
    return 1

exec_context = r"""
import numpy as np
import scipy.spatial.distance
example_array = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(1):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have a raster with a set of unique ID patches/regions which I've converted into a two-dimensional Python numpy array. I would like to calculate pairwise Euclidean distances between all regions to obtain the minimum distance separating the nearest edges of each raster patch. As the array was originally a raster, a solution needs to account for diagonal distances across cells (I can always convert any distances measured in cells back to metres by multiplying by the raster resolution).
I've experimented with the cdist function from scipy.spatial.distance as suggested in this answer to a related question, but so far I've been unable to solve my problem using the available documentation. As an end result I would ideally have a N*N array in the form of "from ID, to ID, distance", including distances between all possible combinations of regions.
Here's a sample dataset resembling my input data:
import numpy as np
import matplotlib.pyplot as plt
# Sample study area array
example_array = np.array([[0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0],
                          [0, 0, 2, 0, 2, 2, 0, 6, 0, 3, 3, 3],
                          [0, 0, 0, 0, 2, 2, 0, 0, 0, 3, 3, 3],
                          [0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 0],
                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3],
                          [1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3],
                          [1, 1, 1, 0, 0, 0, 3, 3, 3, 0, 0, 3],
                          [1, 1, 1, 0, 0, 0, 3, 3, 3, 0, 0, 0],
                          [1, 1, 1, 0, 0, 0, 3, 3, 3, 0, 0, 0],
                          [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                          [1, 0, 1, 0, 0, 0, 0, 5, 5, 0, 0, 0],
                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4]])
# Plot array
plt.imshow(example_array, cmap="spectral", interpolation='nearest')
A:
<code>
import numpy as np
import scipy.spatial.distance
example_arr = np.array([[0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0],
                          [0, 0, 2, 0, 2, 2, 0, 6, 0, 3, 3, 3],
                          [0, 0, 0, 0, 2, 2, 0, 0, 0, 3, 3, 3],
                          [0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 0],
                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3],
                          [1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3],
                          [1, 1, 1, 0, 0, 0, 3, 3, 3, 0, 0, 3],
                          [1, 1, 1, 0, 0, 0, 3, 3, 3, 0, 0, 0],
                          [1, 1, 1, 0, 0, 0, 3, 3, 3, 0, 0, 0],
                          [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                          [1, 0, 1, 0, 0, 0, 0, 5, 5, 0, 0, 0],
                          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4]])
def f(example_array = example_arr):
    # return the solution in this function
    # result = f(example_array)
    ### BEGIN SOLUTION

    import itertools
    n = example_array.max()+1
    indexes = []
    for k in range(1, n):
        tmp = np.nonzero(example_array == k)
        tmp = np.asarray(tmp).T
        indexes.append(tmp)
    result = np.zeros((n-1, n-1))
    for i, j in itertools.combinations(range(n-1), 2):
        d2 = scipy.spatial.distance.cdist(indexes[i], indexes[j], metric='sqeuclidean')
        result[i, j] = result[j, i] = d2.min()**0.5

    return result

import numpy as np
import itertools
import copy
import scipy.spatial.distance

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            example_array = np.array(
                [
                    [0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 2, 0, 2, 2, 0, 6, 0, 3, 3, 3],
                    [0, 0, 0, 0, 2, 2, 0, 0, 0, 3, 3, 3],
                    [0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3],
                    [1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3],
                    [1, 1, 1, 0, 0, 0, 3, 3, 3, 0, 0, 3],
                    [1, 1, 1, 0, 0, 0, 3, 3, 3, 0, 0, 0],
                    [1, 1, 1, 0, 0, 0, 3, 3, 3, 0, 0, 0],
                    [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [1, 0, 1, 0, 0, 0, 0, 5, 5, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4],
                ]
            )
        return example_array

    def generate_ans(data):
        _a = data
        example_array = _a
        n = example_array.max() + 1
        indexes = []
        for k in range(1, n):
            tmp = np.nonzero(example_array == k)
            tmp = np.asarray(tmp).T
            indexes.append(tmp)
        result = np.zeros((n - 1, n - 1))
        for i, j in itertools.combinations(range(n - 1), 2):
            d2 = scipy.spatial.distance.cdist(
                indexes[i], indexes[j], metric="sqeuclidean"
            )
            result[i, j] = result[j, i] = d2.min() ** 0.5
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    assert np.allclose(result, ans)
    return 1

exec_context = r"""
import numpy as np
import scipy.spatial.distance
example_array = test_input
def f(example_array):
[insert]
result = f(example_array)
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(1):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I am able to interpolate the data points (dotted lines), and am looking to extrapolate them in both direction.
How can I extrapolate these curves in Python with NumPy/SciPy?
The code I used for the interpolation is given below,
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
x = np.array([[0.12, 0.11, 0.1, 0.09, 0.08],
              [0.13, 0.12, 0.11, 0.1, 0.09],
              [0.15, 0.14, 0.12, 0.11, 0.1],
              [0.17, 0.15, 0.14, 0.12, 0.11],
              [0.19, 0.17, 0.16, 0.14, 0.12],
              [0.22, 0.19, 0.17, 0.15, 0.13],
              [0.24, 0.22, 0.19, 0.16, 0.14],
              [0.27, 0.24, 0.21, 0.18, 0.15],
              [0.29, 0.26, 0.22, 0.19, 0.16]])
y = np.array([[71.64, 78.52, 84.91, 89.35, 97.58],
              [66.28, 73.67, 79.87, 85.36, 93.24],
              [61.48, 69.31, 75.36, 81.87, 89.35],
              [57.61, 65.75, 71.7, 79.1, 86.13],
              [55.12, 63.34, 69.32, 77.29, 83.88],
              [54.58, 62.54, 68.7, 76.72, 82.92],
              [56.58, 63.87, 70.3, 77.69, 83.53],
              [61.67, 67.79, 74.41, 80.43, 85.86],
              [70.08, 74.62, 80.93, 85.06, 89.84]])
plt.figure(figsize = (5.15,5.15))
plt.subplot(111)
for i in range(5):
    x_val = np.linspace(x[0, i], x[-1, i], 100)
    x_int = np.interp(x_val, x[:, i], y[:, i])
    tck = interpolate.splrep(x[:, i], y[:, i], k = 2, s = 4)
    y_int = interpolate.splev(x_val, tck, der = 0)
    plt.plot(x[:, i], y[:, i], linestyle = '', marker = 'o')
    plt.plot(x_val, y_int, linestyle = ':', linewidth = 0.25, color =  'black')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

That seems only work for interpolation.
I want to use B-spline (with the same parameters setting as in the code) in scipy to do extrapolation. The result should be (5, 100) array containing f(x_val) for each group of x, y(just as shown in the code).

A:
<code>
from scipy import interpolate
import numpy as np
x = np.array([[0.12, 0.11, 0.1, 0.09, 0.08],
              [0.13, 0.12, 0.11, 0.1, 0.09],
              [0.15, 0.14, 0.12, 0.11, 0.1],
              [0.17, 0.15, 0.14, 0.12, 0.11],
              [0.19, 0.17, 0.16, 0.14, 0.12],
              [0.22, 0.19, 0.17, 0.15, 0.13],
              [0.24, 0.22, 0.19, 0.16, 0.14],
              [0.27, 0.24, 0.21, 0.18, 0.15],
              [0.29, 0.26, 0.22, 0.19, 0.16]])
y = np.array([[71.64, 78.52, 84.91, 89.35, 97.58],
              [66.28, 73.67, 79.87, 85.36, 93.24],
              [61.48, 69.31, 75.36, 81.87, 89.35],
              [57.61, 65.75, 71.7, 79.1, 86.13],
              [55.12, 63.34, 69.32, 77.29, 83.88],
              [54.58, 62.54, 68.7, 76.72, 82.92],
              [56.58, 63.87, 70.3, 77.69, 83.53],
              [61.67, 67.79, 74.41, 80.43, 85.86],
              [70.08, 74.62, 80.93, 85.06, 89.84]])
x_val = np.linspace(-1, 1, 100)
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

result = np.zeros((5, 100))
for i in range(5):
    extrapolator = interpolate.UnivariateSpline(x[:, i], y[:, i], k = 2, s = 4)
    y_int = extrapolator(x_val)
    result[i, :] = y_int


Prompt:
Problem:
How do we pass four datasets in scipy.stats.anderson_ksamp?

The anderson function asks only for one parameter and that should be 1-d array. So I am wondering how to pass four different arrays to be compared in it? Thanks
A:
<code>
import numpy as np
import scipy.stats as ss
x1=[38.7,  41.5,  43.8,  44.5,  45.5,  46.0,  47.7,  58.0]
x2=[39.2,  39.3,  39.7,  41.4,  41.8,  42.9,  43.3,  45.8]
x3=[34.0,  35.0,  39.0,  40.0,  43.0,  43.0,  44.0,  45.0]
x4=[34.0,  34.8,  34.8,  35.4,  37.2,  37.8,  41.2,  42.8]
</code>
statistic, critical_values, significance_level = ... # put solution in these variables
BEGIN SOLUTION
<code>

statistic, critical_values, significance_level = ss.anderson_ksamp([x1,x2,x3,x4])

import numpy as np
import copy
import scipy.stats as ss

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            x1 = [38.7, 41.5, 43.8, 44.5, 45.5, 46.0, 47.7, 58.0]
            x2 = [39.2, 39.3, 39.7, 41.4, 41.8, 42.9, 43.3, 45.8]
            x3 = [34.0, 35.0, 39.0, 40.0, 43.0, 43.0, 44.0, 45.0]
            x4 = [34.0, 34.8, 34.8, 35.4, 37.2, 37.8, 41.2, 42.8]
        elif test_case_id == 2:
            np.random.seed(42)
            x1, x2, x3, x4 = np.random.randn(4, 20)
        return x1, x2, x3, x4

    def generate_ans(data):
        _a = data
        x1, x2, x3, x4 = _a
        statistic, critical_values, significance_level = ss.anderson_ksamp(
            [x1, x2, x3, x4]
        )
        return [statistic, critical_values, significance_level]

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    assert abs(result[0] - ans[0]) <= 1e-5
    assert np.allclose(result[1], ans[1])
    assert abs(result[2] - ans[2]) <= 1e-5
    return 1

exec_context = r"""
import numpy as np
import scipy.stats as ss
x1, x2, x3, x4 = test_input
[insert]
result = [statistic, critical_values, significance_level]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
How do we pass two datasets in scipy.stats.anderson_ksamp?

The anderson function asks only for one parameter and that should be 1-d array. So I am wondering how to pass two different arrays to be compared in it?
Further, I want to interpret the result, that is, telling whether the two different arrays are drawn from the same population at the 5% significance level, result should be `True` or `False` .
A:
<code>
import numpy as np
import scipy.stats as ss
x1=[38.7,  41.5,  43.8,  44.5,  45.5,  46.0,  47.7,  58.0]
x2=[39.2,  39.3,  39.7,  41.4,  41.8,  42.9,  43.3,  45.8]
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

s, c_v, s_l = ss.anderson_ksamp([x1,x2])
result = c_v[2] >= s

import numpy as np
import copy
import scipy.stats as ss

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            x1 = [38.7, 41.5, 43.8, 44.5, 45.5, 46.0, 47.7, 58.0]
            x2 = [39.2, 39.3, 39.7, 41.4, 41.8, 42.9, 43.3, 45.8]
        elif test_case_id == 2:
            np.random.seed(42)
            x1, x2 = np.random.randn(2, 20)
        elif test_case_id == 3:
            np.random.seed(20)
            x1 = np.random.randn(10)
            x2 = np.random.normal(4, 5, size=(10,))
        return x1, x2

    def generate_ans(data):
        _a = data
        x1, x2 = _a
        s, c_v, s_l = ss.anderson_ksamp([x1, x2])
        result = c_v[2] >= s
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    assert result == ans
    return 1

exec_context = r"""
import numpy as np
import scipy.stats as ss
x1, x2 = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(3):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I'm trying to use rollapply with a formula that requires 2 arguments. To my knowledge the only way (unless you create the formula from scratch) to calculate kendall tau correlation, with standard tie correction included is:
>>> import scipy
>>> x = [5.05, 6.75, 3.21, 2.66]
>>> y = [1.65, 26.5, -5.93, 7.96]
>>> z = [1.65, 2.64, 2.64, 6.95]
>>> print scipy.stats.stats.kendalltau(x, y)[0]
0.333333333333
I'm also aware of the problem with rollapply and taking two arguments, as documented here:
•	Related Question 1
•	Github Issue
•	Related Question 2
Still, I'm struggling to find a way to do the kendalltau calculation on a dataframe with multiple columns on a rolling basis.
My dataframe is something like this
A = pd.DataFrame([[1, 5, 1], [2, 4, 1], [3, 3, 1], [4, 2, 1], [5, 1, 1]],
                 columns=['A', 'B', 'C'], index = [1, 2, 3, 4, 5])
Trying to create a function that does this
In [1]:function(A, 3)  # A is df, 3 is the rolling window
Out[2]:
   A  B  C     AB     AC     BC
1  1  5  2    NaN    NaN    NaN
2  2  4  4    NaN    NaN    NaN
3  3  3  1  -1.00  -0.333   0.333
4  4  2  2  -1.00  -0.333   0.333
5  5  1  4  -1.00   1.00  -1.00
In a very preliminary approach I entertained the idea of defining the function like this:
def tau1(x):
    y = np.array(A['A']) #  keep one column fix and run it in the other two
    tau, p_value = sp.stats.kendalltau(x, y)
    return tau
 A['AB'] = pd.rolling_apply(A['B'], 3, lambda x: tau1(x))
Off course It didn't work. I got:
ValueError: all keys need to be the same shape
I understand is not a trivial problem. I appreciate any input.
A:
<code>
import pandas as pd
import numpy as np
import scipy.stats as stats
df = pd.DataFrame([[1, 5, 2], [2, 4, 4], [3, 3, 1], [4, 2, 2], [5, 1, 4]],
                 columns=['A', 'B', 'C'], index = [1, 2, 3, 4, 5])

</code>
df = ... # put solution in this variable
BEGIN SOLUTION
<code>

import itertools as IT
for col1, col2 in IT.combinations(df.columns, 2):
    def tau(idx):
        B = df[[col1, col2]].iloc[idx]
        return stats.kendalltau(B[col1], B[col2])[0]
    df[col1+col2] = pd.Series(np.arange(len(df)), index=df.index).rolling(3).apply(tau)

import numpy as np
import pandas as pd
import itertools as IT
import copy
import scipy.stats as stats

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            df = pd.DataFrame(
                [[1, 5, 2], [2, 4, 4], [3, 3, 1], [4, 2, 2], [5, 1, 4]],
                columns=["A", "B", "C"],
                index=[1, 2, 3, 4, 5],
            )
        elif test_case_id == 2:
            df = pd.DataFrame(
                [[1, 3, 2], [2, 5, 4], [2, 3, 1], [1, 2, 2], [5, 8, 4]],
                columns=["A", "B", "C"],
                index=[1, 2, 3, 4, 5],
            )
        return df

    def generate_ans(data):
        _a = data
        df = _a
        for col1, col2 in IT.combinations(df.columns, 2):

            def tau(idx):
                B = df[[col1, col2]].iloc[idx]
                return stats.kendalltau(B[col1], B[col2])[0]

            df[col1 + col2] = (
                pd.Series(np.arange(len(df)), index=df.index).rolling(3).apply(tau)
            )
        return df

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    pd.testing.assert_frame_equal(result, ans, atol=1e-3, check_dtype=False)
    return 1

exec_context = r"""
import pandas as pd
import numpy as np
import scipy.stats as stats
df = test_input
[insert]
result = df
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
What is the canonical way to check if a SciPy CSR matrix is empty (i.e. contains only zeroes)?
I use nonzero():
def is_csr_matrix_only_zeroes(my_csr_matrix):
    return(len(my_csr_matrix.nonzero()[0]) == 0)
from scipy.sparse import csr_matrix
print(is_csr_matrix_only_zeroes(csr_matrix([[1,2,0],[0,0,3],[4,0,5]])))
print(is_csr_matrix_only_zeroes(csr_matrix([[0,0,0],[0,0,0],[0,0,0]])))
print(is_csr_matrix_only_zeroes(csr_matrix((2,3))))
print(is_csr_matrix_only_zeroes(csr_matrix([[0,0,0],[0,1,0],[0,0,0]])))
outputs
False
True
True
False
but I wonder whether there exist more direct or efficient ways, i.e. just get True or False?
A:
<code>
from scipy import sparse
sa = sparse.random(10, 10, density = 0.01, format = 'csr')
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

import copy
from scipy import sparse

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            sa = sparse.random(10, 10, density=0.01, format="csr", random_state=42)
        elif test_case_id == 2:
            sa = sparse.csr_matrix([])
        return sa

    def generate_ans(data):
        _a = data
        sa = _a
        result = sa.count_nonzero() == 0
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    assert result == ans
    return 1

exec_context = r"""
from scipy import sparse
sa = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
What is the canonical way to check if a SciPy lil matrix is empty (i.e. contains only zeroes)?
I use nonzero():
def is_lil_matrix_only_zeroes(my_lil_matrix):
    return(len(my_lil_matrix.nonzero()[0]) == 0)
from scipy.sparse import csr_matrix
print(is_lil_matrix_only_zeroes(lil_matrix([[1,2,0],[0,0,3],[4,0,5]])))
print(is_lil_matrix_only_zeroes(lil_matrix([[0,0,0],[0,0,0],[0,0,0]])))
print(is_lil_matrix_only_zeroes(lil_matrix((2,3))))
print(is_lil_matrix_only_zeroes(lil_matrix([[0,0,0],[0,1,0],[0,0,0]])))
outputs
False
True
True
False
but I wonder whether there exist more direct or efficient ways, i.e. just get True or False?
A:
<code>
from scipy import sparse
sa = sparse.random(10, 10, density = 0.01, format = 'lil')
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

import copy
from scipy import sparse

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            sa = sparse.random(10, 10, density=0.01, format="lil", random_state=42)
        elif test_case_id == 2:
            sa = sparse.lil_matrix([])
        return sa

    def generate_ans(data):
        _a = data
        sa = _a
        result = sa.count_nonzero() == 0
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    assert result == ans
    return 1

exec_context = r"""
from scipy import sparse
sa = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I am looking for a way to convert a nXaXb numpy array into a block diagonal matrix. I have already came across scipy.linalg.block_diag, the down side of which (for my case) is it requires each blocks of the matrix to be given separately. However, this is challenging when n is very high, so to make things more clear lets say I have a
import numpy as np
a = np.random.rand(3,2,2)
array([[[ 0.33599705,  0.92803544],
        [ 0.6087729 ,  0.8557143 ]],
       [[ 0.81496749,  0.15694689],
        [ 0.87476697,  0.67761456]],
       [[ 0.11375185,  0.32927167],
        [ 0.3456032 ,  0.48672131]]])

what I want to achieve is something the same as
from scipy.linalg import block_diag
block_diag(a[0], a[1],a[2])
array([[ 0.33599705,  0.92803544,  0.        ,  0.        ,  0.        ,   0.        ],
       [ 0.6087729 ,  0.8557143 ,  0.        ,  0.        ,  0.        ,   0.        ],
       [ 0.        ,  0.        ,  0.81496749,  0.15694689,  0.        ,   0.        ],
       [ 0.        ,  0.        ,  0.87476697,  0.67761456,  0.        ,   0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.11375185,   0.32927167],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.3456032 ,   0.48672131]])

This is just as an example in actual case a has hundreds of elements.

A:
<code>
import numpy as np
from scipy.linalg import block_diag
np.random.seed(10)
a = np.random.rand(100,2,2)
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

import numpy as np
import copy
from scipy.linalg import block_diag

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            np.random.seed(10)
            a = np.random.rand(100, 2, 2)
        elif test_case_id == 2:
            np.random.seed(20)
            a = np.random.rand(10, 3, 3)
        return a

    def generate_ans(data):
        _a = data
        a = _a
        result = block_diag(*a)
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    np.testing.assert_array_equal(result, ans)
    return 1

exec_context = r"""
import numpy as np
from scipy.linalg import block_diag
a = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have the following code to run Wilcoxon rank-sum test
print stats.ranksums(pre_course_scores, during_course_scores)
RanksumsResult(statistic=8.1341352369246582, pvalue=4.1488919597127145e-16)

However, I am interested in extracting the pvalue from the result. I could not find a tutorial about this. i.e.Given two ndarrays, pre_course_scores, during_course_scores, I want to know the pvalue of ranksum. Can someone help?

A:
<code>
import numpy as np
from scipy import stats
np.random.seed(10)
pre_course_scores = np.random.randn(10)
during_course_scores = np.random.randn(10)
</code>
p_value = ... # put solution in this variable
BEGIN SOLUTION
<code>

import numpy as np
import copy
from scipy import stats

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            np.random.seed(10)
            A = np.random.randn(10)
            B = np.random.randn(10)
        return A, B

    def generate_ans(data):
        _a = data
        pre_course_scores, during_course_scores = _a
        p_value = stats.ranksums(pre_course_scores, during_course_scores).pvalue
        return p_value

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    assert abs(result - ans) <= 1e-5
    return 1

exec_context = r"""
import numpy as np
from scipy import stats
pre_course_scores, during_course_scores = test_input
[insert]
result = p_value
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(1):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have the following code to run Wilcoxon rank-sum test
print stats.ranksums(pre_course_scores, during_course_scores)
RanksumsResult(statistic=8.1341352369246582, pvalue=4.1488919597127145e-16)

However, I am interested in extracting the pvalue from the result. I could not find a tutorial about this. i.e.Given two ndarrays, pre_course_scores, during_course_scores, I want to know the pvalue of ranksum. Can someone help?

A:
<code>
import numpy as np
from scipy import stats
example_pre_course_scores = np.random.randn(10)
example_during_course_scores = np.random.randn(10)
def f(pre_course_scores = example_pre_course_scores, during_course_scores = example_during_course_scores):
    # return the solution in this function
    # p_value = f(pre_course_scores, during_course_scores)
    ### BEGIN SOLUTION

import numpy as np
import copy
from scipy import stats

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            np.random.seed(10)
            A = np.random.randn(10)
            B = np.random.randn(10)
        return A, B

    def generate_ans(data):
        _a = data
        pre_course_scores, during_course_scores = _a
        p_value = stats.ranksums(pre_course_scores, during_course_scores).pvalue
        return p_value

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    assert abs(result - ans) <= 1e-5
    return 1

exec_context = r"""
import numpy as np
from scipy import stats
def f(pre_course_scores, during_course_scores):
[insert]
pre_course_scores, during_course_scores = test_input
result = f(pre_course_scores, during_course_scores)
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(1):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
How to calculate kurtosis (the fourth standardized moment, according to Pearson’s definition) without bias correction?
I have tried scipy.stats.kurtosis, but it gives a different result. I followed the definition in mathworld.
A:
<code>
import numpy as np
a = np.array([   1. ,    2. ,    2.5,  400. ,    6. ,    0. ])
</code>
kurtosis_result = ... # put solution in this variable
BEGIN SOLUTION
<code>

kurtosis_result = (sum((a - np.mean(a)) ** 4)/len(a)) / np.std(a)**4

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.0, 2.0, 2.5, 400.0, 6.0, 0.0])
        elif test_case_id == 2:
            np.random.seed(42)
            a = np.random.randn(10)
        return a

    def generate_ans(data):
        _a = data
        a = _a
        kurtosis_result = (sum((a - np.mean(a)) ** 4) / len(a)) / np.std(a) ** 4
        return kurtosis_result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    assert np.allclose(result, ans)
    return 1

exec_context = r"""
import numpy as np
a = test_input
[insert]
result = kurtosis_result
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
How to calculate kurtosis (according to Fisher’s definition) without bias correction?
A:
<code>
import numpy as np
import scipy.stats
a = np.array([   1. ,    2. ,    2.5,  400. ,    6. ,    0. ])
</code>
kurtosis_result = ... # put solution in this variable
BEGIN SOLUTION
<code>

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:
            a = np.array([1.0, 2.0, 2.5, 400.0, 6.0, 0.0])
        elif test_case_id == 2:
            np.random.seed(42)
            a = np.random.randn(10)
        return a

    def generate_ans(data):
        _a = data
        a = _a
        kurtosis_result = scipy.stats.kurtosis(a)
        return kurtosis_result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    assert np.allclose(result, ans)
    return 1

exec_context = r"""
import numpy as np
import scipy.stats
a = test_input
[insert]
result = kurtosis_result
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have a table of measured values for a quantity that depends on two parameters. So say I have a function fuelConsumption(speed, temperature), for which data on a mesh are known.
Now I want to interpolate the expected fuelConsumption for a lot of measured data points (speed, temperature) from a pandas.DataFrame (and return a vector with the values for each data point).
I am currently using SciPy's interpolate.interp2d for cubic interpolation, but when passing the parameters as two vectors [s1,s2] and [t1,t2] (only two ordered values for simplicity) it will construct a mesh and return:
[[f(s1,t1), f(s2,t1)], [f(s1,t2), f(s2,t2)]]
The result I am hoping to get is:
[f(s1,t1), f(s2, t2)]
How can I interpolate to get the output I want?
I want to use function interpolated on x, y, z to compute values on arrays s and t, and the result should be like mentioned above.
A:
<code>
import numpy as np
import scipy.interpolate
s = np.linspace(-1, 1, 50)
t = np.linspace(-2, 0, 50)
x, y = np.ogrid[-1:1:10j,-2:0:10j]
z = (x + y)*np.exp(-6.0 * (x * x + y * y))
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

import numpy as np
import copy
import scipy.interpolate

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            s = np.linspace(-1, 1, 50)
            t = np.linspace(-2, 0, 50)
        return s, t

    def generate_ans(data):
        _a = data
        s, t = _a
        x, y = np.ogrid[-1:1:10j, -2:0:10j]
        z = (x + y) * np.exp(-6.0 * (x * x + y * y))
        spl = scipy.interpolate.RectBivariateSpline(x, y, z)
        result = spl(s, t, grid=False)
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    np.testing.assert_allclose(result, ans)
    return 1

exec_context = r"""
import numpy as np
import scipy.interpolate
s, t = test_input
x, y = np.ogrid[-1:1:10j,-2:0:10j]
z = (x + y)*np.exp(-6.0 * (x * x + y * y))
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(1):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have a table of measured values for a quantity that depends on two parameters. So say I have a function fuelConsumption(speed, temperature), for which data on a mesh are known.
Now I want to interpolate the expected fuelConsumption for a lot of measured data points (speed, temperature) from a pandas.DataFrame (and return a vector with the values for each data point).
I am currently using SciPy's interpolate.interp2d for cubic interpolation, but when passing the parameters as two vectors [s1,s2] and [t1,t2] (only two ordered values for simplicity) it will construct a mesh and return:
[[f(s1,t1), f(s2,t1)], [f(s1,t2), f(s2,t2)]]
The result I am hoping to get is:
[f(s1,t1), f(s2, t2)]
How can I interpolate to get the output I want?
I want to use function interpolated on x, y, z to compute values on arrays s and t, and the result should be like mentioned above.
A:
<code>
import numpy as np
import scipy.interpolate
exampls_s = np.linspace(-1, 1, 50)
example_t = np.linspace(-2, 0, 50)
def f(s = example_s, t = example_t):
    x, y = np.ogrid[-1:1:10j,-2:0:10j]
    z = (x + y)*np.exp(-6.0 * (x * x + y * y))
    # return the solution in this function
    # result = f(s, t)
    ### BEGIN SOLUTION

import numpy as np
import copy
import scipy.interpolate

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            s = np.linspace(-1, 1, 50)
            t = np.linspace(-2, 0, 50)
        return s, t

    def generate_ans(data):
        _a = data
        s, t = _a
        x, y = np.ogrid[-1:1:10j, -2:0:10j]
        z = (x + y) * np.exp(-6.0 * (x * x + y * y))
        spl = scipy.interpolate.RectBivariateSpline(x, y, z)
        result = spl(s, t, grid=False)
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    np.testing.assert_allclose(result, ans)
    return 1

exec_context = r"""
import numpy as np
import scipy.interpolate
s, t = test_input
def f(s, t):
    x, y = np.ogrid[-1:1:10j,-2:0:10j]
    z = (x + y)*np.exp(-6.0 * (x * x + y * y))
[insert]
result = f(s, t)
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(1):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I think my questions has something in common with this question or others, but anyway, mine is not specifically about them.
I would like, after having found the voronoi tessallination for certain points, be able to check where other given points sit within the tessellination. In particular:
Given say 50 extra-points, I want to be able to count how many of these extra points each voronoi cell contains.
My MWE
from scipy.spatial import ConvexHull, Voronoi
points = [[0,0], [1,4], [2,3], [4,1], [1,1], [2,2], [5,3]]
#voronoi
vor = Voronoi(points)
Now I am given extra points
extraPoints = [[0.5,0.2], [3, 0], [4,0],[5,0], [4,3]]
# In this case we have that the first point is in the bottom left,
# the successive three are in the bottom right and the last one
# is in the top right cell.
I was thinking to use the fact that you can get vor.regions or vor.vertices, however I really couldn't come up with anything..
Is there parameter or a way to make this? The result I want is an np.array containing indices standing for regions occupied by different points, i.e., 1 for [1, 4]’s region.
A:
<code>
import scipy.spatial
points = [[0,0], [1,4], [2,3], [4,1], [1,1], [2,2], [5,3]]
vor = scipy.spatial.Voronoi(points)
extraPoints = [[0.5,0.2], [3, 0], [4,0],[5,0], [4,3]]
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

import numpy as np
import copy
import scipy.spatial

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            points = [[0, 0], [1, 4], [2, 3], [4, 1], [1, 1], [2, 2], [5, 3]]
            extraPoints = [[0.5, 0.2], [3, 0], [4, 0], [5, 0], [4, 3]]
        elif test_case_id == 2:
            np.random.seed(42)
            points = (np.random.rand(15, 2) - 0.5) * 100
            extraPoints = (np.random.rand(10, 2) - 0.5) * 100
        return points, extraPoints

    def generate_ans(data):
        _a = data
        points, extraPoints = _a
        kdtree = scipy.spatial.cKDTree(points)
        _, result = kdtree.query(extraPoints)
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    np.testing.assert_array_equal(result, ans)
    return 1

exec_context = r"""
import scipy.spatial
points, extraPoints = test_input
vor = scipy.spatial.Voronoi(points)
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I think my questions has something in common with this question or others, but anyway, mine is not specifically about them.
I would like, after having found the voronoi tessallination for certain points, be able to check where other given points sit within the tessellination. In particular:
Given say 50 extra-points, I want to be able to count how many of these extra points each voronoi cell contains.
My MWE
from scipy.spatial import ConvexHull, Voronoi
points = [[0,0], [1,4], [2,3], [4,1], [1,1], [2,2], [5,3]]
#voronoi
vor = Voronoi(points)
Now I am given extra points
extraPoints = [[0.5,0.2], [3, 0], [4,0],[5,0], [4,3]]
# In this case we have that the first point is in the bottom left,
# the successive three are in the bottom right and the last one
# is in the top right cell.
I was thinking to use the fact that you can get vor.regions or vor.vertices, however I really couldn't come up with anything..
Is there parameter or a way to make this? The result I want is an np.array containing indices standing for regions occupied by different points, and that should be defined by Voronoi cell.
A:
<code>
import scipy.spatial
points = [[0,0], [1,4], [2,3], [4,1], [1,1], [2,2], [5,3]]
vor = scipy.spatial.Voronoi(points)
extraPoints = [[0.5,0.2], [3, 0], [4,0],[5,0], [4,3]]
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

import numpy as np
import copy
import scipy.spatial

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            points = [[0, 0], [1, 4], [2, 3], [4, 1], [1, 1], [2, 2], [5, 3]]
            extraPoints = [[0.5, 0.2], [3, 0], [4, 0], [5, 0], [4, 3]]
        elif test_case_id == 2:
            np.random.seed(42)
            points = (np.random.rand(15, 2) - 0.5) * 100
            extraPoints = (np.random.rand(10, 2) - 0.5) * 100
        return points, extraPoints

    def generate_ans(data):
        _a = data
        points, extraPoints = _a
        vor = scipy.spatial.Voronoi(points)
        kdtree = scipy.spatial.cKDTree(points)
        _, index = kdtree.query(extraPoints)
        result = vor.point_region[index]
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    np.testing.assert_array_equal(result, ans)
    return 1

exec_context = r"""
import scipy.spatial
points, extraPoints = test_input
vor = scipy.spatial.Voronoi(points)
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have a list of numpy vectors of the format:
    [array([[-0.36314615,  0.80562619, -0.82777381, ...,  2.00876354,2.08571887, -1.24526026]]),
     array([[ 0.9766923 , -0.05725135, -0.38505339, ...,  0.12187988,-0.83129255,  0.32003683]]),
     array([[-0.59539878,  2.27166874,  0.39192573, ..., -0.73741573,1.49082653,  1.42466276]])]

here, only 3 vectors in the list are shown. I have 100s..
The maximum number of elements in one vector is around 10 million
All the arrays in the list have unequal number of elements but the maximum number of elements is fixed.
Is it possible to create a sparse matrix using these vectors in python such that I have padded zeros to the end of elements for the vectors which are smaller than the maximum size?

A:
<code>
import numpy as np
import scipy.sparse as sparse

np.random.seed(10)
max_vector_size = 1000
vectors = [np.random.randint(100,size=900),np.random.randint(100,size=max_vector_size),np.random.randint(100,size=950)]
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

result = sparse.lil_matrix((len(vectors), max_vector_size))
for i, v in enumerate(vectors):
    result[i, :v.size] = v

import numpy as np
import copy
import scipy.sparse as sparse

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            np.random.seed(10)
            max_vector_size = 1000
            vectors = [
                np.random.randint(100, size=900),
                np.random.randint(100, size=max_vector_size),
                np.random.randint(100, size=950),
            ]
        elif test_case_id == 2:
            np.random.seed(42)
            max_vector_size = 300
            vectors = [
                np.random.randint(200, size=200),
                np.random.randint(200, size=max_vector_size),
                np.random.randint(200, size=200),
                np.random.randint(200, size=max_vector_size),
                np.random.randint(200, size=200),
            ]
        return vectors, max_vector_size

    def generate_ans(data):
        _a = data
        vectors, max_vector_size = _a
        result = sparse.lil_matrix((len(vectors), max_vector_size))
        for i, v in enumerate(vectors):
            result[i, : v.size] = v
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    assert len(sparse.find(result != ans)[0]) == 0
    return 1

exec_context = r"""
import numpy as np
import scipy.sparse as sparse
vectors, max_vector_size = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have a binary array, say, a = np.random.binomial(n=1, p=1/2, size=(9, 9)). I perform median filtering on it using a 3 x 3 kernel on it, like say, b = nd.median_filter(a, 3). I would expect that this should perform median filter based on the pixel and its eight neighbours. However, I am not sure about the placement of the kernel. The documentation says,

origin : scalar, optional.
The origin parameter controls the placement of the filter. Default 0.0.

Now, I want to shift this filter one cell to the right.How can I achieve it?
Thanks.

A:
<code>
import numpy as np
import scipy.ndimage

a= np.zeros((5, 5))
a[1:4, 1:4] = np.arange(3*3).reshape((3, 3))
</code>
b = ... # put solution in this variable
BEGIN SOLUTION
<code>

import numpy as np
import copy
import scipy.ndimage

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            a = np.zeros((5, 5))
            a[1:4, 1:4] = np.arange(3 * 3).reshape((3, 3))
        return a

    def generate_ans(data):
        _a = data
        a = _a
        b = scipy.ndimage.median_filter(a, size=(3, 3), origin=(0, 1))
        return b

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    np.testing.assert_array_equal(result, ans)
    return 1

exec_context = r"""
import numpy as np
import scipy.ndimage
a = test_input
[insert]
result = b
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(1):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have a sparse matrix in csr format (which makes sense for my purposes, as it has lots of rows but relatively few columns, ~8million x 90).
My question is, what's the most efficient way to access a particular value from the matrix given a row,column tuple? I can quickly get a row using matrix.getrow(row), but this also returns 1-row sparse matrix, and accessing the value at a particular column seems clunky.
The only reliable method I've found to get a particular matrix value, given the row and column, is:
getting the row vector, converting to dense array, and fetching the element on column.

But this seems overly verbose and complicated. and I don't want to change it to dense matrix to keep the efficiency.
Is there a simpler/faster method I'm missing?

A:
<code>
import numpy as np
from scipy.sparse import csr_matrix

arr = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
M = csr_matrix(arr)
row = 2
column = 3
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

import numpy as np
import copy
import tokenize, io
from scipy.sparse import csr_matrix

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            arr = np.array(
                [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
            )
            row = 2
            column = 3
        elif test_case_id == 2:
            np.random.seed(42)
            arr = np.random.randint(0, 3, (10, 10))
            row = np.random.randint(0, 8)
            column = np.random.randint(0, 8)
        M = csr_matrix(arr)
        return M, row, column

    def generate_ans(data):
        _a = data
        M, row, column = _a
        result = M[row, column]
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    np.testing.assert_array_equal(result, ans)
    return 1

exec_context = r"""
import numpy as np
from scipy.sparse import csr_matrix
M, row, column = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)

def test_string(solution: str):
    tokens = []
    for token in tokenize.tokenize(io.BytesIO(solution.encode("utf-8")).readline):
        tokens.append(token.string)
    assert (
        "toarray" not in tokens
        and "array" not in tokens
        and "todense" not in tokens
        and "A" not in tokens
    )


Prompt:
Problem:
I have a sparse matrix in csr format (which makes sense for my purposes, as it has lots of rows but relatively few columns, ~8million x 90).
My question is, what's the most efficient way to access particular values from the matrix given lists of row,column indices? I can quickly get a row using matrix.getrow(row), but this also returns 1-row sparse matrix, and accessing the value at a particular column seems clunky. The only reliable method I've found to get a particular matrix value, given the row and column, is:
getting the row vector, converting to dense array, and fetching the element on column.

But this seems overly verbose and complicated. and I don't want to change it to dense matrix to keep the efficiency.
for example, I want to fetch elements at (2, 3) and (1, 0), so row = [2, 1], and column = [3, 0].
The result should be a list or 1-d array like: [matirx[2, 3], matrix[1, 0]]
Is there a simpler/faster method I'm missing?

A:
<code>
import numpy as np
from scipy.sparse import csr_matrix

arr = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
M = csr_matrix(arr)
row = [2, 1]
column = [3, 0]
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

import numpy as np
import copy
import tokenize, io
from scipy.sparse import csr_matrix

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            arr = np.array(
                [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
            )
            row = [2, 1]
            column = [3, 0]
        elif test_case_id == 2:
            np.random.seed(42)
            arr = np.random.randint(0, 3, (10, 10))
            row = np.random.randint(0, 8, (2,))
            column = np.random.randint(0, 8, (2,))
        M = csr_matrix(arr)
        return M, row, column

    def generate_ans(data):
        _a = data
        M, row, column = _a
        result = np.array(M[row, column]).squeeze()
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    np.testing.assert_array_equal(result, ans)
    return 1

exec_context = r"""
import numpy as np
from scipy.sparse import csr_matrix
M, row, column = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)

def test_string(solution: str):
    tokens = []
    for token in tokenize.tokenize(io.BytesIO(solution.encode("utf-8")).readline):
        tokens.append(token.string)
    assert "toarray" not in tokens and "todense" not in tokens and "A" not in tokens


Prompt:
Problem:
I have an array which I want to interpolate over the 1st axes. At the moment I am doing it like this example:
import numpy as np
from scipy.interpolate import interp1d
array = np.random.randint(0, 9, size=(100, 100, 100))
new_array = np.zeros((1000, 100, 100))
x = np.arange(0, 100, 1)
x_new = np.arange(0, 100, 0.1)
for i in x:
    for j in x:
        f = interp1d(x, array[:, i, j])
        new_array[:, i, j] = f(xnew)
The data I use represents 10 years of 5-day averaged values for each latitude and longitude in a domain. I want to create an array of daily values.
I have also tried using splines. I don't really know how they work but it was not much faster.
Is there a way to do this without using for loops? The result I want is an np.array of transformed x_new values using interpolated function.
Thank you in advance for any suggestions.
A:
<code>
import numpy as np
import scipy.interpolate
array = np.random.randint(0, 9, size=(10, 10, 10))
x = np.linspace(0, 10, 10)
x_new = np.linspace(0, 10, 100)
</code>
new_array = ... # put solution in this variable
BEGIN SOLUTION
<code>

import numpy as np
import copy
import tokenize, io
import scipy.interpolate

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            np.random.seed(42)
            array = np.random.randint(0, 9, size=(10, 10, 10))
            x = np.linspace(0, 10, 10)
            x_new = np.linspace(0, 10, 100)
        return x, array, x_new

    def generate_ans(data):
        _a = data
        x, array, x_new = _a
        new_array = scipy.interpolate.interp1d(x, array, axis=0)(x_new)
        return new_array

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    assert type(result) == np.ndarray
    assert np.allclose(result, ans)
    return 1

exec_context = r"""
import numpy as np
import scipy.interpolate
x, array, x_new = test_input
[insert]
result = new_array
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(1):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)

def test_string(solution: str):
    tokens = []
    for token in tokenize.tokenize(io.BytesIO(solution.encode("utf-8")).readline):
        tokens.append(token.string)
    assert "while" not in tokens and "for" not in tokens


Prompt:
Problem:

I'm trying to integrate X (X ~ N(u, o2)) to calculate the probability up to position `x`.
However I'm running into an error of:
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
  File "siestats.py", line 349, in NormalDistro
    P_inner = scipy.integrate(NDfx,-dev,dev)
TypeError: 'module' object is not callable
My code runs this:
# Definition of the mathematical function:
def NDfx(x):
    return((1/math.sqrt((2*math.pi)))*(math.e**((-.5)*(x**2))))
# This Function normailizes x, u, and o2 (position of interest, mean and st dev)
# and then calculates the probability up to position 'x'
def NormalDistro(u,o2,x):
    dev = abs((x-u)/o2)
    P_inner = scipy.integrate(NDfx,-dev,dev)
    P_outer = 1 - P_inner
    P = P_inner + P_outer/2
    return(P)

A:
<code>
import scipy.integrate
import math
import numpy as np
def NDfx(x):
    return((1/math.sqrt((2*math.pi)))*(math.e**((-.5)*(x**2))))
x = 2.5
u = 1
o2 = 3
</code>
prob = ... # put solution in this variable
BEGIN SOLUTION
<code>

norm = (x-u)/o2
prob = scipy.integrate.quad(NDfx, -np.inf, norm)[0]

import numpy as np
import math
import copy
import scipy.integrate

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            x = 2.5
            u = 1
            o2 = 3
        elif test_case_id == 2:
            x = -2.5
            u = 2
            o2 = 4
        return x, u, o2

    def generate_ans(data):
        _a = data

        def NDfx(x):
            return (1 / math.sqrt((2 * math.pi))) * (math.e ** ((-0.5) * (x**2)))

        x, u, o2 = _a
        norm = (x - u) / o2
        prob = scipy.integrate.quad(NDfx, -np.inf, norm)[0]
        return prob

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    assert np.allclose(result, ans)
    return 1

exec_context = r"""
import scipy.integrate
import math
import numpy as np
x, u, o2 = test_input
def NDfx(x):
    return((1/math.sqrt((2*math.pi)))*(math.e**((-.5)*(x**2))))
[insert]
result = prob
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:

I'm trying to integrate X (X ~ N(u, o2)) to calculate the probability up to position `x`.
However I'm running into an error of:
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
  File "siestats.py", line 349, in NormalDistro
    P_inner = scipy.integrate(NDfx,-dev,dev)
TypeError: 'module' object is not callable
My code runs this:
# Definition of the mathematical function:
def NDfx(x):
    return((1/math.sqrt((2*math.pi)))*(math.e**((-.5)*(x**2))))
# This Function normailizes x, u, and o2 (position of interest, mean and st dev)
# and then calculates the probability up to position 'x'
def NormalDistro(u,o2,x):
    dev = abs((x-u)/o2)
    P_inner = scipy.integrate(NDfx,-dev,dev)
    P_outer = 1 - P_inner
    P = P_inner + P_outer/2
    return(P)

A:
<code>
import scipy.integrate
import math
import numpy as np
def NDfx(x):
    return((1/math.sqrt((2*math.pi)))*(math.e**((-.5)*(x**2))))
def f(x = 2.5, u = 1, o2 = 3):
    # return the solution in this function
    # prob = f(x, u, o2)
    ### BEGIN SOLUTION

    norm = (x-u)/o2
    prob = scipy.integrate.quad(NDfx, -np.inf, norm)[0]
    return prob

import numpy as np
import math
import copy
import scipy.integrate

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            x = 2.5
            u = 1
            o2 = 3
        elif test_case_id == 2:
            x = -2.5
            u = 2
            o2 = 4
        return x, u, o2

    def generate_ans(data):
        _a = data

        def NDfx(x):
            return (1 / math.sqrt((2 * math.pi))) * (math.e ** ((-0.5) * (x**2)))

        x, u, o2 = _a
        norm = (x - u) / o2
        prob = scipy.integrate.quad(NDfx, -np.inf, norm)[0]
        return prob

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    assert np.allclose(result, ans)
    return 1

exec_context = r"""
import scipy.integrate
import math
import numpy as np
x, u, o2 = test_input
def NDfx(x):
    return((1/math.sqrt((2*math.pi)))*(math.e**((-.5)*(x**2))))
def f(x, u, o2):
[insert]
result = f(x, u, o2)
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:

Using scipy, is there an easy way to emulate the behaviour of MATLAB's dctmtx function which returns a NxN (ortho-mode normed) DCT matrix for some given N? There's scipy.fftpack.dctn but that only applies the DCT. Do I have to implement this from scratch if I don't want use another dependency besides scipy?
A:
<code>
import numpy as np
import scipy.fft as sf
N = 8
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

result = sf.dct(np.eye(N), axis=0, norm= 'ortho')

import numpy as np
import copy
import scipy.fft as sf

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            N = 8
        elif test_case_id == 2:
            np.random.seed(42)
            N = np.random.randint(4, 15)
        return N

    def generate_ans(data):
        _a = data
        N = _a
        result = sf.dct(np.eye(N), axis=0, norm="ortho")
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    assert np.allclose(result, ans)
    return 1

exec_context = r"""
import numpy as np
import scipy.fft as sf
N = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
Having difficulty generating a tridiagonal matrix from numpy arrays. I managed to replicate the results given here, but I'm not able to apply these techniques to my problem. I may also be misunderstanding the application of scipy.sparse.diag.
For context, I'm working on a problem which requires the generation of a tridiagonal matrix to solve an ordinary differential equation numerically using finite differences.
from scipy.sparse import diags
import numpy as np
v1 = [3*i**2 +(i/2) for i in range(1, 6)]
v2 = [-(6*i**2 - 1) for i in range(1, 6)]
v3 = [3*i**2 -(i/2) for i in range(1, 6)]
matrix = np.array([v1, v2, v3])
matrix is equal to.
array([[3.5,   13. ,   28.5,   50. ,   77.5],
       [-5. ,  -23. ,  -53. ,  -95. , -149. ],
       [2.5,   11. ,   25.5,   46. ,   72.5]])
After working through the Scipy documentation and the examples in the link above, I was expecting the following code to yield Tridiagonal_1, but instead get Tridiagonal_2.
diags(matrix, [-1,0,1], (5, 5)).toarray()
expected Tridiagonal_1:
array([[  -5. ,    2.5 ,     0. ,    0. ,     0. ],
       [  13. ,   -23. ,    11. ,    0. ,     0. ],
       [   0. ,    28.5.,  -53. ,   25.5,     0. ],
       [   0. ,    0. ,     50 ,   -95.,     46. ],
       [   0. ,    0. ,      0. ,   77.5., -149. ]])
Code yielded Tridiagonal_2:
array([[  -5. ,    2.5,    0. ,    0. ,    0. ],
       [   3.5,  -23. ,   11. ,    0. ,    0. ],
       [   0. ,   13. ,  -53. ,   25.5,    0. ],
       [   0. ,    0. ,   28.5,  -95. ,   46. ],
       [   0. ,    0. ,    0. ,   50. , -149. ]])
I was expecting offset = [-1,0,1] to shift the diagonal entries to the left, but the first offset is shifting the first diag to the next row. Is this correct or is there an error in my code causing this behaviour?
A:
<code>
from scipy import sparse
import numpy as np
matrix = np.array([[3.5,   13. ,   28.5,   50. ,   77.5],
                   [-5. ,  -23. ,  -53. ,  -95. , -149. ],
                   [2.5,   11. ,   25.5,   46. ,   72.5]])
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

import numpy as np
import copy
from scipy import sparse

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            matrix = np.array(
                [
                    [3.5, 13.0, 28.5, 50.0, 77.5],
                    [-5.0, -23.0, -53.0, -95.0, -149.0],
                    [2.5, 11.0, 25.5, 46.0, 72.5],
                ]
            )
        elif test_case_id == 2:
            np.random.seed(42)
            matrix = np.random.rand(3, 5)
        return matrix

    def generate_ans(data):
        _a = data
        matrix = _a
        result = sparse.spdiags(matrix, (1, 0, -1), 5, 5).T.A
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    np.testing.assert_array_equal(result, ans)
    return 1

exec_context = r"""
from scipy import sparse
import numpy as np
matrix = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
Give the N and P, I want to get a 2D binomial distribution probability matrix M,
for i in range(N+1):
   for j in range(i+1):
      M[i,j] = choose(i, j) * p**j * (1-p)**(i-j)
other value = 0

I want to know is there any fast way to get this matrix, instead of the for loop. the N may be bigger than 100,000

A:
<code>
import numpy as np
import scipy.stats
N = 3
p = 0.5
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

import numpy as np
import copy
import tokenize, io
import scipy.stats

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            N = 3
            p = 0.5
        elif test_case_id == 2:
            np.random.seed(234)
            N = np.random.randint(6, 10)
            p = 0.3
        return N, p

    def generate_ans(data):
        _a = data
        N, p = _a
        n = np.arange(N + 1, dtype=np.int64)
        dist = scipy.stats.binom(p=p, n=n)
        result = dist.pmf(k=np.arange(N + 1, dtype=np.int64)[:, None]).T
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    assert np.allclose(result, ans)
    return 1

exec_context = r"""
import numpy as np
import scipy.stats
N, p = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)

def test_string(solution: str):
    tokens = []
    for token in tokenize.tokenize(io.BytesIO(solution.encode("utf-8")).readline):
        tokens.append(token.string)
    assert "while" not in tokens and "for" not in tokens


Prompt:
Problem:
I have the following data frame:
import pandas as pd
import io
from scipy import stats
temp=u"""probegenes,sample1,sample2,sample3
1415777_at Pnliprp1,20,0.00,11
1415805_at Clps,17,0.00,55
1415884_at Cela3b,47,0.00,100"""
df = pd.read_csv(io.StringIO(temp),index_col='probegenes')
df
It looks like this
                     sample1  sample2  sample3
probegenes
1415777_at Pnliprp1       20        0       11
1415805_at Clps           17        0       55
1415884_at Cela3b         47        0      100
What I want to do is too perform row-zscore calculation using SCIPY. At the end of the day. the result will look like:
                               sample1  sample2  sample3
probegenes
1415777_at Pnliprp1      1.18195176, -1.26346568,  0.08151391
1415805_at Clps         -0.30444376, -1.04380717,  1.34825093
1415884_at Cela3b        -0.04896043, -1.19953047,  1.2484909
A:
<code>
import pandas as pd
import io
from scipy import stats

temp=u"""probegenes,sample1,sample2,sample3
1415777_at Pnliprp1,20,0.00,11
1415805_at Clps,17,0.00,55
1415884_at Cela3b,47,0.00,100"""
df = pd.read_csv(io.StringIO(temp),index_col='probegenes')
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

result = pd.DataFrame(data=stats.zscore(df, axis = 1), index=df.index, columns=df.columns)

import pandas as pd
import io
import copy
from scipy import stats

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            temp = """probegenes,sample1,sample2,sample3
    1415777_at Pnliprp1,20,0.00,11
    1415805_at Clps,17,0.00,55
    1415884_at Cela3b,47,0.00,100"""
        elif test_case_id == 2:
            temp = """probegenes,sample1,sample2,sample3
    1415777_at Pnliprp1,20,2.00,11
    1415805_at Clps,17,0.30,55
    1415884_at Cela3b,47,1.00,100"""
        df = pd.read_csv(io.StringIO(temp), index_col="probegenes")
        return df

    def generate_ans(data):
        _a = data
        df = _a
        result = pd.DataFrame(
            data=stats.zscore(df, axis=1), index=df.index, columns=df.columns
        )
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    pd.testing.assert_frame_equal(result, ans, check_dtype=False)
    return 1

exec_context = r"""
import pandas as pd
import io
from scipy import stats
df = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have the following data frame:
import pandas as pd
import io
from scipy import stats
temp=u"""probegenes,sample1,sample2,sample3
1415777_at Pnliprp1,20,0.00,11
1415805_at Clps,17,0.00,55
1415884_at Cela3b,47,0.00,100"""
df = pd.read_csv(io.StringIO(temp),index_col='probegenes')
df
It looks like this
                     sample1  sample2  sample3
probegenes
1415777_at Pnliprp1       20        0       11
1415805_at Clps           17        0       55
1415884_at Cela3b         47        0      100
What I want to do is too perform column-zscore calculation using SCIPY. At the end of the day. the result will look like:
                               sample1  sample2  sample3
probegenes
1415777_at Pnliprp1             x.xxxxxxxx,    x.xxxxxxxx,  x.xxxxxxxx
1415805_at Clps                 x.xxxxxxxx,    x.xxxxxxxx,  x.xxxxxxxx
1415884_at Cela3b               x.xxxxxxxx,    x.xxxxxxxx,  x.xxxxxxxx
A:
<code>
import pandas as pd
import io
from scipy import stats

temp=u"""probegenes,sample1,sample2,sample3
1415777_at Pnliprp1,20,0.00,11
1415805_at Clps,17,0.00,55
1415884_at Cela3b,47,0.00,100"""
df = pd.read_csv(io.StringIO(temp),index_col='probegenes')
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

result = pd.DataFrame(data=stats.zscore(df, axis = 0), index=df.index, columns=df.columns)

import pandas as pd
import io
import copy
from scipy import stats

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            temp = """probegenes,sample1,sample2,sample3
    1415777_at Pnliprp1,20,0.00,11
    1415805_at Clps,17,0.00,55
    1415884_at Cela3b,47,0.00,100"""
        elif test_case_id == 2:
            temp = """probegenes,sample1,sample2,sample3
    1415777_at Pnliprp1,20,2.00,11
    1415805_at Clps,17,0.30,55
    1415884_at Cela3b,47,1.00,100"""
        df = pd.read_csv(io.StringIO(temp), index_col="probegenes")
        return df

    def generate_ans(data):
        _a = data
        df = _a
        result = pd.DataFrame(
            data=stats.zscore(df, axis=0), index=df.index, columns=df.columns
        )
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    pd.testing.assert_frame_equal(result, ans, check_dtype=False)
    return 1

exec_context = r"""
import pandas as pd
import io
from scipy import stats
df = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have the following data frame:
import pandas as pd
import io
from scipy import stats
temp=u"""probegenes,sample1,sample2,sample3
1415777_at Pnliprp1,20,0.00,11
1415805_at Clps,17,0.00,55
1415884_at Cela3b,47,0.00,100"""
df = pd.read_csv(io.StringIO(temp),index_col='probegenes')
df
It looks like this
                     sample1  sample2  sample3
probegenes
1415777_at Pnliprp1       20        0       11
1415805_at Clps           17        0       55
1415884_at Cela3b         47        0      100
What I want to do is too perform row-zscore calculation using SCIPY. AND I want to show data and zscore together in a single dataframe. At the end of the day. the result will look like:
                               sample1  sample2  sample3
probegenes
1415777_at Pnliprp1   data     20		  0			11
					zscore	  1.18195176 -1.26346568  0.08151391
1415805_at Clps		  data     17		  0			55
					zscore   -0.30444376 -1.04380717  1.34825093
1415884_at Cela3b	  data     47		  0			100
					zscore   -0.04896043 -1.19953047  1.2484909
A:
<code>
import pandas as pd
import io
from scipy import stats

temp=u"""probegenes,sample1,sample2,sample3
1415777_at Pnliprp1,20,0.00,11
1415805_at Clps,17,0.00,55
1415884_at Cela3b,47,0.00,100"""
df = pd.read_csv(io.StringIO(temp),index_col='probegenes')
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

indices = [('1415777_at Pnliprp1', 'data'), ('1415777_at Pnliprp1', 'zscore'), ('1415805_at Clps', 'data'), ('1415805_at Clps', 'zscore'), ('1415884_at Cela3b', 'data'), ('1415884_at Cela3b', 'zscore')]
indices = pd.MultiIndex.from_tuples(indices)
df2 = pd.DataFrame(data=stats.zscore(df, axis = 1), index=df.index, columns=df.columns)
df3 = pd.concat([df, df2], axis=1).to_numpy().reshape(-1, 3)
result = pd.DataFrame(data=df3, index=indices, columns=df.columns)

import pandas as pd
import io
import copy
from scipy import stats

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            temp = """probegenes,sample1,sample2,sample3
    1415777_at Pnliprp1,20,0.00,11
    1415805_at Clps,17,0.00,55
    1415884_at Cela3b,47,0.00,100"""
        elif test_case_id == 2:
            temp = """probegenes,sample1,sample2,sample3
    1415777_at Pnliprp1,20,2.00,11
    1415805_at Clps,17,0.30,55
    1415884_at Cela3b,47,1.00,100"""
        df = pd.read_csv(io.StringIO(temp), index_col="probegenes")
        return df

    def generate_ans(data):
        _a = data
        df = _a
        indices = [
            ("1415777_at Pnliprp1", "data"),
            ("1415777_at Pnliprp1", "zscore"),
            ("1415805_at Clps", "data"),
            ("1415805_at Clps", "zscore"),
            ("1415884_at Cela3b", "data"),
            ("1415884_at Cela3b", "zscore"),
        ]
        indices = pd.MultiIndex.from_tuples(indices)
        df2 = pd.DataFrame(
            data=stats.zscore(df, axis=1), index=df.index, columns=df.columns
        )
        df3 = pd.concat([df, df2], axis=1).to_numpy().reshape(-1, 3)
        result = pd.DataFrame(data=df3, index=indices, columns=df.columns)
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    pd.testing.assert_frame_equal(result, ans, check_dtype=False)
    return 1

exec_context = r"""
import pandas as pd
import io
from scipy import stats
df = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I have the following data frame:
import pandas as pd
import io
from scipy import stats
temp=u"""probegenes,sample1,sample2,sample3
1415777_at Pnliprp1,20,0.00,11
1415805_at Clps,17,0.00,55
1415884_at Cela3b,47,0.00,100"""
df = pd.read_csv(io.StringIO(temp),index_col='probegenes')
df
It looks like this
                     sample1  sample2  sample3
probegenes
1415777_at Pnliprp1       20        0       11
1415805_at Clps           17        0       55
1415884_at Cela3b         47        0      100
What I want to do is too perform column-zscore calculation using SCIPY. AND I want to show data and zscore together in a single dataframe. For each element, I want to only keep 3 decimals places. At the end of the day. the result will look like:
                               sample1  sample2  sample3
probegenes
1415777_at Pnliprp1   data     20.000    0.000    11.000
					zscore	   -0.593    NaN    -1.220
1415805_at Clps		  data     17.000	0.000	55.000
					zscore     -0.815    NaN    -0.009
1415884_at Cela3b	  data     47.000	0.000	100.000
					zscore     1.408     NaN     1.229

A:
<code>
import pandas as pd
import io
import numpy as np
from scipy import stats

temp=u"""probegenes,sample1,sample2,sample3
1415777_at Pnliprp1,20,0.00,11
1415805_at Clps,17,0.00,55
1415884_at Cela3b,47,0.00,100"""
df = pd.read_csv(io.StringIO(temp),index_col='probegenes')
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

indices = [('1415777_at Pnliprp1', 'data'), ('1415777_at Pnliprp1', 'zscore'), ('1415805_at Clps', 'data'), ('1415805_at Clps', 'zscore'), ('1415884_at Cela3b', 'data'), ('1415884_at Cela3b', 'zscore')]
indices = pd.MultiIndex.from_tuples(indices)
df2 = pd.DataFrame(data=stats.zscore(df, axis = 0), index=df.index, columns=df.columns)
df3 = pd.concat([df, df2], axis=1).to_numpy().reshape(-1, 3)
result = pd.DataFrame(data=np.round(df3, 3), index=indices, columns=df.columns)

import numpy as np
import pandas as pd
import io
import copy
from scipy import stats

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            temp = """probegenes,sample1,sample2,sample3
    1415777_at Pnliprp1,20,0.00,11
    1415805_at Clps,17,0.00,55
    1415884_at Cela3b,47,0.00,100"""
        elif test_case_id == 2:
            temp = """probegenes,sample1,sample2,sample3
    1415777_at Pnliprp1,20,2.00,11
    1415805_at Clps,17,0.30,55
    1415884_at Cela3b,47,1.00,100"""
        df = pd.read_csv(io.StringIO(temp), index_col="probegenes")
        return df

    def generate_ans(data):
        _a = data
        df = _a
        indices = [
            ("1415777_at Pnliprp1", "data"),
            ("1415777_at Pnliprp1", "zscore"),
            ("1415805_at Clps", "data"),
            ("1415805_at Clps", "zscore"),
            ("1415884_at Cela3b", "data"),
            ("1415884_at Cela3b", "zscore"),
        ]
        indices = pd.MultiIndex.from_tuples(indices)
        df2 = pd.DataFrame(
            data=stats.zscore(df, axis=0), index=df.index, columns=df.columns
        )
        df3 = pd.concat([df, df2], axis=1).to_numpy().reshape(-1, 3)
        result = pd.DataFrame(data=np.round(df3, 3), index=indices, columns=df.columns)
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    pd.testing.assert_frame_equal(result, ans, atol=1e-3, check_dtype=False)
    return 1

exec_context = r"""
import pandas as pd
import io
import numpy as np
from scipy import stats
df = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I'm searching for examples of using scipy.optimize.line_search. I do not really understand how this function works with multivariable functions. I wrote a simple example
import scipy as sp
import scipy.optimize
def test_func(x):
    return (x[0])**2+(x[1])**2

def test_grad(x):
    return [2*x[0],2*x[1]]

sp.optimize.line_search(test_func,test_grad,[1.8,1.7],[-1.0,-1.0])
And I've got
File "D:\Anaconda2\lib\site-packages\scipy\optimize\linesearch.py", line 259, in phi
return f(xk + alpha * pk, *args)
TypeError: can't multiply sequence by non-int of type 'float'
The result should be the alpha value of line_search
A:
<code>
import scipy
import scipy.optimize
import numpy as np
def test_func(x):
    return (x[0])**2+(x[1])**2

def test_grad(x):
    return [2*x[0],2*x[1]]
starting_point = [1.8, 1.7]
direction = [-1, -1]
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

result = scipy.optimize.line_search(test_func, test_grad, np.array(starting_point), np.array(direction))[0]

import numpy as np
import copy
import scipy.optimize

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            starting_point = [1.8, 1.7]
            direction = [-1, -1]
        elif test_case_id == 2:
            starting_point = [50, 37]
            direction = [-1, -1]
        return starting_point, direction

    def generate_ans(data):
        _a = data

        def test_func(x):
            return (x[0]) ** 2 + (x[1]) ** 2

        def test_grad(x):
            return [2 * x[0], 2 * x[1]]

        starting_point, direction = _a
        result = scipy.optimize.line_search(
            test_func, test_grad, np.array(starting_point), np.array(direction)
        )[0]
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    assert abs(result - ans) <= 1e-5
    return 1

exec_context = r"""
import scipy
import scipy.optimize
import numpy as np
def test_func(x):
    return (x[0])**2+(x[1])**2
def test_grad(x):
    return [2*x[0],2*x[1]]
starting_point, direction = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(2):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I'm trying to create a 2-dimensional array in Scipy/Numpy where each value represents the euclidean distance from the center.
I'm very new to Scipy, and would like to know if there's a more elegant, idiomatic way of doing the same thing. I found the scipy.spatial.distance.cdist function, which seems promising, but I'm at a loss regarding how to fit it into this problem.
def get_distance_2(y, x):
    mid = ...  # needs to be a array of the shape (rows, cols, 2)?
    return scipy.spatial.distance.cdist(scipy.dstack((y, x)), mid)
Just to clarify, what I'm looking for is something like this (for a 6 x 6 array). That is, to compute (Euclidean) distances from center point to every point in the image.
[[ 3.53553391  2.91547595  2.54950976  2.54950976  2.91547595  3.53553391]
 [ 2.91547595  2.12132034  1.58113883  1.58113883  2.12132034  2.91547595]
 [ 2.54950976  1.58113883  0.70710678  0.70710678  1.58113883  2.54950976]
 [ 2.54950976  1.58113883  0.70710678  0.70710678  1.58113883  2.54950976]
 [ 2.91547595  2.12132034  1.58113883  1.58113883  2.12132034  2.91547595]
 [ 3.53553391  2.91547595  2.54950976  2.54950976  2.91547595  3.53553391]]
A:
<code>
import numpy as np
from scipy.spatial import distance
shape = (6, 6)
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

xs, ys = np.indices(shape)
xs = xs.reshape(shape[0] * shape[1], 1)
ys = ys.reshape(shape[0] * shape[1], 1)
X = np.hstack((xs, ys))
mid_x, mid_y = (shape[0]-1)/2.0, (shape[1]-1)/2.0
result = distance.cdist(X, np.atleast_2d([mid_x, mid_y])).reshape(shape)

import numpy as np
import copy
from scipy.spatial import distance

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            shape = (6, 6)
        elif test_case_id == 2:
            shape = (10, 3)
        elif test_case_id == 3:
            np.random.seed(42)
            shape = np.random.randint(2, 15, (2,))
        return shape

    def generate_ans(data):
        _a = data
        shape = _a
        xs, ys = np.indices(shape)
        xs = xs.reshape(shape[0] * shape[1], 1)
        ys = ys.reshape(shape[0] * shape[1], 1)
        X = np.hstack((xs, ys))
        mid_x, mid_y = (shape[0] - 1) / 2.0, (shape[1] - 1) / 2.0
        result = distance.cdist(X, np.atleast_2d([mid_x, mid_y])).reshape(shape)
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    assert np.allclose(result, ans)
    return 1

exec_context = r"""
import numpy as np
from scipy.spatial import distance
shape = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(3):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I'm trying to create a 2-dimensional array in Scipy/Numpy where each value represents the Manhattan distance from the center. It's supposed to have the same shape as the first two dimensions of a 3-dimensional array (an image, created via scipy.misc.fromimage).
I'm very new to Scipy, and would like to know if there's a more elegant, idiomatic way of doing the same thing. I found the scipy.spatial.distance.cdist function, which seems promising, but I'm at a loss regarding how to fit it into this problem.
def get_distance_2(y, x):
    mid = ...  # needs to be a array of the shape (rows, cols, 2)?
    return scipy.spatial.distance.cdist(scipy.dstack((y, x)), mid)
Just to clarify, what I'm looking for is something like this (for a 6 x 6 array). That is, to compute Manhattan distances from center point to every point in the image.
[[5., 4., 3., 3., 4., 5.],
       [4., 3., 2., 2., 3., 4.],
       [3., 2., 1., 1., 2., 3.],
       [3., 2., 1., 1., 2., 3.],
       [4., 3., 2., 2., 3., 4.],
       [5., 4., 3., 3., 4., 5.]]
A:
<code>
import numpy as np
from scipy.spatial import distance
shape = (6, 6)
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

xs, ys = np.indices(shape)
xs = xs.reshape(shape[0] * shape[1], 1)
ys = ys.reshape(shape[0] * shape[1], 1)
X = np.hstack((xs, ys))
mid_x, mid_y = (shape[0]-1)/2.0, (shape[1]-1)/2.0
result = distance.cdist(X, np.atleast_2d([mid_x, mid_y]), 'minkowski', p=1).reshape(shape)

import numpy as np
import copy
from scipy.spatial import distance

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            shape = (6, 6)
        elif test_case_id == 2:
            shape = (10, 3)
        elif test_case_id == 3:
            np.random.seed(42)
            shape = np.random.randint(2, 15, (2,))
        return shape

    def generate_ans(data):
        _a = data
        shape = _a
        xs, ys = np.indices(shape)
        xs = xs.reshape(shape[0] * shape[1], 1)
        ys = ys.reshape(shape[0] * shape[1], 1)
        X = np.hstack((xs, ys))
        mid_x, mid_y = (shape[0] - 1) / 2.0, (shape[1] - 1) / 2.0
        result = distance.cdist(
            X, np.atleast_2d([mid_x, mid_y]), "minkowski", p=1
        ).reshape(shape)
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    np.testing.assert_array_equal(result, ans)
    return 1

exec_context = r"""
import numpy as np
from scipy.spatial import distance
shape = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(3):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I'm trying to create a 2-dimensional array in Scipy/Numpy where each value represents the euclidean distance from the center. It's supposed to have the same shape as the first two dimensions of a 3-dimensional array (an image, created via scipy.misc.fromimage).
I'm very new to Scipy, and would like to know if there's a more elegant, idiomatic way of doing the same thing. I found the scipy.spatial.distance.cdist function, which seems promising, but I'm at a loss regarding how to fit it into this problem.
def get_distance_2(y, x):
    mid = ...  # needs to be a array of the shape (rows, cols, 2)?
    return scipy.spatial.distance.cdist(scipy.dstack((y, x)), mid)
Just to clarify, what I'm looking for is something like this (for a 6 x 6 array). That is, to compute (Euclidean) distances from center point to every point in the image.
[[ 3.53553391  2.91547595  2.54950976  2.54950976  2.91547595  3.53553391]
 [ 2.91547595  2.12132034  1.58113883  1.58113883  2.12132034  2.91547595]
 [ 2.54950976  1.58113883  0.70710678  0.70710678  1.58113883  2.54950976]
 [ 2.54950976  1.58113883  0.70710678  0.70710678  1.58113883  2.54950976]
 [ 2.91547595  2.12132034  1.58113883  1.58113883  2.12132034  2.91547595]
 [ 3.53553391  2.91547595  2.54950976  2.54950976  2.91547595  3.53553391]]
A:
<code>
import numpy as np
from scipy.spatial import distance
def f(shape = (6, 6)):
    # return the solution in this function
    # result = f(shape = (6, 6))
    ### BEGIN SOLUTION

    xs, ys = np.indices(shape)
    xs = xs.reshape(shape[0] * shape[1], 1)
    ys = ys.reshape(shape[0] * shape[1], 1)
    X = np.hstack((xs, ys))
    mid_x, mid_y = (shape[0]-1)/2.0, (shape[1]-1)/2.0
    result = distance.cdist(X, np.atleast_2d([mid_x, mid_y])).reshape(shape)

    return result

import numpy as np
import copy
from scipy.spatial import distance

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            shape = (6, 6)
        elif test_case_id == 2:
            shape = (10, 3)
        elif test_case_id == 3:
            np.random.seed(42)
            shape = np.random.randint(2, 15, (2,))
        return shape

    def generate_ans(data):
        _a = data
        shape = _a
        xs, ys = np.indices(shape)
        xs = xs.reshape(shape[0] * shape[1], 1)
        ys = ys.reshape(shape[0] * shape[1], 1)
        X = np.hstack((xs, ys))
        mid_x, mid_y = (shape[0] - 1) / 2.0, (shape[1] - 1) / 2.0
        result = distance.cdist(X, np.atleast_2d([mid_x, mid_y])).reshape(shape)
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    assert np.allclose(result, ans)
    return 1

exec_context = r"""
import numpy as np
from scipy.spatial import distance
shape = test_input
def f(shape = (6, 6)):
[insert]
result = f(shape)
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(3):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)


Prompt:
Problem:
I would like to resample a numpy array as suggested here Resampling a numpy array representing an image however this resampling will do so by a factor i.e.
x = np.arange(9).reshape(3,3)
print scipy.ndimage.zoom(x, 2, order=1)
Will create a shape of (6,6) but how can I resample an array to its best approximation within a (4,6),(6,8) or (6,10) shape for instance?
A:
<code>
import numpy as np
import scipy.ndimage
x = np.arange(9).reshape(3, 3)
shape = (6, 8)
</code>
result = ... # put solution in this variable
BEGIN SOLUTION
<code>

result = scipy.ndimage.zoom(x, zoom=(shape[0]/x.shape[0], shape[1]/x.shape[1]), order=1)

import numpy as np
import copy
import scipy.ndimage

def generate_test_case(test_case_id):
    def define_test_input(test_case_id):
        if test_case_id == 1:
            shape = (6, 8)
        elif test_case_id == 2:
            shape = (6, 10)
        elif test_case_id == 3:
            shape = (4, 6)
        elif test_case_id == 4:
            np.random.seed(42)
            shape = np.random.randint(4, 20, (2,))
        x = np.arange(9).reshape(3, 3)
        return x, shape

    def generate_ans(data):
        _a = data
        x, shape = _a
        result = scipy.ndimage.zoom(
            x, zoom=(shape[0] / x.shape[0], shape[1] / x.shape[1]), order=1
        )
        return result

    test_input = define_test_input(test_case_id)
    expected_result = generate_ans(copy.deepcopy(test_input))
    return test_input, expected_result

def exec_test(result, ans):
    np.testing.assert_array_equal(result, ans)
    return 1

exec_context = r"""
import numpy as np
import scipy.ndimage
x, shape = test_input
[insert]
"""

def test_execution(solution: str):
    code = exec_context.replace("[insert]", solution)
    for i in range(4):
        test_input, expected_result = generate_test_case(i + 1)
        test_env = {"test_input": test_input}
        exec(code, test_env)
        assert exec_test(test_env["result"], expected_result)