Wine Classification
Wine Classification
import scipy
from scipy import io
import numpy as np
import matplotlib.pyplot as plt
import time
# Load Data
data = scipy.io.loadmat('wine_data/wine.mat')
features = data['X']
labels = data['y']
num_features=len(features[0])
num_examples = len(features)
Inputs:
- features: n x d
- labels: n x 1
- num_features: n
- num_examples: d
Output:
- augmented data matrix: n x (d + 2)
- new num_features: n + 1
'''
Input:
- augmented data matrix: n x (d + 1)
- val_size: k
Output:
- Training Set: (n - k) x (d + 1)
- Validation Set: k x (d + 1)
'''
training_size = n - val_size
t_id = x[:training_size]
v_id = x[training_size:]
training = augmented_data[t_id,:]
validation = augmented_data[v_id,:]
Input:
- Training Set: (n - k) x (d + 1)
- Validation Set: k x (d + 1)
- num_featres: d
Output:
- Normalized Training Set: (n - k) x (d + 1)
- Normalized Validation Set: k x (d + 1)
'''
val_size = 1000
train_size = num_examples - val_size
Inputs:
- w: d x 1 weight vector
- X: n x d feature matrix
Outputs:
- s: n x 1 output of sigmoid
'''
# Q3.2
Inputs:
- train set: n x (d + 1)
- total features: d
- # of training iterations
- learning rate
- l2 regularization constant
Outputs:
- w: d x 1 weight vector
- list of losses: # iters x 1 vector
'''
# Initialize variables
w = np.zeros((total_features,))
grad = np.zeros((total_features,))
for i in np.arange(num_iter):
grad =
w =
s =
loss[i+1] =
if i % 500 == 0:
print("Loss at Iteration {} is {}:".format(i, loss[i+1]))
return w, loss
num_iter = 7000
start_time = time.time()
end_time = time.time()
calc_time(start_time, end_time)
You should expect to see the loss decrease significantly in the first 1000 iterations. The
validation accuracy should be greater than 99%
Inputs:
- train set: n x (d + 1)
- total features: d
- train_size: n
- # of training iterations
- learning rate
- l2 regularization constant
- decaying lr: boolean to determine whether or not we are
decaying the learning rate
Outputs:
- w: d x 1 weight vector
- list of losses: # iters x 1 vector
'''
# We want to keep track of the loss per iteration so that we can
plot it later
loss = np.zeros((num_iter+1,))
# Initialize variables
w = np.zeros((total_features,))
grad = np.zeros((total_features,))
original_lr = lr
for i in np.arange(num_iter):
if decay:
lr =
sample_index =
grad =
w =
s =
loss[i+1]=
if i % 500 == 0:
print("Loss at Iteration {} is {}:".format(i, loss[i+1]))
return w, loss
start_time = time.time()
end_time = time.time()
calc_time(start_time, end_time)
What do you notice about the speed of computation for the classifier trained on SGD vs
Batch Gradient Descent?
What do you notice about the accuracy?
calc_time(start_time, end_time)
Great Job Finishing! Here are some Key Takeaways from this Exercise:
1) Logistic Regression is a classifier that can predict between two classes. It can be
iteratively optimized via gradient descent
2) Stochastic Gradient Descent runs computationally quicker than Batch Gradient Descent,
at the cost of some model performance
3) One strategy to improve model training is to decay the learning rate of the optimizer
over time. While this can lead to good results, one must be tricky about setting the decay
rate and the initial learning rate.
Wally is infinitely grateful for your help! Now that he has a wine classifier, he can pass
down the family business in peace :)