Skip to content
Advertisement

Tag: numpy

How to find the global minima from a matplotlib graph?

I have two arrays x and y. Here I’ve plotted them using Matplotlib and found the global minima using this simple logic. Here is the output that I’m getting: After that I’ve smoothen the graph BSpline Now my position of the global minima has changed and that simple logic will not work here. I want to know how I can

Fill up column based on condition goes wrong

I am trying to fill a column based on condition but something goes wrong. So I am expecting if it is not 2017 there should be a no. But yet the years 2019 and 2020 also get filled up with yes. Does anyone know what went wrong? Answer Use Series.between instead & for bitwise AND, for chain by bitwise OR

Losing cell formats when accessing rows

In some circumstances the format (int, float, etc) of a cell is lost when accessing via its row. In that example the first column has integers and the second floats. But the 111 is converted into 111.0. The output I would expect is like this I have an idea why this happens. But IMHO this isn’t user friendly. Can I

Generate random binary matrix constrained to no null row

I want to generate a random binary matrix, so I’m using W=np.random.binomial(1, p, (n,n)). It works fine, but I want a constraint that no row is just of 0s. I create the following function: It also works fine, but it seems to me too inefficient. Is there a faster way to create this constraint? Answer When the matrix is large,

Get all permutations of bool array

I need all permutations of a bool array, the following code is inefficient, but does what I want: However it is inefficient and fails for long arrays. Is there a more efficent implementation? Answer What about sampling the combinations of indices of the True values: Output:

Centering matrix

I want to write a function for centering an input data matrix by multiplying it with the centering matrix. The function shall subtract the row-wise mean from the input. My code: But I get a wrong result matrix, it is not centered. Thanks! Answer The centering matrix is Here is a list of issues in your original formulation: np.ones(n).T is

Cholesky factorisation not lower triangular

I am building a cholesky factorisation algorthim as proposed from the book: Linear Algebra and Optimisation for Machine Learning They provide the following algorthim: I have attempted this with python with the following algorithm: When testing this on a matrix I get upper triangular as opposed to lower triangular. Where was I mistaken? Answer You don’t even need to look

Advertisement