Calculate F1 Score from Predicted and True Labels

Easy
Data Science Interview Prep

Implement a function to calculate the F1 score given predicted and true labels. The F1 score is a widely used metric in machine learning, combining precision and recall into a single measure. round your solution to the 3rd decimal place

Examples

Example 1:
Input: y_true = [1, 0, 1, 1, 0], y_pred = [1, 0, 0, 1, 1]
Output: 0.667
Explanation: The true positives, false positives, and false negatives are calculated from the given labels. Precision and recall are derived, and the F1 score is computed as their harmonic mean.

Starter Code

def calculate_f1_score(y_true, y_pred):
	"""
	Calculate the F1 score based on true and predicted labels.

	Args:
		y_true (list): True labels (ground truth).
		y_pred (list): Predicted labels.

	Returns:
		float: The F1 score rounded to three decimal places.
	"""
	# Your code here
	pass
	return round(f1,3)
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews