When I run the code to predict the t-learner:
# Use the t-learner to predict the risk reduction for patients in the validation set
rr_t_val = t_learner.predict(X_val.drop(['TRTMT'], axis=1))
print(f"X_val num of patients {X_val.shape[0]}")
print(f"rr_t_val num of patient predictions {rr_t_val.shape[0]}")
I get the error:
NotFittedError: This RandomForestClassifier instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.
However, in the TLearner
class we implemented, the predict
method does not have a y
argument that we can use to call the classifier’s predict
.
Is there another way to fit the classifer?
Hi @wdduncan,
I think you might have missed running one of the cells, because there is a random forest model that is initialized before the cell that you are referring to. Trying going back and checking if all the cells were actually run.
HI @wdduncan
Yes, there is another way to fit the classifier. In the code you provided, it seems that the T-Learner class has not been fitted to the data yet, which is causing the “NotFittedError” when trying to make predictions. In order to fit the classifier, you need to call the fit
method on an instance of the T-Learner class and pass in the appropriate training data and labels.
Here is an example of how you can fit the classifier:
t_learner = TLearner()
t_learner.fit(X_train.drop([‘TRTMT’], axis=1), y_train)
Once the classifier is fitted, you should be able to make predictions using the predict method without any errors.
rr_t_val = t_learner.predict(X_val.drop([‘TRTMT’], axis=1))
Make sure that you are passing the correct data and labels to the fit
method and also check if the predict
method is implemented correctly in the TLearner class.
Hope so this answers your question
Regards
Muhammad John Abbas
@Muhammad_John_Abbas The signature for the TLearner predict method doesn’t include a parameter for the predictions (i.e., y
):
def predict(self, X)
@Samuel_Chazy I just reran the all the cells again and got the same error 
I wonder if I’m missing a cell in the notebook?
Hi @wdduncan,
Maybe you deleted the cell by mistake? Can you check if you have the below cell in your notebook:
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression(penalty=‘l2’,solver=‘lbfgs’, max_iter=10000).fit(X_dev, y_dev)
Hi @Samuel_Chazy ,
Yes. I have that cell. I completed the notebook and passed the autograder. Should I share the notebook with you? I think you can do this … right?
Hi @wdduncan,
Yes, you can pass me the notebook privately, but if you passed the auto-grader after completing the notebook, then everything is ok, right?
Yes. I passed the autograder. Right now I’m just curious as to why my notebook won’t run to completion. I’ll send my notebook id.
Hi @wdduncan,
In the following cell:
UNQ_C11 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
Can you replace all ‘1’ with True and all ‘0’ with False and run the notebook again. Let me know this works?
@Samuel_Chazy I just changed ‘1’ to True and ‘0’ to False, and I got the same error.
Hi @wdduncan,
I finally found the error in your notebook. You have to go back to UNQ_C10 exercise. You made a error there by predicting the X_val_hp instead of predicting the probabilities (preds = estimator.predict_proba(X_val_hp)[:,1]). I have corrected this and ran the notebook, and got no error afterwords. Hope that helps!
Nice catch! I’ve made the change you noted, and my output for UNQ_C10 now matches the expected output.
Weirdly, I am still getting the NotFittedError
error when I run the cell:
# Use the t-learner to predict the risk reduction for patients in the validation set
rr_t_val = t_learner.predict(X_val.drop(['TRTMT'], axis=1))
print(f"X_val num of patients {X_val.shape[0]}")
print(f"rr_t_val num of patient predictions {rr_t_val.shape[0]}")
Hi @wdduncan,
I had made the changes for UNQ_C10 on your notebook, and re-ran the whole notebook and it worked perfectly. Try re-running the notebook from the beginning.
Thanks for patience and working on this @Samuel_Chazy !
I just re-ran the notebook (with the changes made to UNQ_C10) and got the same error.
I have no idea why, but don’t worry about it. I completed the exercise, and I have moved on to the others exercises. So, I am fine if it remains a mystery 
Best,
Bill
Hi @wdduncan,
Ok, no worries.