-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
Copy pathtsne_xor.py
36 lines (27 loc) · 971 Bytes
/
tsne_xor.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
# https://deeplearningcourses.com/c/unsupervised-deep-learning-in-python
# https://www.udemy.com/unsupervised-deep-learning-in-python
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future
import numpy as np
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
def get_xor_data():
X1 = np.random.random((100, 2))
X2 = np.random.random((100, 2)) - np.array([1, 1])
X3 = np.random.random((100, 2)) - np.array([1, 0])
X4 = np.random.random((100, 2)) - np.array([0, 1])
X = np.vstack((X1, X2, X3, X4))
Y = np.array([0]*200 + [1]*200)
return X, Y
def main():
X, Y = get_xor_data()
plt.scatter(X[:,0], X[:,1], s=100, c=Y, alpha=0.5)
plt.show()
tsne = TSNE(perplexity=40)
Z = tsne.fit_transform(X)
plt.scatter(Z[:,0], Z[:,1], s=100, c=Y, alpha=0.5)
plt.show()
if __name__ == '__main__':
main()