Skip to content

Instantly share code, notes, and snippets.

@shellandbull
Last active May 19, 2019 14:24
Show Gist options
  • Save shellandbull/7d98f1f3efc3bc4cd4e0dea160bcdb72 to your computer and use it in GitHub Desktop.
Save shellandbull/7d98f1f3efc3bc4cd4e0dea160bcdb72 to your computer and use it in GitHub Desktop.
ml udacity error

given

from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import make_scorer
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import precision_score

def fit_model(X, y):
    """ Performs grid search over the 'max_depth' parameter for a 
        decision tree regressor trained on the input data [X, y]. """
    
    print('The scikit-learn version is {}.'.format(sklearn.__version__))
    
    # Create cross-validation sets from the training data
    print(X.shape[0])
    cv_sets = ShuffleSplit(X.shape[0], n_iter = 10, test_size = 0.20, random_state = 0)

    # TODO: Create a decision tree regressor object
    regressor = DecisionTreeRegressor()

    # TODO: Create a dictionary for the parameter 'max_depth' with a range from 1 to 10
    params = { 'max_depth': range(1, 11) }

    # TODO: Transform 'performance_metric' into a scoring function using 'make_scorer' 
    scoring_fnc = make_scorer(precision_score)

    # TODO: Create the grid search object
    grid = GridSearchCV(regressor, params, scoring_fnc, cv_sets)

    # Fit the grid search object to the data to compute the optimal model
    grid = grid.fit(X, y)

    # Return the optimal model after fitting the data
    return grid.best_estimator_
The scikit-learn version is 0.17.1.
391
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-26-192f7c286a58> in <module>()
      1 # Fit the training data to the model using grid search
----> 2 reg = fit_model(X_train, y_train)
      3 
      4 # Produce the value for 'max_depth'
      5 print "Parameter 'max_depth' is {} for the optimal model.".format(reg.get_params()['max_depth'])

<ipython-input-25-d72c80aed4d6> in fit_model(X, y)
     27 
     28     # Fit the grid search object to the data to compute the optimal model
---> 29     grid = grid.fit(X, y)
     30 
     31     # Return the optimal model after fitting the data

/Users/mariogintili/anaconda2/lib/python2.7/site-packages/sklearn/grid_search.pyc in fit(self, X, y)
    802 
    803         """
--> 804         return self._fit(X, y, ParameterGrid(self.param_grid))
    805 
    806 

/Users/mariogintili/anaconda2/lib/python2.7/site-packages/sklearn/grid_search.pyc in _fit(self, X, y, parameter_iterable)
    551                                     self.fit_params, return_parameters=True,
    552                                     error_score=self.error_score)
--> 553                 for parameters in parameter_iterable
    554                 for train, test in cv)
    555 

/Users/mariogintili/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.pyc in __call__(self, iterable)
    798             # was dispatched. In particular this covers the edge
    799             # case of Parallel used with an exhausted iterator.
--> 800             while self.dispatch_one_batch(iterator):
    801                 self._iterating = True
    802             else:

/Users/mariogintili/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.pyc in dispatch_one_batch(self, iterator)
    656                 return False
    657             else:
--> 658                 self._dispatch(tasks)
    659                 return True
    660 

/Users/mariogintili/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.pyc in _dispatch(self, batch)
    564 
    565         if self._pool is None:
--> 566             job = ImmediateComputeBatch(batch)
    567             self._jobs.append(job)
    568             self.n_dispatched_batches += 1

/Users/mariogintili/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.pyc in __init__(self, batch)
    178         # Don't delay the application, to avoid keeping the input
    179         # arguments in memory
--> 180         self.results = batch()
    181 
    182     def get(self):

/Users/mariogintili/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.pyc in __call__(self)
     70 
     71     def __call__(self):
---> 72         return [func(*args, **kwargs) for func, args, kwargs in self.items]
     73 
     74     def __len__(self):

/Users/mariogintili/anaconda2/lib/python2.7/site-packages/sklearn/cross_validation.pyc in _fit_and_score(estimator, X, y, scorer, train, test, verbose, parameters, fit_params, return_train_score, return_parameters, error_score)
   1515     fit_params = fit_params if fit_params is not None else {}
   1516     fit_params = dict([(k, _index_param_value(X, v, train))
-> 1517                       for k, v in fit_params.items()])
   1518 
   1519     if parameters is not None:

AttributeError: 'ShuffleSplit' object has no attribute 'items'
@helloimyames
Copy link

Thank you!!!

@princegaurav
Copy link

Thanks! Saved my day.

@theKevinmoyouknow
Copy link

Thanks!!!

@sahujayant
Copy link

Thanks !!

@hhtsaie
Copy link

hhtsaie commented Oct 26, 2017

Thanks!

@samuelgorla
Copy link

Thank you so much Pojda!

@quents
Copy link

quents commented Dec 10, 2017

Thank you very much! :)

@jimtipr
Copy link

jimtipr commented Dec 18, 2017

I can answer the why...

The first three parameters are the correct positional parameters of the function. The fourth parameter is not. Positionally, cv is the eighth parameter, so cv_sets without the 'cv=' is actually setting fit_params, which is the fourth positional parameter.

Therefore 'cv=cv_sets' is required.

@abdelmalek13
Copy link

thanks

@questionstorer
Copy link

Thanks a lot ! that saves a lot of time

@protikbasu
Copy link

Thank you so much. You are a damn life saver :)

@ubaid-qureshi
Copy link

thanks @pojda and @jimtipr

@seiyamiyaoka
Copy link

thx!

@carollei926
Copy link

carollei926 commented Mar 1, 2018

Thank you so much @pojda and @jimtipr! I spent about 20 minutes checking my codes, reloading the page... etc. ended up its because of the order of parameters is off. Good lesson learned.

@MartinSnow
Copy link

Thank you very much

Copy link

ghost commented May 5, 2018

Thank you, i spent 2 hours for this!

@bahcetepe
Copy link

bahcetepe commented May 10, 2018

Thank you @pojda

@zhang261007
Copy link

Thank you so much !

@mefryar
Copy link

mefryar commented May 27, 2018

Thanks @pojda for the solution and @jimtipr for the explanation!

@Joseph516
Copy link

Thank a lot.But why can't I just type cv? It's still wrong.
original:
grid = GridSearchCV(regressor, params, scoring_fnc, cv_sets)
Still Wrong?
grid = GridSearchCV(regressor, params, scoring_fnc, cv)

@osafaRmldspyaws
Copy link

Thanks @pojda and @jimtipr

@punitaojha
Copy link

Thanks @pojda you are the real rockstar .

@pinks1995
Copy link

Thanks @pojda !

@Christian-Huynh
Copy link

amazing, thanks so much!

@jean-colombel
Copy link

Well thank you good sir ! Made my day !

@YounusShalaby
Copy link

thanks a lot

@Zhengqi-Li
Copy link

Thanks!

@WANGXUANDING
Copy link

thanks a lot dude @pojda

@NiranjanPS
Copy link

Thanks.

@nivedithaHN
Copy link

Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment