-
Notifications
You must be signed in to change notification settings - Fork 0
/
bird_chirping_example.py
72 lines (60 loc) · 2.57 KB
/
bird_chirping_example.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
# -*- coding: utf-8 -*-
"""Bird Chirping Example
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1bMDe2sni_az6VEvBpW5fqkTWR-m1jrW0
This code is from this site: https://machinelearningmastery.com/binary-classification-tutorial-with-the-keras-deep-learning-library/ and has been lightly adapted to be used with colab
"""
# Binary Classification with Sonar Dataset: Standardized Smaller
from pandas import read_csv
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
import numpy as np
"""Before running the code below dowload the sonar dataset from https://archive.ics.uci.edu/ml/machine-learning-databases/undocumented/connectionist-bench/sonar/sonar.all-data and rename it sonar.csv then upload it from the next block"""
#from google.colab import files
#uploaded = files.upload()
# load dataset
dataframe = read_csv("RecipiesCleanNoIndexHalfRealHalfRandomRandomStartsLine4780LabelsInColCUZ1.csv", header=None)
dataset = dataframe.values
# split into input (X) and output (Y) variables
X = dataset[:,0:2599].astype(float)
Y = dataset[:,2599]
# encode class values as integers
encoder = LabelEncoder()
encoder.fit(Y)
encoded_Y = encoder.transform(Y)
# smaller model
def create_smaller():
# create model
model = Sequential()
model.add(Dense(30, input_dim=2599, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
#estimators = []
#estimators.append(('standardize', StandardScaler()))
#estimators.append(('mlp', KerasClassifier(build_fn=create_smaller, epochs=2, batch_size=5, verbose=1)))
#pipeline = Pipeline(estimators)
#kfold = StratifiedKFold(n_splits=10, shuffle=True)
#results = cross_val_score(pipeline, X, encoded_Y, cv=kfold)
Y = [1 if y == 'R' else 0 for y in Y]
model = create_smaller()
results = model.fit(X, Y, batch_size=5, epochs=2)
print(results)
#print("Smaller: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))
#predict(x, batch_size=None, verbose=0, steps=None, callbacks=None, max_queue_size=10, workers=1, use_multiprocessing=False)
input = np.zeros(2599)
input[0] = 1
input[1] = 1
input[2] = 1
input[3] = 1
input[4] = 1
result = model.predict(np.asarray([input]))
print(result)