Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import pandas as pd
- def rand_times(n):
- """Generate n rows of random 24-hour times (seconds past midnight)"""
- rand_seconds = np.random.randint(0, 24*60*60, n)
- return pd.DataFrame(data=dict(seconds=rand_seconds))
- # Create raw data
- n_rows = 1000
- df = rand_times(n_rows)
- # sort for the sake of graphing
- df = df.sort_values('seconds').reset_index(drop=True)
- df.seconds.plot()
- # Sine & cosine transform
- seconds_in_day = 24*60*60
- df['sin_time'] = np.sin(2*np.pi*df.seconds/seconds_in_day)
- df['cos_time'] = np.cos(2*np.pi*df.seconds/seconds_in_day)
- df.drop('seconds', axis=1, inplace=True)
- # Combine the two yield a cyclical pattern
- df.sin_time.plot()
- df.cos_time.plot()
- df.sample(30).plot.scatter('sin_time','cos_time').set_aspect('equal')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement