Periodic Data with Machine Learning (Like Degree Angles -> 179 is 2 different from -179)

后端 未结 4 1403
执念已碎
执念已碎 2021-02-02 02:52

I\'m using Python for kernel density estimations and gaussian mixture models to rank likelihood of samples of multidimensional data. Every piece of data is an angle, and I\'m no

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-02 03:38

    As Tal Darom wrote in the comments, you can replace every periodic feature x with two features cos(x) and sin(x) after normalizing to radians. That solves the 359 ≈ 1 problem:

    >>> def fromdeg(d):
    ...     r = d * np.pi / 180.
    ...     return np.array([np.cos(r), np.sin(r)])
    ... 
    >>> np.linalg.norm(fromdeg(1) - fromdeg(359))
    0.03490481287456796
    >>> np.linalg.norm(fromdeg(1) - fromdeg(180))
    1.9999238461283426
    >>> np.linalg.norm(fromdeg(90) - fromdeg(270))
    2.0
    

    norm(a - b) is the good old Euclidean distance between vectors a and b. As you can verify using a simple plot, or by realizing that these (cos,sin) pairs are really coordinates on the unit circle, that this distance is maximal (and the dot product minimal) between two of these (cos,sin) vectors when the original angles differ by 180°.

提交回复
热议问题