Please refer to the original post.
The problem was the
np.radians(angle). I corrected that.
Edit: The problem was not the radian conversion but the different scales of the x- and y-axis.
45 degrees rotation was simply not enough.
Let's use a modified version of the code you posted:
def rotate_vector(data, angle):
# make rotation matrix
theta = np.radians(angle)
co = np.cos(theta)
si = np.sin(theta)
rotation_matrix = np.array(((co, -si), (si, co)))
# rotate data vector
rotated_vector = data.dot(rotation_matrix)
# return index of elbow
Now, we define a square as test-data:
data = np.array([
[1, 0], [2, 0], [3, 0], [4, 0], [5, 0],
[0, 0], [0, 1], [0, 2], [0, 3], [0, 4],
[5, 1], [5, 2], [5, 3], [5, 4], [5, 5],
[0, 5], [1, 5], [2, 5], [3, 5], [4, 5],
plt.scatter(data[:, 0], data[:, 1])
Plotting the data gives us the following figure:
Now, we rotate that vector using the function from above:
rotated_data = rotate_vector(data, 45)
plt.scatter(rotated_data[:, 0], rotated_data[:, 1])
If we plot the rotated data, we get the following figure:
So it does work. However, if we want to rotate the graph that you posted to find the minimum, we need to remember that the scales of the two axes are different. That means the graph may look like a 45° rotation would be enough while actually, it is not.
I corrected the original post again. Also, I added a method to find an approximate rotation angle to account for the different axis scales.