Skip to content

Incomplete code for Exercise 2 (Logistic Regression), Regularization  #110

@Official-Dev-long

Description

@Official-Dev-long

According to the requirements of the exercise, the polynomial terms should be up to sixth power, which mean transformed into a 28-dimensional vector, but the code provided only consists 11 dimension. I found that this error occurs due to these code (generating polynomials) in the original script:

degree = 5
x1 = data2['Test 1']
x2 = data2['Test 2']

data2.insert(3, 'Ones', 1)

for i in range(1, degree):
    for j in range(0, i):
        data2['F' + str(i) + str(j)] = np.power(x1, i-j) * np.power(x2, j)

data2.drop('Test 1', axis=1, inplace=True)
data2.drop('Test 2', axis=1, inplace=True)

data2.head()

here I provided my code which could generate the right 28-dimensional vector, which higher the accuracy up to 83.05% (using sklearn package), while original code provided only hit 66.10% when only 11-d vector used as input

max_degree = 6

x1 = data2['Test 1'] 
x2 = data2['Test 2']

for i in range(2, max_degree + 1):
    for j in range(i+1):
        data2['x1^' + str(i-j) + '_x2^' + str(j)] = np.power(x1, i-j) * np.power(x2, j)
data2

the result of my code also more readable, here is the column of the data after generated the 28-d vector:

data.columns

Index(['Ones', 'Test 1', 'Test 2', 'Accepted', 'x1^2_x2^0', 'x1^1_x2^1',
'x1^0_x2^2', 'x1^3_x2^0', 'x1^2_x2^1', 'x1^1_x2^2', 'x1^0_x2^3',
'x1^4_x2^0', 'x1^3_x2^1', 'x1^2_x2^2', 'x1^1_x2^3', 'x1^0_x2^4',
'x1^5_x2^0', 'x1^4_x2^1', 'x1^3_x2^2', 'x1^2_x2^3', 'x1^1_x2^4',
'x1^0_x2^5', 'x1^6_x2^0', 'x1^5_x2^1', 'x1^4_x2^2', 'x1^3_x2^3',
'x1^2_x2^4', 'x1^1_x2^5', 'x1^0_x2^6'],
dtype='object')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions