content
stringlengths
189
4.87k
INSTRUCTION: Problem: I am trying to convert a MATLAB code in Python. I don't know how to initialize an empty matrix in Python. MATLAB Code: demod4(1) = []; I want to create an empty numpy array, with shape = (3,0) A: <code> import numpy as np </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: result = np.array([[], [], []])
INSTRUCTION: Problem: I'm sorry in advance if this is a duplicated question, I looked for this information but still couldn't find it. Is it possible to get a numpy array (or python list) filled with the indexes of the elements in increasing order? For instance, the array: a = array([4, 1, 0, 8, 5, 2]) The indexes of the elements in increasing order would give : 0 --> 2 1 --> 1 2 --> 5 4 --> 0 5 --> 4 8 --> 3 result = [2,1,5,0,4,3] Thanks in advance! A: <code> import numpy as np a = np.array([4, 1, 0, 8, 5, 2]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: result = np.argsort(a)
INSTRUCTION: Problem: What is the equivalent of R's ecdf(x)(x) function in Python, in either numpy or scipy? Is ecdf(x)(x) basically the same as: import numpy as np def ecdf(x): # normalize X to sum to 1 x = x / np.sum(x) return np.cumsum(x) or is something else required? What I want to do is to apply the generated ECDF function to an eval array to gets corresponding values for elements in it. A: <code> import numpy as np grades = np.array((93.5,93,60.8,94.5,82,87.5,91.5,99.5,86,93.5,92.5,78,76,69,94.5, 89.5,92.8,78,65.5,98,98.5,92.3,95.5,76,91,95,61)) eval = np.array([88, 87, 62]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: def ecdf_result(x): xs = np.sort(x) ys = np.arange(1, len(xs)+1)/float(len(xs)) return xs, ys resultx, resulty = ecdf_result(grades) result = np.zeros_like(eval, dtype=float) for i, element in enumerate(eval): if element < resultx[0]: result[i] = 0 elif element >= resultx[-1]: result[i] = 1 else: result[i] = resulty[(resultx > element).argmax()-1]
INSTRUCTION: Problem: Right now, I have my data in a 2D numpy array `a`. If I was to use MinMaxScaler fit_transform on the array, it will normalize it column by column, whereas I wish to normalize the entire np array all together. Is there anyway to do that? A: <code> import numpy as np from sklearn.preprocessing import MinMaxScaler a = np.array([[-1, 2], [-0.5, 6]]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: scaler = MinMaxScaler() a_one_column = a.reshape(-1, 1) result_one_column = scaler.fit_transform(a_one_column) result = result_one_column.reshape(a.shape)
INSTRUCTION: Problem: I have an array of random floats and I need to compare it to another one that has the same values in a different order. For that matter I use the sum, product (and other combinations depending on the dimension of the table hence the number of equations needed). Nevertheless, I encountered a precision issue when I perform the sum (or product) on the array depending on the order of the values. Here is a simple standalone example to illustrate this issue : import numpy as np n = 10 m = 4 tag = np.random.rand(n, m) s1 = np.sum(tag, axis=1) s2 = np.sum(tag[:, ::-1], axis=1) # print the number of times s1 is not equal to s2 (should be 0) print np.nonzero(s1 != s2)[0].shape[0] If you execute this code it sometimes tells you that s1 and s2 are not equal and the differents is of magnitude of the computer precision. However, such elements should be considered as equal under this circumstance. The problem is I need to use those in functions like np.in1d where I can't really give a tolerance... What I want as the result is the number of truly different elements in s1 and s2, as shown in code snippet above. Is there a way to avoid this issue? A: <code> import numpy as np n = 20 m = 10 tag = np.random.rand(n, m) s1 = np.sum(tag, axis=1) s2 = np.sum(tag[:, ::-1], axis=1) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: result = (~np.isclose(s1,s2)).sum()
INSTRUCTION: Problem: How do I convert a numpy array to tensorflow tensor? A: <code> import tensorflow as tf import numpy as np a = np.ones([2,3,4]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(a_tf) </code> SOLUTION: a_tf = tf.convert_to_tensor(a)
INSTRUCTION: Problem: Let's say I have a 2d numpy integer array like this a = array([[1,0,3], [2,4,1]]) I would like to encode this as a 2D one-hot array(in C order, e.g., a[1,1] corresponds to b[4]) for integers. b = array([[0,1,0,0,0], [1,0,0,0,0], [0,0,0,1,0], [0,0,1,0,0], [0,0,0,0,1], [0,1,0,0,0]]) The leftmost element always corresponds to the smallest element in `a`, and the rightmost vice versa. Is there a quick way to do this only using numpy? Quicker than just looping over a to set elements of b, that is. A: <code> import numpy as np a = np.array([[1,0,3], [2,4,1]]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(b) </code> SOLUTION: temp = (a - a.min()).ravel() b = np.zeros((a.size, temp.max()+1)) b[np.arange(a.size), temp]=1
INSTRUCTION: Problem: I want to reverse & convert a 1-dimensional array into a 2-dimensional array by specifying the number of columns in the 2D array. Something that would work like this: > import numpy as np > A = np.array([1,2,3,4,5,6,7]) > B = vec2matrix(A,ncol=2) > B array([[7, 6], [5, 4], [3, 2]]) Note that when A cannot be reshaped into a 2D array, we tend to discard elements which are at the beginning of A. Does numpy have a function that works like my made-up function "vec2matrix"? (I understand that you can index a 1D array like a 2D array, but that isn't an option in the code I have - I need to make this conversion.) A: <code> import numpy as np A = np.array([1,2,3,4,5,6,7]) ncol = 2 </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(B) </code> SOLUTION: col = ( A.shape[0] // ncol) * ncol B = A[len(A)-col:][::-1] B = np.reshape(B, (-1, ncol))
INSTRUCTION: Problem: I have data of sample 1 and sample 2 (`a` and `b`) – size is different for sample 1 and sample 2. I want to do a weighted (take n into account) two-tailed t-test. I tried using the scipy.stat module by creating my numbers with np.random.normal, since it only takes data and not stat values like mean and std dev (is there any way to use these values directly). But it didn't work since the data arrays has to be of equal size. Any help on how to get the p-value would be highly appreciated. A: <code> import numpy as np import scipy.stats a = np.random.randn(40) b = 4*np.random.randn(50) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(p_value) </code> SOLUTION: _, p_value = scipy.stats.ttest_ind(a, b, equal_var = False)
INSTRUCTION: Problem: I have two arrays: • a: a 3-dimensional source array (N x M x T) • b: a 2-dimensional index array (N x M) containing 0, 1, … T-1s. I want to use the indices in b to compute sum of corresponding elements of a in its third dimension. Here is the example as code: import numpy as np a = np.array( # dims: 3x3x4 [[[ 0, 1, 2, 3], [ 2, 3, 4, 5], [ 4, 5, 6, 7]], [[ 6, 7, 8, 9], [ 8, 9, 10, 11], [10, 11, 12, 13]], [[12, 13, 14, 15], [14, 15, 16, 17], [16, 17, 18, 19]]] ) b = np.array( # dims: 3x3 [[0, 1, 2], [2, 1, 3], [1, 0, 3]] ) # select and sum the elements in a according to b # to achieve this result: desired = 85 At first, I thought this must have a simple solution but I could not find one at all. Since I would like to port it to tensorflow, I would appreciate if somebody knows a numpy-type solution for this. A: <code> import numpy as np a = np.array( [[[ 0, 1, 2, 3], [ 2, 3, 4, 5], [ 4, 5, 6, 7]], [[ 6, 7, 8, 9], [ 8, 9, 10, 11], [10, 11, 12, 13]], [[12, 13, 14, 15], [14, 15, 16, 17], [16, 17, 18, 19]]] ) b = np.array( [[0, 1, 2], [2, 1, 3], [1, 0, 3]] ) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: arr = np.take_along_axis(a, b[..., np.newaxis], axis=-1)[..., 0] result = np.sum(arr)
INSTRUCTION: Problem: Is it possible to perform circular cross-/auto-correlation on 1D arrays with a numpy/scipy/matplotlib function? I have looked at numpy.correlate() and matplotlib.pyplot.xcorr (based on the numpy function), and both seem to not be able to do circular cross-correlation. To illustrate the difference, I will use the example of an array of [1, 2, 3, 4]. With circular correlation, a periodic assumption is made, and a lag of 1 looks like [2, 3, 4, 1]. The python functions I've found only seem to use zero-padding, i.e., [2, 3, 4, 0]. Is there a way to get these functions to do periodic circular correlation of array a and b ? I want b to be the sliding periodic one, and a to be the fixed one. If not, is there a standard workaround for circular correlations? A: <code> import numpy as np a = np.array([1,2,3,4]) b = np.array([5, 4, 3, 2]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: result = np.correlate(a, np.hstack((b[1:], b)), mode='valid')
INSTRUCTION: Problem: I am waiting for another developer to finish a piece of code that will return an np array of shape (100,2000) with values of either -1,0, or 1. In the meantime, I want to randomly create an array of the same characteristics so I can get a head start on my development and testing. The thing is that I want this randomly created array to be the same each time, so that I'm not testing against an array that keeps changing its value each time I re-run my process. I can create my array like this, but is there a way to create it so that it's the same each time. I can pickle the object and unpickle it, but wondering if there's another way. r = np.random.randint(3, size=(100, 2000)) - 1 Specifically, I want r_old, r_new to be generated in the same way as r, but their result should be the same. A: <code> import numpy as np </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(r_old, r_new) </code> SOLUTION: np.random.seed(0) r_old = np.random.randint(3, size=(100, 2000)) - 1 np.random.seed(0) r_new = np.random.randint(3, size=(100, 2000)) - 1
INSTRUCTION: Problem: I am new to Python and I need to implement a clustering algorithm. For that, I will need to calculate distances between the given input data. Consider the following input data - a = np.array([[1,2,8,...], [7,4,2,...], [9,1,7,...], [0,1,5,...], [6,4,3,...],...]) What I am looking to achieve here is, I want to calculate distance of [1,2,8,…] from ALL other points. And I have to repeat this for ALL other points. I am trying to implement this with a FOR loop, but I think there might be a way which can help me achieve this result efficiently. I looked online, but the 'pdist' command could not get my work done. The result should be a symmetric matrix, with element at (i, j) being the distance between the i-th point and the j-th point. Can someone guide me? TIA A: <code> import numpy as np dim = np.random.randint(4, 8) a = np.random.rand(np.random.randint(5, 10),dim) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: result = np.linalg.norm(a - a[:, None], axis = -1)
INSTRUCTION: Problem: Lists have a very simple method to insert elements: a = [1,2,3,4] a.insert(2,66) print a [1, 2, 66, 3, 4] For a numpy array I could do: a = np.asarray([1,2,3,4]) a_l = a.tolist() a_l.insert(2,66) a = np.asarray(a_l) print a [1 2 66 3 4] but this is very convoluted. Is there an insert equivalent for numpy arrays? A: <code> import numpy as np a = np.asarray([1,2,3,4]) pos = 2 element = 66 </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(a) </code> SOLUTION: a = np.insert(a, pos, element)
INSTRUCTION: Problem: Suppose I have a hypotetical function I'd like to approximate: def f(x): return a+ b * x + c * x ** 2 + … Where a, b, c,… are the values I don't know. And I have certain points where the function output is known, i.e. x = [-1, 2, 5, 100] y = [123, 456, 789, 1255] (actually there are way more values) I'd like to get the parameters while minimizing the squared error . What is the way to do that in Python for a given degree? The result should be an array like […, c, b, a], from highest order to lowest order. There should be existing solutions in numpy or anywhere like that. A: <code> import numpy as np x = [-1, 2, 5, 100] y = [123, 456, 789, 1255] degree = 3 </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: result = np.polyfit(x, y, degree)
INSTRUCTION: Problem: I have integers in the range 0..2**m - 1 and I would like to convert them to binary numpy arrays of length m. For example, say m = 4. Now 15 = 1111 in binary and so the output should be (1,1,1,1). 2 = 10 in binary and so the output should be (0,0,1,0). If m were 3 then 2 should be converted to (0,1,0). I tried np.unpackbits(np.uint8(num)) but that doesn't give an array of the right length. For example, np.unpackbits(np.uint8(15)) Out[5]: array([0, 0, 0, 0, 1, 1, 1, 1], dtype=uint8) I would like a method that worked for whatever m I have in the code. Given an n-element integer array, I want to process it as above to generate a (n, m) matrix. A: <code> import numpy as np a = np.array([1, 2, 3, 4, 5]) m = 8 </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: result = (((a[:,None] & (1 << np.arange(m))[::-1])) > 0).astype(int)
INSTRUCTION: Problem: When testing if a numpy array c is member of a list of numpy arrays CNTS: import numpy as np c = np.array([[[ 75, 763]], [[ 57, 763]], [[ 57, 749]], [[ 75, 749]]]) CNTS = [np.array([[[ 78, 1202]], [[ 63, 1202]], [[ 63, 1187]], [[ 78, 1187]]]), np.array([[[ 75, 763]], [[ 57, 763]], [[ 57, 749]], [[ 75, 749]]]), np.array([[[ 72, 742]], [[ 58, 742]], [[ 57, 741]], [[ 57, 727]], [[ 58, 726]], [[ 72, 726]]]), np.array([[[ 66, 194]], [[ 51, 194]], [[ 51, 179]], [[ 66, 179]]])] print(c in CNTS) I get: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() However, the answer is rather clear: c is exactly CNTS[1], so c in CNTS should return True! How to correctly test if a numpy array is member of a list of numpy arrays? The same problem happens when removing: CNTS.remove(c) ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() Application: test if an opencv contour (numpy array) is member of a list of contours, see for example Remove an opencv contour from a list of contours. A: <code> import numpy as np c = np.array([[[ 75, 763]], [[ 57, 763]], [[ 57, 749]], [[ 75, 749]]]) CNTS = [np.array([[[ 78, 1202]], [[ 63, 1202]], [[ 63, 1187]], [[ 78, 1187]]]), np.array([[[ 75, 763]], [[ 57, 763]], [[ 57, 749]], [[ 75, 749]]]), np.array([[[ 72, 742]], [[ 58, 742]], [[ 57, 741]], [[ 57, 727]], [[ 58, 726]], [[ 72, 726]]]), np.array([[[ 66, 194]], [[ 51, 194]], [[ 51, 179]], [[ 66, 179]]])] </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: result = any(np.array_equal(c, x) for x in CNTS)
INSTRUCTION: Problem: I have a file with arrays or different shapes. I want to zeropad all the array to match the largest shape. The largest shape is (93,13). To test this I have the following code: a = np.ones((41,12)) how can I zero pad this array to match the shape of (93,13)? And ultimately, how can I do it for thousands of rows? Specifically, I want to pad the array to left, right equally and top, bottom equally. If not equal, put the rest row/column to the bottom/right. e.g. convert [[1]] into [[0,0,0],[0,1,0],[0,0,0]] A: <code> import numpy as np a = np.ones((41, 12)) shape = (93, 13) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: def to_shape(a, shape): y_, x_ = shape y, x = a.shape y_pad = (y_-y) x_pad = (x_-x) return np.pad(a,((y_pad//2, y_pad//2 + y_pad%2), (x_pad//2, x_pad//2 + x_pad%2)), mode = 'constant') result = to_shape(a, shape)
INSTRUCTION: Problem: Say I have these 2D arrays A and B. How can I get elements from A that are not in B, and those from B that are not in A? (Symmetric difference in set theory: A△B) Example: A=np.asarray([[1,1,1], [1,1,2], [1,1,3], [1,1,4]]) B=np.asarray([[0,0,0], [1,0,2], [1,0,3], [1,0,4], [1,1,0], [1,1,1], [1,1,4]]) #elements in A first, elements in B then. in original order. #output = array([[1,1,2], [1,1,3], [0,0,0], [1,0,2], [1,0,3], [1,0,4], [1,1,0]]) A: <code> import numpy as np A=np.asarray([[1,1,1], [1,1,2], [1,1,3], [1,1,4]]) B=np.asarray([[0,0,0], [1,0,2], [1,0,3], [1,0,4], [1,1,0], [1,1,1], [1,1,4]]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(output) </code> SOLUTION: dims = np.maximum(B.max(0),A.max(0))+1 result = A[~np.in1d(np.ravel_multi_index(A.T,dims),np.ravel_multi_index(B.T,dims))] output = np.append(result, B[~np.in1d(np.ravel_multi_index(B.T,dims),np.ravel_multi_index(A.T,dims))], axis = 0)
INSTRUCTION: Problem: I need to do some analysis on a large dataset from a hydrolgeology field work. I am using NumPy. I want to know how I can: 1. multiply e.g. the col-th column of my array by a number (e.g. 5.2). And then 2. calculate the cumulative sum of the numbers in that column. As I mentioned I only want to work on a specific column and not the whole array.The result should be an 1-d array --- the cumulative sum. A: <code> import numpy as np a = np.random.rand(8, 5) col = 2 multiply_number = 5.2 </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: a[:, col-1] *= multiply_number result = np.cumsum(a[:, col-1])
INSTRUCTION: Problem: I have a time-series A holding several values. I need to obtain a series B that is defined algebraically as follows: B[0] = a*A[0] B[t] = a * A[t] + b * B[t-1] where we can assume a and b are real numbers. Is there any way to do this type of recursive computation in Pandas or numpy? As an example of input: > A = pd.Series(np.random.randn(10,)) 0 -0.310354 1 -0.739515 2 -0.065390 3 0.214966 4 -0.605490 5 1.293448 6 -3.068725 7 -0.208818 8 0.930881 9 1.669210 A: <code> import numpy as np import pandas as pd A = pd.Series(np.random.randn(10,)) a = 2 b = 3 </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(B) </code> SOLUTION: B = np.empty(len(A)) for k in range(0, len(B)): if k == 0: B[k] = a*A[k] else: B[k] = a*A[k] + b*B[k-1]
INSTRUCTION: Problem: I have created a multidimensional array in Python like this: self.cells = np.empty((r,c),dtype=np.object) Now I want to iterate through all elements of my two-dimensional array `X` and store element at each moment in result (an 1D list), in 'C' order. How do I achieve this? A: <code> import numpy as np X = np.random.randint(2, 10, (5, 6)) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: result = [] for value in X.flat: result.append(value)
INSTRUCTION: Problem: Say that you have 3 numpy arrays: lat, lon, val: import numpy as np lat=np.array([[10, 20, 30], [20, 11, 33], [21, 20, 10]]) lon=np.array([[100, 102, 103], [105, 101, 102], [100, 102, 103]]) val=np.array([[17, 2, 11], [86, 84, 1], [9, 5, 10]]) And say that you want to create a pandas dataframe where df.columns = ['lat', 'lon', 'val'], but since each value in lat is associated with both a long and a val quantity, you want them to appear in the same row. Also, you want the row-wise order of each column to follow the positions in each array, so to obtain the following dataframe: lat lon val 0 10 100 17 1 20 102 2 2 30 103 11 3 20 105 86 ... ... ... ... So basically the first row in the dataframe stores the "first" quantities of each array, and so forth. How to do this? I couldn't find a pythonic way of doing this, so any help will be much appreciated. A: <code> import numpy as np import pandas as pd example_lat=np.array([[10, 20, 30], [20, 11, 33], [21, 20, 10]]) example_lon=np.array([[100, 102, 103], [105, 101, 102], [100, 102, 103]]) example_val=np.array([[17, 2, 11], [86, 84, 1], [9, 5, 10]]) def f(lat = example_lat, lon = example_lon, val = example_val): </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> return df </code> SOLUTION: df = pd.DataFrame({'lat': lat.ravel(), 'lon': lon.ravel(), 'val': val.ravel()})
INSTRUCTION: Problem: I want to process a gray image in the form of np.array. *EDIT: chose a slightly more complex example to clarify Suppose im = np.array([ [0,0,0,0,0,0] [0,0,1,1,1,0] [0,1,1,0,1,0] [0,0,0,1,1,0] [0,0,0,0,0,0]]) I'm trying to create this: [ [0,1,1,1], [1,1,0,1], [0,0,1,1] ] That is, to remove the peripheral zeros(black pixels) that fill an entire row/column. I can brute force this with loops, but intuitively I feel like numpy has a better means of doing this. A: <code> import numpy as np im = np.array([[0,0,0,0,0,0], [0,0,1,1,1,0], [0,1,1,0,1,0], [0,0,0,1,1,0], [0,0,0,0,0,0]]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: mask = im == 0 rows = np.flatnonzero((~mask).sum(axis=1)) cols = np.flatnonzero((~mask).sum(axis=0)) if rows.shape[0] == 0: result = np.array([]) else: result = im[rows.min():rows.max()+1, cols.min():cols.max()+1]
INSTRUCTION: Problem: I want to be able to calculate the mean of A: import numpy as np A = ['inf', '33.33', '33.33', '33.37'] NA = np.asarray(A) AVG = np.mean(NA, axis=0) print AVG This does not work, unless converted to: A = [inf, 33.33, 33.33, 33.37] Is it possible to compute AVG WITHOUT loops? A: <code> import numpy as np A = ['inf', '33.33', '33.33', '33.37'] NA = np.asarray(A) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(AVG) </code> SOLUTION: AVG = np.mean(NA.astype(float), axis = 0)
INSTRUCTION: Problem: How do i get the length of the row in a 2D array? example, i have a nD array called a. when i print a.shape, it returns (1,21). I want to do a for loop, in the range of the row size (21) of the array a. How do i get the value of row size as result? A: <code> import numpy as np a = np.random.rand(np.random.randint(5, 10), np.random.randint(6, 10)) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: result = a.shape[1]
INSTRUCTION: Problem: So in numpy arrays there is the built in function for getting the diagonal indices, but I can't seem to figure out how to get the diagonal ending at bottom left rather than botton right(might not on the corner for non-square matrix). This is the normal code to get starting from the top left, assuming processing on 5x6 array: >>> import numpy as np >>> a = np.arange(30).reshape(5,6) >>> diagonal = np.diag_indices(5) >>> a array([[ 0, 1, 2, 3, 4, 5], [ 5, 6, 7, 8, 9, 10], [10, 11, 12, 13, 14, 15], [15, 16, 17, 18, 19, 20], [20, 21, 22, 23, 24, 25]]) >>> a[diagonal] array([ 0, 6, 12, 18, 24]) so what do I use if I want it to return: array([[0, 6, 12, 18, 24] [4, 8, 12, 16, 20]) How to get that in a general way, That is, can be used on other arrays with different shape? A: <code> import numpy as np a = np.array([[ 0, 1, 2, 3, 4, 5], [ 5, 6, 7, 8, 9, 10], [10, 11, 12, 13, 14, 15], [15, 16, 17, 18, 19, 20], [20, 21, 22, 23, 24, 25]]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: dim = min(a.shape) b = a[:dim,:dim] result = np.vstack((np.diag(b), np.diag(np.fliplr(b))))
INSTRUCTION: Problem: How do I convert a tensorflow tensor to numpy? A: <code> import tensorflow as tf import numpy as np a = tf.ones([2,3,4]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(a_np) </code> SOLUTION: a_np = a.numpy()
INSTRUCTION: Problem: How can I get get the indices of the largest value in a multi-dimensional NumPy array `a`? Note that I want to get the unraveled index of it, in C order. A: <code> import numpy as np a = np.array([[10,50,30],[60,20,40]]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: result = np.unravel_index(a.argmax(), a.shape)
INSTRUCTION: Problem: How to get one maximal set of linearly independent vectors of a given matrix `a`? For example, [[0 1 0 0], [0 0 1 0], [1 0 0 1]] in [[0 1 0 0], [0 0 1 0], [0 1 1 0], [1 0 0 1]] A: <code> import numpy as np a = np.array([[0,1,0,0], [0,0,1,0], [0,1,1,0], [1,0,0,1]]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: def LI_vecs(M): dim = M.shape[0] LI=[M[0]] for i in range(dim): tmp=[] for r in LI: tmp.append(r) tmp.append(M[i]) #set tmp=LI+[M[i]] if np.linalg.matrix_rank(tmp)>len(LI): #test if M[i] is linearly independent from all (row) vectors in LI LI.append(M[i]) #note that matrix_rank does not need to take in a square matrix return LI #return set of linearly independent (row) vectors result = LI_vecs(a)
INSTRUCTION: Problem: I want to make an 4 dimensional array of zeros in python. I know how to do this for a square array but I want the lists to have different lengths. Right now I use this: arr = numpy.zeros((20,)*4) Which gives them all length 20 but I would like to have arr's lengths 20,10,10,2 because now I have a lot of zeros in arr that I don't use A: <code> import numpy as np </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(arr) </code> SOLUTION: arr = np.zeros((20,10,10,2))
INSTRUCTION: Problem: I have two arrays: • a: a 3-dimensional source array (N x M x T) • b: a 2-dimensional index array (N x M) containing 0, 1, … T-1s. I want to use the indices in b to select the corresponding elements of a in its third dimension. The resulting array should have the dimensions N x M. Here is the example as code: import numpy as np a = np.array( # dims: 3x3x4 [[[ 0, 1, 2, 3], [ 2, 3, 4, 5], [ 4, 5, 6, 7]], [[ 6, 7, 8, 9], [ 8, 9, 10, 11], [10, 11, 12, 13]], [[12, 13, 14, 15], [14, 15, 16, 17], [16, 17, 18, 19]]] ) b = np.array( # dims: 3x3 [[0, 1, 2], [2, 1, 3], [1, 0, 3]] ) # select the elements in a according to b # to achieve this result: desired = np.array( [[ 0, 3, 6], [ 8, 9, 13], [13, 14, 19]] ) At first, I thought this must have a simple solution but I could not find one at all. Since I would like to port it to tensorflow, I would appreciate if somebody knows a numpy-type solution for this. A: <code> import numpy as np a = np.array( [[[ 0, 1, 2, 3], [ 2, 3, 4, 5], [ 4, 5, 6, 7]], [[ 6, 7, 8, 9], [ 8, 9, 10, 11], [10, 11, 12, 13]], [[12, 13, 14, 15], [14, 15, 16, 17], [16, 17, 18, 19]]] ) b = np.array( [[0, 1, 2], [2, 1, 3], [1, 0, 3]] ) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: result = np.take_along_axis(a, b[..., np.newaxis], axis=-1)[..., 0]
INSTRUCTION: Problem: What is the quickest way to convert the non-diagonal elements of a square symmetrical numpy ndarray to 0? I don't wanna use LOOPS! A: <code> import numpy as np a = np.array([[1,0,2,3],[0,5,3,4],[2,3,2,10],[3,4, 10, 7]]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(a) </code> SOLUTION: result = np.einsum('ii->i', a) save = result.copy() a[...] = 0 result[...] = save
INSTRUCTION: Problem: Suppose I have a hypotetical function I'd like to approximate: def f(x): return a * x ** 2 + b * x + c Where a, b and c are the values I don't know. And I have certain points where the function output is known, i.e. x = [-1, 2, 5, 100] y = [123, 456, 789, 1255] (actually there are way more values) I'd like to get a, b and c while minimizing the squared error . What is the way to do that in Python? The result should be an array like [a, b, c], from highest order to lowest order. There should be existing solutions in numpy or anywhere like that. A: <code> import numpy as np x = [-1, 2, 5, 100] y = [123, 456, 789, 1255] </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: result = np.polyfit(x, y, 2)
INSTRUCTION: Problem: The clamp function is clamp(x, min, max) = min if x < min, max if x > max, else x I need a function that behaves like the clamp function, but is smooth (i.e. has a continuous derivative). Maybe using 3x^2 – 2x^3 to smooth the function? A: <code> import numpy as np x = 0.25 x_min = 0 x_max = 1 </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> result = smoothclamp(x) print(result) </code> SOLUTION: def smoothclamp(x): return np.where(x < x_min, x_min, np.where(x > x_max, x_max, 3*x**2 - 2*x**3))
INSTRUCTION: Problem: What's the more pythonic way to pad an array with zeros at the end? def pad(A, length): ... A = np.array([1,2,3,4,5]) pad(A, 8) # expected : [1,2,3,4,5,0,0,0] pad(A, 3) # expected : [1,2,3,0,0] In my real use case, in fact I want to pad an array to the closest multiple of 1024. Ex: 1342 => 2048, 3000 => 3072, so I want non-loop solution. A: <code> import numpy as np A = np.array([1,2,3,4,5]) length = 8 </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: if length > A.shape[0]: result = np.pad(A, (0, length-A.shape[0]), 'constant') else: result = A.copy() result[length:] = 0
INSTRUCTION: Problem: I have two 2D numpy arrays like this, representing the x/y distances between three points. I need the x/y distances as tuples in a single array. So from: x_dists = array([[ 0, -1, -2], [ 1, 0, -1], [ 2, 1, 0]]) y_dists = array([[ 0, 1, -2], [ -1, 0, 1], [ -2, 1, 0]]) I need: dists = array([[[ 0, 0], [-1, 1], [-2, -2]], [[ 1, -1], [ 0, 0], [-1, 1]], [[ 2, -2], [ 1, 1], [ 0, 0]]]) I've tried using various permutations of dstack/hstack/vstack/concatenate, but none of them seem to do what I want. The actual arrays in code are liable to be gigantic, so iterating over the elements in python and doing the rearrangement "manually" isn't an option speed-wise. A: <code> import numpy as np x_dists = np.array([[ 0, -1, -2], [ 1, 0, -1], [ 2, 1, 0]]) y_dists = np.array([[ 0, 1, -2], [ -1, 0, 1], [ -2, 1, 0]]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(dists) </code> SOLUTION: dists = np.vstack(([x_dists.T], [y_dists.T])).T
INSTRUCTION: Problem: I have a file with arrays or different shapes. I want to zeropad all the array to match the largest shape. The largest shape is (93,13). To test this I have the following code: arr = np.ones((41,13)) how can I zero pad this array to match the shape of (93,13)? And ultimately, how can I do it for thousands of rows? Specifically, I want to pad to the right and bottom of original array in 2D. A: <code> import numpy as np example_arr = np.ones((41, 13)) def f(arr = example_arr, shape=(93,13)): </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> return result </code> SOLUTION: result = np.pad(arr, ((0, shape[0]-arr.shape[0]), (0, shape[1]-arr.shape[1])), 'constant')
INSTRUCTION: Problem: I want to figure out how to remove nan values from my array. For example, My array looks something like this: x = [1400, 1500, 1600, nan, nan, nan ,1700] #Not in this exact configuration How can I remove the nan values from x to get sth like: x = [1400, 1500, 1600, 1700] A: <code> import numpy as np x = np.array([1400, 1500, 1600, np.nan, np.nan, np.nan ,1700]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(x) </code> SOLUTION: x = x[~np.isnan(x)]
INSTRUCTION: Problem: I have created a multidimensional array in Python like this: self.cells = np.empty((r,c),dtype=np.object) Now I want to iterate through all elements of my two-dimensional array `X` and store element at each moment in result (an 1D list), in 'Fortran' order. How do I achieve this? A: <code> import numpy as np X = np.random.randint(2, 10, (5, 6)) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: result = [] for value in X.T.flat: result.append(value)
INSTRUCTION: Problem: Let X be a M x N matrix, with all elements being positive. Denote xi the i-th column of X. Someone has created a 3 dimensional N x M x M array Y consisting of M x M matrices xi.dot(xi.T). How can I restore the original M*N matrix X using numpy? A: <code> import numpy as np Y = np.array([[[81, 63, 63], [63, 49, 49], [63, 49, 49]], [[ 4, 12, 8], [12, 36, 24], [ 8, 24, 16]], [[25, 35, 25], [35, 49, 35], [25, 35, 25]], [[25, 30, 10], [30, 36, 12], [10, 12, 4]]]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(X) </code> SOLUTION: X = np.zeros([Y.shape[1], Y.shape[0]]) for i, mat in enumerate(Y): diag = np.sqrt(np.diag(mat)) X[:, i] += diag
INSTRUCTION: Problem: The clamp function is clamp(x, min, max) = min if x < min, max if x > max, else x I need a function that behaves like the clamp function, but is smooth (i.e. has a continuous derivative). N-order Smoothstep function might be a perfect solution. A: <code> import numpy as np x = 0.25 x_min = 0 x_max = 1 N = 5 </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> result = smoothclamp(x, N=N) print(result) </code> SOLUTION: from scipy.special import comb def smoothclamp(x, x_min=0, x_max=1, N=1): if x < x_min: return x_min if x > x_max: return x_max x = np.clip((x - x_min) / (x_max - x_min), 0, 1) result = 0 for n in range(0, N + 1): result += comb(N + n, n) * comb(2 * N + 1, N - n) * (-x) ** n result *= x ** (N + 1) return result
INSTRUCTION: Problem: I have two arrays A (len of 3.8million) and B (len of 20k). For the minimal example, lets take this case: A = np.array([1,1,2,3,3,3,4,5,6,7,8,8]) B = np.array([1,2,8]) Now I want the resulting array to be: C = np.array([3,3,3,4,5,6,7]) i.e. if any value in B is found in A, remove it from A, if not keep it. I would like to know if there is any way to do it without a for loop because it is a lengthy array and so it takes long time to loop. A: <code> import numpy as np A = np.array([1,1,2,3,3,3,4,5,6,7,8,8]) B = np.array([1,2,8]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(C) </code> SOLUTION: C = A[~np.in1d(A,B)]
INSTRUCTION: Problem: How can I read a Numpy array from a string? Take a string like: "[[ 0.5544 0.4456], [ 0.8811 0.1189]]" and convert it to an array: a = from_string("[[ 0.5544 0.4456], [ 0.8811 0.1189]]") where a becomes the object: np.array([[0.5544, 0.4456], [0.8811, 0.1189]]). There's nothing I can find in the NumPy docs that does this. A: <code> import numpy as np string = "[[ 0.5544 0.4456], [ 0.8811 0.1189]]" </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(a) </code> SOLUTION: a = np.array(np.matrix(string.replace(',', ';')))
INSTRUCTION: Problem: I have a 2-d numpy array as follows: a = np.array([[1,5,9,13,17], [2,6,10,14,18], [3,7,11,15,19], [4,8,12,16,20]] I want to extract it into patches of 2 by 2 sizes with out repeating the elements. Pay attention that if the shape is indivisible by patch size, we would just ignore the rest row/column. The answer should exactly be the same. This can be 3-d array or list with the same order of elements as below: [[[1,5], [2,6]], [[3,7], [4,8]], [[9,13], [10,14]], [[11,15], [12,16]]] How can do it easily? In my real problem the size of a is (36, 73). I can not do it one by one. I want programmatic way of doing it. A: <code> import numpy as np a = np.array([[1,5,9,13,17], [2,6,10,14,18], [3,7,11,15,19], [4,8,12,16,20]]) patch_size = 2 </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: x = a[:a.shape[0] // patch_size * patch_size, :a.shape[1] // patch_size * patch_size] result = x.reshape(x.shape[0]//patch_size, patch_size, x.shape[1]// patch_size, patch_size).swapaxes(1, 2).transpose(1, 0, 2, 3).reshape(-1, patch_size, patch_size)
INSTRUCTION: Problem: I'm looking for a fast solution to MATLAB's accumarray in numpy. The accumarray accumulates the elements of an array which belong to the same index. Note that there might be negative indices in accmap, and we treat them like list indices in Python. An example: a = np.arange(1,11) # array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) accmap = np.array([0,1,0,0,0,-1,-1,2,2,1]) Result should be array([13, 12, 30]) Is there a built-in numpy function that can do accumulation like this? Using for-loop is not what I want. Or any other recommendations? A: <code> import numpy as np a = np.arange(1,11) accmap = np.array([0,1,0,0,0,-1,-1,2,2,1]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: add = np.max(accmap) mask = accmap < 0 accmap[mask] += add+1 result = np.bincount(accmap, weights = a)
INSTRUCTION: Problem: How do I convert a numpy array to pytorch tensor? A: <code> import torch import numpy as np a = np.ones(5) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(a_pt) </code> SOLUTION: a_pt = torch.Tensor(a)
INSTRUCTION: Problem: I have a list of numpy arrays, and want to check if all the arrays are equal. What is the quickest way of doing this? I am aware of the numpy.array_equal function (https://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.array_equal.html), however as far as I am aware this only applies to two arrays and I want to check N arrays against each other. I also found this answer to test all elements in a list: check if all elements in a list are identical. However, when I try each method in the accepted answer I get an exception (ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()) Thanks, A: <code> import numpy as np a = [np.array([1,2,3]),np.array([1,2,3]),np.array([1,2,3])] </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: def all_equal(iterator): try: iterator = iter(iterator) first = next(iterator) return all(np.array_equal(first, rest) for rest in iterator) except StopIteration: return True result = all_equal(a)
INSTRUCTION: Problem: Lists have a very simple method to insert elements: a = [1,2,3,4] a.insert(2,66) print a [1, 2, 66, 3, 4] For a numpy array I could do: a = np.asarray([1,2,3,4]) a_l = a.tolist() a_l.insert(2,66) a = np.asarray(a_l) print a [1 2 66 3 4] but this is very convoluted. Is there an insert equivalent for numpy arrays? A: <code> import numpy as np example_a = np.asarray([1,2,3,4]) def f(a = example_a, pos=2, element = 66): </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> return a </code> SOLUTION: a = np.insert(a, pos, element)
INSTRUCTION: Problem: What is the most efficient way to remove negative elements in an array? I have tried numpy.delete and Remove all specific value from array and code of the form x[x != i]. For: import numpy as np x = np.array([-2, -1.4, -1.1, 0, 1.2, 2.2, 3.1, 4.4, 8.3, 9.9, 10, 14, 16.2]) I want to end up with an array: [0, 1.2, 2.2, 3.1, 4.4, 8.3, 9.9, 10, 14, 16.2] A: <code> import numpy as np x = np.array([-2, -1.4, -1.1, 0, 1.2, 2.2, 3.1, 4.4, 8.3, 9.9, 10, 14, 16.2]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: result = x[x >=0]
INSTRUCTION: Problem: I want to process a gray image in the form of np.array. *EDIT: chose a slightly more complex example to clarify Suppose: im = np.array([ [0,0,0,0,0,0] [0,0,5,1,2,0] [0,1,8,0,1,0] [0,0,0,7,1,0] [0,0,0,0,0,0]]) I'm trying to create this: [ [0,5,1,2], [1,8,0,1], [0,0,7,1] ] That is, to remove the peripheral zeros(black pixels) that fill an entire row/column. In extreme cases, an image can be totally black, and I want the result to be an empty array. I can brute force this with loops, but intuitively I feel like numpy has a better means of doing this. A: <code> import numpy as np im = np.array([[0,0,0,0,0,0], [0,0,5,1,2,0], [0,1,8,0,1,0], [0,0,0,7,1,0], [0,0,0,0,0,0]]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: mask = im == 0 rows = np.flatnonzero((~mask).sum(axis=1)) cols = np.flatnonzero((~mask).sum(axis=0)) if rows.shape[0] == 0: result = np.array([]) else: result = im[rows.min():rows.max()+1, cols.min():cols.max()+1]
INSTRUCTION: Problem: How can I get get the indices of the largest value in a multi-dimensional NumPy array `a`? Note that I want to get the unraveled index of it, in Fortran order. A: <code> import numpy as np a = np.array([[10,50,30],[60,20,40]]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: result = np.unravel_index(a.argmax(), a.shape, order = 'F')
INSTRUCTION: Problem: Is there any way to create an array of equally spaced date-time objects, given the start/stop epochs and the desired number of intervening elements? t0 = dateutil.parser.parse("23-FEB-2015 23:09:19.445506") tf = dateutil.parser.parse("24-FEB-2015 01:09:22.404973") n = 10**4 series = pandas.period_range(start=t0, end=tf, periods=n) This example fails, maybe pandas isn't intended to give date ranges with frequencies shorter than a day? I could manually estimate a frequecy, i.e. (tf-t0)/n, but I'm concerned that naively adding this timedelta repeatedly (to the start epoch) will accumulate significant rounding errors as I approach the end epoch. I could resort to working exclusively with floats instead of datetime objects. (For example, subtract the start epoch from the end epoch, and divide the timedelta by some unit such as a second, then simply apply numpy linspace..) But casting everything to floats (and converting back to dates only when needed) sacrifices the advantages of special data types (simpler code debugging). Is this the best solution? What I want as a naïve result is a linearspace filled with timestamps(in pd.DatetimeIndex type) . A: <code> import numpy as np import pandas as pd start = "23-FEB-2015 23:09:19.445506" end = "24-FEB-2015 01:09:22.404973" n = 50 </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: result = pd.DatetimeIndex(np.linspace(pd.Timestamp(start).value, pd.Timestamp(end).value, num = n, dtype=np.int64))
INSTRUCTION: Problem: >>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) >>> del_col = [1, 2, 4, 5] >>> arr array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]]) I am deleting some columns(in this example, 1st, 2nd and 4th) def_col = np.array([1, 2, 4, 5]) array([[ 3], [ 7], [ 11]]) Note that del_col might contain out-of-bound indices, so we should ignore them. Are there any good way ? Please consider this to be a novice question. A: <code> import numpy as np a = np.arange(12).reshape(3, 4) del_col = np.array([1, 2, 4, 5]) </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: mask = (del_col <= a.shape[1]) del_col = del_col[mask] - 1 result = np.delete(a, del_col, axis=1)
INSTRUCTION: Problem: I could not find a built-in function in Python to generate a log uniform distribution given a min and max value (the R equivalent is here), something like: loguni[n, min, max, base] that returns n log uniformly distributed in the range min and max. The closest I found though was numpy.random.uniform. That is, given range of x, I want to get samples of given size (n) that suit log-uniform distribution. Any help would be appreciated! A: <code> import numpy as np def f(min=1, max=np.e, n=10000): </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> return result </code> SOLUTION: import scipy.stats result = scipy.stats.loguniform.rvs(a = min, b = max, size = n)
INSTRUCTION: Origin Problem: Following-up from this question years ago, is there a canonical "shift" function in numpy? I don't see anything from the documentation. Using this is like: In [76]: xs Out[76]: array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]) In [77]: shift(xs, 3) Out[77]: array([ nan, nan, nan, 0., 1., 2., 3., 4., 5., 6.]) In [78]: shift(xs, -3) Out[78]: array([ 3., 4., 5., 6., 7., 8., 9., nan, nan, nan]) This question came from my attempt to write a fast rolling_product yesterday. I needed a way to "shift" a cumulative product and all I could think of was to replicate the logic in np.roll(). A: <code> import numpy as np a = np.array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]) shift = 3 </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: def solution(xs, n): e = np.empty_like(xs) if n >= 0: e[:n] = np.nan e[n:] = xs[:-n] else: e[n:] = np.nan e[:n] = xs[-n:] return e result = solution(a, shift)
INSTRUCTION: Problem: I'm sorry in advance if this is a duplicated question, I looked for this information but still couldn't find it. Is it possible to get a numpy array (or python list) filled with the indexes of the N biggest elements in decreasing order? For instance, the array: a = array([4, 1, 0, 8, 5, 2]) The indexes of the biggest elements in decreasing order would give (considering N = 3): 8 --> 3 5 --> 4 4 --> 0 result = [3, 4, 0] Thanks in advance! A: <code> import numpy as np a = np.array([4, 1, 0, 8, 5, 2]) N = 3 </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: result = np.argsort(a)[::-1][:N]
INSTRUCTION: Problem: numpy seems to not be a good friend of complex infinities How do I compute mean of an array of complex numbers? While we can evaluate: In[2]: import numpy as np In[3]: np.mean([1, 2, np.inf]) Out[3]: inf The following result is more cumbersome: In[4]: np.mean([1 + 0j, 2 + 0j, np.inf + 0j]) Out[4]: (inf+nan*j) ...\_methods.py:80: RuntimeWarning: invalid value encountered in cdouble_scalars ret = ret.dtype.type(ret / rcount) I'm not sure the imaginary part make sense to me. But please do comment if I'm wrong. Any insight into interacting with complex infinities in numpy? A: <code> import numpy as np def f(a = np.array([1 + 0j, 2 + 3j, np.inf + 0j])): </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> return result </code> SOLUTION: n = len(a) s = np.sum(a) result = np.real(s) / n + 1j * np.imag(s) / n
INSTRUCTION: Problem: I have an array : a = np.array([[ 0, 1, 2, 3, 5, 6, 7, 8], [ 4, 5, 6, 7, 5, 3, 2, 5], [ 8, 9, 10, 11, 4, 5, 3, 5]]) I want to extract array by its columns in RANGE, if I want to take column in range 1 until 10, It will return a = np.array([[ 1, 2, 3, 5, 6, 7, 8], [ 5, 6, 7, 5, 3, 2, 5], [ 9, 10, 11, 4, 5, 3, 5]]) Pay attention that if the high index is out-of-bound, we should constrain it to the bound. How to solve it? Thanks A: <code> import numpy as np a = np.array([[ 0, 1, 2, 3, 5, 6, 7, 8], [ 4, 5, 6, 7, 5, 3, 2, 5], [ 8, 9, 10, 11, 4, 5, 3, 5]]) low = 1 high = 10 </code> BEGIN SOLUTION <code> [insert] </code> END SOLUTION <code> print(result) </code> SOLUTION: high = min(high, a.shape[1]) result = a[:, low:high]
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt y = 2 * np.random.rand(10) x = np.arange(10) # make the y axis go upside down # SOLUTION START SOLUTION: ax = plt.gca() ax.invert_yaxis()
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt x = np.arange(10) y = np.arange(10) plt.plot(y, x) plt.xticks(range(0, 10, 2)) # Add extra ticks [2.1, 3, 7.6] to existing xticks # SOLUTION START SOLUTION: plt.xticks(list(plt.xticks()[0]) + [2.1, 3, 7.6])
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt x = np.arange(10) y = np.arange(10) # plot y over x # use a tick interval of 1 on the a-axis # SOLUTION START SOLUTION: plt.plot(x, y) plt.xticks(np.arange(min(x), max(x) + 1, 1.0))
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt x = np.arange(10) y = np.arange(10) # make 4 by 4 subplots with a figure size (5,5) # in each subplot, plot y over x and show axis tick labels # give enough spacing between subplots so the tick labels don't overlap # SOLUTION START SOLUTION: fig, axes = plt.subplots(nrows=4, ncols=4, figsize=(5, 5)) for ax in axes.flatten(): ax.plot(x, y) fig.tight_layout()
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns x = np.random.randn(10) y = np.random.randn(10) (l,) = plt.plot(range(10), "o-", lw=5, markersize=30) # set the face color of the markers to have an alpha (transparency) of 0.2 # SOLUTION START SOLUTION: l.set_markerfacecolor((1, 1, 0, 0.2))
INSTRUCTION: import numpy import pandas import matplotlib.pyplot as plt import seaborn seaborn.set(style="ticks") numpy.random.seed(0) N = 37 _genders = ["Female", "Male", "Non-binary", "No Response"] df = pandas.DataFrame( { "Height (cm)": numpy.random.uniform(low=130, high=200, size=N), "Weight (kg)": numpy.random.uniform(low=30, high=100, size=N), "Gender": numpy.random.choice(_genders, size=N), } ) # make seaborn relation plot and color by the gender field of the dataframe df # SOLUTION START SOLUTION: seaborn.relplot( data=df, x="Weight (kg)", y="Height (cm)", hue="Gender", hue_order=_genders )
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns x = 10 * np.random.randn(10) plt.plot(x) # highlight in red the x range 2 to 4 # SOLUTION START SOLUTION: plt.axvspan(2, 4, color="red", alpha=1)
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns x = np.random.randn(10) y = np.random.randn(10) sns.distplot(x, label="a", color="0.25") sns.distplot(y, label="b", color="0.25") # add legends # SOLUTION START SOLUTION: plt.legend()
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt x = np.arange(10) y = np.arange(10) # Plot y over x in a line chart and label the line "y over x" # Show legend of the plot and give the legend box a title "Legend" # Bold the legend title # SOLUTION START SOLUTION: plt.plot(x, y, label="y over x") plt.legend(title="legend", title_fontproperties={"weight": "bold"})
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt x = np.arange(10) y = np.arange(10) # plot y over x on a 2 by 2 subplots with a figure size of (15, 15) # repeat the plot in each subplot # SOLUTION START SOLUTION: f, axs = plt.subplots(2, 2, figsize=(15, 15)) for ax in f.axes: ax.plot(x, y)
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt x = np.arange(10) y = np.arange(10) plt.plot(x, y) # Remove the margin before the first ytick but use greater than zero margin for the xaxis # SOLUTION START SOLUTION: plt.margins(y=0)
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns x = np.arange(10) y = np.random.randn(10) plt.scatter(x, y) # show grids # SOLUTION START SOLUTION: ax = plt.gca() ax.grid(True)
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt x = np.arange(10) y = np.arange(10) a = np.arange(10) z = np.arange(10) # Plot y over x and a over z in two side-by-side subplots. # Label them "y" and "a" and make a single figure-level legend using the figlegend function # SOLUTION START SOLUTION: fig, axs = plt.subplots(1, 2) axs[0].plot(x, y, label="y") axs[1].plot(z, a, label="a") plt.figlegend(["y", "a"])
INSTRUCTION: import numpy as np import matplotlib.pyplot as plt data = [1000, 1000, 5000, 3000, 4000, 16000, 2000] # Make a histogram of data and renormalize the data to sum up to 1 # Format the y tick labels into percentage and set y tick labels as 10%, 20%, etc. # SOLUTION START SOLUTION: plt.hist(data, weights=np.ones(len(data)) / len(data)) from matplotlib.ticker import PercentFormatter ax = plt.gca() ax.yaxis.set_major_formatter(PercentFormatter(1))
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt x = np.arange(10) y = np.arange(10) # Plot y over x. Give the plot a title "Figure 1". bold the word "Figure" in the title but do not bold "1" # SOLUTION START SOLUTION: plt.plot(x, y) plt.title(r"$\bf{Figure}$ 1")
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt x = np.arange(10) y = np.arange(10) # make a two columns and one row subplots. Plot y over x in each subplot. # Give the plot a global title "Figure" # SOLUTION START SOLUTION: fig = plt.figure(constrained_layout=True) axs = fig.subplots(1, 2) for ax in axs.flat: ax.plot(x, y) fig.suptitle("Figure")
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt x = np.arange(10) y = np.arange(10) # Plot y over x with a legend of "Line" # Adjust the length of the legend handle to be 0.3 # SOLUTION START SOLUTION: plt.plot(x, y, label="Line") plt.legend(handlelength=0.3)
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt x = np.arange(0, 1000, 50) y = np.arange(0, 1000, 50) # plot y over x on a log-log plot # mark the axes with numbers like 1, 10, 100. do not use scientific notation # SOLUTION START SOLUTION: fig, ax = plt.subplots() ax.plot(x, y) ax.axis([1, 1000, 1, 1000]) ax.loglog() from matplotlib.ticker import ScalarFormatter for axis in [ax.xaxis, ax.yaxis]: formatter = ScalarFormatter() formatter.set_scientific(False) axis.set_major_formatter(formatter)
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt x = np.arange(10) y = np.arange(10) # Plot y over x # Turn minor ticks on and show gray dashed minor grid lines # Do not show any major grid lines # SOLUTION START SOLUTION: plt.plot(y, x) plt.minorticks_on() plt.grid(color="gray", linestyle="dashed", which="minor")
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt x = np.arange(10) y = np.arange(10) # make two side-by-side subplots and and in each subplot, plot y over x # Title each subplot as "Y" # SOLUTION START SOLUTION: fig, axs = plt.subplots(1, 2) for ax in axs: ax.plot(x, y) ax.set_title("Y")
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt x = np.arange(10) y = np.arange(10) # plot y over x # do not show xticks for the plot # SOLUTION START SOLUTION: plt.plot(y, x) plt.tick_params( axis="x", # changes apply to the x-axis which="both", # both major and minor ticks are affected bottom=False, # ticks along the bottom edge are off top=False, # ticks along the top edge are off labelbottom=False, ) # labels along the bottom edge are off
INSTRUCTION: import numpy as np import matplotlib.pyplot as plt H = np.random.randn(10, 10) # show the 2d array H in black and white # SOLUTION START SOLUTION: plt.imshow(H, cmap="gray")
INSTRUCTION: from matplotlib import pyplot as plt import numpy as np x = np.arange(10) y = np.arange(1, 11) error = np.random.random(y.shape) # Plot y over x and show the error according to `error` # Plot the error as a shaded region rather than error bars # SOLUTION START SOLUTION: plt.plot(x, y, "k-") plt.fill_between(x, y - error, y + error)
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt x = np.random.random((10, 2)) # Plot each column in x as an individual line and label them as "a" and "b" # SOLUTION START SOLUTION: [a, b] = plt.plot(x) plt.legend([a, b], ["a", "b"])
INSTRUCTION: import matplotlib.pyplot as plt import numpy as np, pandas as pd import seaborn as sns tips = sns.load_dataset("tips") # Make a seaborn joint regression plot (kind='reg') of 'total_bill' and 'tip' in the tips dataframe # do not use scatterplot for the joint plot # SOLUTION START SOLUTION: sns.jointplot( x="total_bill", y="tip", data=tips, kind="reg", joint_kws={"scatter": False} )
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns data = { "reports": [4, 24, 31, 2, 3], "coverage": [35050800, 54899767, 57890789, 62890798, 70897871], } df = pd.DataFrame(data) sns.factorplot(y="coverage", x="reports", kind="bar", data=df, label="Total") # do not use scientific notation in the y axis ticks labels # SOLUTION START SOLUTION: plt.ticklabel_format(style="plain", axis="y")
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt x = np.arange(10) y = np.arange(10) plt.plot(x, y, marker="*", label="Line") # Show a legend of this plot and show two markers on the line # SOLUTION START SOLUTION: plt.legend(numpoints=2)
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns x = np.linspace(0, 2 * np.pi, 10) y = np.cos(x) plt.plot(x, y, label="sin") # rotate the x axis labels counter clockwise by 45 degrees # SOLUTION START SOLUTION: plt.xticks(rotation=-45)
INSTRUCTION: import matplotlib.pyplot as plt import numpy as np, pandas as pd import seaborn as sns tips = sns.load_dataset("tips") # Make a seaborn joint regression plot (kind='reg') of 'total_bill' and 'tip' in the tips dataframe # change the line color in the regression to green but keep the histograms in blue # SOLUTION START SOLUTION: sns.jointplot( x="total_bill", y="tip", data=tips, kind="reg", line_kws={"color": "green"} )
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt x = np.arange(10) y = np.arange(10) # Plot y over x in a line plot # Show marker on the line plot. Make the marker have a 0.5 transparency but keep the lines solid. # SOLUTION START SOLUTION: (l,) = plt.plot(x, y, "o-", lw=10, markersize=30) l.set_markerfacecolor((1, 1, 0, 0.5)) l.set_color("blue")
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns # draw a line segment from (0,0) to (1,2) # SOLUTION START SOLUTION: p1 = (0, 0) p2 = (1, 2) plt.plot((p1[0], p2[0]), (p1[1], p2[1]))
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt x = np.arange(10) y = np.arange(10) # Plot y over x in a line chart. Show x axis ticks on both top and bottom of the figure. # SOLUTION START SOLUTION: plt.plot(x, y) plt.tick_params(top=True)
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns df = sns.load_dataset("penguins")[ ["bill_length_mm", "bill_depth_mm", "flipper_length_mm", "body_mass_g"] ] # make a seaborn scatter plot of bill_length_mm and bill_depth_mm # use markersize 30 for all data points in the scatter plot # SOLUTION START SOLUTION: sns.scatterplot(x="bill_length_mm", y="bill_depth_mm", data=df, s=30)
INSTRUCTION: import matplotlib.pyplot as plt # draw a circle centered at (0.5, 0.5) with radius 0.2 # SOLUTION START SOLUTION: import matplotlib.pyplot as plt circle1 = plt.Circle((0.5, 0.5), 0.2) plt.gca().add_patch(circle1)
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns x = np.linspace(0, 2 * np.pi, 10) y = np.cos(x) # set xlabel as "X" # put the x label at the right end of the x axis # SOLUTION START SOLUTION: plt.plot(x, y) ax = plt.gca() label = ax.set_xlabel("X", fontsize=9) ax.xaxis.set_label_coords(1, 0)
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt x = np.arange(10) y = np.arange(10) # plot y over x # use font size 20 for title, font size 18 for xlabel and font size 16 for ylabel # SOLUTION START SOLUTION: plt.plot(x, y, label="1") plt.title("test title", fontsize=20) plt.xlabel("xlabel", fontsize=18) plt.ylabel("ylabel", fontsize=16)
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns df = sns.load_dataset("exercise") # Make catplots of scatter plots by using "time" as x, "pulse" as y, "kind" as hue, and "diet" as col # Change the xlabels to "Exercise Time" and "Exercise Time" # SOLUTION START SOLUTION: g = sns.catplot(x="time", y="pulse", hue="kind", col="diet", data=df) axs = g.axes.flatten() axs[0].set_xlabel("Exercise Time") axs[1].set_xlabel("Exercise Time")
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns x = np.arange(10) y = np.sin(x) df = pd.DataFrame({"x": x, "y": y}) sns.lineplot(x="x", y="y", data=df) # remove x tick labels # SOLUTION START SOLUTION: ax = plt.gca() ax.set(xticklabels=[])
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns x = np.arange(10) # draw a line (with random y) for each different line style # SOLUTION START SOLUTION: from matplotlib import lines styles = lines.lineStyles.keys() nstyles = len(styles) for i, sty in enumerate(styles): y = np.random.randn(*x.shape) plt.plot(x, y, sty) # print(lines.lineMarkers.keys())
INSTRUCTION: import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns x = np.random.randn(10) y = x plt.scatter(x, y) # put y ticks at -1 and 1 only # SOLUTION START SOLUTION: ax = plt.gca() ax.set_yticks([-1, 1])
INSTRUCTION: import matplotlib.pyplot as plt import numpy as np xvec = np.linspace(-5.0, 5.0, 100) x, y = np.meshgrid(xvec, xvec) z = -np.hypot(x, y) plt.contourf(x, y, z) # draw x=0 and y=0 axis in my contour plot with white color # SOLUTION START SOLUTION: plt.axhline(0, color="white") plt.axvline(0, color="white")