Issue
Working on jupyter notebooks, I came across this problem:
TypeError:unsupported format string passed to function .__format__
This is where the code starts:
def evaluate_preds(y_true, y_preds):
"""
Performs Evaluation comp on y_true labels and y_preds labels on a classification
"""
accuracy = accuracy_score(y_true,y_preds)
precision = precision_score(y_true,y_preds)
recall = recall_score(y_true,y_preds)
f1 = f1_score(y_true,y_preds)
metric_dict = {"accuracy": round(accuracy,2),
"precision": round(precision,2),
"recall": round(recall,2),
"f1": round(f1,2)}
print(f"Accuracy :{ accuracy_score*100:.2f}%")
print(f"Precision :{ precision_score:.2f}")
print(f"Recall :{ recall_score:.2f}")
print(f"F1 :{ f1_score:.2f}")
return metric_dict
This below code is being run on a different cell in jupyter notebook
from sklearn.ensemble import RandomForestClassifier
np.random.seed(42)
heart_disease_shuffled= heart_disease.sample(frac=1)
X= heart_disease_shuffled.drop("target",axis =1)
y = heart_disease_shuffled["target"]
train_split = round(0.7 * len(heart_disease_shuffled))
valid_split = round(train_split + 0.15 * len(heart_disease_shuffled))
X_train,y_train = X[:train_split], y[:train_split]
X_valid,y_valid = X[train_split:valid_split], y[train_split:valid_split]
X_test,y_test = X[valid_split:], y[:valid_split]
clf = RandomForestClassifier()
clf.fit(X_train,y_train)
# Make Baseline preds
y_preds = clf.predict(X_valid)
# Evaluate classifier on validation set
baseline_metrics = evaluate_preds(y_valid, y_preds)
baseline_metrics
How can I resolve it?
Tried changing the parameters and a bunch of other things but all of them popped up some errors like the one listed above
Solution
You are trying to print functions instead of the values they return. For instance, you assign
accuracy = accuracy_score(y_true,y_preds)
but then try to format the function, not the result
f"Accuracy :{ accuracy_score*100:.2f}%"
Instead, you should use the calculated value
f"Accuracy :{ accuracy*100:.2f}%"
But you've also created a dictionary with rounded values. You could use that dictionary with an f-string or use the .format
method instead
f"Accuracy :{ metric_dict['accuracy']:.2f}"
# -- or --
"Accuracy : {accuracy:.2f}".format(**metric_dict)
Answered By - tdelaney
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.