SOF_demo / numpy.txt
Rubywong123's picture
Upload numpy.txt
f0ec026
Setup
temperature = 0.7, topP = 0.95, turns = 10
A0: change example
A1: change logits(decimal places, array, etc)
A2: change output type (array -> dict, etc)
A3: analogy
A4: dimension(index) involved
A5: inverted operation
A6: order
A7: ±condition/operation
combinations involved, only show the highest level.
MAP
1.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
1
1
1
1
1
1
1
1
0.7
A1
0
0
0
0
1
0
0
1
1
1
1
0.4
A3
0
0
0
0
0
1
0
1
1
1
1
0.4
Origin:
Problem:
I want to multiply the columns of A with the elements in X in the following order: the first element of X multiplies to the first column of A, the second element to the second column and so on.
For example, given:
import numpy as np
X=np.array([10. , 2.46421304, 4.99073939, 5.79902063, 0. ]
A=np.array([[0, 1, 1, 1, 0],
[1, 0, 1, 0, 1],
[1, 1, 0, 1, 1],
[1, 0, 1, 0, 1],
[0, 1, 1, 1, 0]])
I want to produce:
array([[0, 2.464, 4.991, 5.799, 0],
[10, 0, 4.991, 0, 0],
[10, 2.464, 0, 5.799, 0],
[10, 0, 4.991, 0, 0],
[0, 2.464, 4.991, 5.799, 0]])
A:
<code>
import numpy as np
X=np.array([10. , 2.46421304, 4.99073939, 5.79902063, 0. ])
A=np.array([[0, 1, 1, 1, 0],
[1, 0, 1, 0, 1],
[1, 1, 0, 1, 1],
[1, 0, 1, 0, 1],
[0, 1, 1, 1, 0]])
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(B)
</code>
test:
ans = A * X
try:
np.testing.assert_array_equal(ans, B)
print('Test passed!')
except:
print('Test failed...')
A1:
Problem:
I want to multiply the columns of A with the elements in X in the following order: the first element of X multiplies to the first column of A, the second element to the second column and so on.
For example, given:
import numpy as np
X=np.array([10. , 2.46421304, 4.99073939, 5.79902063, 0. ]
A=np.array([[0, 1, 1, 1, 0],
[1, 0, 1, 0, 1],
[1, 1, 0, 1, 1],
[1, 0, 1, 0, 1],
[0, 1, 1, 1, 0]])
I want to produce:
array([[0, 2.464, 4.991, 5.799, 0],
[10, 0, 4.991, 0, 0],
[10, 2.464, 0, 5.799, 0],
[10, 0, 4.991, 0, 0],
[0, 2.464, 4.991, 5.799, 0]])
Note that the result should be kept 3 decimal places just as the example.
A:
<code>
import numpy as np
X=np.array([10. , 2.46421304, 4.99073939, 5.79902063, 0. ])
A=np.array([[0, 1, 1, 1, 0],
[1, 0, 1, 0, 1],
[1, 1, 0, 1, 1],
[1, 0, 1, 0, 1],
[0, 1, 1, 1, 0]])
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(B)
</code>
test:
ans = np.round(A * X, 3)
try:
np.testing.assert_array_equal(ans, B)
print('Test passed!')
except:
print('Test failed...')
A3:
Problem:
I want to multiply the columns of A with the elements in X in the following order: the first element of X multiplies to the first row of A, the second element to the second row and so on.
For example, given:
import numpy as np
X=np.array([10. , 2.46421304, 4.99073939, 5.79902063, 0. ]
A=np.array([[0, 1, 1, 1, 0],
[1, 0, 1, 0, 1],
[1, 1, 0, 1, 1],
[1, 0, 1, 0, 1],
[0, 1, 1, 1, 0]])
I want to produce:
array([[0, 2.464, 4.991, 5.799, 0],
[10, 0, 4.991, 0, 0],
[10, 2.464, 0, 5.799, 0],
[10, 0, 4.991, 0, 0],
[0, 2.464, 4.991, 5.799, 0]])
Note that the result should be kept 3 decimal places just as the example.
A:
<code>
import numpy as np
X=np.array([10. , 2.46421304, 4.99073939, 5.79902063, 0. ])
A=np.array([[0, 1, 1, 1, 0],
[1, 0, 1, 0, 1],
[1, 1, 0, 1, 1],
[1, 0, 1, 0, 1],
[0, 1, 1, 1, 0]])
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(B)
</code>
Test:
ans = np.round((A.T * X).T, 3)
try:
np.testing.assert_array_equal(ans, B)
print('Test passed!')
except:
print('Test failed...')
2.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
0
0
0
0
0
0
0
0
0
Origin:
Problem:
I have a NumPy record array of floats:
ar = np.array([(238.03, 238.0, 237.0),
(238.02, 238.0, 237.01),
(238.05, 238.01, 237.0)],
dtype=[('A', 'f'), ('B', 'f'), ('C', 'f')])
How can I determine min from each column of this record array?
desired:
[238.02 ,238. ,237. ]
A:
<code>
import numpy as np
ar = np.array([(238.03, 238.0, 237.0),
(238.02, 238.0, 237.01),
(238.05, 238.01, 237.0)],
dtype=[('A', 'f'), ('B', 'f'), ('C', 'f')])
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(result)
</code>
Test:
ar_view = ar.view((ar.dtype[0], len(ar.dtype.names)))
ans = ar_view.min(axis=0)
try:
np.testing.assert_array_equal(ans, result)
print('Test passed!')
except:
print('Test failed...')
3.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
1
0
1
0
1
1
0
0
0
1
0.5
A2
1
1
1
1
1
1
0
1
1
1
1
0.9
Origin:
Problem:
Let x be an array [2, 2, 1, 5, 4, 5, 1, 2, 3]. Get two arrays of unique elements and their counts.
A:
<code>
import numpy as np
x = np.array([2, 2, 1, 5, 4, 5, 1, 2, 3])
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(u, indices)
</code>
Test:
try:
np.testing.assert_array_equal(u, np.array([1, 2, 3, 4, 5]))
np.testing.assert_array_equal(indices, np.array([2, 3, 1, 1, 2]))
print('Test passed!')
except:
print('Test failed...')
A2:
Problem:
Let x be an array [2, 2, 1, 5, 4, 5, 1, 2, 3]. Get two arrays of unique elements and their counts.
Desired output(dict):
{1: 2, 2: 3, 3: 1, 4: 1, 5: 2}
A:
<code>
import numpy as np
x = np.array([2, 2, 2, 1, 5, 4, 5, 1, 2, 3])
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(result)
</code>
test:
try:
assert result == {2: 4, 1: 2, 5: 2, 4: 1, 3: 1}
print('Test passed!')
except:
print('Test failed...')
4.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
0
0
0
0
0
0
0
0
0
Origin:
Problem:
Using NumPy, complete the function below. The function should create and return the following 2-D array. You must find a way to generate the array without typing it explicitly:
[[1, 6, 11],
[2, 7, 12],
[3, 8, 13],
[4, 9, 14],
[5, 10, 15]]
A:
<code>
import numpy as np
def create_array():
### BEGIN SOLUTION
[insert]
### END SOLUTION
return result
</code>
test:
try:
np.testing.assert_array_equal(create_array(), np.array([[1,6,11],[2,7,12],[3,8,13],[4,9,14],[5,10,15]]))
print('Test passed!')
except:
print('Test failed...')
5.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
0
0
0
0
0
0
0
0
0
Origin:
Problem:
Complete the function below. The function must return an array that contains the third column of the array "original" which is passed as an argument. The argument must be a 2-D array. If the argument is invalid, return None.
A:
<code>
import numpy as np
def new_array_second_column(original):
### BEGIN SOLUTION
[insert]
### END SOLUTION
return result
</code>
Test:
case = np.arange(16)[1:].reshape((3,5)).T
try:
np.testing.assert_array_equal(new_array_second_column(case), np.array([[11],[12],[13],[14],[15]]))
np.testing.assert_array_equal(new_array_second_column(np.array([1,2,3])), None)
np.testing.assert_array_equal(new_array_second_column(np.array([[1,2],[4,5],[7,8]])), None)
print('Test passed!')
except:
print('Test failed...')
6.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
0
0
0
0
0
0
0
0
0
Origin:
Problem:
I have an array that looks like below:
array([[0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6, 6, 6], [7, 7, 7, 7, 7, 7, 7, 7]])
How can I use reshape to divide it into 4 chucks, such that it looks like
array([[[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]], [[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]], [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7]], [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7]]])
A:
<code>
import numpy as np
a = np.arange(8)[:,None].repeat(8,axis=1)
#BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
b = a.reshape(2,4,2,4).transpose(0,2,1,3)
try:
np.testing.assert_array_equal(ans, b)
print('Test passed!')
except:
print('Test failed...')
7.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
1
1
1
0
0
0
0
0
1
0
1
0.4
A4
1
0
0
0
0
0
0
0
1
0
1
0.2
Origin:
Problem:
I have a numpy array of shape (3, 3, k), where the length k is fixed. The array was processed to a flatten one dimensional one with:
mat2 = numpy.transpose(data, (1, 0, 2)).flatten('C')
How do I reverse this transpose / flattening process to get the original (3, 3, k) shape and ordering of the data array?
A:
<code>
import numpy as np
k = 10
a = np.linspace(0, 89, 90).reshape((3, 3, k))
b = np.transpose(a, (1, 0, 2)).flatten('C')
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans.shape)
</code>
test:
try:
assert id(ans) != id(a)
np.testing.assert_array_equal(ans, a)
print('Test passed!')
except:
print('Test failed...')
A4:
Problem:
I have a numpy array of shape (3, 3, k), where the length k is fixed. The array was processed to a flatten one dimensional one with:
mat2 = numpy.transpose(data, (1, 2, 0)).flatten('C')
How do I reverse this transpose / flattening process to get the original (3, 3, k) shape and ordering of the data array?
A:
<code>
import numpy as np
k = 10
a = np.linspace(0, 89, 90).reshape((3, 3, k))
b = np.transpose(a, (1, 2, 0)).flatten('C')
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans.shape)
</code>
test:
try:
assert id(ans) != id(a)
np.testing.assert_array_equal(ans, a)
print('Test passed!')
except:
print('Test failed...')
8.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
1
1
1
1
1
0
1
1
1
1
1
0.9
A5
1
0
1
1
0
1
1
0
1
1
1
0.7
Origin:
Problem:
I'm generating matrix representations of images with height*width size, and I need to transform them into a vector of pixels. To generate the images, I'm using the following instruction
np.array([[np.random.randint(0, 255, 3) for dummy_row in range(height)] for dummy_col in range(width)])
e.g., (2x2) image
array([[[132, 235, 40], [234, 1, 160]], [[ 69, 108, 218], [198, 179, 165]]])
when I'm requiring
array([[132, 235, 40], [234, 1, 160], [69, 108, 218], [198, 179, 165]])
A:
<code>
import numpy as np
def f(arr):
### BEGIN SOLUTION
[insert]
### END SOLUTION
return result
</code>
tset:
a = np.array([[[132, 235, 40], [234, 1, 160]], [[ 69, 108, 218], [198, 179, 165]]])
b = np.array([[132, 235, 40], [234, 1, 160], [69, 108, 218], [198, 179, 165]])
try:
np.testing.assert_array_equal(f(a), b)
print('Test passed!')
except:
print('Test failed...')
A5:
Problem:
I'm generating matrix representations of images with height*width size, and I need to transform them into a vector of pixels. To generate the images, I'm using the following instruction
e.g., (2x2) image
array([[132, 235, 40], [234, 1, 160], [69, 108, 218], [198, 179, 165]])
when I'm requiring
array([[[132, 235, 40], [234, 1, 160]], [[ 69, 108, 218], [198, 179, 165]]])
A:
<code>
import numpy as np
def f(arr):
### BEGIN SOLUTION
[insert]
### END SOLUTION
return result
</code>
tset:
a = np.array([[[132, 235, 40], [234, 1, 160]], [[ 69, 108, 218], [198, 179, 165]]])
b = np.array([[132, 235, 40], [234, 1, 160], [69, 108, 218], [198, 179, 165]])
try:
np.testing.assert_array_equal(f(b), a)
print('Test passed!')
except:
print('Test failed...')
9.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
1
1
1
1
1
0
1
1
1
1
1
0.9
A4
0
0
0
0
0
0
0
0
0
0
0
0
Origin:
Problem:
I have a df like this:
import pandas as pd
a=[['1/2/2014', 'a', '6', 'z1'], ['1/2/2014', 'a', '3', 'z1'], ['1/3/2014', 'c', '1', 'x3'], ]
df = pd.DataFrame.from_records(a[0:],columns=a[0])
I want to flatten the df so it is one continuous list like so:
['1/2/2014', 'a', '6', 'z1', '1/2/2014', 'a', '3', 'z1','1/3/2014', 'c', '1', 'x3']
A:
<code>
import pandas as pd
import numpy as np
a=[['1/2/2014', 'a', '6', 'z1'], ['1/2/2014', 'a', '3', 'z1'], ['1/3/2014', 'c', '1', 'x3'], ]
df = pd.DataFrame.from_records(a[0:],columns=a[0])
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
try:
np.testing.assert_array_equal(df.to_numpy().flatten(),ans)
print('Test passed!')
except:
print('Test failed...')
A4:
Problem:
I have a df like this:
import pandas as pd
a=[['1/2/2014', 'a', '6', 'z1'], ['1/2/2014', 'a', '3', 'z1'], ['1/3/2014', 'c', '1', 'x3'], ]
df = pd.DataFrame.from_records(a[0:],columns=a[0])
I want to flatten the df so it is one continuous list like so:
['1/2/2014', '1/2/2014', '1/3/2014', 'a', 'a', 'c', '6', '3', '1', 'z1', 'z1', 'x3']
A:
<code>
import pandas as pd
import numpy as np
a=[['1/2/2014', 'a', '6', 'z1'], ['1/2/2014', 'a', '3', 'z1'], ['1/3/2014', 'c', '1', 'x3'], ]
df = pd.DataFrame.from_records(a[0:],columns=a[0])
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
try:
np.testing.assert_array_equal(df.to_numpy().T.flatten(),ans)
print('Test passed!')
except:
print('Test failed...')
10.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
0
0
0
1
1
0
0
1
0.2
A4
0
0
0
0
0
0
0
0
0
0
0
0
Origin:
Problem:
I would like to find a way to quickly manipulate an array of arrays in Numpy like this one, which has a shape of (10,):
[array([0, 1, 3]) ,array([0, 1, 7]), array([2]), array([0, 3]), array([4]), array([5]), array([6]) ,array([1, 7]), array([8]), array([9])]
For instance, I'd like to compute the total number of array elements, which is 16 for the array above, but without doing a for loop since in practice my "nested array" will be quite large.
A:
<code>
import numpy as np
from numpy import array
a = [array([0, 1, 3]) ,array([0, 1, 7]), array([2]), array([0, 3]), array([4]),
array([5]), array([6]) ,array([1, 7]), array([8]), array([9])]
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
try:
np.testing.assert_array_equal(ans,len(np.concatenate(a).ravel()))
print('Test passed!')
except:
print('Test failed...')
+for elimination
A4:
Problem:
I would like to find a way to quickly manipulate an array of arrays in Numpy like this one, which has a shape of (10,):
[array([0, 1, 3]) ,array([[0, 1, 7]]), array([2]), array([[0, 3]]), array([4]), array([5]), array([6]) ,array([1, 7]), array([8]), array([9])]
For instance, I'd like to compute the total number of array elements, but without doing a for loop since in practice my "nested array" will be quite large.
A:
<code>
import numpy as np
from numpy import array
a = [array([0, 1, 3]) ,array([[0, 1, 7]]), array([2]), array([[0, 3]]), array([4]),
array([5]), array([6]) ,array([1, 7]), array([8]), array([9])]
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
a = map(lambda x: x.flatten(), a)
result = sum(map(len, a))
try:
assert result == ans
print('Test passed!')
except:
print('Test failed...')
+for elimination
11.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
1
1
0
0
0
1
0
1
0.3
A1
0
0
0
0
0
0
0
0
0
0
0
0
Origin:
Problem:
I have an array, R. I would like to remove elements corresponding to indices in Remove and then flatten them with the remaining elements. The desired output is attached.
R=np.array([[1.05567452e+11, 1.51583103e+11, 5.66466172e+08],
[6.94076420e+09, 1.96129124e+10, 1.11642674e+09],
[1.88618492e+10, 1.73640817e+10, 4.84980874e+09]])
Remove = [(0, 1),(0,2)]
R1 = R.flatten()
print([R1])
The desired output is
array([1.05567452e+11, 6.94076420e+09, 1.96129124e+10, 1.11642674e+09,
1.88618492e+10, 1.73640817e+10, 4.84980874e+09])
A:
<code>
import numpy as np
R = np.array([[1.05567452e+11, 1.51583103e+11, 5.66466172e+08],
[6.94076420e+09, 1.96129124e+10, 1.11642674e+09],
[1.88618492e+10, 1.73640817e+10, 4.84980874e+09]])
Remove = [(0, 1), (0, 2)]
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
a = np.array([1.05567452e+11,6.94076420e+09,1.96129124e+10,1.11642674e+09,
1.88618492e+10, 1.73640817e+10, 4.84980874e+09])
try:
np.testing.assert_array_equal(a, ans)
print('Test passed!')
except:
print('Test failed...')
A1:
Problem:
I have an array, R. I would like to remove elements corresponding to indices in Remove and then flatten them with the remaining elements. The desired output is attached.
R=np.array([[1.05567452, 1.51583103, 5.66466172],
[6.94076420, 1.96129124, 1.11642674],
[1.88618492, 1.73640817, 4.84980874]])
Remove = [(0, 1),(0,2)]
R1 = R.flatten()
print([R1])
and I want to just keep 2 decimal places.
The desired output is
array([1.06, 6.94, 1.96, 1.12, 1.89, 1.74, 4.85])
A:
<code>
import numpy as np
R = np.array([[1.05567452, 1.51583103, 5.66466172],
[6.94076420, 1.808484, 1.11642674],
[1.88618492, 1.73640817, 4.84980874]])
Remove = [(0, 1), (0, 2)]
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
a = np.array([1.06, 6.94, 1.81, 1.12, 1.89, 1.74, 4.85])
try:
np.testing.assert_array_equal(a, ans)
print('Test passed!')
except:
print('Test failed...')
12.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
1
0
1
1
1
1
0
1
0.5
A4
0
0
1
0
0
0
0
0
0
0
1
0.1
Origin:
Problem:
Now I have a 3D numpy array with shape (2,3,4) as follows:
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
Now, I want to reshape the array to (2,4,3) by swapping the last 2 dimensions of the array as follows:
[[[ 0 4 8]
[ 1 5 9]
[ 2 6 10]
[ 3 7 11]]
[[12 16 20]
[13 17 21]
[14 18 22]
[15 19 23]]]
A:
<code>
import numpy as np
arr = np.array([[[ 0 , 1, 2, 3], [ 4 , 5, 6, 7], [ 8 , 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]])
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
a = np.transpose(arr, axes=(0, 2, 1))
try:
np.testing.assert_array_equal(a, ans)
print('Test passed!')
except:
print('Test failed...')
A4:
Problem:
Now I have a 3D numpy array with shape (2,3,4) as follows:
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
Now, I want to reshape the array by swapping the axes of the array as follows:
[[[ 0, 4, 8],
[12, 16, 20]],
[[ 1, 5, 9],
[13, 17, 21]],
[[ 2, 6, 10],
[14, 18, 22]],
[[ 3, 7, 11],
[15, 19, 23]]]
A:
<code>
import numpy as np
arr = np.array([[[ 0 , 1, 2, 3], [ 4 , 5, 6, 7], [ 8 , 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]])
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
a = np.transpose(arr, axes=(2, 0, 1))
try:
np.testing.assert_array_equal(a, ans)
print('Test passed!')
except:
print('Test failed...')
13.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
0
0
1
0
0
0
0
1
0.1
Origin:
Problem:
I have a numpy array x = np.array([145100, [ 1,2,3 ], [6,5,4]]) and I wish to ravel it to this: [145100, 1,2,3 , 6,5,4]
I tried this, but it didn't give any results:
x = np.ravel(x)
As the shape was still (3,) instead of (5,). What am I missing?
A:
<code>
import numpy as np
x = np.array([145100, [1, 2, 3], [6,5,4]])
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
a = np.hstack(x)
try:
np.testing.assert_array_equal(a, ans)
print('Test passed!')
except:
print('Test failed...')
14.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
1
0
0
0
0
1
0
0
0
1
0.2
Origin:
Problem:
I have an array H of dimension MxN, and an array A of dimension M . I want to scale H rows with array A. I do it this way, taking advantage of element-wise behavior of Numpy
H = numpy.swapaxes(H, 0, 1)
H /= A
H = numpy.swapaxes(H, 0, 1)
It works, but the two swapaxes operations are not very elegant, and I feel there is a more elegant and concise way to achieve the result, without creating temporaries. Would you tell me how ?
A:
<code>
import numpy as np
H = np.array([[ 1.05550870e+00, -1.54640644e-01, 2.01796906e+00],
[6.59741375e-02, 4.69242500e-01, -5.57339470e-03],
[-2.12376646e-01, -9.17792113e-01, -1.20153176e+00],
[3.68068789e-01, -9.98131619e+00, -1.14438249e+01]])
A = np.array([ 1.1845468 , 1.30376536, -0.44912446, 0.04675434])
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
a = H/A[:, None]
try:
np.testing.assert_array_equal(a, ans)
print('Test passed!')
except:
print('Test failed...')
+for detection
15.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
1
0
0
0
0
0
0
0
0
0
1
0.1
Origin:
Problem:
I am trying to convert a string into n-dimensioned numpy array (x, 4, 4). Basic requirement is a 4x4 array with column major filling of values. We will use as many 4x4 arrays as per the length of the input string. For example if my string is:
'A quick brown fox jumps over dog'
The resultant array should look like this:
[[['A' 'i' 'b' 'n']
[' ' 'c' 'r' ' ']
['q' 'k' 'o' 'f']
['u' ' ' 'w' 'o']]
[['x' 'm' 'o' ' ']
[' ' 'p' 'v' 'd']
['j' 's' 'e' 'o']
['u' ' ' 'r' 'g']]]
Note that instead of the conventional row-first filling of values requirement is for the filling to be column first within the 4x4 subarray.
A:
<code>
import numpy as np
string = 'A quick brown fox jumps over dog'
#BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
test:
matrix2 = np.array(list(string)).reshape(-1,4,4).swapaxes(1,2)
try:
np.testing.assert_array_equal(matrix2, ans)
print('Test passed!')
except:
print('Test failed...')
16.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
0
0
0
0
0
0
0
0
0
Origin:
Problem:
Consider the following arrays:
a = np.array([0,1])[:,None]
b = np.array([1,2,3])
print(a)
array([[0],
[1]])
print(b)
b = np.array([1,2,3])
Is there a simple way to concatenate these two arrays in a way that the latter is broadcast, in order to obtain the following?
array([[0, 1, 2, 3],
[1, 1, 2, 3]])
A:
<code>
import numpy as np
a = np.array([0,1])[:,None]
b = np.array([1,2,3])
#BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
test:
b_new = np.broadcast_to(b,(a.shape[0],b.shape[0]))
c = np.concatenate((a,b_new),axis=1)
try:
np.testing.assert_array_equal(c, ans)
print('Test passed!')
except:
print('Test failed...')
17.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
1
0
1
1
0
1
1
0
1
0
1
0.6
A4
0
0
0
0
0
0
0
0
1
0
1
0.1
Origin:
Problem:
Is there a Pythonic way to calculate the array z without using the loop?
import numpy as np
x = np.array([[1, 2, 3], [6, 7, 8]])
y = np.array([5, 8])
z = np.array([x[i] * y[i] for i in range(0, len(x))])
A:
<code>
import numpy as np
x = np.array([[1, 2, 3], [6, 7, 8]])
y = np.array([5, 8])
#BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
test:
z = x * np.expand_dims(y, 1)
try:
np.testing.assert_array_equal(z, ans)
print('Test passed!')
except:
print('Test failed...')
+for detection
A0:
Problem:
Is there a Pythonic way to calculate the array z without using the loop?
import numpy as np
x = np.array([[1, 2, 3], [3, 4, 5], [6, 7, 8]])
y = np.array([5, 8, 10])
z = np.array([x[i] * y[i] for i in range(0, len(x))])
A:
<code>
import numpy as np
x = np.array([[1, 2, 3], [3, 4, 5], [6, 7, 8]])
y = np.array([5, 8, 10])
#BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
z = x * np.expand_dims(y, 1)
try:
np.testing.assert_array_equal(z, ans)
print('Test passed!')
except:
print('Test failed...')
+for detection
18.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
0
0
0
0
0
0
0
0
0
Origin:
Problem:
I have a table in a Python script with numpy in the following shape:
[array([[a1, b1, c1], ..., [x1, y1, z1]]),
array([a2, b2, c2, ..., x2, y2, z2])
]
I would like to reshape it to a format like this:
(array([[a2],
[b2],
.
.
.
[z2]],
dtype = ...),
array([[a1],
[b1],
.
.
.
[z1]])
)
To be honest, I'm also quite confused about the different parentheses. array1, array2] is a list of arrays, right? What is (array1, array2), then?
A:
<code>
import numpy as np
a = [
np.array([[1, 2, 3], [4, 5, 6]]),
np.array([10, 11, 12, 13, 14])
]
#BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
b = (
np.expand_dims(a[1], axis=1),
np.expand_dims(a[0].flatten(), axis=1)
)
try:
np.testing.assert_array_equal(b, ans)
print('Test passed!')
except:
print('Test failed...')
+for detection
19.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
1
1
0
1
1
0
1
1
0
0
1
0.6
A4
0
0
0
0
0
0
0
0
0
0
0
0
Origin:
Problem:
I have a three dimensional numpy source array and a two-dimensional numpy array of indexes.
For example:
src = np.array([[[1,2,3],[4,5,6]],
[[7,8,9],[10,11,12]]])
idx = np.array([[0,1],
[1,2]])
I'd like to get a 2d array, where each element represents the indexed value in the innermost dimension in that position:
array([[1,5],
[8,12]])
How do I do this with numpy?
A:
<code>
import numpy as np
src = np.array([[[1,2,3],[4,5,6]],
[[7,8,9],[10,11,12]]])
idx = np.array([[0,1],
[1,2]])
#BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
idx = np.expand_dims(idx, axis=-1)
res = np.take_along_axis(src, idx, axis=2).squeeze(-1)
try:
np.testing.assert_array_equal(res, ans)
print('Test passed!')
except:
print('Test failed...')
A4:
Problem:
I have a three dimensional numpy source array and a two-dimensional numpy array of indexes.
For example:
src = np.array([[[1,2,3],[4,5,6]],
[[7,8,9],[10,11,12]]])
idx = np.array([[0,2],
[1,2]])
I'd like to get a 2d array:
array([[1,5],
[9,12]])
For example, the 5 on the top right corresponds to the 1st element of [4, 5, 6], and the 9 on the bottom left corresponds to the 2nd element of [7, 8, 9]
In other words, the indices on idx[0, 1] and idx[1, 0] corresponds to src[1, 0] and src[0, 1]
How do I do this with numpy?
A:
<code>
import numpy as np
src = np.array([[[1,2,3],[4,5,6]],
[[7,8,9],[10,11,12]]])
idx = np.array([[0,2],
[1,2]])
#BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
idx = np.expand_dims(idx.T, axis=-1)
res = np.take_along_axis(src, idx, axis=2).squeeze(-1)
try:
np.testing.assert_array_equal(res, ans)
print('Test passed!')
except:
print('Test failed...')
+for detection
20.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
0
0
0
0
0
1
0
1
0.1
Origin:
Problem:
I have an issue in applying argmax to an array which has multiple brackets. In real life I am getting this as a result of a pytorch tensor. Here I can put an example:
a = np.array([[1.0, 1.1],[2.1,2.0]])
np.argmax(a,axis=1)
array([1, 0])
It is correct. But:
a = np.array([[[1.0, 1.1]],[[2.1,2.0]]])
np.argmax(a,axis=1)
array([[0, 0],
[0, 0]])
It does not give me what I expect. Consider that in reality I have this level of inner brackets:
a = np.array([[[[1.0, 1.1]]],[[[2.1,2.0]]]])
A:
<code>
import numpy as np
a = np.array([[[[1.0, 1.1]]], [[[2.1, 2.0]]]])
#BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
try:
np.testing.assert_array_equal(np.argmax(a, axis=-1).squeeze(), ans)
print('Test passed!')
except:
print('Test failed...')
21.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
1
1
1
1
0
0
1
1
0.5
Origin:
Problem:
I have a large list files that contain 2D numpy arrays pickled through numpy.save. I am trying to read the first column of each file and create a new 2D array.
I currently read each column using numpy.load with a mmap. The 1D arrays are now in a list.
col_list = []
for f in file_list:
Temp = np.load(f,mmap_mode='r')
col_list.append(Temp[:,0])
How can I convert this into a 2D array?
A:
<code>
import numpy as np
def f(arrays):
### BEGIN SOLUTION
[insert]
### END SOLUTION
return result
</code>
test:
arrs = [np.array([1,2,3]), np.array([4,5,6]), np.array([7,8,9])]
try:
np.testing.assert_array_equal(f(arrs), np.stack(arrs, axis=0))
print('Test passed!')
except:
print('Test failed...')
22.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
1
0
0
0
0
0
0
0
0
1
0.1
Origin:
Problem:
I.m facing a little issue to combine arrays in a certain manner. Let's say we have
a=array([[1,1,1],[2,2,2],[3,3,3]])
b=array([[10,10,10],[20,20,20],[30,30,30]])
I wish to get
c=array([[[1,1,1],[10,10,10]],[[2,2,2],[20,20,20]],[[3,3,3],[30,30,30]]])
The real issue is that my arrays a and b are much longer than 3 coordinates!
A:
<code>
import numpy as np
a = np.array([[1,1,1],[2,2,2],[3,3,3], [4,4,4]])
b = np.array([[10,10,10],[20,20,20],[30,30,30], [40, 40, 40]])
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
test:
c = np.concatenate((a[:, None, :], b[:, None, :]), axis=1)
try:
np.testing.assert_array_equal(c, ans)
print('Test passed!')
except:
print('Test failed...')
+for detection
23.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
0
1
0
0
1
0
0
1
0.2
A7
0
0
0
0
0
0
0
0
0
0
0
0
Origin:
Problem:
I currently looking for method in which i can split a ndarray into smaller ndarrays.
example: given ndarray with shape (78,1440,3), from which i want to extract a list of smaller ndarrays of the size (78,72,3), that would be 20 smaller sub ndarrays.
I tried using numpy.split.
numpy.split(matrix,72,axis=1)
which generates a list of length 72 and the first entry has the shape (78,20,3)..
Why am I not able to extract the size I need?
A:
<code>
import numpy as np
matrix = np.random.rand(78,1440,3)
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
c = np.split(matrix, matrix.shape[1]//72, axis=1)
try:
np.testing.assert_array_equal(c, ans)
print('Test passed!')
except:
print('Test failed...')
A7:
Problem:
I currently looking for method in which i can split a ndarray into smaller ndarrays.
example: given ndarray with shape (78,1440,3), from which i want to extract a list of smaller ndarrays of the size (78,73,3).
Note that if shape[1] is not divisible by new size(in this example: 1440 is not divisible by 73), then fill zeros on the axis until it is dividible.
Why am I not able to extract the size I need?
A:
<code>
import numpy as np
matrix = np.random.rand(78,1440,3)
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
t = matrix.shape[1] // 73
if t * 73 < matrix.shape[1]:
new_arr = np.zeros((78, (t+1)*73-1440, 3))
matrix = np.hstack([matrix, new_arr])
c = np.split(matrix, matrix.shape[1] // 73, axis = 1)
try:
np.testing.assert_array_equal(c, ans)
print('Test passed!')
except:
print('Test failed...')
24.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
0
0
0
0
0
0
0
0
0
Origin:
Problem:
Suppose I have an array like:
import numpy as np
np.array([[0, 0, 0],
[1, 1, 1]])
Here has shape (2,3) but it can be (n,3). I would like to transform it into a list of arrays representing columns.
Desired Output
[array([[0],[1]]), array([[0],[1]]), array([[0],[1]])]
I tried list comprehension, reshape etc. but I did not manage to get there.
A:
<code>
import numpy as np
a=np.array([[0, 0, 0],[1, 1, 1]])
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
c = [np.hsplit(a,3)]
try:
np.testing.assert_array_equal(c, ans)
print('Test passed!')
except:
print('Test failed...')
+for detection
25.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
0
0
0
0
0
0
0
0
0
Origin:
Problem:
I have a numpy array of size nxm. I want the number of columns to be limited to k and the rest of the columns to be extended in new rows. Following is the scenario -
Initial array: nxm
Final array: pxk
where p = (m/k)*n
Eg. n = 2, m = 6, k = 2
Initial array:
[[1, 2, 3, 4, 5, 6,],
[7, 8, 9, 10, 11, 12]]
Final array:
[[1, 2],
[7, 8],
[3, 4],
[9, 10],
[5, 6],
[11, 12]]
I tried using reshape but I did not get the desired result.
A:
<code>
import numpy as np
q = np.array([[1, 2, 3, 4, 5, 6,], [7, 8, 9, 10, 11, 12]])
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
test:
c = q.T.reshape(-1,2,2).swapaxes(1,2).reshape(-1,2)
try:
np.testing.assert_array_equal(c, ans)
print('Test passed!')
except:
print('Test failed...')
26.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
0
0
0
0
0
0
0
0
0
Origin:
Problem:
Simple question here:
I'm trying to get an array that alternates values (1, -1, 1, -1.....) for a given length. np.repeat just gives me (1, 1, 1, 1,-1, -1,-1, -1). Thoughts?
A:
<code>
import numpy as np
def f(n):
### BEGIN SOLUTION
[insert]
### END SOLUTION
return result
</code>
test:
a = np.array([1, -1, 1, -1, 1, -1, 1, -1])
b = np.array([1, -1, 1, -1, 1, -1, 1, -1, 1])
try:
np.testing.assert_array_equal(a, f(8))
np.testing.assert_array_equal(b, f(9))
print('Test passed!')
except:
print('Test failed...')
27.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
0
0
0
0
0
0
0
0
0
Origin:
Problem:
Simple question here:
I am trying to break a numpy array into chunks with a fixed size and pad the last one with 0. For example: [1,2,3,4,5,6,7] into chunks of 3 returns [[1,2,3],[4,5,6],[7,0,0]].
A:
<code>
import numpy as np
l = np.array([1,2,3,4,5,6,7])
ans = l.copy()
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
t = l.copy()
t.resize((3,3), refcheck=False)
try:
np.testing.assert_array_equal(ans, t)
print('Test passed!')
except:
print('Test failed...')
28.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
1
0
0
1
0
0
0
1
0.2
Origin:
Problem:
Suppose I have the following array:
a = np.array([1,0,2,3,0,4,5,0])
for each zero I would like to duplicate a zero and add it to the array such that I get:
np.array([1,0,0,2,3,0,0,4,5,0,0])
A:
<code>
import numpy as np
a = np.array([1, 0, 2, 3, 0, 4, 5, 0])
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(a)
</code>
test:
b = np.array([1, 0, 2, 3, 0, 4, 5, 0])
i = 0
while i < len(b):
if b[i] == 0:
b = np.insert(b, i, 0)
i += 1
i += 1
try:
np.testing.assert_array_equal(a, b)
print('Test passed!')
except:
print('Test failed...')
29.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
1
0
0
0
0
0
0
0
0
1
0.1
Origin:
Problem:
Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an array
R = [[4,3,2,1], [5,4,3,2], [6,5,4,3], ..., [14,13,12,11]]
A:
<code>
import numpy as np
Z = np.arange(1, 15, dtype=np.uint32)
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(R)
</code>
test:
A = np.arange(11, dtype=np.uint32).reshape(-1, 1) + np.broadcast_to(Z[3::-1], (11, 4))
try:
np.testing.assert_array_equal(A, R)
print('Test passed!')
except:
print('Test failed...')
30.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
0
0
0
0
0
0
0
0
0
Origin:
Problem:
Converts a 1-dimensional array to a binary representation matrix. For every row in the matrix, the i-th element is 0 or 1, representing 2^i. The order is from left to right.
example:
given:
[1,2,3,4]
desired:
[[1,0,0],
[0,1,0],
[1,1,0],
[0,0,1]]
A:
<code>
import numpy as np
A = np.array([1,2,3,4])
A = A.reshape((-1,1))
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(ans)
</code>
Test:
B = 2**np.arange(3)
M = A & B
M[M != 0] = 1
try:
np.testing.assert_array_equal(ans, M)
print('Test passed!')
except:
print('Test failed...')
Numpy-100
15.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
1
1
1
1
1
1
1
1
0
1
0.8
A3
0
0
0
1
1
1
1
1
1
1
1
0.7
Origin:
Problem:
Create a 2d array with 1 on the border and 0 inside.
A:
<code>
import numpy as np
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(Z)
</code>
Test:
ans = np.ones((10,10))
ans[1:-1,1:-1] = 0
try:
np.testing.assert_array_equal(ans, Z)
print('Test passed!')
except:
print('Test failed...')
A3:
Problem:
Create a 10*5 array with 2 on the border and 3 inside.
A:
<code>
import numpy as np
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(Z)
</code>
test:
ans = 2* np.ones((10,5))
ans[1:-1,1:-1] = 3
try:
np.testing.assert_array_equal(ans, Z)
print('Test passed!')
except:
print('Test failed...')
18.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
1
1
0
1
1
0
1
1
1
1
1
0.8
A1
0
0
0
0
0
0
0
0
0
0
0
0
A3
1
1
1
0
1
1
1
1
0
0
1
0.7
Origin:
Problem:
Create a 5x5 matrix with values 1,2,3,4 just below the diagonal.
A:
<code>
import numpy as np
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(Z)
</code>
test:
ans = np.diag(1+np.arange(4), k=-1)
try:
np.testing.assert_array_equal(ans, Z)
print('Test passed!')
except:
print('Test failed...')
A1:
Problem:
Create a 5x5 matrix with values 1,3,4,5 just below the diagonal.
A:
<code>
import numpy as np
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(Z)
</code>
test:
ans = np.diag(2+np.arange(4), k=-1)
ans[1][0] = 1
try:
np.testing.assert_array_equal(ans, Z)
print('Test passed!')
except:
print('Test failed...')
A3:
Problem:
Create a 5x5 matrix with values 1,2,3,4 just above the diagonal.
A:
<code>
import numpy as np
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(Z)
</code>
test:
ans = np.diag(1+np.arange(4), k=1)
try:
np.testing.assert_array_equal(ans, Z)
print('Test passed!')
except:
print('Test failed...')
20.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
1
1
0
1
0
0
0
1
0
1
0.4
A1
0
0
0
0
0
0
0
0
0
0
0
0
A6
0
0
0
1
0
0
0
0
0
0
1
0.1
Origin:
Problem:
Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?
A:
<code>
import numpy as np
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(index)
</code>
Test:
try:
np.testing.assert_array_equal(index, np.unravel_index(99, (6,7,8)))
print('Test passed!')
except:
print('Test failed...')
A1:
Problem:
Consider a (6,7,8) shape array, what is the index (x,y,z) of the 99th element?
A:
<code>
import numpy as np
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(index)
</code>
Test:
try:
np.testing.assert_array_equal(index, np.unravel_index(98, (6,7,8)))
print('Test passed!')
except:
print('Test failed...')
A6:
Problem:
Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element from back to front?
A:
<code>
import numpy as np
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(index)
</code>
Test:
try:
np.testing.assert_array_equal(index, np.unravel_index(6*7*8-100, (6,7,8)))
print('Test passed!')
except:
print('Test failed...')
25.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
0
0
0
0
0
0
0
0
0
0
0
0
Origin:
Problem:
Given a 1D array, negate all elements which are between 3 and 8, or (3, 8), in place.
A:
<code>
import numpy as np
Z = np.arange(11)
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(Z)
</code>
Test:
test_Z = np.arange(11)
test_Z[(3<test_Z) & (test_Z <8)] *= -1
try:
np.testing.assert_array_equal(Z, test_Z)
print('Test passed!')
except:
print('Test failed...')
37.
Score:
1
2
3
4
5
6
7
8
9
10
Top-10
Avg
Origin
1
1
1
1
1
1
1
1
1
1
1
1
A1
0
0
0
0
0
0
0
0
0
0
0
0
Origin:
Problem:
Create a 5x5 matrix with row values ranging from 0 to 4.
A:
<code>
import numpy as np
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(Z)
</code>
Test:
test_Z = np.zeros((5, 5))
test_Z += np.arange(5)
try:
np.testing.assert_array_equal(Z, test_Z)
print('Test passed!')
except:
print('Test failed...')
A1:
Problem:
Create a 5x5 matrix with row values equals 1, 3, 4, 5, 6.
A:
<code>
import numpy as np
### BEGIN SOLUTION
[insert]
### END SOLUTION
print(Z)
</code>
test:
test_Z = np.ones((5, 5))
test_Z += np.arange(5)
test_Z[:, 1:] += 1
try:
np.testing.assert_array_equal(Z, test_Z)
print('Test passed!')
except:
print('Test failed...')