-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1st model.py
59 lines (53 loc) · 1.79 KB
/
1st model.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
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 14 19:27:15 2020
@author: tonim
"""
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
from sklearn.datasets.samples_generator import make_circles
X, y = make_circles(n_samples=1000,
noise=0.1,
factor=0.2,
random_state=0)
X
X.shape
plt.figure(figsize=(5, 5))
plt.plot(X[y==0, 0], X[y==0, 1], 'ob', alpha=0.5)
plt.plot(X[y==1, 0], X[y==1, 1], 'xr', alpha=0.5)
plt.xlim(-1.5, 1.5)
plt.ylim(-1.5, 1.5)
plt.legend(['0', '1'])
plt.title("Blue circles and Red crosses")
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
model=Sequential()
model.add(Dense(4, input_shape=(2,), activation='tanh'))##gmoidmodel.add(Dense(4, input_shape=(2,), activation='softmax'))
model.add(Dense(1, activation='sigmoid'))
model.compile(SGD(lr=0.5), 'binary_crossentropy', metrics=['accuracy'])
model.fit(X, y, epochs=20)
hticks = np.linspace(-1.5, 1.5, 101)
vticks = np.linspace(-1.5, 1.5, 101)
aa, bb = np.meshgrid(hticks, vticks)
ab = np.c_[aa.ravel(), bb.ravel()]
c = model.predict(ab)
cc= c.reshape(aa.shape)
c1=np.resize(c,(aa.shape))
plt.figure(figsize=(5, 5))
plt.contourf(aa, bb,cc,map='bwr', alpha=0.2)
plt.plot(X[y==0, 0], X[y==0, 1], 'ob', alpha=0.5)
plt.plot(X[y==1, 0], X[y==1, 1], 'xr', alpha=0.5)
plt.xlim(-1.5, 1.5)
plt.ylim(-1.5, 1.5)
plt.legend(['0', '1'])
plt.title("Blue circles and Red crosse with reshape")
plt.figure(figsize=(5, 5))
plt.contourf(aa, bb,c1,map='bwr', alpha=0.2)
plt.plot(X[y==0, 0], X[y==0, 1], 'ob', alpha=0.5)
plt.plot(X[y==1, 0], X[y==1, 1], 'xr', alpha=0.5)
plt.xlim(-1.5, 1.5)
plt.ylim(-1.5, 1.5)
plt.legend(['0', '1'])
plt.title("Blue circles and Red crosseswith resize")