AttributeError: 'numpy.ndarray' object has no attribute '_iter_test_masks' [closed] - numpy

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to use sklearn GridSearchCV to perform K-fold cross-validation to select a bandwidth for KernelDensity estimation.
When I implement grid.fit(data), I receive the error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\mubar\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\utils\validation.py", line 73, in inner_f
return f(**kwargs)
File "C:\Users\mubar\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\model_selection\_search.py", line 736, in fit
self._run_search(evaluate_candidates)
File "C:\Users\mubar\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\model_selection\_search.py", line 1188, in _run_search
evaluate_candidates(ParameterGrid(self.param_grid))
File "C:\Users\mubar\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\model_selection\_search.py", line 714, in evaluate_candidates
in product(candidate_params,
File "C:\Users\mubar\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\model_selection\_split.py", line 80, in split
for test_index in self._iter_test_masks(X, y, groups):
AttributeError: 'numpy.ndarray' object has no attribute '_iter_test_masks'
Here is my code:
import numpy as np
from sklearn.model_selection import GridSearchCV, LeaveOneOut
train = np.random.rand(12,2)
target = np.array([0,0,1,2,3,3,3,4,5,5,6,6])
bw = np.linspace(0.01,0.1,10)
grid = GridSearchCV(KernelDensity(kernel='gaussian'),
{'bandwidth': bw},
cv=LeaveOneOut)
grid.fit(train,target[:,None])

You just forgot to instantiate the LeaveOneOut cross-validator in your GridSearchCV definition.
train = np.random.rand(12,2)
target = np.array([0,0,1,2,3,3,3,4,5,5,6,6])
bw = np.linspace(0.01,0.1,10)
grid = GridSearchCV(KernelDensity(kernel='gaussian'),
{'bandwidth': bw},
cv=LeaveOneOut() # <-- typo was here
)
grid.fit(train,target[:,None])
This will resolve the issue.

Related

DataFrame object is not callable with AIF360

I encounter TypeError: 'DataFrame' object is not callable with the following. Anyone can help? Thanks.
%cd -
dataset_orig = df_data_1(protected_attribute_names=['Gender'],
privileged_classes=['Male'],
features_to_drop=[])
dataset_orig_train, dataset_orig_test = dataset_orig.split([0.7], shuffle=True)
privileged_groups = [{'Gender': 1}]
unprivileged_groups = [{'Gender': 0}]
/home/wsuser/work
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-59-8c624cfec261> in <module>
5 # consider in this evaluation
6 privileged_classes=['Male'], # male is considered privileged
----> 7 features_to_drop=[]) # ignore all other attributes
8
9 dataset_orig_train, dataset_orig_test = dataset_orig.split([0.7], shuffle=True)
TypeError: 'DataFrame' object is not callable
It appears that df_data_1 is your dataset dataframe, right? If it is, you would need to update your script to convert it to StandardDataset:
from aif360.datasets import StandardDataset
dataset_orig = StandardDataset(df_data_1,
protected_attribute_names=['Gender'],
privileged_classes=['Male'],
features_to_drop=[],
favorable_classes=[1] # Update this with label values which are considered favorable in your dataset
)
I am not sure how your dataset looks like, but you can adapt the complete reproducible example here to do this process for your dataset.

TypeError: 'function' object is not subscriptable how to resolve this error while reading csv file [duplicate]

This question already has answers here:
Import CSV file as a Pandas DataFrame
(6 answers)
Closed 1 year ago.
when I am writing this code
test_df1=pd.read_csv[CSVFILE]
it show the below error
Traceback (most recent call last):
File "<ipython-input-13-cb3d7014d176>", line 1, in <module>
test_df1=pd.read_csv[CSVFILE]
TypeError: 'function' object is not subscriptable
how to resolve this
Instead of
test_df1=pd.read_csv[CSVFILE]
because read_csv() is a method so it is called by () not []
so use :-
df1=pd.read_csv('CSVFILE.csv')

Pandas 0.24.0 breaks my pandas dataframe with special column identifiers

