简体   繁体   中英

pandas apply list of function to data frame

Lets take boston data set available in the from sklearn.datasets import load_boston

boston = load_boston()
X = pd.DataFrame(boston["data"])

           0     1      2    3      4      5      6       7     8      9     10      11     12
0     0.00632  18.0   2.31  0.0  0.538  6.575   65.2  4.0900   1.0  296.0  15.3  396.90   4.98
1     0.02731   0.0   7.07  0.0  0.469  6.421   78.9  4.9671   2.0  242.0  17.8  396.90   9.14
2     0.02729   0.0   7.07  0.0  0.469  7.185   61.1  4.9671   2.0  242.0  17.8  392.83   4.03
3     0.03237   0.0   2.18  0.0  0.458  6.998   45.8  6.0622   3.0  222.0  18.7  394.63   2.94
4     0.06905   0.0   2.18  0.0  0.458  7.147   54.2  6.0622   3.0  222.0  18.7  396.90   5.33
5     0.02985   0.0   2.18  0.0  0.458  6.430   58.7  6.0622   3.0  222.0  18.7  394.12   5.21
6     0.08829  12.5   7.87  0.0  0.524  6.012   66.6  5.5605   5.0  311.0  15.2  395.60  12.43

I have built a machine learning model (RF) and have obtained all estimators in the model.

estimators = model.estimators_

You can think this has list of functions that takes row level data and return a value.

>> estimators = model.estimators_
>> estimators
[DecisionTreeRegressor(criterion='mse', max_depth=60, max_features=8,
           max_leaf_nodes=None, min_impurity_decrease=0.0,
           min_impurity_split=None, min_samples_leaf=5,
           min_samples_split=12, min_weight_fraction_leaf=0.0,
           presort=False, random_state=1838148368, splitter='best'), DecisionTreeRegressor(criterion='mse', max_depth=60, max_features=8,
           max_leaf_nodes=None, min_impurity_decrease=0.0,
           min_impurity_split=None, min_samples_leaf=5,
           min_samples_split=12, min_weight_fraction_leaf=0.0,
           presort=False, random_state=1754873550, splitter='best'), DecisionTreeRegressor(criterion='mse', max_depth=60, max_features=8,
           max_leaf_nodes=None, min_impurity_decrease=0.0,....]

I want each estimator/function in list to be apply to every row in the data frame.

If I don't convert the data to data frame boston['data'] returns a 2D Array. I can use two for loops to accomplish above. Assume X is a 2D array then I can do following

for x in range(len(X)):
    vals = []
    for estimator in model.estimators_:
        vals.append(estimator.predict(X[x])[0])

I don't want to use 2D array option because I want to keep the index information of the DataFrame for future operations.

In the latest version of pandas , df.agg should be able to do exactly this.

Unfortunately it appears to be broken for the current version when axis=1 : https://github.com/pandas-dev/pandas/issues/16679

Here's a hacky way around it:

X.T.agg(estimators).T

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM