1
+ # https://deeplearningcourses.com/c/deep-learning-prerequisites-the-numpy-stack-in-python
2
+ # https://www.udemy.com/deep-learning-prerequisites-the-numpy-stack-in-python
3
+
4
+ from __future__ import print_function , division
5
+ from future .utils import iteritems
6
+ from builtins import range , input
7
+ # Note: you may need to update your version of future
8
+ # sudo pip install -U future
9
+
10
+ import numpy as np
11
+ import matplotlib .pyplot as plt
12
+
13
+ def get_spiral ():
14
+ # Idea: radius -> low...high
15
+ # (don't start at 0, otherwise points will be "mushed" at origin)
16
+ # angle = low...high proportional to radius
17
+ # [0, 2pi/6, 4pi/6, ..., 10pi/6] --> [pi/2, pi/3 + pi/2, ..., ]
18
+ # x = rcos(theta), y = rsin(theta) as usual
19
+
20
+ radius = np .linspace (1 , 10 , 100 )
21
+ thetas = np .empty ((6 , 100 ))
22
+ for i in range (6 ):
23
+ start_angle = np .pi * i / 3.0
24
+ end_angle = start_angle + np .pi / 2
25
+ points = np .linspace (start_angle , end_angle , 100 )
26
+ thetas [i ] = points
27
+
28
+ # convert into cartesian coordinates
29
+ x1 = np .empty ((6 , 100 ))
30
+ x2 = np .empty ((6 , 100 ))
31
+ for i in range (6 ):
32
+ x1 [i ] = radius * np .cos (thetas [i ])
33
+ x2 [i ] = radius * np .sin (thetas [i ])
34
+
35
+ # inputs
36
+ X = np .empty ((600 , 2 ))
37
+ X [:,0 ] = x1 .flatten ()
38
+ X [:,1 ] = x2 .flatten ()
39
+
40
+ # add noise
41
+ X += np .random .randn (600 , 2 )* 0.5
42
+
43
+ # targets
44
+ Y = np .array ([0 ]* 100 + [1 ]* 100 + [0 ]* 100 + [1 ]* 100 + [0 ]* 100 + [1 ]* 100 )
45
+ return X , Y
46
+
47
+
48
+ X , Y = get_spiral ()
49
+ plt .scatter (X [:,0 ], X [:,1 ], c = Y )
50
+ plt .show ()
0 commit comments