Sundar0989 / XuniVerse

xverse (XuniVerse) is collection of transformers for feature engineering and feature selection
MIT License
116 stars 38 forks source link

UnboundLocalError: local variable 'bins_X_grouped' referenced before assignment #13

Open itsamejoshab opened 3 years ago

itsamejoshab commented 3 years ago

I can work around this problem by passing the entire dataframe in... along w/ the y, but I'm not sure if this is expected behavior. X implies passing dataframe without y included.

UnboundLocalError: local variable 'bins_X_grouped' referenced before assignment

df = pd.DataFrame({'x1': list(range(100)), 'x2': list(range(100)), 'x3': list(range(100)), 'y': list(range(100))})

df['y'] = np.where(df['x1']>50,1,0)

from xverse.transformer import MonotonicBinning clf = MonotonicBinning()

X = df[['x1','x2','x3']] y = df[['y']]

if not isinstance(X, pd.DataFrame): print("Not a dataframe") else: print("Is a DataFrame")

clf.fit(X, y)


UnboundLocalError Traceback (most recent call last)

in 17 print("Is a DataFrame") 18 ---> 19 clf.fit(X, y) ~/opt/anaconda3/lib/python3.7/site-packages/xverse/transformer/_binning.py in fit(self, X, y) 120 121 #apply the monotonic train function on dataset --> 122 fit_X.apply(lambda x: self.train(x, y), axis=0) 123 return self 124 ~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in apply(self, func, axis, raw, result_type, args, **kwds) 7766 kwds=kwds, 7767 ) -> 7768 return op.get_result() 7769 7770 def applymap(self, func, na_action: Optional[str] = None) -> DataFrame: ~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/apply.py in get_result(self) 183 return self.apply_raw() 184 --> 185 return self.apply_standard() 186 187 def apply_empty_result(self): ~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/apply.py in apply_standard(self) 274 275 def apply_standard(self): --> 276 results, res_index = self.apply_series_generator() 277 278 # wrap results ~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/apply.py in apply_series_generator(self) 288 for i, v in enumerate(series_gen): 289 # ignore SettingWithCopy here in case the user mutates --> 290 results[i] = self.f(v) 291 if isinstance(results[i], ABCSeries): 292 # If we have a view on v, we need to make a copy because ~/opt/anaconda3/lib/python3.7/site-packages/xverse/transformer/_binning.py in (x) 120 121 #apply the monotonic train function on dataset --> 122 fit_X.apply(lambda x: self.train(x, y), axis=0) 123 return self 124 ~/opt/anaconda3/lib/python3.7/site-packages/xverse/transformer/_binning.py in train(self, X, y) 172 We still want our code to produce bins. 173 """ --> 174 if len(bins_X_grouped) == 1: 175 bins = algos.quantile(X, np.linspace(0, 1, force_bins)) #creates a new binnning based on forced bins 176 if len(np.unique(bins)) == 2: UnboundLocalError: local variable 'bins_X_grouped' referenced before assignment
RalucaBala commented 3 years ago

Yes, I have the same issue. It's because of the if statement on line 169 here https://github.com/Sundar0989/XuniVerse/blob/master/xverse/transformer/_binning.py .

image

Should be firstly assigned...

Kailash-Thiyagarajan commented 3 years ago

I m having similar issues. If there is a fix, please share.

josephthomaa commented 3 years ago

Passing y as numpy array fixed it for me... y = df['target_varible'].to_numpy()

RamezQasim commented 2 years ago

This has worked for me: y_train.T.squeeze()

you can check the issue here https://github.com/Sundar0989/XuniVerse/issues/3

RishabhKash commented 2 years ago

I had similar issue. However after trying above solutions, I'm getting a new error -


AttributeError Traceback (most recent call last) Input In [11], in <cell line: 2>() 1 clf = MonotonicBinning() ----> 2 clf.fit(X,y)

File ~\Anaconda3\lib\site-packages\xverse\transformer_binning.py:122, in MonotonicBinning.fit(self, X, y) 118 raise ValueError("The input feature(s) should be numeric type. Some of the input features \ 119 has character values in it. Please use a encoder before performing monotonic operations.") 121 #apply the monotonic train function on dataset --> 122 fit_X.apply(lambda x: self.train(x, y), axis=0) 123 return self

File ~\Anaconda3\lib\site-packages\pandas\core\frame.py:8833, in DataFrame.apply(self, func, axis, raw, result_type, args, **kwargs) 8822 from pandas.core.apply import frame_apply 8824 op = frame_apply( 8825 self, 8826 func=func, (...) 8831 kwargs=kwargs, 8832 ) -> 8833 return op.apply().finalize(self, method="apply")

File ~\Anaconda3\lib\site-packages\pandas\core\apply.py:727, in FrameApply.apply(self) 724 elif self.raw: 725 return self.apply_raw() --> 727 return self.apply_standard()

File ~\Anaconda3\lib\site-packages\pandas\core\apply.py:851, in FrameApply.apply_standard(self) 850 def apply_standard(self): --> 851 results, res_index = self.apply_series_generator() 853 # wrap results 854 return self.wrap_results(results, res_index)

File ~\Anaconda3\lib\site-packages\pandas\core\apply.py:867, in FrameApply.apply_series_generator(self) 864 with option_context("mode.chained_assignment", None): 865 for i, v in enumerate(series_gen): 866 # ignore SettingWithCopy here in case the user mutates --> 867 results[i] = self.f(v) 868 if isinstance(results[i], ABCSeries): 869 # If we have a view on v, we need to make a copy because 870 # series_generator will swap out the underlying data 871 results[i] = results[i].copy(deep=False)

File ~\Anaconda3\lib\site-packages\xverse\transformer_binning.py:122, in MonotonicBinning.fit..(x) 118 raise ValueError("The input feature(s) should be numeric type. Some of the input features \ 119 has character values in it. Please use a encoder before performing monotonic operations.") 121 #apply the monotonic train function on dataset --> 122 fit_X.apply(lambda x: self.train(x, y), axis=0) 123 return self

File ~\Anaconda3\lib\site-packages\xverse\transformer_binning.py:170, in MonotonicBinning.train(self, X, y) 165 """ 166 Execute this block when monotonic relationship is not identified by spearman technique. 167 We still want our code to produce bins. 168 """ 169 if len(bins_X_grouped) == 1: --> 170 bins = algos.quantile(X, np.linspace(0, 1, force_bins)) #creates a new binnning based on forced bins 171 if len(np.unique(bins)) == 2: 172 bins = np.insert(bins, 0, 1)

AttributeError: module 'pandas.core.algorithms' has no attribute 'quantile

yogesh7025 commented 1 year ago

This solution worked for me

for the solution: y = df['target_varible'].to_numpy() the shape of the y is (row_number, 1)

whereas for the solution y = y_train.T.squeeze() the shape of the y is (row_number, )

it seems y as vector works.