numpy having no "isin" attribute - python

I tried using the np.isin() function but everytime I do, it returns me the error:
AttributeError: 'module' object has no attribute 'isin'
here is exactly what I do
import numpy as np
a = np.arange(9).reshape((3,3))
test = np.arange(5)
print np.isin(a, test)
I havent found any information about this problem, I use the latest version of numpy and havent had any problem with other numpy module, why does it returns me this error?

The isin function was added in NumPy 1.13:
New np.isin function, improves on in1d.
You're probably using an older version.

Reading through the Notes section of the docs shows
New in version 1.13.0.
I suspect that if you do
print(np.__version__)
you will see something less than 1.13.0.

Following the [source] link in the docs I find that:
def isin(element, test_elements, assume_unique=False, invert=False):
"..."
element = np.asarray(element)
return in1d(element, test_elements, assume_unique=assume_unique,
invert=invert).reshape(element.shape)
It's not doing anything that you can't already do with in1d.
The containing file can be downloaded, and used from your own directory. It has an enhanced unique.

Related

How to get sample indices from RandomUnderSampler in imblearn

Does anyone know if/how one can get the indices of the selected samples after undersampling with imblearn's RandomUnderSampler?
There used to be the argument "return_indices=True" which was now removed for the new version and supposingly was replaced with an attribute "sample_indices_". However, if I try to use that attribute, it doesn't work (see code below). I'm using imblearn version 0.6.2.
russs = RandomUnderSampler(random_state=0,sampling_strategy={6: 600}).fit(X_train_point,y_train_point)
russs.sample_indices_
AttributeError Traceback (most recent call last)
<ipython-input-78-8397ba40f19b> in <module>
1 russs = RandomUnderSampler(random_state=0,sampling_strategy={6: 600}).fit(X_train_point,y_train_point)
----> 2 russs.sample_indices
AttributeError: 'RandomUnderSampler' object has no attribute 'sample_indices'
I also found a workaround. As the undersampling is solely based on the y_vector, one can add a counter-variable instead of the the x-vector/array and write it as follows:
counter=range(0,len(y_train_point))
index,y_resampled=RandomUnderSampler(random_state=0,sampling_strategy={6:600}).fit(counter,y_train_point)
X_resampled=X_train_point[index]
Also facing this.. Despite the fact that the docs say
Deprecated since version 0.4: return_indices is deprecated. Use the attribute sample_indices_ instead.
I reverted to 0.5.0 and am able to use the old return_indices=True argument.
pip install imbalanced-learn==0.5.0
I had this problem yesterday and I could access the attribute all right in the end.
Make sure you're not forgetting that underscore in the end, from the error message it seems you have.
It should be
russs.sample_indices_
not
russs.sample_indices

Scipy.signal method 'filtfilt()' doesn't recognized correctly

It's my first time working with scipy.signal library and I am experimenting an error with the method filtfilt().
This is the code I am trying to execute:
Fs = 1000
# s is an array of numbers
a=signal.firwin(10, cutoff=0.5/(Fs/2))
ss = s - np.mean(s)
se = signal.filtfilt(a, 1, ss, method="gust")
When I execute this code I get the next error:
TypeError: filtfilt() got an unexpected keyword argument 'method'
But in the documentation of the method it is clearly shown that the parameter 'method' exists.
What could be the problem?
I would guess you have different versions of scipy in use. The documentation of filtfilt says the 'gust' method was added in 0.16. I assume the method parameter does not exist in earlier versions.

Using SymPy's New Assumptions

I'm having some issues with SymPy's current assumptions.
Look at this thread. One of the hints said to use the assume module (reference here).
I tried doing the following computation $\lim_{x \to \infty} \frac{\ln{x}}{x^k}$. I want to evaluate this limit for $k >0$.
So I tried this:
with assuming(k>0):
limit((log(x))/(x**k),x,oo)
I also tried this:
eval(limit((log(x))/(x**k),x,oo),k>0)
But regardless, I get this error:
NotImplementedError: Result depends on the sign of -sign(k)
In the case of
with assume(k>0):
limit((log(x))/(x**k),x,oo)
I get this error:
TypeError: 'module' object is not callable
Any ideas what I'm doing wrong?
This seems to work. The first answer in the thread that you linked says that "The assumption system of SymPy is kind of a mess right now". I'm not sure if that has changed since then.
k = Symbol('k', positive=True)
print limit((log(x))/(x**k),x,oo)

Pickling cv2.KeyPoint causes PicklingError

