“””
You may need to import necessary modules like numpy and pandas. However, you can’t use any external
libraries such as sci-kit learn, etc. to implement the perceptron and the training of the perceptron.
The implementation must be done completely by yourself.
We are allowing you to use two packages from nltk for text processing: nltk.stem and nltk.tokenize. You cannot import
nltk in general, but we are allowing the use of these two packages only. We will check the code in your programs to
make sure this is the case and if other packages in nltk are used then we will deduct points from your assignment.
“””
“””
This is a Python class meant to represent the perceptron model, any sort of feature processing that you may do, and
the implementation of stochastic gradient descent. You have a lot of flexibility on how you want to implement the
training of the perceptron but below I have listed functionality that should not change:
– Arguments to the __init__ function
– Arguments and return statement of the train function
– Arguments and return statement of the predict function
When you want the program (perceptron) to train on a dataset, the train function will only take one input which is the
raw copy of the data file as a pandas dataframe. Below, is example code of how this is done:
data = pd.read_csv(‘data.csv’)
model = Perceptron()
model.train(data) # Train the model on data.csv
It is assumed when this program is evaluated, the call function takes one input which is the raw copy of the
data file as a pandas dataframe, one keyword argument to specify whether the perceptron should predict labels, and
produce as output the list of predicted labels. Below is example code of how this is done:
data = pd.read_csv(‘data.csv’)
model = Perceptron()
predicted_labels = model.predict(data) # Produce predictions using model on data.csv
I have added several optional helper methods for you to use in building the pipeline of training the perceptron. It is
up to your discretion on if you want to use them or add your own methods.
“””
class Perceptron():
def __init__(self):
“””
The __init__ function initializes the instance attributes for the class. There should be no inputs to this
function at all. However, you can setup whatever instance attributes you would like to initialize for this
class. Below, I have just placed as an example the weights and bias of the perceptron as instance attributes.
“””
self.weights = None
self.bias = None
def feature_extraction(self):
“””
Optional helper method to code the feature extraction function to transform the raw dataset into a processed
dataset to be used in perceptron training.
“””
return
def sgn_function(self, perceptron_input):
“””
Optional helper method to code the sign function for the perceptron.
“””
return
def logistic_loss(self, predicted_label, true_label):
“””
Optional helper method to code the logistic function.
“””
return
def stochastic_gradient_descent(self, data, labels):
“””
Optional helper method to compute a gradient update for a single point.
“””
return
def update_weights(self, new_weights):
“””
Optional helper method to update the weights of the perceptron during stochastic gradient descent.
“””
self.weights = new_weights
def update_bias(self, new_bias):
“””
Optional helper method to update the bias of the perceptron during stochastic gradient descent.
“””
self.bias = new_bias
def predict_labels(self, data_point):
“””
Optional helper method to produce predictions for a single data point
“””
return
def train(self, labeled_data, learning_rate=None, max_iter=None):
“””
You must implement this function and it must take in as input data in the form of a pandas dataframe. This
dataframe must have the label of the data points stored in a column called ‘label’. For example, the column
labeled_data[‘Label’] must return the labels of every data point in the dataset. Additionally, this function
should not return anything.
The goal of this function is to train the perceptron on the labeled data. Feel free to code this however you
want.
“””
return
def predict(self, data):
predicted_labels = []
“””
This function is designed to produce labels on some data input. It is required that it take in 3 inputs. The
first input is the data in the form of a pandas dataframe. The second and third inputs are the learning rate
and max iterations as two keyword arguments respectfully. Once you find the optimal values of the
hyperparameters, update the default values for each keyword argument to reflect those values.
Finally, you must return the variable predicted_labels which should contain a list of all the
predicted labels on the input dataset. This list should only contain integers that are either 0 (negative) or 1
(positive) for each data point.
The rest of the implementation can be fully customized.
“””
return predicted_labels