I had code that worked fine until I tried to run it on a coworker's machine, whereupon I discovered that while it worked using pandas 0.22.0, it broke on pandas 0.24.0. For the moment, we've solved this problem by downgrading their copy of pandas, but I would like to find a better solution if one exists.
The problem seems to be that I am creating a user-defined class to use as identifiers for my columns in the dataframe. When trying to compare two dataframes it for some reason tries to call my column labels as functions, and then throws an exception because they aren't callable
Here's some example code:
import pandas as pd
import numpy as np
class label(object):
def __init__(self, var):
self.var = var
def __eq__(self,other):
return self.var == other.var
df = pd.DataFrame(np.eye(5),columns=[label(ii) for ii in range(5)])
df == df
This produces the following stack trace:
Traceback (most recent call last):
File "<ipython-input-4-496e4ab3f9d9>", line 1, in <module>
df==df1
File "C:\...\site-packages\pandas\core\ops.py", line 2098, in f
return dispatch_to_series(self, other, func, str_rep)
File "C:\...\site-packages\pandas\core\ops.py", line 1157, in dispatch_to_series
new_data = expressions.evaluate(column_op, str_rep, left, right)
File "C:\...\site-packages\pandas\core\computation\expressions.py", line 208, in evaluate
return _evaluate(op, op_str, a, b, **eval_kwargs)
File "C:\...\site-packages\pandas\core\computation\expressions.py", line 68, in _evaluate_standard
return op(a, b)
File "C:\...\site-packages\pandas\core\ops.py", line 1135, in column_op
for i in range(len(a.columns))}
File "C:\...\site-packages\pandas\core\ops.py", line 1135, in <dictcomp>
for i in range(len(a.columns))}
File "C:\...\site-packages\pandas\core\ops.py", line 1739, in wrapper
name=res_name).rename(res_name)
File "C:\...\site-packages\pandas\core\series.py", line 3733, in rename
return super(Series, self).rename(index=index, **kwargs)
File "C:\...\site-packages\pandas\core\generic.py", line 1091, in rename
level=level)
File "C:\...\site-packages\pandas\core\internals\managers.py", line 171, in rename_axis
obj.set_axis(axis, _transform_index(self.axes[axis], mapper, level))
File "C:\...\site-packages\pandas\core\internals\managers.py", line 2004, in _transform_index
items = [func(x) for x in index]
TypeError: 'label' object is not callable
I've found I can fix the problem by making my class callable with a single argument and returning that argument, but that breaks .loc indexing, which will default to treating my objects as callables.
This problem only occurs when the custom objects are in the columns - the index can handle them just fine.
Is this a bug or a change in usage, and is there any way I can work around it without giving up my custom labels?

TypeError on read_csv, working in pandas .7, error in .8.0rc2, possible dependency error?

I am attempting to execute the following within python:
from pandas import *
tickdata = read_csv('/home/user/sourcefile.csv',index_col=0,parse_dates='TRUE')
The csv files has rows that look like:
2011/11/23 23:56:00.554389,1165.2500
2011/11/23 23:56:02.310943,1165.5000
2011/11/23 23:56:05.564009,1165.2500
On pandas .7, this executes fine. On pandas .8.0rc2, I get the error below. Because I have .7 and .8 installed on two different systems, I have not ruled out a dependency or python version difference. Any ideas on how to get this to execute under .8 are appreciated.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/pandas-0.8.0rc2-py2.7-linux-x86_64.egg/pandas/io/parsers.py", line 225, in read_csv
return _read(TextParser, filepath_or_buffer, kwds)
File "/usr/local/lib/python2.7/dist-packages/pandas-0.8.0rc2-py2.7-linux-x86_64.egg/pandas/io/parsers.py", line 192, in _read
return parser.get_chunk()
File "/usr/local/lib/python2.7/dist-packages/pandas-0.8.0rc2-py2.7-linux-x86_64.egg/pandas/io/parsers.py", line 728, in get_chunk
index = self._agg_index(index)
File "/usr/local/lib/python2.7/dist-packages/pandas-0.8.0rc2-py2.7-linux-x86_64.egg/pandas/io/parsers.py", line 846, in _agg_index
if try_parse_dates and self._should_parse_dates(self.index_col):
File "/usr/local/lib/python2.7/dist-packages/pandas-0.8.0rc2-py2.7-linux-x86_64.egg/pandas/io/parsers.py", line 874, in _should_parse_dates
return i in to_parse or name in to_parse
TypeError: 'in <string>' requires string as left operand, not int
I fixed the parser bug shown in the stack trace that you pasted. However, I'm wondering whether your date column is named "TRUE" or did you mean to just pass a boolean? I haven't dug through pandas history but I know that in 0.8 we are supporting much more complex date parsing behavior as part of the time series API so here we're interpreting the string value as a column name.
I've reported the bug on GitHub (best place for bug reports):
https://github.com/pydata/pandas/issues/1544
Should have a resolution today or tomorrow.

Matplotlib: re-open a closed figure? [duplicate]

This question already has an answer here:
Matplotlib: how to show a figure that has been closed
(1 answer)
Closed 3 years ago.
Is it possible to re-open a closed figure (i.e., one the user X'd) in matplotlib? The following code shows the naive approach:
In [14]: fig = figure(10)
In [15]: close(fig)
In [16]: fig.show()
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 1410, in __call__
return self.func(*args)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 495, in callit
func(*args)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/backends/backend_tkagg.py", line 253, in idle_draw
self.draw()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/backends/backend_tkagg.py", line 239, in draw
tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/backends/tkagg.py", line 19, in blit
tk.call("PyAggImagePhoto", photoimage, id(aggimage), colormode, id(bbox_array))
TclError: this isn't a Tk application
I'm trying to create a figure with widgets in it, so a workaround is available (just make a new figure), but I would like to know if the figure instance is totally useless after being closed.
Yes, closing the figure deletes it completely.