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

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here's one for java and one for c# and here's… May 12, 2026 at 1:06 am
  • Editorial Team
    Editorial Team added an answer eval and exec are handy quick-and-dirty way to get some… May 12, 2026 at 1:06 am
  • Editorial Team
    Editorial Team added an answer Since you're floating both #inner-1 and #inner-2, you'll need a… May 12, 2026 at 1:06 am

Related Questions

First off let me just say I'm very new to coding so there are
I am trying to use VIM for some programming. I am a total beginner
I'm playing about with lighttpd on a small virtual private server. I two domains
I have spent lot of time doing research on VIM. I am Windows guy

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.