Skip to content

Commit 74f003f

Browse files
authored
Add files via upload
1 parent f8ea364 commit 74f003f

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

L9 Sonar Different Network Sizes.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#import libraries
2+
import numpy
3+
import pandas
4+
from keras.models import Sequential
5+
from keras.layers import Dense
6+
from keras.wrappers.scikit_learn import KerasClassifier
7+
from sklearn.cross_validation import cross_val_score
8+
from sklearn.preprocessing import LabelEncoder
9+
from sklearn.cross_validation import StratifiedKFold
10+
from sklearn.preprocessing import StandardScaler
11+
from sklearn.pipeline import Pipeline
12+
13+
#load data
14+
dataframe = pandas.read_csv("sonar.csv", header = None)
15+
dataset = dataframe.values
16+
# split into input (X) and output (Y)
17+
X = dataset[:,0:60].astype(float)
18+
Y = dataset[:,60]
19+
20+
# one hot encoding
21+
encoder = LabelEncoder()
22+
encoder.fit(Y)
23+
encoded_Y = encoder.transform(Y)
24+
25+
def create_smaller():
26+
#create a network with less neurons in first hidden layer
27+
# 60i - 30 - 1o
28+
model = Sequential()
29+
model.add(Dense(30, input_dim = 60, init = 'normal', activation = 'relu'))
30+
model.add(Dense(1, init = 'normal', activation = 'sigmoid'))
31+
#compile model
32+
model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
33+
return model
34+
35+
def create_larger():
36+
#create a network with more hidden layers
37+
# 60i - 60 - 30 - 1o
38+
model = Sequential()
39+
model.add(Dense(60, input_dim = 60, init = 'normal', activation = 'relu'))
40+
model.add(Dense(30, init = 'normal', activation = 'relu'))
41+
model.add(Dense(1, init = 'normal', activation = 'sigmoid'))
42+
model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
43+
return model
44+
45+
seed = 7
46+
numpy.random.seed(seed)
47+
48+
#evaluating the smaller network
49+
estimatorsSmall = []
50+
estimatorsSmall.append(('standardize', StandardScaler()))
51+
estimatorsSmall.append(('mlp', KerasClassifier(build_fn = create_smaller, nb_epoch = 100, batch_size = 5, verbose = 0)))
52+
pipelineSmall = Pipeline(estimatorsSmall)
53+
kfold = StratifiedKFold(y = encoded_Y, n_folds = 10, shuffle = True, random_state = seed)
54+
resultsSmall = cross_val_score(pipelineSmall, X, encoded_Y, cv = kfold)
55+
56+
#evaluating the larger network
57+
estimatorsLarge = []
58+
estimatorsLarge.append(('standardize', StandardScaler()))
59+
estimatorsLarge.append(('mlp', KerasClassifier(build_fn = create_larger, nb_epoch = 100, batch_size = 5, verbose = 0)))
60+
pipelineLarge = Pipeline(estimatorsLarge)
61+
kfold = StratifiedKFold(y = encoded_Y, n_folds = 10, shuffle = True, random_state = seed)
62+
resultsLarge = cross_val_score(pipelineLarge, X, encoded_Y, cv = kfold)
63+
64+
# results
65+
print("Smaller: %.2f%% (%.2f%%)" % (resultsSmall.mean() * 100, resultsSmall.std() * 100))
66+
print("Larger: %.2f%% (%.2f%%)" % (resultsLarge.mean() * 100, resultsLarge.std() * 100))
67+
68+

0 commit comments

Comments
 (0)