@@ -89,20 +89,22 @@ def fit(
8989 # early_stopping_rounds=self.early_stopping_rounds,
9090 verbose = self .verbosity ,
9191 )
92- # X.columns = X.columns.str.replace('[^a-zA-Z0-9_]', '', regex=True)
93- # Change columns names ([LightGBM] Do not support special JSON characters in feature name.)
94- new_names = {col : re .sub (r"[^A-Za-z0-9_]+" , "" , col ) for col in X .columns }
95- new_n_list = list (new_names .values ())
96- # [LightGBM] Feature appears more than one time.
97- new_names = {
98- col : f"{ new_col } _{ i } " if new_col in new_n_list [:i ] else new_col
99- for i , (col , new_col ) in enumerate (new_names .items ())
100- }
101- X = X .rename (columns = new_names )
92+
93+ X_fit = X
94+ if isinstance (X , pd .DataFrame ):
95+ # Change columns names ([LightGBM] Do not support special JSON characters in feature name.)
96+ new_names = {col : re .sub (r"[^A-Za-z0-9_]+" , "" , col ) for col in X .columns }
97+ new_n_list = list (new_names .values ())
98+ # [LightGBM] Feature appears more than one time.
99+ new_names = {
100+ col : f"{ new_col } _{ i } " if new_col in new_n_list [:i ] else new_col
101+ for i , (col , new_col ) in enumerate (new_names .items ())
102+ }
103+ X_fit = X .rename (columns = new_names )
102104
103105 y = np .ravel (y )
104106
105- self .model .fit (X , y )
107+ self .model .fit (X_fit , y )
106108 if self .objective == "binary" :
107109 self .classes_ = np .unique (y )
108110 return self
@@ -127,18 +129,20 @@ def predict(self, X: pd.DataFrame) -> np.ndarray:
127129 "Model has not been fitted yet. Call 'fit' before 'predict'."
128130 )
129131
130- # Change columns names ([LightGBM] Do not support special JSON characters in feature name.)
131- new_names = {
132- col : re .sub (r"[^A-Za-z0-9_]+" , "" , col ) for col in X .columns
133- }
134- new_n_list = list (new_names .values ())
135- # [LightGBM] Feature appears more than one time.
136- new_names = {
137- col : f"{ new_col } _{ i } " if new_col in new_n_list [:i ] else new_col
138- for i , (col , new_col ) in enumerate (new_names .items ())
139- }
140- X = X .rename (columns = new_names )
141- return self .model .predict (X )
132+ X_pred = X
133+ if isinstance (X , pd .DataFrame ):
134+ # Change columns names ([LightGBM] Do not support special JSON characters in feature name.)
135+ new_names = {
136+ col : re .sub (r"[^A-Za-z0-9_]+" , "" , col ) for col in X .columns
137+ }
138+ new_n_list = list (new_names .values ())
139+ # [LightGBM] Feature appears more than one time.
140+ new_names = {
141+ col : f"{ new_col } _{ i } " if new_col in new_n_list [:i ] else new_col
142+ for i , (col , new_col ) in enumerate (new_names .items ())
143+ }
144+ X_pred = X .rename (columns = new_names )
145+ return self .model .predict (X_pred )
142146
143147 def score (self , X : pd .DataFrame , y : Union [pd .Series , np .ndarray ]) -> float :
144148 """Returns the mean accuracy on the given test data and labels.
0 commit comments