Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 253531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:46:10+00:00 2026-05-11T21:46:10+00:00

Could you guys please tell me how I can make the following code more

  • 0

Could you guys please tell me how I can make the following code more pythonic?

The code is correct. Full disclosure – it’s problem 1b in Handout #4 of this machine learning course. I’m supposed to use newton’s algorithm on the two data sets for fitting a logistic hypothesis. But they use matlab & I’m using scipy

Eg one question i have is the matrixes kept rounding to integers until I initialized one value to 0.0. Is there a better way?

Thanks

import os.path
import math
from numpy import matrix
from scipy.linalg import inv #, det, eig

x = matrix( '0.0;0;1'  )
y = 11
grad = matrix( '0.0;0;0'  )
hess = matrix('0.0,0,0;0,0,0;0,0,0')
theta = matrix( '0.0;0;0'  ) 


# run until convergence=6or7
for i in range(1, 6):
  #reset
  grad = matrix( '0.0;0;0'  )
  hess = matrix('0.0,0,0;0,0,0;0,0,0')

  xfile = open("q1x.dat", "r")
  yfile = open("q1y.dat", "r")


  #over whole set=99 items  
  for i in range(1, 100):    
    xline = xfile.readline()
    s= xline.split("  ")
    x[0] = float(s[1])
    x[1] = float(s[2])
    y = float(yfile.readline())

    hypoth = 1/ (1+ math.exp(-(theta.transpose() * x)))

    for j in range(0,3):
      grad[j] = grad[j] + (y-hypoth)* x[j]      
      for k in range(0,3):
        hess[j,k] = hess[j,k] - (hypoth *(1-hypoth)*x[j]*x[k])


  theta = theta - inv(hess)*grad #update theta after construction

  xfile.close()
  yfile.close()

print "done"
print theta
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:46:11+00:00Added an answer on May 11, 2026 at 9:46 pm
    x = matrix([[0.],[0],[1]])
    theta = matrix(zeros([3,1]))
    for i in range(5):
      grad = matrix(zeros([3,1]))
      hess = matrix(zeros([3,3]))
      [xfile, yfile] = [open('q1'+a+'.dat', 'r') for a in 'xy']
      for xline, yline in zip(xfile, yfile):
        x.transpose()[0,:2] = [map(float, xline.split("  ")[1:3])]
        y = float(yline)
        hypoth = 1 / (1 + math.exp(theta.transpose() * x))
        grad += (y - hypoth) * x
        hess -= hypoth * (1 - hypoth) * x * x.transpose()
      theta += inv(hess) * grad
    print "done"
    print theta
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I could be not more sorry guys - the question was a very wrong
Guys, could you please describe TortoiseHg extensions which comes with the installation package? What
I am not very experienced with .htaccess files and editing, could you guys please
Hi guys please could you help do this I tried to read about jquery,
could you guys please explain for me HOW these databases ARE CREATED : mysql
Could guys please help me find what causes memory leak ? It drives me
Could you guys please help me find memory leak ? I'm from C++ world
My problem is related to Android. I have the following code (non-relevant parts omitted):
Hi Guys could you please help me refactor this so that it is sensibly
Could you guys please point what I'm missing here ? I have two sample

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.