<jupyter_start><jupyter_text>**Importing the libraries:**<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD

SEED = 2017<jupyter_output>Using TensorFlow backend.<jupyter_text>**Load the dataset:**<jupyter_code>data = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv", 
                   sep = ';')

y = data['quality']
X = data.drop(['quality'], axis = 1)<jupyter_output><empty_output><jupyter_text>**Split the dataset into training and testing sets:**<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X, y,
                                                    test_size = 0.2,
                                                    random_state = SEED)<jupyter_output><empty_output><jupyter_text>**Normalizing the input data:**<jupyter_code>scaler = StandardScaler().fit(X_train)

X_train = pd.DataFrame(scaler.transform(X_train))
X_test = pd.DataFrame(scaler.transform(X_test))<jupyter_output><empty_output><jupyter_text>**Define the model, optimizer and compile:**<jupyter_code>model = Sequential()

model.add(Dense(1024, input_dim = X_train.shape[1], 
                activation = 'relu'))
model.add(Dense(1024, activation = 'relu'))
model.add(Dense(1024, activation = 'relu'))
model.add(Dense(1024, activation = 'relu'))

# Output layer
model.add(Dense(1, activation = 'linear'))

# Set optimizer
opt = SGD(lr = 0.01)

# Compile model
model.compile(loss = 'mse', optimizer = opt, metrics = ['accuracy'])<jupyter_output>WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.<jupyter_text>**Set the hyperparameters and train the model:**<jupyter_code>n_epochs = 500
batch_size = 256

history = model.fit(X_train.values, y_train, 
                    batch_size = batch_size,
                    epochs = n_epochs,
                    validation_split = 0.2, verbose = 0)<jupyter_output>WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.<jupyter_text>**Predict on the test set:**<jupyter_code>predictions = model.predict(X_test.values)

print('Test accuracy: {:f>2}%'.format(np.round(np.sum([y_test == predictions.flatten().round()]) / y_test.shape[0] * 100, 2)))<jupyter_output>Test accuracy: 54.06%<jupyter_text>**Plot the training and validation accuracy:**<jupyter_code>plt.plot(np.arange(len(history.history['acc'])), 
         history.history['acc'], label = 'training')
plt.plot(np.arange(len(history.history['val_acc'])),
         history.history['val_acc'], label = 'validation')

plt.title('Accuracy')
plt.xlabel('epochs')
plt.ylabel('accuracy')
plt.legend(loc = 0)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.132312404287902, 'fraction_numerical': 0.01868300153139357, 'mean_word_length': 4.660311958405546, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 30, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '320313', 'license': 'MIT', 'n_tokens_mistral': 1141, 'n_tokens_neox': 1072, 'n_words': 204, 'path': 'Chapter 2/5_Experiment with hidden layers and hidden units.ipynb', 'repo_name': 'Anacoder1/Python_DeepLearning_Cookbook', 'size': '3265'}