-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbounced_data_noise.py
128 lines (106 loc) · 5.09 KB
/
bounced_data_noise.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import pandas as pd
import numpy as np
from pydbgen import pydbgen
import math
import csv
import datetime
import sys
size = sys.argv[1]
size = int(size)
noise = sys.argv[2]
noise = int(noise)
class DataGenerator:
def __init__(self, datasetSize=1):
self.pyDb = pydbgen.pydb()
self.datasetSize = datasetSize
self.cityname = pd.read_csv('other_data/city_master.csv')['CityName'].to_numpy()
self.username = pd.read_csv('other_data/user_master.csv')['UserName'].to_numpy()
# Generate array of bounced-at levels, select b/w 0 & 1
def getBouncedAt(self, size, levels=[0, 1, None], prob=[(100-noise)/200, (100-noise)/200, noise/100]):
return (np.random.choice(levels, size=size, p=prob))
# Generate array of usernames from username dataset
def getUserName(self, size):
return (np.random.choice(self.username, size=size, replace=True))
# Generate array of random timestamps
def getTimeStamp(self, size, low=0, high=365):
randomInt = np.random.uniform(low, high, size).astype(int)
randomHr = np.random.randint(0, 24, size)
randomMin = np.random.randint(0, 60, size)
randomSec = np.random.randint(0, 60, size)
randomHr = pd.DataFrame(randomHr)
randomHr.rename(columns={0: 'I'}, inplace=True)
randomHr['I'] = randomHr['I'].astype(str)
randomHr.loc[(randomHr['I'].str.len() == 1),
'I'] = '0' + randomHr['I'].astype(str)
randomMin = pd.DataFrame(randomMin)
randomMin.rename(columns={0: 'I'}, inplace=True)
randomMin['I'] = randomMin['I'].astype(str)
randomMin.loc[(randomMin['I'].str.len() == 1),
'I'] = '0' + randomMin['I'].astype(str)
randomSec = pd.DataFrame(randomSec)
randomSec.rename(columns={0: 'I'}, inplace=True)
randomSec['I'] = randomSec['I'].astype(str)
randomSec.loc[(randomSec['I'].str.len() == 1),
'I'] = '0' + randomSec['I'].astype(str)
randomTime = randomHr['I'] + ':' + \
randomMin['I'] + ':' + randomSec['I']
randomTime = randomTime.to_numpy()
timeArr = np.datetime64('today') - randomInt
timeArr = np.datetime_as_string(timeArr)
arr_list = [timeArr, randomTime]
final_time_arr = np.apply_along_axis(' '.join, 0, arr_list)
return (final_time_arr)
# Generate array of random device ID's
def getDeviceID(self, size):
return (np.random.randint(1000000, 10000000, size=size))
# Generate array of device OS
def getFlavor(self, size, levels=['Android', 'IOS', None], prob=[(100-noise)/200, (100-noise)/200, noise/100]):
return (np.random.choice(levels, size=size, p=prob))
# Generate array with int values specifying number of people
def getNumOfPeople(self, size):
return (np.random.randint(1, 10, size=size))
# Generate array of Checkin Dates
def getCheckIn(self, size, low=0, high=120, prob = [(100-noise)/100, noise/100]):
randomInt = np.random.uniform(low, high, size).astype(int)
randomDate = np.datetime64('today') + randomInt
checkInDateArr = []
for i in range(size):
r = np.random.choice(randomDate)
checkInDateArr.append(np.random.choice([r,None],p =prob))
return (checkInDateArr)
# Generate complete dataset with properties containing array of described values
def genDataset(self):
size = self.datasetSize
data = {
'BouncedAt': self.getBouncedAt(size),
'UserName': self.getUserName(size),
'TimeStamp': self.getTimeStamp(size),
'DeviceID': self.getDeviceID(size),
'Flavor': self.getFlavor(size),
'CheckIn': self.getCheckIn(size),
'NumOfPeople': self.getNumOfPeople(size)
}
return (pd.DataFrame(data))
myDataGen = DataGenerator(size)
myDataFrame = myDataGen.genDataset()
# myDataFrame.insert(loc = 3, column = 'CityID', value = 0)
CityD = pd.read_csv("other_data/city_master.csv", usecols = ["CityName","CityId"]).to_numpy()
citydetails = CityD[np.random.choice(CityD.shape[0], size, replace = True)]
d = pd.DataFrame(citydetails, columns=['CityName', 'CityId'])
myDataFrame = pd.concat([myDataFrame, d], axis=1)
# insert NumOfRooms to the dataframe
room_calculator = pd.DataFrame(data={'People': myDataFrame['NumOfPeople']})
room_calculator['People_2'] = (
room_calculator['People']/2).apply(np.ceil).astype(int)
room_calculator['range'] = [
list(range(i, j+1)) for i, j in room_calculator[['People_2', 'People']].values]
room_calculator.drop(['People_2', 'People'], axis=1)
room_calculator['Rooms'] = room_calculator['range'].apply(np.random.choice)
myDataFrame['NumofRooms'] = room_calculator['Rooms']
del room_calculator
# insert Checkout to the dataframe
randomInt = np.random.randint(1, 30, size=size)
randomtime = [np.timedelta64(z,'D') for z in randomInt]
myDataFrame['CheckOut'] = pd.to_datetime(myDataFrame['CheckIn']) + pd.to_timedelta(randomtime)
filename = 'bounced_data_noise/bounced_data_%s.csv' % datetime.datetime.now().strftime('%s')
myDataFrame.to_csv(filename, index=False)