Skip to content

Commit 46205c7

Browse files
committed
merged ciricli branch
2 parents 1abc290 + ec71b72 commit 46205c7

File tree

10 files changed

+49
-603
lines changed

10 files changed

+49
-603
lines changed

README.rst

Lines changed: 16 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
Sklearn-pandas
33
==============
44

5-
.. image:: https://circleci.com/gh/scikit-learn-contrib/sklearn-pandas.svg?style=svg
6-
:target: https://circleci.com/gh/scikit-learn-contrib/sklearn-pandas
5+
.. image:: https://circleci.com/gh/ragrawal/sklearn-pandas.svg?style=svg
6+
:target: https://circleci.com/gh/ragrawal/sklearn-pandas
77

88
This module provides a bridge between `Scikit-Learn <http://scikit-learn.org/stable>`__'s machine learning methods and `pandas <https://pandas.pydata.org>`__-style Data Frames.
99

@@ -231,8 +231,9 @@ Multiple transformers for the same column
231231
Multiple transformers can be applied to the same column specifying them
232232
in a list::
233233

234+
>>> from sklearn.impute import SimpleImputer
234235
>>> mapper3 = DataFrameMapper([
235-
... (['age'], [sklearn.preprocessing.Imputer(),
236+
... (['age'], [SimpleImputer(),
236237
... sklearn.preprocessing.StandardScaler()])])
237238
>>> data_3 = pd.DataFrame({'age': [1, np.nan, 3]})
238239
>>> mapper3.fit_transform(data_3)
@@ -302,7 +303,7 @@ into generator, and then use returned definition as ``features`` argument for ``
302303
... classes=[sklearn.preprocessing.LabelEncoder]
303304
... )
304305
>>> feature_def
305-
[('col1', [LabelEncoder()]), ('col2', [LabelEncoder()]), ('col3', [LabelEncoder()])]
306+
[('col1', [LabelEncoder()], {}), ('col2', [LabelEncoder()], {}), ('col3', [LabelEncoder()], {})]
306307
>>> mapper5 = DataFrameMapper(feature_def)
307308
>>> data5 = pd.DataFrame({
308309
... 'col1': ['yes', 'no', 'yes'],
@@ -318,22 +319,24 @@ If it is required to override some of transformer parameters, then a dict with '
318319
transformer parameters should be provided. For example, consider a dataset with missing values.
319320
Then the following code could be used to override default imputing strategy:
320321

322+
>>> from sklearn.impute import SimpleImputer
323+
>>> import numpy as np
321324
>>> feature_def = gen_features(
322325
... columns=[['col1'], ['col2'], ['col3']],
323-
... classes=[{'class': sklearn.preprocessing.Imputer, 'strategy': 'most_frequent'}]
326+
... classes=[{'class': SimpleImputer, 'strategy':'most_frequent'}]
324327
... )
325328
>>> mapper6 = DataFrameMapper(feature_def)
326329
>>> data6 = pd.DataFrame({
327-
... 'col1': [None, 1, 1, 2, 3],
328-
... 'col2': [True, False, None, None, True],
329-
... 'col3': [0, 0, 0, None, None]
330+
... 'col1': [np.nan, 1, 1, 2, 3],
331+
... 'col2': [True, False, np.nan, np.nan, True],
332+
... 'col3': [0, 0, 0, np.nan, np.nan]
330333
... })
331334
>>> mapper6.fit_transform(data6)
332-
array([[1., 1., 0.],
333-
[1., 0., 0.],
334-
[1., 1., 0.],
335-
[2., 1., 0.],
336-
[3., 1., 0.]])
335+
array([[1.0, True, 0.0],
336+
[1.0, False, 0.0],
337+
[1.0, True, 0.0],
338+
[2.0, True, 0.0],
339+
[3.0, True, 0.0]], dtype=object)
337340

338341

339342
Feature selection and other supervised transformations
@@ -366,59 +369,6 @@ A ``DataFrameMapper`` will return a dense feature array by default. Setting ``sp
366369

367370
The stacking of the sparse features is done without ever densifying them.
368371

369-
Cross-Validation
370-
****************
371-
372-
Now that we can combine features from pandas DataFrames, we may want to use cross-validation to see whether our model works. ``scikit-learn<0.16.0`` provided features for cross-validation, but they expect numpy data structures and won't work with ``DataFrameMapper``.
373-
374-
To get around this, sklearn-pandas provides a wrapper on sklearn's ``cross_val_score`` function which passes a pandas DataFrame to the estimator rather than a numpy array::
375-
376-
>>> pipe = sklearn.pipeline.Pipeline([
377-
... ('featurize', mapper),
378-
... ('lm', sklearn.linear_model.LinearRegression())])
379-
>>> np.round(cross_val_score(pipe, X=data.copy(), y=data.salary, scoring='r2'), 2)
380-
array([ -1.09, -5.3 , -15.38])
381-
382-
Sklearn-pandas' ``cross_val_score`` function provides exactly the same interface as sklearn's function of the same name.
383-
384-
``CategoricalImputer``
385-
**********************
386-
387-
Since the ``scikit-learn`` ``Imputer`` transformer currently only works with
388-
numbers, ``sklearn-pandas`` provides an equivalent helper transformer that
389-
works with strings, substituting null values with the most frequent value in
390-
that column. Alternatively, you can specify a fixed value to use.
391-
392-
Example: imputing with the mode:
393-
394-
>>> from sklearn_pandas import CategoricalImputer
395-
>>> data = np.array(['a', 'b', 'b', np.nan], dtype=object)
396-
>>> imputer = CategoricalImputer()
397-
>>> imputer.fit_transform(data)
398-
array(['a', 'b', 'b', 'b'], dtype=object)
399-
400-
Example: imputing with a fixed value:
401-
402-
>>> from sklearn_pandas import CategoricalImputer
403-
>>> data = np.array(['a', 'b', 'b', np.nan], dtype=object)
404-
>>> imputer = CategoricalImputer(strategy='constant', fill_value='a')
405-
>>> imputer.fit_transform(data)
406-
array(['a', 'b', 'b', 'a'], dtype=object)
407-
408-
409-
``FunctionTransformer``
410-
***********************
411-
412-
Often one wants to apply simple transformations to data such as ``np.log``. ``FunctionTransformer`` is a simple wrapper that takes any function and applies vectorization so that it can be used as a transformer.
413-
414-
Example:
415-
416-
>>> from sklearn_pandas import FunctionTransformer
417-
>>> array = np.array([10, 100])
418-
>>> transformer = FunctionTransformer(np.log10)
419-
420-
>>> transformer.fit_transform(array)
421-
array([1., 2.])
422372

423373
Changelog
424374
---------

setup.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ def run(self):
2929
raise SystemExit(errno)
3030

3131

32-
setup(name='sklearn-pandas',
32+
setup(name='sklearn-pandas2',
3333
version=__version__,
3434
description='Pandas integration with sklearn',
35-
maintainer='Israel Saeta Pérez',
36-
maintainer_email='israel.saeta@dukebody.com',
37-
url='https://github.com/paulgb/sklearn-pandas',
35+
maintainer='Ritesh Agrawal',
36+
maintainer_email='ragrawal@gmail.com',
37+
url='https://github.com/ragrawal/sklearn-pandas',
3838
packages=['sklearn_pandas'],
3939
keywords=['scikit', 'sklearn', 'pandas'],
4040
install_requires=[
41-
'scikit-learn>=0.15.0',
42-
'scipy>=0.14',
43-
'pandas>=0.11.0',
44-
'numpy>=1.6.1',
41+
'scikit-learn>=0.23.0',
42+
'scipy>=1.4.1',
43+
'pandas>=1.0.5',
44+
'numpy>=1.18.1',
4545
'tqdm>=4.46.0'],
4646
tests_require=['pytest', 'mock'],
4747
cmdclass={'test': PyTest},

sklearn_pandas/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22

33
from .dataframe_mapper import DataFrameMapper # NOQA
44
from .cross_validation import cross_val_score, GridSearchCV, RandomizedSearchCV # NOQA
5-
from .transformers import CategoricalImputer, FunctionTransformer # NOQA
65
from .features_generator import gen_features # NOQA

sklearn_pandas/categorical_imputer.py

Lines changed: 0 additions & 134 deletions
This file was deleted.

sklearn_pandas/dataframe_mapper.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,19 +267,22 @@ def get_names(self, columns, transformer, x, alias=None, prefix='',
267267
# Otherwise use the only estimator present
268268
else:
269269
names = _get_feature_names(transformer)
270+
270271
if names is not None and len(names) == num_cols:
271-
output = ['%s_%s' % (name, o) for o in names]
272-
# otherwise, return name concatenated with '_1', '_2', etc.
272+
output = [f"{name}_{o}" for o in names]
273+
# otherwise, return name concatenated with '_1', '_2', etc.
273274
else:
274275
output = [name + '_' + str(o) for o in range(num_cols)]
275276
else:
276277
output = [name]
277278

279+
if not prefix and not suffix:
280+
return output
281+
278282
prefix = prefix or ''
279283
suffix = suffix or ''
280284
return ['{}{}{}'.format(prefix, x, suffix) for x in output]
281285

282-
283286
def get_dtypes(self, extracted):
284287
dtypes_features = [self.get_dtype(ex) for ex in extracted]
285288
return [dtype for dtype_feature in dtypes_features
@@ -326,6 +329,7 @@ def _transform(self, X, y=None, do_fit=False):
326329
alias = options.get('alias')
327330
prefix = options.get('prefix')
328331
suffix = options.get('suffix')
332+
329333
self.transformed_names_ += self.get_names(
330334
columns, transformers, Xt, alias, prefix, suffix)
331335

0 commit comments

Comments
 (0)