concept rotation in category data

appears as: The rotation, Rotation, rotation, rotations
Data Science Bookcamp: Five Python Projects

This is an excerpt from Manning's book Data Science Bookcamp: Five Python Projects.

The linear relationship between height and weight is clearly visible within the plot. Also, as expected, the height and weight axes are scaled differently. As a reminder, Matplotlib manipulates its 2D axes to make the final plot aesthetically pleasing. Normally, this is a good thing. However, we’ll soon be rotating the plot, in order to simplify our data. The rotation will shift the axes-scaling, making it difficult to compare. Consequently, we should equalize our axes to obtain a consistent visual output. Lets equalize the axes by calling plt.axis('equal'). Afterwards, we’ll regenerate the plot.

Figure 14.6. A plot of centered_data, before and after a rotation. The data has been rotated 90 degrees about the origin. It is now positioned closer to the x-axis.
fig14 6
Listing 14.12. Maximizing horizontal dispersion
def rotate(angle, data=centered_data): #1
    angle = np.radians(-angle)
    rotation_matrix = np.array([[cos(angle), -sin(angle)],
                                [sin(angle), cos(angle)]])
    return rotation_matrix @ data

angles = np.arange(1, 180, 0.1) #2
x_variances = [(rotate(angle)[0].var()) for angle in angles] #3

percent_x_variances = 100 * np.array(x_variances) / total_variance
optimal_index = np.argmax(percent_x_variances)
optimal_angle = angles[optimal_index] #4
plt.plot(angles, percent_x_variances)
plt.axvline(optimal_angle, c='k') #5
plt.xlabel('Angle (degrees)')
plt.ylabel('% x-axis coverage')
plt.show()

max_coverage = percent_x_variances[optimal_index]
max_x_var = x_variances[optimal_index]

print("The horizontal variance is maximized to approximately "
      f"{int(max_x_var)} after a {optimal_angle:.1f} degree rotation.")
print(f"That rotation distributes {max_coverage:.2f}% of the total "
       "variance onto the x-axis.")