I want to search surfs in all images in a given directory and save their keypoints and descriptors for future use. I decided to use pickle as shown below:
#!/usr/bin/env python
import os
import pickle
import cv2
class Frame:
def __init__(self, filename):
surf = cv2.SURF(500, 4, 2, True)
self.filename = filename
self.keypoints, self.descriptors = surf.detect(cv2.imread(filename, cv2.CV_LOAD_IMAGE_GRAYSCALE), None, False)
if __name__ == '__main__':
Fdb = open('db.dat', 'wb')
base_path = "img/"
frame_base = []
for filename in os.listdir(base_path):
frame_base.append(Frame(base_path+filename))
print filename
pickle.dump(frame_base,Fdb,-1)
Fdb.close()
When I try to execute, I get a following error:
File "src/pickle_test.py", line 23, in <module>
pickle.dump(frame_base,Fdb,-1)
...
pickle.PicklingError: Can't pickle <type 'cv2.KeyPoint'>: it's not the same object as cv2.KeyPoint
Does anybody know, what does it mean and how to fix it? I am using Python 2.6 and Opencv 2.3.1
Thank you a lot
The problem is that you cannot dump cv2.KeyPoint to a pickle file. I had the same issue, and managed to work around it by essentially serializing and deserializing the keypoints myself before dumping them with Pickle.
So represent every keypoint and its descriptor with a tuple:
temp = (point.pt, point.size, point.angle, point.response, point.octave,
point.class_id, desc)
Append all these points to some list that you then dump with Pickle.
Then when you want to retrieve the data again, load all the data with Pickle:
temp_feature = cv2.KeyPoint(x=point[0][0],y=point[0][1],_size=point[1], _angle=point[2],
_response=point[3], _octave=point[4], _class_id=point[5])
temp_descriptor = point[6]
Create a cv2.KeyPoint from this data using the above code, and you can then use these points to construct a list of features.
I suspect there is a neater way to do this, but the above works fine (and fast) for me. You might have to play around with your data format a bit, as my features are stored in format-specific lists. I tried to present the above using my idea at its generic base. I hope that this may help you.
Part of the issue is cv2.KeyPoint is a function in python that returns a cv2.KeyPoint object. Pickle is getting confused because, literally, "<type 'cv2.KeyPoint'> [is] not the same object as cv2.KeyPoint". That is, cv2.KeyPoint is a function object, while the type was cv2.KeyPoint. Why OpenCV is like that, I can only make guesses at unless I go digging. I have a feeling it has something to do with it being a wrapper around a C/C++ library.
Python does give you the ability to fix this yourself. I found the inspiration on this post about pickling methods of classes.
I actually use this clip of code, highly modified from the original in the post
import copyreg
import cv2
def _pickle_keypoints(point):
return cv2.KeyPoint, (*point.pt, point.size, point.angle,
point.response, point.octave, point.class_id)
copyreg.pickle(cv2.KeyPoint().__class__, _pickle_keypoints)
Key points of note:
In Python 2, you need to use copy_reg instead of copyreg and point.pt[0], point.pt[1] instead of *point.pt.
You can't directly access the cv2.KeyPoint class for some reason, so you make a temporary object and use that.
The copyreg patching will use the otherwise problematic cv2.KeyPoint function as I have specified in the output of _pickle_keypoints when unpickling, so we don't need to implement an unpickling routine.
And to be nauseatingly complete, cv2::KeyPoint::KeyPoint is an overloaded function in C++, but in Python, this isn't exactly a thing. Whereas in the C++, there's a function that takes the point for the first argument, in Python, it would try to interpret that as an int instead. The * unrolls the point into two arguments, x and y to match the only int argument constructor.
I had been using casper's excellent solution until I realized this was possible.
A similar solution to the one provided by Poik. Just call this once before pickling.
def patch_Keypoint_pickiling(self):
# Create the bundling between class and arguments to save for Keypoint class
# See : https://stackoverflow.com/questions/50337569/pickle-exception-for-cv2-boost-when-using-multiprocessing/50394788#50394788
def _pickle_keypoint(keypoint): # : cv2.KeyPoint
return cv2.KeyPoint, (
keypoint.pt[0],
keypoint.pt[1],
keypoint.size,
keypoint.angle,
keypoint.response,
keypoint.octave,
keypoint.class_id,
)
# C++ Constructor, notice order of arguments :
# KeyPoint (float x, float y, float _size, float _angle=-1, float _response=0, int _octave=0, int _class_id=-1)
# Apply the bundling to pickle
copyreg.pickle(cv2.KeyPoint().__class__, _pickle_keypoint)
More than for the code, this is for the incredibly clear explanation available there : https://stackoverflow.com/a/50394788/11094914
Please note that if you want to expand this idea to other "unpickable" class of openCV, you only need to build a similar function to "_pickle_keypoint". Be sure that you store attributes in the same order as the constructor. You can consider copying the C++ constructor, even in Python, as I did. Mostly C++ and Python constructors seems not to differ too much.
I has issue with the "pt" tuple. However, a C++ constructor exists for X and Y separated coordinates, and thus, allow this fix/workaround.

Python ioapiTools module can't do basic math operations

I've installed ioapiTools, a python module to manage ioapi format files. The module is supposed to handle file and perform operations on them, including basic arithmetic operations. But something is wrong and when I try to, say, multiply an array by a float or an integer, the result is a zero-valued array (both the array and the float/integer are different from zero).
The module in question creates a temporary variable using cdms2 according to the following syntax:
import cdms2 as cdms, cdtime, MV2 as MV, cdutil
import numpy as N
..........
def __mul__(self, other):
"""
Wrapper around cdms tvariable multiply
"""
tmpVar = cdms.tvariable.TransientVariable.__mul__(self,other)
iotmpVar = createVariable(tmpVar, self.ioM, id = self.id,\
attributes=self.attributes, copyFlag = False)
return iotmpVar
But the variable returns nothing but zeros.
Any ideas?
I tried to use ioapiTools, and latest version i found was 0.3.2 from http://www2-pcmdi.llnl.gov/Members/azubrow/ioapiTools/download-source-file .
unfortunately, the code doesn't seem to catchup with evolution of cdat, which now recommend using numpy instead of Numeric. automated translation tool may be resolving some problems, but not all. For example, the class iovar (defined in ioapiTools.py:2103) now needs to have _____new_____ method, as it is a subclass of numpy masked array (i dont know how things are in Numeric). With that, i seems to have _____mul_____ working. i couldn't reproduce your problem though, because i couldn't even get an instance of iovar without having _____new_____ method defined.
i can pass what i got to you if you still need one, but i am sure there are more problems hiding... let me know if you need it though.

Categories

Resources