如何将参数传递给scikit中只有一部分管道对象学习?

时间:2016-02-25 16:15:14

标签: python pandas scikit-learn pipeline

我需要将参数import React from 'react'; var LoginDataModel = require ('./loginDataModel'); var LoginPage = React.createClass({ getInitialState: function() { console.log(LoginDataModel); return LoginDataModel }, render: function() { console.log(LoginDataModel.getUsername); return ( <div className="container view-header"> <LoginForm/> </div> ); } }); var LoginForm = React.createClass({ render: function() { return ( <form className="form-signin" name="loginForms"> <UsernameInput/> <PasswordInput/> <SubmitButton/> </form> ); } }) var UsernameInput = React.createClass({ getInitialState: function() { return {userValue: ''}; }, handleChange: function(event) { this.setState({userValue: event.target.value}); }, render: function() { return ( <div className="form-group"> <label htmlFor="inputEmail">Username</label> <input autofocus className="form-control" id="inputEmail" placeholder="Username" name="loginUsername" required type="text" ref="usernameInput" value={this.state.userValue} onChange={this.handleChange}></input> </div> ); } }); var PasswordInput = React.createClass({ getInitialState: function() { return {passValue: ''}; }, handleChange: function(event) { this.setState({passValue: event.target.value}); }, render: function() { return ( <div className="form-group"> <label htmlFor="inputPassword">Password</label> <input autofocus className="form-control" id="inputPassword" placeholder="Password" name="loginPassword" required type="text" ref="passwordInput" value={this.state.passValue} onChange={this.handleChange}></input> </div> ); } }); var SubmitButton = React.createClass({ contextTypes: { userValue: React.PropTypes.any, passValue: React.PropTypes.any }, render: function () { return ( <div> Active Item: {this.context.userValue} </div> ); } }) export default() => <LoginPage/>; 传递给我的sample_weight,如下所示:

RandomForestClassifier

以上代码完美无缺。然后,我尝试在管道对象中执行此操作,使用管道对象而不是仅使用随机林:

X = np.array([[2.0, 2.0, 1.0, 0.0, 1.0, 3.0, 3.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 5.0, 3.0,
        2.0, '0'],
       [15.0, 2.0, 5.0, 5.0, 0.466666666667, 4.0, 3.0, 2.0, 0.0, 0.0, 0.0,
        0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
        7.0, 14.0, 2.0, '0'],
       [3.0, 4.0, 3.0, 1.0, 1.33333333333, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0,
        0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
        9.0, 8.0, 2.0, '0'],
       [3.0, 2.0, 3.0, 0.0, 0.666666666667, 2.0, 2.0, 1.0, 0.0, 0.0, 0.0,
        0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
        5.0, 3.0, 1.0, '0']], dtype=object)

y = np.array([ 0.,  0.,  1.,  0.])

m = sklearn.ensemble.RandomForestClassifier(
        random_state=0, 
        oob_score=True, 
        n_estimators=100,
        min_samples_leaf=5, 
        max_depth=10)

m.fit(X, y, sample_weight=np.array([3,4,2,3]))

现在,这会在m = sklearn.pipeline.Pipeline([ ('feature_selection', sklearn.feature_selection.SelectKBest( score_func=sklearn.feature_selection.f_regression, k=25)), ('model', sklearn.ensemble.RandomForestClassifier( random_state=0, oob_score=True, n_estimators=500, min_samples_leaf=5, max_depth=10))]) m.fit(X, y, sample_weight=np.array([3,4,2,3])) 方法中使用“fit”。

ValueError: need more than 1 value to unpack

