(data, feature_name, categorical_feature, pandas_categorical)
| 315 | |
| 316 | |
| 317 | def _data_from_pandas(data, feature_name, categorical_feature, pandas_categorical): |
| 318 | if isinstance(data, DataFrame): |
| 319 | if len(data.shape) != 2 or data.shape[0] < 1: |
| 320 | raise ValueError('Input data must be 2 dimensional and non empty.') |
| 321 | if feature_name == 'auto' or feature_name is None: |
| 322 | data = data.rename(columns=str) |
| 323 | cat_cols = list(data.select_dtypes(include=['category']).columns) |
| 324 | cat_cols_not_ordered = [col for col in cat_cols if not data[col].cat.ordered] |
| 325 | if pandas_categorical is None: # train dataset |
| 326 | pandas_categorical = [list(data[col].cat.categories) for col in cat_cols] |
| 327 | else: |
| 328 | if len(cat_cols) != len(pandas_categorical): |
| 329 | raise ValueError('train and valid dataset categorical_feature do not match.') |
| 330 | for col, category in zip_(cat_cols, pandas_categorical): |
| 331 | if list(data[col].cat.categories) != list(category): |
| 332 | data[col] = data[col].cat.set_categories(category) |
| 333 | if len(cat_cols): # cat_cols is list |
| 334 | data = data.copy() # not alter origin DataFrame |
| 335 | data[cat_cols] = data[cat_cols].apply(lambda x: x.cat.codes).replace({-1: np.nan}) |
| 336 | if categorical_feature is not None: |
| 337 | if feature_name is None: |
| 338 | feature_name = list(data.columns) |
| 339 | if categorical_feature == 'auto': # use cat cols from DataFrame |
| 340 | categorical_feature = cat_cols_not_ordered |
| 341 | else: # use cat cols specified by user |
| 342 | categorical_feature = list(categorical_feature) |
| 343 | if feature_name == 'auto': |
| 344 | feature_name = list(data.columns) |
| 345 | bad_indices = _get_bad_pandas_dtypes(data.dtypes) |
| 346 | if bad_indices: |
| 347 | raise ValueError("DataFrame.dtypes for data must be int, float or bool.\n" |
| 348 | "Did not expect the data types in the following fields: " |
| 349 | + ', '.join(data.columns[bad_indices])) |
| 350 | data = data.values |
| 351 | if data.dtype != np.float32 and data.dtype != np.float64: |
| 352 | data = data.astype(np.float32) |
| 353 | else: |
| 354 | if feature_name == 'auto': |
| 355 | feature_name = None |
| 356 | if categorical_feature == 'auto': |
| 357 | categorical_feature = None |
| 358 | return data, feature_name, categorical_feature, pandas_categorical |
| 359 | |
| 360 | |
| 361 | def _label_from_pandas(label): |
no test coverage detected