Suppose I have an array containing scores for n
characters over t
days. I have some more arrays of the same shape (t, n)
containing different types of scores for the same n
characters. For concreteness, let’s say I have 5 scores, i.e. 5 arrays of shape (t, n)
. The individual scores (each normalized between 0 and 100) will change over time and my goal is to create a composite score using the area of the radar chart of the five scores for each character per day. (FYI: the area of the polygon is calculated by first assigning polar coordinates for each score (score_value, theta)
, converting these to (x, y)
co-ordinates and then using a standard formula.) The final output containing the areas should also be a (t, n)
array.
My initial idea was to combine the 5 scores into an array of shape (t, n, 5)
achieved using np.dstack
. Not sure if that’s a good idea. Here’s what I have so far:
import numpy as np
# Random data for demonstration
mat_1 = np.ones((20, 10))
mat_2 = np.ones((20, 10))
mat_3 = np.ones((20, 10))
mat_4 = np.ones((20, 10))
mat_5 = np.ones((20, 10))
matrix = [mat_1, mat_2, mat_3, mat_4]
my_list = np.dstack(matrix)
def radar_plot_area(data):
num_vars = len(data)
# Compute angle for each axis
angles = np.linspace(0, 2 * np.pi, num_vars, endpoint=False)
# Calculate area using shoelace formula
x = [r * np.cos(a) for r, a in zip(data, angles)]
y = [r * np.sin(a) for r, a in zip(data, angles)]
area = 0.5 * np.abs(np.sum(x * np.roll(y, -1)) - np.sum(x * np.roll(y, 1)))
return area
# Create output matrix
areas_matrix = np.zeros((20, 10))
for i in range(20):
for j in range(10):
areas_matrix[i][j] = radar_plot_area(my_list[i][j])
print(areas_matrix)
I can see it does output the correct values for the area at the end but I’m having trouble generalizing it to work with larger arrays. Ideally, I would prefer to work with arrays all the way throughout without having to explicitly refer to the indices too much, i.e. I want to have a function as follows:
def radar_plot_area(*scores: np.array): #the inputs are the 5 scores (which are arrays) I mentioned
...
return area #output is a (t, n) matrix containing the areas for each character per day
Is there a better way to achieve what I’m describing? Any suggestions would be much appreciated.
DirkDiggler890 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.