Files
python-VM/random_sampling/test.py

65 lines
1.4 KiB
Python

import numpy as np
import pandas as pd
import itertools
import seaborn as sns
import matplotlib.pyplot as plt
# np.random.seed(127)
low = 1000
high = 1200
repeat = 120
anom_perc = 0.2
sns.set()
sns.set_style("darkgrid")
def generator(low: float, high: float, repeat: int):
for i in range(repeat):
num = np.random.uniform(low, high)
curve = float(i) ** 2
num = num + curve
if num > high * 1.2:
low = num - 10
high = num + 5
elif num < low:
low = num - 5
high = num + 10
else:
pass
yield num
mygenerator = generator(low, high, repeat)
df = pd.DataFrame([])
for i, j in zip(mygenerator, np.arange(repeat)):
df = df.append([pd.DataFrame({'time': j, 'bets': i, },
index=[0])]).round(0)
def create_anom(dataframe, number):
for _ in itertools.repeat(None, number):
rows = dataframe.shape[0]
anomaly = f"{float(dataframe.iloc[-1]['bets']) * (1 - anom_perc)}"
anomaly = float(anomaly)
dataframe = dataframe.append([pd.DataFrame(
{'time': rows, 'bets': anomaly},
index=[0])])
return dataframe
df_anom = create_anom(df, 3)
f, axes = plt.subplots(2, 1)
for ax in axes:
ax.set_ylim([low * 0.5, high * 1.5])
sns.lineplot(x='time', y='bets', data=df, ax=axes[0])
sns.lineplot(x='time', y='bets', data=df_anom, ax=axes[1])
plt.show()