Skip to content

Instantly share code, notes, and snippets.

@chepo92
Forked from yabyzq/Keras - basic script
Last active April 27, 2021 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chepo92/95fa34057fef8fa9132872b1ab4ada0b to your computer and use it in GitHub Desktop.
Save chepo92/95fa34057fef8fa9132872b1ab4ada0b to your computer and use it in GitHub Desktop.
#Load data
import pandas as pd
white = pd.read_csv("http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-white.csv", sep=';')
red = pd.read_csv("http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv", sep=';')
#define Target
red['type'] = 1
white['type'] = 0
wines = red.append(white, ignore_index = True)
#split training/testing
from sklearn.model_selection import train_test_split
import numpy as np
# X = wines.ix[:,0:11] # Deprecated ix
X = wines.iloc[:,0:11]
y=np.ravel(wines.type)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size =0.33, random_state = 1)
#preprocess
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler().fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
#Keras - build model
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(12, activation = 'relu', input_shape = (11,)))
model.add(Dense(8, activation = 'relu'))
model.add(Dense(1, activation = 'sigmoid'))
model.summary()
#Keras - run model
model.compile(loss = 'binary_crossentropy',
optimizer = 'adam',
metrics = ['accuracy'])
model.fit(X_train, y_train, epochs = 20, batch_size = 5, verbose = 1)
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score, cohen_kappa_score
y_pred = model.predict(X_test).astype('int')
score = model.evaluate(X_test, y_test, verbose = 1)
print('loss and accuracy:]\n',score)
print('\nconfusion matrix:\n',confusion_matrix(y_test, y_pred))
print('\nprecision:', precision_score(y_test, y_pred))
print('\nrecall:', recall_score(y_test, y_pred))
print('\nF1:', f1_score(y_test, y_pred))
print('\nKappa:', cohen_kappa_score(y_test, y_pred))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment