[Python] NotFittedError: Estimator not fitted, call `fit` before exploiting the model

Discussão em 'Python' iniciado por Stack, Abril 2, 2025.

  1. Stack

    Stack Membro Participativo

    I am running Python 3.5.2 on a Macbook OSX 10.2.1 (Sierra).

    While attempting to run some code for the Titanic Dataset from Kaggle, I keep getting the following error:


    NotFittedError Traceback (most recent call last) in () 6 7 # Make your prediction using the test set and print them. ----> 8 my_prediction = my_tree_one.predict(test_features) 9 print(my_prediction) 10

    /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sklearn/tree/tree.py in predict(self, X, check_input) 429 """ 430 --> 431 X = self._validate_X_predict(X, check_input) 432 proba = self.tree_.predict(X) 433 n_samples = X.shape[0]

    /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/sklearn/tree/tree.py in _validate_X_predict(self, X, check_input) 386 """Validate X whenever one tries to predict, apply, predict_proba""" 387 if self.tree_ is None: --> 388 raise NotFittedError("Estimator not fitted, " 389 "call fit before exploiting the model.") 390

    NotFittedError: Estimator not fitted, call fit before exploiting the model.
    ​

    The offending code seems to be this:

    # Impute the missing value with the median
    test.Fare[152] = test.Fare.median()

    # Extract the features from the test set: Pclass, Sex, Age, and Fare.
    test_features = test[["Pclass", "Sex", "Age", "Fare"]].values

    # Make your prediction using the test set and print them.
    my_prediction = my_tree_one.predict(test_features)
    print(my_prediction)

    # Create a data frame with two columns: PassengerId & Survived. Survived contains your predictions
    PassengerId =np.array(test["PassengerId"]).astype(int)
    my_solution = pd.DataFrame(my_prediction, PassengerId, columns = ["Survived"])
    print(my_solution)

    # Check that your data frame has 418 entries
    print(my_solution.shape)

    # Write your solution to a csv file with the name my_solution.csv
    my_solution.to_csv("my_solution_one.csv", index_label = ["PassengerId"])


    And here is a link to the rest of the code.

    Since I already have called the 'fit' function, I cannot understand this error message. Where am I going wrong? Thanks for your time.

    Edit: Turns out that the problem is inherited from the previous block of code.

    # Fit your first decision tree: my_tree_one
    my_tree_one = tree.DecisionTreeClassifier()
    my_tree_one = my_tree_one.fit(features_one, target)

    # Look at the importance and score of the included features
    print(my_tree_one.feature_importances_)
    print(my_tree_one.score(features_one, target))


    With the line: my_tree_one = my_tree_one.fit(features_one, target)

    generating the error:


    ValueError: Input contains NaN, infinity or a value too large for dtype('float32').
    ​

    Continue reading...

Compartilhe esta Página