-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatagen.py
78 lines (65 loc) · 2.27 KB
/
datagen.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
import random
import pandas as pd
import numpy as np
def gen_data(dimension, x_lower_range, x_higher_range, m_lower_range, m_higher_range, epoch):
data = []
slopes = [] # incase if needes
# y = c + m1x1 + m2x2 + .....
# 2d data generation is handled differently
if dimension == 2:
x = []
for i in range(0, epoch):
xTemp = random.uniform(x_lower_range, x_higher_range)
x.append(xTemp)
x = np.array(x)
x = np.sort(x, axis=0)
x.sort()
y = []
m = np.random.randint(m_lower_range, m_higher_range,
size=dimension, dtype=int)
for i in range(0, epoch):
yTemp = np.sum(x[i] * m) + random.randint(-5, 5)
y.append(yTemp)
# adding heading to the csv
header_data = {}
header_data = {"Y": y, "X0": x}
# writing data to csv file
df = pd.DataFrame(header_data)
df.to_csv('data.csv', header=True, index=False)
else:
for i in range(epoch):
return_li = []
x = []
m = [] # m0 is the constant
for i in range(dimension - 1):
x_item = random.uniform(x_lower_range, x_higher_range)
x.append(x_item)
for i in range(dimension):
m_item = random.uniform(m_lower_range, m_higher_range)
m.append(m_item)
y = m[0]
for i in range(0, len(x)-1):
y += m[i+1] * x[i]
return_li.append(y)
for i in x:
return_li.append(i)
data.append(return_li)
slopes.append(m)
# adding heading to the csv
header_data = {}
Y = []
for i in data:
Y.append(i[0])
header_data.update({'Y': Y})
for i in range(1, len(data[0])):
X = []
for j in data:
X.append(j[i])
header_data.update({f"X{i-1}": X})
Y = []
for i in data:
Y.append(i[0])
header_data.update({'Y': Y})
# writing data to csv file
df = pd.DataFrame(header_data)
df.to_csv('data.csv', header=True, index=False)