fengdu78 / Coursera-ML-AndrewNg-Notes

吴恩达老师的机器学习课程个人笔记
31.44k stars 10.56k forks source link

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

Open Official-Dev-long opened 10 months ago

Official-Dev-long commented 10 months ago

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')

xiadongdong commented 10 months ago

您的邮件我已经收到,我会尽快处理的,谢谢!

sullinger commented 10 months ago

哒啦哒哒哒,收到啦!ps.这里是HFer_杨少

yanyiting commented 10 months ago

 收到,谢谢!

Enya191113 commented 10 months ago

您好!      已经收到您的邮件,我会尽快处理。祝身体健康,万事胜意!