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

£可爱£侵袭症+ 提交于 2019-12-02 21:11:45

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°.

An alternative to the methods already posted would be to model the angular variables using the Von Mises distribution.

This distribution appears to be supported by scipy so shouldn't be too difficult to fit into a mixture model.

Another simpler way could be to use time as angle measurements than degree measurements (not DMS though). Since many analytics software features time as a datatype, you can use its periodicity to do your job.

But remember, you need to scale 360 degrees to 24 hours.

You need to use the mod function. In straight python this would be (ang2-ang1)%360 but with scipy it looks like you can use numpy.mod() - see the documentation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!