# What is a 2D Symmetric Array Subclass in Python and how to create it?

I was going through my Data Science course material which contained the phenomenon of creating a 2D Symmetric Array Subclass in Python using NumPy, which didn’t make any sense to me. Can anyone brief me on what is this in maybe 2-3 lines, and also explain this phenomenon using a code snippet and tell me how is the concept implemented in the code?

1 Like

Hello @safiaa.02, a symmetric array has the same values across its diagonal axis, meaning the value at the `i-th` row and `j-th` column is equal to the value at the `j-th` row and `i-th` column. These arrays are useful in linear algebra, network analysis, and graph theory. Here is how you can implement this in code:

• In this code, a custom function is created that takes a NumPy array as input and returns a new array that is symmetric along its diagonal.
• It uses `np.triu()` to extract the upper triangular part of the input array. and `np.tril()` to extract the lower triangular part of the transposed array and `k=1` is specified which means that the diagonal elements are excluded.
• Lastly, the function adds the upper and lower triangular parts together to create a symmetric array. The resulting array has the same shape as the input array, and each element `Z[i,j]` is equal to `Z[j,i]`.

I hope this will be helpful to you.