-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_gen.py
78 lines (52 loc) · 1.85 KB
/
dataset_gen.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 numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt
import random
from shapely.geometry.polygon import Polygon
from shapely.geometry.point import Point
def get_random_point_in_polygon(poly, np):
random.seed(12)
(minx, miny, maxx, maxy) = poly.bounds
num_points = 0
all_points = []
while True:
p = Point(random.uniform(minx, maxx), random.uniform(miny, maxy))
if poly.contains(p) and (p.coords not in all_points):
num_points += 1
all_points.append(p.coords)
if num_points == np:
break
return all_points
def main():
random.seed(12)
# concentric circles
X,y = datasets.make_circles(n_samples=40000, shuffle=False, noise=0.04, random_state=50, factor=0.5)
X = X*35
p1 = Polygon([(45, 45), (85, 85), (125, 45), (85, 5)])
p2 = Polygon([(125, 45), (145, 65), (165, 45), (145, 25)])
point_in_poly1 = get_random_point_in_polygon(p1,30000)
point_in_poly2 = get_random_point_in_polygon(p2,30000)
all_points1 = []
for point in point_in_poly1:
all_points1.append(np.array([point[0][0],point[0][1]]))
all_points1 = np.array(all_points1)
all_points2 = []
for point in point_in_poly2:
all_points2.append(np.array([point[0][0],point[0][1]]))
all_points2 = np.array(all_points2)
plt.scatter(X[:,0], X[:,1], c = 'blue', s = 0.01)
plt.scatter(all_points1[:,0], all_points1[:,1], c = 'blue', s = 0.01)
plt.scatter(all_points2[:,0], all_points2[:,1], c = 'blue', s = 0.01)
plt.show()
with open('new_dataset.txt','w') as fw:
for x in X:
fw.write(str(x[0]) + ' ' + str(x[1]) + '\n')
for x in all_points1:
fw.write(str(x[0]) + ' ' + str(x[1]) + '\n')
for count, x in enumerate(all_points2):
fw.write(str(x[0]) + ' ' + str(x[1]))
if count != len(all_points2)-1:
fw.write('\n')
if __name__ == '__main__':
random.seed(12)
main()