How to get sample indices from RandomUnderSampler in imblearn - python

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

Related

TypeError: tensor() got an unexpected keyword argument 'names'

so I started reading deep learning with pytorch, and got to the point of setting names to the dimensions inside the tensor, to make it more friendly, but as soon as I use the names argument, I get the error:
TypeError: tensor() got an unexpected keyword argument 'names'
can anyone help me out?
The code is simple:
import torch
weights_named = torch.tensor([0.2126, 0.7152, 0.0722], names=['channels'])
weights_named
Just want to run this, to see how to set names to the dimensions. Thanks in advance.
This is due to your PyTorch Version. Upgrade your Pytorch version and should work. In my case,
import torch
torch.__version__ #1.7
weights_named = torch.tensor([0.2126, 0.7152, 0.0722], names=['channels'])
# __main__:1: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change.
Named tensors and functions supporting it on latest 1.9 release, but on 1.7 (my version) is still experimental.

'cupy.core.core.ndarray' object has no attribute 'unique'

I was transforming categorical features using factorize() function which returns a tuple of a cupy array and strings. I assigned the cupy array into a variable named codes. However, I can't seem to get the unique values of codes using codes.unique()
It returns an error message:
AttrubuteError: 'cupy.core.core.ndarray' object has no attribute 'unique'
# use factorize to create continuous integers from a categorical feature (returns a tuple)
codes, uniques = df_train['product_id'].factorize()
codes.unique()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-59-27db0fac06a1> in <module>()
----> 1 codes.unique()
AttributeError: 'cupy.core.core.ndarray' object has no attribute 'unique'
code
Appreciate the help and suggestion to make it work
CuPy is designed to be highly compatible with NumPy, and in this case note that numpy.ndarray does not have a unique() method either; for both NumPy and CuPy it lives under the main namespace as a regular function (numpy.unique()/cupy.unique()). So it is an invalid usage not specific to CuPy IMHO.
I think you need to call cupy.unique(codes) instead of codes.unique() like you could if it were a regular NumPy array. Docs: https://docs.cupy.dev/en/stable/reference/generated/cupy.unique.html
It was added in this PR: https://github.com/cupy/cupy/pull/1140 which as you may be able to see did not add it as a method on the array type.

how to complete fit_transform on tuple, or be able to complete a fit_transform on the data, by changing tuple to...?

So first question: I read up on this error and it says it is type: tuple, so I am confused what the error is and what to do to fix it. A beginner, so please provide detailed suggestions. tried changing (df_x) to [df_x], as per instructions in similar question, without success.
cv2=TfidfVectorizer(sublinear_tf=True,min_df=1),#stop_words='english')
updated_x2=cv2.fit_transform[df_x]
fn2=cv2.get_feature_names()
AttributeError Traceback (most recent call last)
<ipython-input-52-ddf0f04628ff> in <module>
1 cv2=TfidfVectorizer(sublinear_tf=True,min_df=1),#stop_words='english')
----> 2 updated_x2=cv2.fit_transform[df_x]
3 fn2=cv2.get_feature_names()
AttributeError: 'tuple' object has no attribute 'fit_transform'
cv2=TfidfVectorizer(sublinear_tf=True,min_df=1),
creates cv2 as a tuple because of the unintended trailing comma. If you adjust your comment to precede it, i.e.
cv2=TfidfVectorizer(sublinear_tf=True,min_df=1)#, stop_words='english')
It should get rid of that error

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.

AttributeError: 'module' object has no attribute 'doc

i am a beginner and i trying to model system dynamic model using python programming.the problem is when i trying to print the components of the sd model, the error message comes out like this:
"AttributeError: 'module' object has no attribute 'doc'"
my code:
import pysd
educationmodel = pysd.read_vensim('Education.mdl')
print educationmodel.components.doc()
As far as understand from the git repo, the doc() method is inside Class PySD. Also, read_vensim returns an instance of this class.
So your problem should get solved if you directly use educationmodel.doc().
That may be my fault - I had to move the .doc() function to the model object instead of the components object as a way to work towards including Vensim macros properly. If it's still an issue, may want to update to the latest release (0.7.4). If that doesn't help either, then we may have to fix something. =)

Categories

Resources