我正在使用 ValueError Traceback (most recent call last) <ipython-input-212-c4299f5b3008> in <module>() 25 max_depth=10))]) 26 ---> 27 m.fit(X, y, sample_weights=np.array([3,4,2,3])) /usr/local/lib/python2.7/dist-packages/sklearn/pipeline.pyc in fit(self, X, y, **fit_params) 128 data, then fit the transformed data using the final estimator. 129 """ --> 130 Xt, fit_params = self._pre_transform(X, y, **fit_params) 131 self.steps[-1][-1].fit(Xt, y, **fit_params) 132 return self /usr/local/lib/python2.7/dist-packages/sklearn/pipeline.pyc in _pre_transform(self, X, y, **fit_params) 113 fit_params_steps = dict((step, {}) for step, _ in self.steps) 114 for pname, pval in six.iteritems(fit_params): --> 115 step, param = pname.split('__', 1) 116 fit_params_steps[step][param] = pval 117 Xt = X ValueError: need more than 1 value to unpack 版本sklearn 我认为问题在于管道中的0.14步骤不会为sample_weights接受参数。如何运行“F selection”,如何将此参数传递给管道中的一个步骤?感谢。

3 个答案:

答案 0 :(得分:15)

From the documentation:

  

管道的目的是组装几个可以的步骤   在设置不同参数的同时进行交叉验证。为了这,   它可以使用它们的名称设置各个步骤的参数   和参数名称用'__'分隔,如下例所示。

因此,您只需在要传递到model__步骤的任何合适参数kwargs前插入'model'

m.fit(X, y, model__sample_weight=np.array([3,4,2,3]))

答案 1 :(得分:1)

您还可以使用方法set_params并在步骤名称前添加

m = sklearn.pipeline.Pipeline([
    ('feature_selection', sklearn.feature_selection.SelectKBest(
        score_func=sklearn.feature_selection.f_regression,
        k=25)),
    ('model', sklearn.ensemble.RandomForestClassifier(
        random_state=0, 
        oob_score=True, 
        n_estimators=500,
        min_samples_leaf=5, 
        max_depth=10))])
m.set_params(model__sample_weight=np.array([3,4,2,3]))

答案 2 :(得分:0)

希望我可以在上面的@rovyko帖子上留下评论,而不是单独的答案,但是我没有足够的stackoverflow信誉来发表评论,所以这里是。

您不能使用:

import UIKit import GoogleMaps import GooglePlaces class ViewController: UIViewController, UISearchBarDelegate { @IBOutlet weak var mapContainer: UIView! var mapView: GMSMapView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.mapView = GMSMapView(frame: self.mapContainer.frame) self.view.addSubview(self.mapView) } // Code from https://developers.google.com/places/ios-sdk/autocomplete#add_an_autocomplete_ui_control @IBAction func searchByAddress(_ sender: Any) { // Present the Autocomplete view controller when the button is pressed. let autocompleteController = GMSAutocompleteViewController() autocompleteController.delegate = self // Specify the place data types to return. let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.name.rawValue) | UInt(GMSPlaceField.placeID.rawValue))! autocompleteController.placeFields = fields // Display the autocomplete view controller. present(autocompleteController, animated: true, completion: nil) } } extension ViewController: GMSAutocompleteViewControllerDelegate { // Handle the user's selection. func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) { let position: CLLocationCoordinate2D = place.coordinate let camera = GMSCameraPosition.camera(withLatitude: position.latitude, longitude: position.longitude, zoom: 10) let newMapView = GMSMapView.map(withFrame: self.mapContainer.frame, camera: camera) self.mapView = newMapView self.view.addSubview(newMapView) let marker = GMSMarker() marker.position = position marker.title = place.name marker.map = self.mapView viewController.dismiss(animated: true, completion: nil) } func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) { // TODO: handle the error. print("Error: ", error.localizedDescription) } // User canceled the operation. func wasCancelled(_ viewController: GMSAutocompleteViewController) { viewController.dismiss(animated: true, completion: nil) } // Turn the network activity indicator on and off again. func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) { UIApplication.shared.isNetworkActivityIndicatorVisible = true } func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) { UIApplication.shared.isNetworkActivityIndicatorVisible = false } }

设置Pipeline.set_params(model__sample_weight=np.array([3,4,2,3])方法的参数。代码(here中所指示的RandomForestClassifier.fit()仅用于管道中各个步骤的初始化参数。 Pipeline.set_params()没有名为RandomForestClassifier的初始化参数(请参见其sample_weight方法here)。 __init__()实际上是sample_weight的{​​{1}}方法的输入参数,因此只能由正确标记的答案中表示的方法@ali_m设置,即@p_p

RandomForestClassifier