Posts

Showing posts with the label numpy

numpy conditional list membership element wise

I have a 2D numpy array: I have a list of values to keep: I want to test for list membership for each element in the array. Something like: The result I want: You can use isin isin is an element-wise function version of the python keyword in An added benefit of isin is that it's flexible with arrays of different dimensions: Here is one way using broadcasting: Which is faster than isin for small arrays (less than 100 rows): For large arrays it's better to use isin because broadcasting in 3D is not very efficient for large arrays/matrices. By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Python equivalent of MATLAB function findchangepts

I need a python equivalent of the MATLAB command findchangepts . I am unable to find one yet. Essentially I want to find K abrupt changes in my time series. I am able to use the matlab command but unable to find an equivalent function in python. Any help/pointers to existing libraries will be very helpful. I am not proficient in the optimization modules of scipy and thus would prefer an existing python package. Thanks. By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Replacing values in array or dataframe

I have a numpy array of values (simplified from a much larger dataset): Which I want to turn into these values: I have a lookup array, which I intended to use, but couldn't work out how to use it: I couldn't work out a way to do this using numpy arrays, so I turned data into a Pandas dataframe: And tried to convert the values using a dictionary: This obviously didn't work, but also seems like a really clunky, terrible way of approaching the problem. There must be a way of looking up the lookup array rather than writing the dictionary. Does anyone have any pointers? You need use numpy.vectorize in order to directly map values. Output: You array data actually contains indices into the array lookup , except that is starts at 1. You just ave to index it: Maybe you can adapt your code to create directly the lookup2 array, but it depends on how you get your data. By clicking "Post Your Answer", you acknowledge that you have ...