Calculate Mean by Row or Column

Easy
Machine Learning

Write a Python function that calculates the mean of a matrix either by row or by column, based on a given mode. The function should take a matrix (list of lists) and a mode ('row' or 'column') as input and return a list of means according to the specified mode.

Examples

Example 1:
Input: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], mode = 'column'
Output: [4.0, 5.0, 6.0]
Explanation: Calculating the mean of each column results in [(1+4+7)/3, (2+5+8)/3, (3+6+9)/3].

Starter Code

def calculate_matrix_mean(matrix: list[list[float]], mode: str) -> list[float]:
	return means
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews