I have a data frame consisting of lists as elements. I want to find the closest matching values within a percentage of a given value. My code: Present solution: Expected solution: Answer Idea is get difference with val and then replace to missing values if not match tolerance, last get np.nanargmin which raise error if all missing values, so added
Tag: numpy
Graphing Inequalities in python
I’m looking to create a program that will randomly generate lines (that are inequalities) and that will show the area that satisfies the constraints. I don’t mind which libraries are used so feel free to use sympy, numpy etc I will show my current code but this just fills the area between 2 lines and doesn’t use inequalities at all.
NumPy array row differences
I have a NumPy array vectors = np.random.randn(rows, cols). I want to find differences between its rows according to some other array diffs which is sparse and “2-hot”: containing a 1 in its column corresponding to the first row of vectors and a -1 corresponding to the second row. Perhaps an example shall make it clearer: then I can compute
how to calculate correlation coefficient for a scatter-plot in scipy
i am trying to calculate the correlation coefficient for a scatterplot with scipy, the thing is, i have a kind of complex dataset in an ndarray, and the basic syntax does not work for me… here is my full code: Answer pearsonr works fine on your data You were passing two floats (namely values at the row i) as the
np.diag not including 0s in the array
I have an expression that yields the following result: I want to make a diagonal 2X2 matrix which has 0.5 and 0 on its diagonal. But when I use the following code: A being the above array, I get the following result: Why does python not include the second element from A on the array and how can I include
Replace all non-intersecting values with 0, between two numpy arrays
Say I have a multi-dimensional array, for example: And say I have a one-dimensional array (b), with some values which may be seen in my multi-dimensional array (a): Now I want to replace all of the non-intersecting values in array a with zero. I want to get the following result: How could I achieve this? I tried several ways of
Scipy ifft gives different results with seemingly identical input
Why would xcorr and xcorr2 be quite different here? M1 and M2 are numpy matrices. M1.shape[0] = M2.shape[0]. xcorr is what I would expect with this operation, but xcorr2 is something totally different and has imaginary numbers. xcorr does not have imaginary numbers. Answer Try giving xcorr and xcorr2 dtype=complex. According to scipy docs, the output from both fft and
How to write an object typed array into csv file with NumPy?
I have two numpy arrays(A, B) and 2 scalar values(C,D) that I want to store in a csv file. I know how to write a single numpy array in it: I want the first two columns of my csv-file to contain the 2 arrays A and B and then have the 2 scalar values C and D as the first
How to make a boolean array by checking if the items in an array is in a list?
I’m trying to find every item in an numpy array arr that’s also in an arbitrary list lst and replace them, but while arr > 0 will generate a boolean array for easy masking, arr in lst only works with all() or any() which isn’t what I need. Example input: array (1, 2, 3, 4, 5), list [2, 4, 6,
Getting all row indices in numpy 2d array where elements in each row exists more than 2 times in entire array
I am working with graph data defined as 2d array of edges. I.e. Defines a graph, all elements define a node id, there are no self loops, it is directed, and no value in a column exists in the other column. Now to the question, I need to select all edges where both ‘nodes’ occur more than once in the