Lab 3 Solution

$30.00

Category: Tag:

Description

Instructions

  1. This note book contains instructions for COMP9318-Lab3.

  2. You are required to complete your implementation in a file submission.py provided along with this notebook.

  3. You are not allowed to print out unnecessary stuff. We will not consider any output printed out on the screen. All results should be returned in appropriate data structures via corresponding functions.

  4. You can submit your implementation for Lab3 via following link: http://kg.cse.unsw.edu.au/submit/ .

  5. For each question, we have provided you with detailed instructions along with question headings. In case of any problem, you can post your query @ Piazza.

  • You are allowed to add other functions and/or import modules (you may have to in this lab), but you are not allowed to define global variables. Only functions are allowed in submission.py.

  • You should not import unnecessary modules/libraries, failing to import such modules at test time will lead to errors.

  • We will provide immediate feedback on your submission. You can access your scores using the online submission portal on the same day.

  • For Final Evaluation we will be using a different dataset, so your final scores may vary.

  • You are allowed to submit as many times as you want before the deadline, but ONLY the latest version will be kept and marked.

  • Submission deadline for this assignment is 23:59:59 on 2nd April, 2019. We will NOT accept any late submissions.

Question-1: Logistic Regression using Gradient Descent

  • In this lab you are required to implement Logistic Regression using Gradient Descent.

  • The training data is 2-dimensional data points from two classes namely: class-0, and class-1.

In [18]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


data_file='./asset/a'
raw_data = pd.read_csv(data_file, sep=',')
raw_data.head()

Out[18]:

Col1

Col2

Label

0

0.464726

-0.552165

0.0

1

-0.465277

0.155947

0.0

2

-0.367907

0.337509

0.0

3

-1.703355

-0.511965

0.0

4

-0.193367

0.642282

0.0

In [27]:

raw_data.groupby('Label').t(x='Col1', y='Col2', ='o')

Out[27]:

Label
0.0    AxesSubplot(0.125,0.125;0.775x0.755)
1.0    AxesSubplot(0.125,0.125;0.775x0.755)
dtype: object

You need to implement logistic regression classifier (i.e., logistic_regression() in the file: submission.py). The input arguments of logistic_regression() are:

  • data: Data in 2-columns format

  • labels: Class-labels

  • weights: The cofficients to be computed, initialized with ZEROS

  • num_epochs: Number of epochs

  • learning_rate: Learning rate of the algorithm

The return value of logistic_regression() should be a numpy array containing the logistic regression coefficients. The bias term should be appended at the start of the array.

For example, a sample output is shown in the cell given below:

In [2]:

import submission as submission

## Read in the Data...
raw_data = pd.read_csv(data_file, sep=',')
labels=raw_data['Label'].values
data=np.stack((raw_data['Col1'].values,raw_data['Col2'].values), axis=-1)

## Fixed Parameters. Please do not change values of these parameters...
weights = np.zeros(3) # We compute the weight for the intercept as well...
num_epochs = 50000
learning_rate = 50e-5

coefficients=submission.logistic_regression(data, labels, weights, num_epochs, learning_rate)
print(coefficients)
# [-10.20342907  -2.29067514   5.31366198]
[-10.20342907  -2.29067514   5.31366198]

Test Environment

For testing, we have pre-installed the requisite modules and/or libraries in the testing environment. You are only allowed to use following libraries:

  • python: 3.6.5

  • pandas: 0.22.0

  • numpy: 1.14.3

NOTE:

  • You are required to implement the logistic regression by yourself. You are not allowed to import SKLEARN and/or any OTHER LIBRARY in Lab3.

In [ ]:


error: Content is protected !!