file write IOError Invalid argument

2009-07-16 Thread Robert Robert
Hello All,
I am trying to write a binary string to file on a windows network share. I get 
an IOError. I've read that it is because the file size is too large. I did a 
type( binaryString) and saw that it was type "str". So I loop through it one by 
one element and use f.write to write to file, and it worked without any error. 
However it took a long while to write to file. I was wondering how I could 
split this binary string into N number of chunks efficiently and write them to 
file. 
thanks,

rh 




  -- 
http://mail.python.org/mailman/listinfo/python-list


fil open close very slow

2009-08-01 Thread Robert Robert
hiya all,
I have a "large" script written in python. There is in that big script at a 
certain point a loop in which I create a lots of simple small text files. Each 
text file just contain I think 4 or 5 short lines. When I simply run it in 
IDLE, it runs fast. But when I compile it to binary files, it becomes very 
slow. I turned on the profiler and discorvered it was slow at the open() and 
close() calls. I removed those calls to confirm this, and yes my program runs 
very fast then. Does anyone has an idea or suggestion why this can be possible. 
I only compile them to .pyc files, which as what I see, when I normally run in 
IDLe these files are created as well, so I really got no clue.
rh.


  -- 
http://mail.python.org/mailman/listinfo/python-list


ActivePython: multiple versions on OSX?

2011-07-25 Thread Robert

Is it possible to install the 2 and 3 series side by side?

--
Robert


--
http://mail.python.org/mailman/listinfo/python-list


Re: 3.1 -> 3.2: base64 lost deprecation warning

2011-02-28 Thread Robert

On 2011-02-28 15:51:32 -0500, Ethan Furman said:


Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
--> import base64
--> base64.encodestring(b'this is a test')
__main__:1: DeprecationWarning: encodestring() is a deprecated alias,
use encodebytes()
b'dGhpcyBpcyBhIHRlc3Q=\n'


Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit
(Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
--> import base64
--> base64.encodestring(b'another test')
b'dGhpcyBpcyBhIHRlc3Q=\n'


The deprecation warning has gone away in 3.2, but the function
remains... does anyone know if this was intentional?

~Ethan~


I only found this:

Issue #3613: base64.{encode,decode}string are now called
 base64.{encode,decode}bytes which reflects what type they accept and return.
 The old names are still there as deprecated aliases.

Doesn't exlain the "no warning" though.

--
Robert


--
http://mail.python.org/mailman/listinfo/python-list


What do you use with Python for GUI programming and why?

2011-03-10 Thread Robert
Is there a push to one toolkit or the other?

--
Robert
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What do you use with Python for GUI programming and why?

2011-03-11 Thread Robert

On 2011-03-11 01:14:01 -0500, Dan Stromberg said:



You're probably best off with Pyjamas.  Then you get something that 
runs on the web and on the desktop, from the same code - similar to 
GWT, but for Python.  The desktop version runs overtop of CPython, the 
web version is AJAX and is automatically translated from a very 2.x-ish 
dialect of Python to Javascript.  Programming an AJAX app this way 
feels just like programming to a set of desktop widgets - because they 
are also desktop widgets.


But what OSes are you using?

On Thu, Mar 10, 2011 at 6:25 PM, Robert  wrote:
Is there a push to one toolkit or the other?


I am mostly on OSX...but I would target at least the big three 
(Windows, Linux, OSX).


--
Robert


--
http://mail.python.org/mailman/listinfo/python-list


[OSX] side by side python

2011-03-21 Thread Robert
Can I install Python 2.7 and 3.2 (from python.org) side by side on OSX
without them stepping all over each other?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: side by side python

2011-03-21 Thread Robert
On Mar 21, 9:45 am, macm  wrote:
> On Mar 21, 9:31 am, Robert  wrote:
>
> > Can I install Python 2.7 and 3.2 (from python.org) side by side on OSX
> > without them stepping all over each other?
>
> Yes, sure! Look for "python environment"
>
> http://pypi.python.org/pypi/virtualenv
>
> Regards

Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


[Book] Python 3 Web Development Beginner's Guide

2011-05-21 Thread Robert
Has anyone read this one? If so, what did you think?

--
Bob
-- 
http://mail.python.org/mailman/listinfo/python-list


What use is of this 'cast=float ,'?

2017-10-27 Thread Robert
Hi,

I read below code snippet on line. I am interested in the second of the last 
line.

 cast=float ,


I've tried it in Python. Even simply with 

float


it has no error, but what use is it?


I do see a space before the comma ','. Is it a typo or not?


Thanks,



self.freqslider=forms.slider(
 parent=self.GetWin( ),
 sizer=freqsizer,
 value=self.freq,
 callback= self.setfreq,
 minimum=−samprate/2,
 maximum=samprate/2,
 num_steps=100,
 style=wx.SL_HORIZONTAL,
 cast=float ,
 proportion=1,
)
-- 
https://mail.python.org/mailman/listinfo/python-list


Is it useful to set a fraction number here to the mask value?

2017-11-30 Thread Robert
Hi,

I am new to Python. Now I follow a thread on mask array usage on line:


https://stackoverflow.com/questions/31563970/fitting-a-binomial-distribution-with-pymc-raises-zeroprobability-error-for-certa


I understand the problem, but I don't understand the answer follow the link.

Because the 'mask' array is composed of integer, if it is assigned a fraction
number as suggested a 1.5, it will be formatted to an integer 1.
 

observed_values = sp.random.binomial(n = 10.0, p = 0.1, size = 100)
...
mask = sp.zeros_like(observed_values)


Are you clear the answer's meaning?


"You can give it a non-integer value in order to avoid the problem that you 
cite. 
For example, if you fill with, say, 1.5 that should work."




Thanks in advance
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is it useful to set a fraction number here to the mask value?

2017-11-30 Thread Robert
On Thursday, November 30, 2017 at 6:17:05 PM UTC-5, Robert wrote:
> Hi,
> 
> I am new to Python. Now I follow a thread on mask array usage on line:
> 
> 
> https://stackoverflow.com/questions/31563970/fitting-a-binomial-distribution-with-pymc-raises-zeroprobability-error-for-certa
> 
> 
> I understand the problem, but I don't understand the answer follow the link.
> 
> Because the 'mask' array is composed of integer, if it is assigned a fraction
> number as suggested a 1.5, it will be formatted to an integer 1.
>  
> 
> observed_values = sp.random.binomial(n = 10.0, p = 0.1, size = 100)
> ...
> mask = sp.zeros_like(observed_values)
> 
> 
> Are you clear the answer's meaning?
> 
> 
> "You can give it a non-integer value in order to avoid the problem that you 
> cite. 
> For example, if you fill with, say, 1.5 that should work."
> 
> 
> 
> 
> Thanks in advance

Excuse me for the top post. Now I find the more specific question from the below
link:

https://pymc-devs.github.io/pymc/tutorial.html


At almost the bottom part of the web page, when I run the script

"masked_values = masked_array(disasters_array, mask=disasters_array==-999)"

has an error:

... masked_values = masked_array(disasters_array, mask=disasters_array==-999)
Traceback (most recent call last):
  File "", line 7, in 
NameError: name 'masked_array' is not defined

I use Python 2.7 on Ubuntu 16.04, 64-bit. Is there an error in the pymc 
tutorial web page?

Can you tell me what is wrong?

Thanks,

===
# Switchpoint
switch = DiscreteUniform('switch', lower=0, upper=110)
# Early mean
early_mean = Exponential('early_mean', beta=1)
# Late mean
late_mean = Exponential('late_mean', beta=1)

@deterministic(plot=False)
def rate(s=switch, e=early_mean, l=late_mean):
"""Allocate appropriate mean to time series"""
out = np.empty(len(disasters_array))
# Early mean prior to switchpoint
out[:s] = e
# Late mean following switchpoint
out[s:] = l
return out


# The inefficient way, using the Impute function:
# D = Impute('D', Poisson, disasters_array, mu=r)
#
# The efficient way, using masked arrays:
# Generate masked array. Where the mask is true,
# the value is taken as missing.
masked_values = masked_array(disasters_array, mask=disasters_array==-999)

# Pass masked array to data stochastic, and it does the right thing
disasters = Poisson('disasters', mu=rate, value=masked_values, observed=True)
-- 
https://mail.python.org/mailman/listinfo/python-list


What meaning is "if k in [0, len(n_trials) - 1] else None"?

2016-12-03 Thread Robert
Hi,

I am trying to understand the meaning of the below code snippet. Though I have
a Python IDLE at computer, I can't get a way to know below line:

if k in [0, len(n_trials) - 1] else None

I feel it is strange for what returns when the 'if' condition is true?
The second part 'None' is clear to me though.

Could you explain it to me?


thanks,







%matplotlib inline
from IPython.core.pylabtools import figsize
import numpy as np
from matplotlib import pyplot as plt
figsize(11, 9)

import scipy.stats as stats

dist = stats.beta
n_trials = [0, 1, 2, 3, 4, 5, 8, 15, 50, 500]
data = stats.bernoulli.rvs(0.5, size=n_trials[-1])
x = np.linspace(0, 1, 100)

# For the already prepared, I'm using Binomial's conj. prior.
for k, N in enumerate(n_trials):
sx = plt.subplot(len(n_trials) / 2, 2, k + 1)
plt.xlabel("$p$, probability of heads") \
if k in [0, len(n_trials) - 1] else None
plt.setp(sx.get_yticklabels(), visible=False)
heads = data[:N].sum()
y = dist.pdf(x, 1 + heads, 1 + N - heads)
plt.plot(x, y, label="observe %d tosses,\n %d heads" % (N, heads))
plt.fill_between(x, 0, y, color="#348ABD", alpha=0.4)
plt.vlines(0.5, 0, 4, color="k", linestyles="--", lw=1)

leg = plt.legend()
leg.get_frame().set_alpha(0.4)
plt.autoscale(tight=True
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What meaning is "if k in [0, len(n_trials) - 1] else None"?

2016-12-03 Thread Robert
On Saturday, December 3, 2016 at 6:09:02 PM UTC-5, Robert wrote:
> Hi,
> 
> I am trying to understand the meaning of the below code snippet. Though I have
> a Python IDLE at computer, I can't get a way to know below line:
> 
> if k in [0, len(n_trials) - 1] else None
> 
> I feel it is strange for what returns when the 'if' condition is true?
> The second part 'None' is clear to me though.
> 
> Could you explain it to me?
> 
> 
> thanks,
> 
> 
> 
> 
> 
> 
> 
> %matplotlib inline
> from IPython.core.pylabtools import figsize
> import numpy as np
> from matplotlib import pyplot as plt
> figsize(11, 9)
> 
> import scipy.stats as stats
> 
> dist = stats.beta
> n_trials = [0, 1, 2, 3, 4, 5, 8, 15, 50, 500]
> data = stats.bernoulli.rvs(0.5, size=n_trials[-1])
> x = np.linspace(0, 1, 100)
> 
> # For the already prepared, I'm using Binomial's conj. prior.
> for k, N in enumerate(n_trials):
> sx = plt.subplot(len(n_trials) / 2, 2, k + 1)
> plt.xlabel("$p$, probability of heads") \
> if k in [0, len(n_trials) - 1] else None
> plt.setp(sx.get_yticklabels(), visible=False)
> heads = data[:N].sum()
> y = dist.pdf(x, 1 + heads, 1 + N - heads)
> plt.plot(x, y, label="observe %d tosses,\n %d heads" % (N, heads))
> plt.fill_between(x, 0, y, color="#348ABD", alpha=0.4)
> plt.vlines(0.5, 0, 4, color="k", linestyles="--", lw=1)
> 
> leg = plt.legend()
> leg.get_frame().set_alpha(0.4)
> plt.autoscale(tight=True

I just notice that there is a slash character (\) before the if line.
What is it for?

I've learn Python for a while, but I don't use it for more than 2 years now.
Thanks.
-- 
https://mail.python.org/mailman/listinfo/python-list


Small python24.dll / how to strip off asian codecs to separate package(s) ?

2005-09-22 Thread Robert
updating a py2exe'd software I was impressed by python24.dll's footprint -
double size of python23.dll

Is there a version without/separate asianc codecs (which seem to mainly blow
up python24.dll)? Or how to build one?

Robert



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Small python24.dll / how to strip off asian codecs to separate package(s) ?

2005-09-24 Thread Robert
thanks, I go for that.

Wouldn't it be an issue to think about if future win-python distributions
should keep on including the asian codecs in the main-dll?
See some reason in including smaller functional pyd's lik  zip or even ssl,
etc. (as they are used regularly in projects >30%) - but asian codecs?

Robert

"Martin v. Löwis" <[EMAIL PROTECTED]> schrieb im Newsbeitrag
news:[EMAIL PROTECTED]
> Robert wrote:
> > Or how to build one?
>
> Just download the source, and follow the instructions in
> PCBuild/readme.txt. Then, edit the pythoncore project to remove
> the files you don't want to include, and edit config.c to remove
> the dangling references.
>
> Regards,
> Martin


-- 
http://mail.python.org/mailman/listinfo/python-list

wxPython + pyPlot

2005-10-18 Thread Robert
Hellow!

I'm writing program with wxpython and pyplot. 
I need to put a graph (example):

def _draw1Objects():
# 100 points sin function, plotted as green circles
data1 = 2.*Numeric.pi*Numeric.arange(200)/200.
data1.shape = (100, 2)
data1[:,1] = Numeric.sin(data1[:,0])
markers1 = PolyMarker(data1, legend='Green Markers', 
colour='green', marker='circle',size=1)

# 50 points cos function, plotted as red line
data1 = 2.*Numeric.pi*Numeric.arange(100)/100.
data1.shape = (50,2)
data1[:,1] = Numeric.cos(data1[:,0])
lines = PolyLine(data1, legend= 'Red Line', colour='red')

# A few more points...
pi = Numeric.pi
markers2 = PolyMarker([(0., 0.), (pi/4., 1.), (pi/2, 0.),
(3.*pi/4., -1)], legend='Cross Legend', colour='blue',
marker='cross')

return PlotGraphics([markers1, lines, markers2],"Graph 
Title", "X Axis", "Y Axis") 


 on the PlotConvas object 

  self.notebook1 = wx.Notebook(id=wxID_FRAME1NOTEBOOK1, name='notebook1',
  parent=self, pos=wx.Point(8, 24), size=wx.Size(800, 360),
  style=0)
  
  self.Okno_PT = wx.Window(id=wxID_FRAME1Okno_PT, name='Okno_PT',
  parent=self.notebook1, pos=wx.Point(0, 0),
  size=wx.Size(798,328), style=0)
  
  self.Wykres_PT = wx.lib.plot.PlotCanvas(id=wxID_FRAME1WYKRES_PT,
  name=u'Wykres_PT', parent=self.Okno_PT, pos=wx.Point(0, 0),
  size=wx.Size(510, 328), style=0)


and I don't now how to do that.

thanks for any help.
Robert
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: wxPython + pyPlot

2005-10-18 Thread Robert
Maebe, does anyone have some examples with wxPython and pyplot?

Thanks again,
Robert 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: wxPython + pyPlot

2005-10-18 Thread Robert
Philippe C. Martin wrote:

> I think wxWidget comes with a sample
> 
> Philippe
Yes I use it, but there is not a sample with pyplot.

Robert
-- 
http://mail.python.org/mailman/listinfo/python-list


MySQLdb and cgi

2005-10-30 Thread robert
 Dear group,

I have a problem with importing MySQLdb in a cgi-script.
The code fragment at the beginning:

  1 #!/usr/bin/env python
  2 
  3 print "Content-Type: text/html\n\n"
  4 
  5 import re
  6 import cgi
  7 import MySQLdb
8 print "something"

but the print statement in line 8 will never be executed. The Apache error log says:
"No module named MySQLdb, referer: http://localhost/sqlForm.html"

But when executing the import statement outside of an cgi-script, for example at the command line, the import works fine.
I assume, it has something to do with the apache. Why does the python interpreter not find the MySQLdb module when executed in 
apache context?

thanks, roebi
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: MySQLdb and cgi

2005-10-31 Thread robert
I am sending it again, since it didn't appear on the mailing list.
 Dear group,

I have a problem with importing MySQLdb in a cgi-script.
The code fragment at the beginning:

  1 #!/usr/bin/env python
  2 
  3 print "Content-Type: text/html\n\n"
  4 
  5 import re
  6 import cgi
  7 import MySQLdb
8 print "something"

but the print statement in line 8 will never be executed. The Apache error log says:
"No module named MySQLdb, referer: http://localhost/sqlForm.html"

But when executing the import statement outside of an cgi-script, for example at the command line, the import works fine.
I assume, it has something to do with the apache. Why does the python interpreter not find the MySQLdb module when executed in 
apache context?

thanks, roebi


-- 
http://mail.python.org/mailman/listinfo/python-list

Monster python24.dll / stripping off asian codecs to separate package(s) ?

2005-11-04 Thread Robert
Martin v. Löwis schrieb:
> Robert wrote:
> > Wouldn't it be an issue to think about if future win-python distributions
> > should keep on including the asian codecs in the main-dll?
>
> Indeed, it would. As I said before: if somebody defines a clear, fair
> policy which finds agreement in the community, I'm willing to change the
> current policy on what goes into python24.dll, and what is built
> separately. Bonus points if the policy is accompanied with a patch.
> The policy should not be static, i.e. it should also give a guideline
> for modules introduced in the future.
> At the moment, the policy is this: "everything that does not depend on
> an external library should go into pythonXY.dll". As a result of this

reducing the big python24.dll is still a tangling issue: keeping up
with new python versions.  creating an installer or copying files
around. unsecurty, if the compiler is error free ...
for me the big DLL size is the only reason to rebuild python. Guess
there are more people suffering from the doubled size of python24.dll
vs python23.dll

I guess there is no good mathematical policy. But maybe a practical one
(as pythons policy is practicability - not to be a LISP): the size of
the libraries. 'd perfectly happy if the asian codecs are out.

A practical policy: respect size + usage frequency :

For future independent libs: Check their size. If they they are
monsters, check if the lib is used in more than 80% of applications. if
not, keep monster libs sparately, as they blow up installers and memory
usage.

Robert

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Monster python24.dll / stripping off asian codecs to separate package(s) ?

2005-11-04 Thread Robert
A bugfix release 2.3.6 would also be a good idea.

I suffered at once from 2 severe errors pending in 2.3 (fixed in 2.4)
when going back to 2.3.5 (because of the monster python24.dll issue):

* httplib.HTTP(x)Connect  inserts still double Content-length headers
whichs breaks on many servers.

* _ssl.pyd has not the recent fixes.

Robert

-- 
http://mail.python.org/mailman/listinfo/python-list


Bugfix release 2.3.6 ?

2005-11-04 Thread Robert
A bugfix release 2.3.6 would also be a good idea.

I suffered at once from 2 severe bugs pending in 2.3 (fixed in 2.4)
when going back to 2.3.5 (because of the monster python24.dll issue):

* httplib.HTTP(x)Connection  inserts still double Content-length
headers whichs breaks on many servers.

* _ssl.pyd has not the recent fixes.

Robert

-- 
http://mail.python.org/mailman/listinfo/python-list


something wrong in wx

2005-11-11 Thread Robert
0, 
wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
addrSizer.Add(cstLbl, 0, 
wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
addrSizer.Add((10,10)) # some empty space

# the city, state, zip fields are in a sub-sizer
cstSizer = wx.BoxSizer(wx.HORIZONTAL)
cstSizer.Add(self.ilosc_wejsc, 1)
cstSizer.Add(self.ukryte, 0, wx.LEFT|wx.RIGHT, 5)
cstSizer.Add(self.ilosc_wyjsc)
addrSizer.Add(cstSizer, 0, wx.EXPAND)

addrSizer.Add(uczenie_Lbl, 0, 
wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
addrSizer.Add(self.uczenie, 0, wx.EXPAND)
addrSizer.Add(oczekiwany_blad_Lbl, 0, wx.ALIGN_RIGHT|   
wx.ALIGN_CENTER_VERTICAL)
addrSizer.Add(self.oczekiwany_blad, 0, wx.EXPAND)
addrSizer.Add(powtorzen_Lbl, 0, 
wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
addrSizer.Add(self.powtorzen, 0, wx.EXPAND)
addrSizer.Add(raporty_Lbl, 0, 
wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
addrSizer.Add(self.raporty, 0, wx.EXPAND)

# now add the addrSizer to the mainSizer
mainSizer.Add(addrSizer, 0, wx.EXPAND|wx.ALL, 10)

# The buttons sizer will put them in a row with resizeable
# gaps between and on either side of the buttons
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
btnSizer.Add((20,20), 1)
btnSizer.Add(trenujBtn)
btnSizer.Add((20,20), 1)
btnSizer.Add(cancelBtn)
btnSizer.Add((20,20), 1)

mainSizer.Add(btnSizer, 0, wx.EXPAND|wx.BOTTOM, 10)


# Finally, tell the panel to use the sizer for layout
panel.SetSizer(mainSizer)

# Fit the frame to the needs of the sizer. The frame will
# automatically resize the panel as needed. Also prevent the
# frame from getting smaller than this size.
mainSizer.Fit(self)
self.SetSizeHints(*self.GetSize())


def on_trenujBtn(self, event):

connection_rate = float(self.polaczenia.GetValue()*0.01)
learning_rate = float(self.uczenie.GetValue()*0.01)
num_layers = int(self.ilosc_warstw.GetValue())
num_input = int(self.ilosc_wejsc.GetValue())
num_neurons_hidden = int(self.ukryte.GetValue())
num_output = int(self.ilosc_wyjsc.GetValue())

ann = fann.fann_create(connection_rate, learning_rate, 
num_layers,  
num_input, num_neurons_hidden, num_output)

max_iterations = int(self.powtorzen.GetValue())
iterations_between_reports = int(self.raporty.GetValue())
desired_error = 0.1 
#float(1/(self.oczekiwany_blad.GetValue()))
print desired_error
fann.fann_train_on_file(ann, "zapis.txt", max_iterations,
iterations_between_reports, desired_error)
fann.fann_save(ann, "po_nauce.net")

fann.fann_destroy(ann)

app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
app.MainLoop()


###
Thanks for any help.

Robert
-- 
http://mail.python.org/mailman/listinfo/python-list


search backward

2005-01-04 Thread Robert
I need to find the location of a short string in a long string. The
problem however is that i need to search backward.

Does anybody know how to search in reverse direction?


Thanks,
Robert
-- 
http://mail.python.org/mailman/listinfo/python-list


execute python code from db

2005-01-26 Thread robert
Hello,

Anybody knows if it's possible to execute python code from an db.

db=MySQLdb.connect(host="localhost",user="r",passwd="j",db="v")

c=db.cursor()
c.execute("""SELECT * FROM table
WHERE id = %s""", (id,))

for python_code in c.fetchall():
execute (python_code)

Maybe feed python with stdin??.


robert.
-- 
http://mail.python.org/mailman/listinfo/python-list


installing tkiner

2005-01-28 Thread Robert
I am trying to use tkinter on macos 10.2.6. 

I am not having any luck.  I have tried this.

python
python2.4 after downloading
downloaded TclTkAquaBI-8.4.6.1-Jaguar.dmg and installed
python2.4 could not find tkiniter

downloaded macpython 2.3.3 macphyton find tkinter but got an application 
exception.

I was under the impression that macpython included tkinter, so I 
manually deleted TclTkAquaBI the best I could.

macpython could not find tkinter.  

In the packages section of macpython, I saw...
('no', '_tkinter-2.3-binary', 'Enables Tkinter GUI building with 
Python.')

Please advise.  

Robert
-- 
http://mail.python.org/mailman/listinfo/python-list


convert ftp.retrbinary to file object? - Python language lacks expression?

2005-02-03 Thread Robert
I just tried to convert a (hugh size) ftp.retrbinary run into a
pseudo-file object with .read(bytes) method in order to not consume
500MB on a copy operation.

First I thought, its easy as usual with python using something like
'yield' or so.

Yet I didn't manage to do (without using threads or rewriting
'retrbinary')? Any ideas?

 I tried a pattern like:
 
def open(self,ftppath,mode='rb'):
class FTPFile: #TODO
...
def iter_retr()
...
def callback(blk):
how-to-yield-from-here-to-iter_retr blk???
ftp.retrbinary("RETR %s" % relpath,callback)
def read(self, bytes=-1):
... 
self.buf+=self.iter.next()
...

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: convert ftp.retrbinary to file object? - Python language lacks expression?

2005-02-03 Thread Robert
That turns into periodic new RETR commands with offset. Think its more 
an "odd" trick. I'd even prefer a threaded approach (thread puts the 
blocks into a stack; a while ... yield generator loop in the main thread 
serves the .read() function of the pseudo file object, which is my 
wish). Yet such tricks are all kind of OS-level tricks with a lot of 
overhead.

I wonder really, if the Python language itself can express an elegant 
flat solution to turn the block delivering callback function into a 
generator/.read(bytes) solution? I found no way.

(Looking over some Ruby stuff, Ruby seems to be able to do so from the 
language. I am not really familiar to Ruby. I always felt Python to be 
as complete - but much more clean. I became somewhat jealous ... :-) )

As the solution in my case has to stand many different file systems 
compatibly ( file.read(bytes) function !) and also other FTPS & SFTP 
classes with different retrbinary functions have to be compatible, I 
cannot even make a simple FTP subclassed retrbinary without getting 
really weired. Thus the existing .retrbinary with callback is the 
"official interface in this game".


> >> def callback(self, data):
> >> self.offset = self.offset + len(data)
> >> self.data = data
> >> ## now quit the RETR command?
> >> raise TransferAbort("stop right now")
--
http://mail.python.org/mailman/listinfo/python-list


Re: importing pyc from memory?

2005-07-04 Thread Robert

"Derek van Vliet" <[EMAIL PROTECTED]> schrieb im Newsbeitrag
news:[EMAIL PROTECTED]
> Using the Python/C API, is there a way I can import a pyc file that I
> have in memory (as opposed to loading from disk)?
>
> I'm trying to save compiled python code in a proprietary file format to
> cut reduce the overhead of compiling all my scripts when my app starts
> up.


you can use a zip file as "proprietary file format"? with .pyc's or .py's in
and add the zippath to sys.path ?

other than that you can load the code object of .pyc's content in a string
like

co = marshal.loads( pyc_s[8:] )
mod=imp.new_module('xy')
exec co in mod.__dict__


Robert


-- 
http://mail.python.org/mailman/listinfo/python-list


Error with Python

2005-08-22 Thread Robert
Hello

I have created a tool in Python 2.3 under WinXP
Now I extended this tool and now my tool makes Python crash. I see Dr Watson
showing a stacktrace.
This tool also uses wxPython.

Dr Watson says:
AppName: python.exe
AppVer: 0.0.0.0
ModName: python23.dll
ModVer: 2.3.5150.1012
Offset: 0003736a

The stacktrace is very long and I don't know how to copy and paste it,
because it cannot be selected. (I have made some screenshots)

Python crashes as I can see from my tools log file, when the tool is
handling messages on the Queue object. My tool is multi threaded and unsing
Queue to send messages between threads and the main GUI thread.

My questions now are:
- can I get some help from somebody on this problem (is Python 2.3
supported)?
- how do I find out if this is Python or wxPython related problem?
- which information or tool could help me to search for the problem myself?
- do I have to be the absolute Python guru to find the problem or is it
enough to know programming a little bit?
- where to start?


I would change to Python 2.4 but then I have also to change to new wxPython
and this is a big migration effort (I have tried already).

Hope someone can give me some hints...

Regards

Robert


-- 
http://mail.python.org/mailman/listinfo/python-list


sslerror: (8, 'EOF occurred in violation of protocol') ???

2005-08-29 Thread Robert
On some connections only from some computers/network setups I get this
error:

Traceback (most recent call last):
  File "", line 1, in ?
  File "ClientCookie\_urllib2_support.pyo", line 524, in open
  File "ClientCookie\_urllib2_support.pyo", line 424, in http_response
  File "ClientCookie\_urllib2_support.pyo", line 541, in error
  File "urllib2.pyo", line 306, in _call_chain
  File "ClientCookie\_urllib2_support.pyo", line 185, in http_error_302
  File "ClientCookie\_urllib2_support.pyo", line 518, in open
  File "urllib2.pyo", line 326, in open
  File "urllib2.pyo", line 306, in _call_chain
  File "ClientCookie\_urllib2_support.pyo", line 759, in https_open
  File "ClientCookie\_urllib2_support.pyo", line 608, in do_open
  File "httplib.pyo", line 712, in endheaders
  File "httplib.pyo", line 597, in _send_output
  File "httplib.pyo", line 564, in send
  File "httplib.pyo", line 985, in connect
  File "socket.pyo", line 73, in ssl
sslerror: (8, 'EOF occurred in violation of protocol')


The server is always the same and ok. but different
client-OS/proxies/firewalls/routers are in use.
What is the nature of this error? How is it possible that an EOF in
ssl-connections (during connect?) occures? What can be done to
handle/circumvent that problem?

Robert


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sslerror: (8, 'EOF occurred in violation of protocol') ???

2005-08-30 Thread Robert

"Robert" <[EMAIL PROTECTED]> schrieb im Newsbeitrag
news:[EMAIL PROTECTED]
> On some connections only from some computers/network setups I get this
> error:
>
> Traceback (most recent call last):
>   File "", line 1, in ?
>   File "ClientCookie\_urllib2_support.pyo", line 524, in open
>   File "ClientCookie\_urllib2_support.pyo", line 424, in http_response
>   File "ClientCookie\_urllib2_support.pyo", line 541, in error
>   File "urllib2.pyo", line 306, in _call_chain
>   File "ClientCookie\_urllib2_support.pyo", line 185, in http_error_302
>   File "ClientCookie\_urllib2_support.pyo", line 518, in open
>   File "urllib2.pyo", line 326, in open
>   File "urllib2.pyo", line 306, in _call_chain
>   File "ClientCookie\_urllib2_support.pyo", line 759, in https_open
>   File "ClientCookie\_urllib2_support.pyo", line 608, in do_open
>   File "httplib.pyo", line 712, in endheaders
>   File "httplib.pyo", line 597, in _send_output
>   File "httplib.pyo", line 564, in send
>   File "httplib.pyo", line 985, in connect
>   File "socket.pyo", line 73, in ssl
> sslerror: (8, 'EOF occurred in violation of protocol')
>

In http://groups.google.de/group/comp.lang.python/msg/252b421a7d9ff037 Jp
Calderone wrote:

On 21 Jun 2005 08:39:02 -0700, Matthias Kluwe <[EMAIL PROTECTED]> wrote:



>> From: "Paul Rubin" "http://phr.cx"@NOSPAM.invalid

>>> "Matthias Kluwe" <[EMAIL PROTECTED]> writes:
>>> After getting a @gmail.com address, I recognized I had to use TLS in my
>>> python scripts using smtplib in order to get mail to the smtp.gmail.com
>>> server.


>>> [...]


>>> The server accepts and delivers my messages, but the last command
>>> raises


>>> socket.sslerror: (8, 'EOF occurred in violation of protocol')


>> [...]


>> Have you verified that its your end that is broken,  not gmail's,  do
other
>> servers give the same response ?


>No, I have not -- I should have, as I know now: Connecting, starttls,
>login and sending mail works fine without the above mentioned error
>using my previous mail provider.


>Does that mean Gmail is in error here? I don't know...



Most SSL servers and clients (primarily HTTP, but some SMTP as well) are
broken in this regard: they do not properly negotiate TLS connection
shutdown.  This causes one end or the other to notice an SSL protocol error.
Most of the time, the only thing left to do after the TLS connection
shutdown is normal TCP connection shutdown, so the error doesn't lead to any
problems (which is probably why so much software generates this problem).
Of course, there's no way to *tell* if this is the case or not, at least
programmatically.  If you receive an OK response to your DATA, you probably
don't need to worry, since you have gotten what you wanted out of the
conversation.

It's entirely possible that the fault here lies on gmail's end, but it is
also possible that the fault is in your code or the standard library ssl
support.  Unless you want to dive into Python's OpenSSL bindings or start
examining network traces of SSL traffic, you probably won't be able to
figure out who's to blame in this particular case.  The simplest thing to do
is probably just capture and discard that particular error (again, assuming
you are getting an OK resposne to your DATA command).




In fact I saw the same problem with SSL-SMTP and Googles GMAIL. the sslerror
can be ignored.

But in my case here (HTTPS) the transaction story (redirected https-request)
is not yet
completed. I don't have the essential data communicated at the moment of the
error. What can be done?

Robert



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: micro-python - is it possible?

2005-08-30 Thread Robert

"Evil Bastard" <[EMAIL PROTECTED]> schrieb im Newsbeitrag
news:[EMAIL PROTECTED]
> Hi,
>
> Has anyone done any serious work on producing a subset of python's
> language definition that would suit it to a tiny microcontroller
> environment?
>

remember there was some work done for minimizing py1.5.2 for mics. see
Google.  even bare py1.5.2 would be possible with about 1.5MB overhead
statically compiled.
remember an own short trial on a AMD SC400 with Embedded Linux. all in 4 or
8 MB or so.

but Py2.x's became big nasty beasts with also nasty memory behaviour.

guess py1.5.2  is still very nice, tiny and fast for mics.  Happy to have a
good basic script lng you won't use new style / meta classes / iterators


> Is it even possible to devise a tiny python subset that has at least
> some of python's advantages, but at the same time can be compiled to
> low-footprint resource-lean code?
>
> And could such a mini-python be worth using over C, Forth, etc?

guess so if your app has  non-trivial complexity: Faktor 5 in programming
speed and clarity of code - if you can grant 0.5..2MB overhead.

but guess you still need some 1%..5% C for time critical stuff.


Robert


-- 
http://mail.python.org/mailman/listinfo/python-list


socket.sslerror:(1, 'error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol')

2005-08-31 Thread Robert
socket.sslerror:(1, 'error:140770FC:SSL
routines:SSL23_GET_SERVER_HELLO:unknown protocol')

Got this from another computer/setup, which I cannot debug. What can be the
cause for this problem?

Python 2.3.5

trace:

[ ...(\'ClientCookie_urllib2_support.pyo\', 572, \'open\', None),
(\'ClientCookie_urllib2_support.pyo\', 472, \'http_response\', None),
(\'ClientCookie_urllib2_support.pyo\', 589, \'error\', None),
(\'urllib2.pyo\', 306, \'_call_chain\', None),
(\'ClientCookie_urllib2_support.pyo\', 181, \'http_error_302\', None),
(\'ClientCookie_urllib2_support.pyo\', 566, \'open\', None),
(\'urllib2.pyo\', 326, \'open\', None), (\'urllib2.pyo\', 306,
\'_call_chain\', None), (\'ClientCookie_urllib2_support.pyo\', 807,
\'https_open\', None), (\'ClientCookie_urllib2_support.pyo\', 656,
\'do_open\', None), (\'httplib.pyo\', 715, \'endheaders\', None),
(\'httplib.pyo\', 600, \'_send_output\', None), (\'httplib.pyo\', 567,
\'send\', None), (\'httplib.pyo\', 988, \'connect\', None), (\'socket.pyo\',
73, \'ssl\', None)]

Robert


-- 
http://mail.python.org/mailman/listinfo/python-list


Python / web

2005-09-01 Thread Robert
Hi,
I know general Python pretty well and interested in using Python for a 
web project. It will have the standard display, user input, fields, 
look-ups, reports, database routines, etc. Been looking though the 
Python web docs. and seeing stuff like mod_python, CGI, PSP, CherryPy, 
etc...,  Also a fair amount of googling. I'll say there's a large 
amount of technology to pick from.  Rather than spend time going down 
  the wrong road, can I get some feedback as directions from you folks 
that's "been there, done that."

thanks a bunch.
Robert
-- 
http://mail.python.org/mailman/listinfo/python-list


sslerror: (8, 'EOF occurred in violation of protocol')

2005-09-05 Thread Robert
I'm using the https-protocol. On my systems and network everything works ok.
But on some connections from some other users' computers/network I get this
error (Python 2.3.5 on Windows):

Traceback (most recent call last):
  File "", line 1, in ?
  File "ClientCookie\_urllib2_support.pyo", line 524, in open
  File "ClientCookie\_urllib2_support.pyo", line 424, in http_response
  File "ClientCookie\_urllib2_support.pyo", line 541, in error
  File "urllib2.pyo", line 306, in _call_chain
  File "ClientCookie\_urllib2_support.pyo", line 185, in http_error_302
  File "ClientCookie\_urllib2_support.pyo", line 518, in open
  File "urllib2.pyo", line 326, in open
  File "urllib2.pyo", line 306, in _call_chain
  File "ClientCookie\_urllib2_support.pyo", line 759, in https_open
  File "ClientCookie\_urllib2_support.pyo", line 608, in do_open
  File "httplib.pyo", line 712, in endheaders
  File "httplib.pyo", line 597, in _send_output
  File "httplib.pyo", line 564, in send
  File "httplib.pyo", line 985, in connect
  File "socket.pyo", line 73, in ssl
sslerror: (8, 'EOF occurred in violation of protocol')


The target-server is always the same and ok. but different
client-OS(Windows)/proxies/firewalls/routers are in use.
What is the nature of this error? How is it possible that an EOF in
ssl-connections (during connect?) occures?
What can be done to handle/circumvent that problem?

Robert


-- 
http://mail.python.org/mailman/listinfo/python-list


time.strptime intolerant on weekday string length?

2005-09-13 Thread Robert
A certain DAV server reports a time stamp as "Tues, 30 Aug 2005 20:48:31"
( but not "Tue, ..."  as usual)

This produces the error below.

>>> time.strptime("Tue, 30 Aug 2005 20:48:31","%a, %d %b %Y %H:%M:%S")
(2005, 8, 30, 20, 48, 31, 1, 242, -1)
>>> time.strptime("Tues, 30 Aug 2005 20:48:31","%a, %d %b %Y %H:%M:%S")
Traceback (most recent call last):
  File "", line 1, in ?
  File "C:\Python23\lib\_strptime.py", line 425, in strptime
raise ValueError("time data did not match format:  data=%s  fmt=%s" %
ValueError: time data did not match format:  data=Tues, 30 Aug 2005 20:48:31
fmt=%a, %d %b %Y %H:%M:%S
>>>


I also don't see how to alter the pattern for strptime to be tolerante for
more long weekday strings?
Any ideas?
Shouldn't it be possible to insert real regexp-like stuff in the pattern?

Robert


-- 
http://mail.python.org/mailman/listinfo/python-list


Zope side-by-side with Python 2.4

2004-12-03 Thread Robert
If I have Python 2.4 installed and I want to install the latest stable
Zope, will Zope have problems or does Zope looks to its own setup and
not my install of Python 2.4?

Robert
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How is Python designed?

2004-12-04 Thread Robert
Sorry for my interrupting the discussion : ).  I am a graduate student
in a chinese  university, and i am very interested in the Yuan
language. I'd like to join in the development of Yuan and do some work
for this language. BTW, i like this word, "Yuan" : )

I have 3 or 4 years experience in C++, and i have strong programming
skills about C++(at least in my opinion :) ) . I am interested in the
compiling technology and virtual machine technology. And i am reading
the  written by
Bill Blunden now. I had the thought of starting an open source project
which contains a virtual machine and a scripting language a few days
ago, just like a very simple python. Well, i know it is very difficult,
but it is my dream : ). Luckly i  found "Yuan" here. So it is my
pleasure if i can become a member of "Yuan" :)

Waiting for your reply. :)

Best regards.


Ru Chen

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: New versions breaking extensions, etc.

2004-12-10 Thread Robert
VS7 is a really a vastly different beastie than VS6.


On 12/10/04 9:31 PM, in article [EMAIL PROTECTED],
"Jive" <[EMAIL PROTECTED]> wrote:

> Can someone explain to me why Python 2.4 on MS Windows has these backward
> compatibility problems?  What am I missing?  Why won't extensions compiled
> to run with 2.3 also work with 2.4?  Why does it matter whether a component
> was compiled with VC++ 6.0 or 7.1?  I've been using MS stuff for 6 years.
> Before I started using Python, I don't remember ever having a program tell
> me I had to use an OLDER version of something to make the program work.
> Newer, maybe, but older, never.   I had to revert to Python 2.3 because I
> have applications that will not run under Python 2.4.
> 
> Although I have been a full-time software developer since the late 70's, I
> don't know much about Microsoft stuff.  (Amazing trick that, eh?) But I've
> been reading some things on the MS web pages that say dot-NET and all that
> should NOT break existing stuff.
> http://msdn.microsoft.com/library/en-us/vclib/html/_crt_c_run.2d.time_librarie
> s.asp I
> don't pretend to understand it all, but certainly one would not expect MS to
> screw things up too badly on purpose.  The value of MS Windows lies in the
> applications that it can run.  That's part of the genius of the Evil Genius.
> He understood when others didn't that when Joe Blow in his basement develops
> a program to run under MS Windows, Joe Blow is making money for the E.G.,
> and it doesn't cost the E.G. a penny.
> 
> In my own small domain,  I cannot even consider coming out with new releases
> that are not drop-in replacements for what went before.  Customers won't go
> for it.   It's an absolute deal-breaker.
> 
> What puzzles me is that, apparently, the incompatibility of Python 2.4 and
> extensions built for 2.3 did not come as a nasty surprise.  I get the
> impression that it was expected.  Can someone explain that to me?
> 
> I hope this doesn't sound like I'm complaining.  I think Python is great.
> And the price can't be beat!
> 
> 

-- 
http://mail.python.org/mailman/listinfo/python-list


SSL and Proxy problem with urllib2 ?

2005-04-05 Thread Robert
using a proxy and https/SSL together I get the following SSL error :

  File "dscore.pyo", line 2257, in UrlOpenEx
  File "ClientCookie\_urllib2_support.pyo", line 572, in open
  File "ClientCookie\_urllib2_support.pyo", line 472, in http_response
  File "ClientCookie\_urllib2_support.pyo", line 589, in error
  File "urllib2.pyo", line 304, in _call_chain
  File "ClientCookie\_urllib2_support.pyo", line 181, in http_error_302
  File "ClientCookie\_urllib2_support.pyo", line 566, in open
  File "urllib2.pyo", line 324, in open
  File "urllib2.pyo", line 304, in _call_chain
  File "ClientCookie\_urllib2_support.pyo", line 807, in https_open
  File "ClientCookie\_urllib2_support.pyo", line 656, in do_open
  File "httplib.pyo", line 712, in endheaders
  File "httplib.pyo", line 597, in _send_output
  File "httplib.pyo", line 564, in send
  File "httplib.pyo", line 985, in connect
  File "socket.pyo", line 73, in ssl
sslerror: (1, 'error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown 
protocol')

using python 2.3.5

any Ideas about the problem?

Robert
-- 
http://mail.python.org/mailman/listinfo/python-list


"unknown protocol" error in httplib using HTTPS

2005-04-05 Thread Robert
did you solve this problem? It seems to be still present here with
py2.3.5.

Robert

--
From: Manish Jethani <[EMAIL PROTECTED]>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.6b)
Gecko/20031205 Thunderbird/0.4
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: comp.lang.python
Subject: "unknown protocol" error in httplib using HTTPS
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 48
Message-ID: <[EMAIL PROTECTED]>
Date: Sun, 21 Dec 2003 18:44:53 +0530
NNTP-Posting-Host: 152.69.162.185
X-Trace: news.oracle.com 1072013963 152.69.162.185 (Sun, 21 Dec 2003
05:39:23 PST)
NNTP-Posting-Date: Sun, 21 Dec 2003 05:39:23 PST


I'm using Python 2.3.3 to do HTTPS using the httplib module. 
Here's the code:

   from httplib import HTTPSConnection
   con = HTTPSConnection('www-proxy', 80)
   con.set_debuglevel(1)
   con.request('GET',
 'https://nexus.passport.com:443/rdr/pprdr.asp')
   res = con.getresponse()
   print res.status, res.reason
   print res.read()

Here, "www-proxy" is the HTTPS proxy server that I have to go 
through (I can't connect directly). I'm trying to GET 
"https://nexus.passport.com:443/rdr/pprdr.asp"; via the proxy, 
but I'm getting a "unknown protocol" error in return:

   File "test.py", line 9, in ?
 con.request('GET', 
'https://nexus.passport.com:443/rdr/pprdr.asp')
   File "c:\python23\lib\httplib.py", line 718, in request
 self._send_request(method, url, body, headers)
   File "c:\python23\lib\httplib.py", line 739, in _send_request
 self.endheaders()
   File "c:\python23\lib\httplib.py", line 712, in endheaders
 self._send_output()
   File "c:\python23\lib\httplib.py", line 597, in _send_output
 self.send(msg)
   File "c:\python23\lib\httplib.py", line 564, in send
 self.connect()
   File "c:\python23\lib\httplib.py", line 985, in connect 
ssl = socket.ssl(sock, self.key_file, self.cert_file)
   File "c:\python23\lib\socket.py", line 73, in ssl
 return _realssl(sock, keyfile, certfile) socket.sslerror: 
(1, 'error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown 
protocol')

What's the problem here? I just want to use SSL over my HTTPS 
proxy using httplib.

Any help.

Thanks!

Manish

-- 
http://mannu.livejournal.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


pygtk and long running process

2005-04-24 Thread Robert
Hi,

I have a command line app that can take up to 20 minutes to complete and
every minute or so updates it's status (spits it out to console).  I am
writing a front end for this app in python/gtk and was wondering what
command I use to a) invoke the command and b) how to capture it's out put
and for instance update a text box.

Regards,

Rob
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pygtk and long running process

2005-04-25 Thread Robert
On Sun, 24 Apr 2005 19:46:52 -0600, Daniel Cer wrote:

> Daniel Cer wrote:
>> Robert wrote:
>> 
>>> I have a command line app that can take up to 20 minutes to complete and
>>> every minute or so updates it's status (spits it out to console).  I am
>>> writing a front end for this app in python/gtk and was wondering what
>>> command I use to a) invoke the command and b) how to capture it's out put
>>> and for instance update a text box.
>> 
>> 
>> os.popen() will probably do what you want.
>> 
>> e.g.:
> 
> ()
> 
> As for the latter part of (b)
> 
> I don't know much about pygtk, but I imagine you should be able to put 
> the code that monitors the external program in a thread, and then 
> whenever you get some new status information update the text box.
> 
> -Dan

Thanks for your help

-- 
http://mail.python.org/mailman/listinfo/python-list


Question about split method

2015-12-02 Thread Robert
Hi,

I learn split method online. When I try to run the line with ss1 beginning,
I don't understand why its output of ss1 and ss2. I have check the help
about split. It looks like that it is a numpy method.
What is the split method parameter (within "  ") for?


Thanks,



...
ss0="1, 2, 4, 8, 16".split(", ")

ss0
Out[2]: ['1', '2', '4', '8', '16']

ss1="1, 2, 4, 8, 16".split(", ")

ss1
Out[4]: ['1, 2, 4, 8, 16']

ss2="1, 2, 4, 8, 16".split(",   ")

ss2
Out[9]: ['1, 2, 4, 8, 16']

help(split)
Help on function split in module numpy.lib.shape_base:
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about split method

2015-12-02 Thread Robert
On Wednesday, December 2, 2015 at 3:45:34 PM UTC-5, Ian wrote:
> On Wed, Dec 2, 2015 at 2:37 PM, Robert  wrote:
> > Hi,
> >
> > I learn split method online. When I try to run the line with ss1 beginning,
> > I don't understand why its output of ss1 and ss2. I have check the help
> > about split. It looks like that it is a numpy method.
> > What is the split method parameter (within "  ") for?
> >
> >
> > Thanks,
> >
> >
> >
> > ...
> > ss0="1, 2, 4, 8, 16".split(", ")
> >
> > ss0
> > Out[2]: ['1', '2', '4', '8', '16']
> >
> > ss1="1, 2, 4, 8, 16".split(", ")
> >
> > ss1
> > Out[4]: ['1, 2, 4, 8, 16']
> >
> > ss2="1, 2, 4, 8, 16".split(",   ")
> >
> > ss2
> > Out[9]: ['1, 2, 4, 8, 16']
> >
> > help(split)
> > Help on function split in module numpy.lib.shape_base:
> 
> That's just some random function that you've imported into globals by
> doing "from numpy import *" or some such. What you're calling in these
> examples is a string method, not a global function.
> 
> Try help(str.split)

Thanks. I didn't know numpy has been automatically imported, not by me.
And your demo line code is helpful.
-- 
https://mail.python.org/mailman/listinfo/python-list


'string.join' is wrong in my Python console

2015-12-02 Thread Robert
Hi,

I read the tutorial on "Why is join() a string method instead of a list
 or tuple method?"
at link:
https://docs.python.org/2/faq/design.html#why-must-self-be-used-explicitly-in-method-definitions-and-calls

I have a problem on running the last line:
---
If none of these arguments persuade you, then for the moment you can
 continue to use the join() function from the string module, which allows
 you to write

string.join(['1', '2', '4', '8', '16'], ", ")
---

My Python console is 2.7. It should be no problem because I see the tutorial
is 2.7 too.

The console has these display:

string.join(['1', '2', '4', '8', '16'], ", ")
---
NameError Traceback (most recent call last)
 in ()
> 1 string.join(['1', '2', '4', '8', '16'], ", ")

NameError: name 'string' is not defined 


>From the context, I don't see string should be replaced by something else.

Could you tell me why I have such an error?


Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


Is there a way to set several list elements a same value with one line code

2015-12-03 Thread Robert
Hi,

I remember that there is a way to set several list elements a same value with
 one line code. Excuse me, I don't remember the accurate syntax on the code
 snippet. But the basic format looks like this.

1. There is a four-element list, such as: 
   bb=[[[]],[[]],[[]],[[]]]
2. An assignment line is here:
   bb[0]='a'
3. Then, all 4 element of bb is set with the above value.
   bb=[['a'],['a'],['a'],['a']]

The above three line codes are what I guess (I forgot the original tutorial
 now). Do you remember there is such a list application?


Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is there a way to set several list elements a same value with one line code

2015-12-03 Thread Robert
On Thursday, December 3, 2015 at 7:59:16 PM UTC-5, MRAB wrote:
> On 2015-12-04 00:30, Robert wrote:
> > Hi,
> >
> > I remember that there is a way to set several list elements a same value 
> > with
> >   one line code. Excuse me, I don't remember the accurate syntax on the code
> >   snippet. But the basic format looks like this.
> >
> > 1. There is a four-element list, such as:
> > bb=[[[]],[[]],[[]],[[]]]
> > 2. An assignment line is here:
> > bb[0]='a'
> > 3. Then, all 4 element of bb is set with the above value.
> > bb=[['a'],['a'],['a'],['a']]
> >
> > The above three line codes are what I guess (I forgot the original tutorial
> >   now). Do you remember there is such a list application?
> >
> Do you mean this behaviour:
> 
> >>> bb=[[[]]] * 4
> >>> print(bb)
> [[[]], [[]], [[]], [[]]]
> >>> bb[0][0]='a'
> >>> print(bb)
> [['a'], ['a'], ['a'], ['a']]
> 
> ?
> 
> That's because the bb contains 4 references to the same list.

Yes! What you post is I want. Thanks.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about split method

2015-12-05 Thread Robert
On Saturday, December 5, 2015 at 2:29:28 PM UTC-5, Peter Pearson wrote:
> On Wed, 2 Dec 2015 14:44:30 -0600, Ian Kelly  wrote:
> > On Wed, Dec 2, 2015 at 2:37 PM, Robert  wrote:
> [snip]
> >> ss0="1, 2, 4, 8, 16".split(", ")
> [snip]
> > Try help(str.split)
> 
> Or if, like me, you can't remember the magic word "str", ask:
> 
> help("".split)
> 
> and you know you're asking about the right "split".
> 
> -- 
> To email me, substitute nowhere->runbox, invalid->com.

Thanks for your smart method.
-- 
https://mail.python.org/mailman/listinfo/python-list


Help on for loop understanding

2015-12-07 Thread Robert
Hi,
When I learn for loop with below link:

http://www.shutupandship.com/2012/01/understanding-python-iterables-and.html

it has such explanation:

\
for loop under the hood

First let's look at the for loop under the hood. When Python executes the
 for loop, it first invokes the __iter__() method of the container to get the
 iterator of the container. It then repeatedly calls the next() method
 (__next__() method in Python 3.x) of the iterator until the iterator raises a
 StopIteration exception. Once the exception is raised, the for loop ends.
\

When I follow a list example from the above link, and one example of myself:

//
xx=[1,2,3,4,5]

xx.next
---
AttributeErrorTraceback (most recent call last)
 in ()
> 1 xx.next

AttributeError: 'list' object has no attribute 'next' 

xx.__iter__
Out[77]: 

for c in xx: print c
1
2
3
4
5
//

I am puzzled that the list examples have no next method, but it can run as
a for loop. Could you explain it to me the difference?

Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help on for loop understanding

2015-12-07 Thread Robert
On Monday, December 7, 2015 at 8:14:46 PM UTC-5, Robin Koch wrote:
> Am 08.12.2015 um 02:05 schrieb Robert:
> > Hi,
> > When I learn for loop with below link:
> >
> > http://www.shutupandship.com/2012/01/understanding-python-iterables-and.html
> >
> > it has such explanation:
> >
> > \
> > for loop under the hood
> >
> > First let's look at the for loop under the hood. When Python executes the
> >   for loop, it first invokes the __iter__() method of the container to get 
> > the
> >   iterator of the container. It then repeatedly calls the next() method
> >   (__next__() method in Python 3.x) of the iterator until the iterator 
> > raises a
> >   StopIteration exception. Once the exception is raised, the for loop ends.
> > \
> >
> > When I follow a list example from the above link, and one example of myself:
> >
> > //
> > xx=[1,2,3,4,5]
> >
> > xx.next
> > ---
> > AttributeErrorTraceback (most recent call last)
> >  in ()
> > > 1 xx.next
> >
> > AttributeError: 'list' object has no attribute 'next'
> >
> > xx.__iter__
> > Out[77]: 
> >
> > for c in xx: print c
> > 1
> > 2
> > 3
> > 4
> > 5
> > //
> >
> > I am puzzled that the list examples have no next method, but it can run as
> > a for loop. Could you explain it to me the difference?
> 
> Lists don't have a next method. Their iterators have:
> 
> xx.__iter__().__next__()
> 
> or
> 
> xxIterator = xx.__iter__()
> xxIterator.__next__()
> xxIterator.__next__()
> xxIterator.__next__()
> xxIterator.__next__()
> xxIterator.__next__()
> 
> That's also what your quoted paragraph states:
> 
> | It then repeatedly calls the next() method
> | (__next__() method in Python 3.x) of the iterator
> 
> -- 
> Robin Koch

I use Python 2.7. I have tried these commands:

xx=[1,2,3,4,5]

xx.__iter__
Out[2]: 

xx.__iter__().__next__()
---
AttributeErrorTraceback (most recent call last)
 in ()
> 1 xx.__iter__().__next__()

AttributeError: 'listiterator' object has no attribute '__next__' 

xx.__iter__.__next__
---
AttributeErrorTraceback (most recent call last)
 in ()
> 1 xx.__iter__.__next__

AttributeError: 'method-wrapper' object has no attribute '__next__' 

xx.__iter__()
Out[5]: 

xx.__iter__().__next__
---
AttributeErrorTraceback (most recent call last)
 in ()
> 1 xx.__iter__().__next__

AttributeError: 'listiterator' object has no attribute '__next__' 

xxIterator = xx.__iter__()

xxIterator
Out[8]: 

xxIterator.__next__()
---
AttributeErrorTraceback (most recent call last)
 in ()
> 1 xxIterator.__next__()

AttributeError: 'listiterator' object has no attribute '__next__' 

for c in xx: print c
1
2
3
4
5


I don't find a way to show __next__ yet. 
Can we explicitly get the iterator for a list?
Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help on for loop understanding

2015-12-07 Thread Robert
On Monday, December 7, 2015 at 8:32:30 PM UTC-5, Robert wrote:
> On Monday, December 7, 2015 at 8:14:46 PM UTC-5, Robin Koch wrote:
> > Am 08.12.2015 um 02:05 schrieb Robert:
> > > Hi,
> > > When I learn for loop with below link:
> > >
> > > http://www.shutupandship.com/2012/01/understanding-python-iterables-and.html
> > >
> > > it has such explanation:
> > >
> > > \
> > > for loop under the hood
> > >
> > > First let's look at the for loop under the hood. When Python executes the
> > >   for loop, it first invokes the __iter__() method of the container to 
> > > get the
> > >   iterator of the container. It then repeatedly calls the next() method
> > >   (__next__() method in Python 3.x) of the iterator until the iterator 
> > > raises a
> > >   StopIteration exception. Once the exception is raised, the for loop 
> > > ends.
> > > \
> > >
> > > When I follow a list example from the above link, and one example of 
> > > myself:
> > >
> > > //
> > > xx=[1,2,3,4,5]
> > >
> > > xx.next
> > > ---
> > > AttributeErrorTraceback (most recent call 
> > > last)
> > >  in ()
> > > > 1 xx.next
> > >
> > > AttributeError: 'list' object has no attribute 'next'
> > >
> > > xx.__iter__
> > > Out[77]: 
> > >
> > > for c in xx: print c
> > > 1
> > > 2
> > > 3
> > > 4
> > > 5
> > > //
> > >
> > > I am puzzled that the list examples have no next method, but it can run as
> > > a for loop. Could you explain it to me the difference?
> > 
> > Lists don't have a next method. Their iterators have:
> > 
> > xx.__iter__().__next__()
> > 
> > or
> > 
> > xxIterator = xx.__iter__()
> > xxIterator.__next__()
> > xxIterator.__next__()
> > xxIterator.__next__()
> > xxIterator.__next__()
> > xxIterator.__next__()
> > 
> > That's also what your quoted paragraph states:
> > 
> > | It then repeatedly calls the next() method
> > | (__next__() method in Python 3.x) of the iterator
> > 
> > -- 
> > Robin Koch
> 
> I use Python 2.7. I have tried these commands:
> 
> xx=[1,2,3,4,5]
> 
> xx.__iter__
> Out[2]: 
> 
> xx.__iter__().__next__()
> ---
> AttributeErrorTraceback (most recent call last)
>  in ()
> > 1 xx.__iter__().__next__()
> 
> AttributeError: 'listiterator' object has no attribute '__next__' 
> 
> xx.__iter__.__next__
> ---
> AttributeErrorTraceback (most recent call last)
>  in ()
> > 1 xx.__iter__.__next__
> 
> AttributeError: 'method-wrapper' object has no attribute '__next__' 
> 
> xx.__iter__()
> Out[5]: 
> 
> xx.__iter__().__next__
> ---
> AttributeErrorTraceback (most recent call last)
>  in ()
> > 1 xx.__iter__().__next__
> 
> AttributeError: 'listiterator' object has no attribute '__next__' 
> 
> xxIterator = xx.__iter__()
> 
> xxIterator
> Out[8]: 
> 
> xxIterator.__next__()
> ---
> AttributeErrorTraceback (most recent call last)
>  in ()
> > 1 xxIterator.__next__()
> 
> AttributeError: 'listiterator' object has no attribute '__next__' 
> 
> for c in xx: print c
> 1
> 2
> 3
> 4
> 5
> 
> 
> I don't find a way to show __next__ yet. 
> Can we explicitly get the iterator for a list?
> Thanks,

Excuse me. I find it as the following:

xx.__iter__().next
Out[16]: 

xx.__iter__().next()
Out[17]: 1


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help on for loop understanding

2015-12-07 Thread Robert
On Monday, December 7, 2015 at 8:39:48 PM UTC-5, Robert wrote:
> On Monday, December 7, 2015 at 8:32:30 PM UTC-5, Robert wrote:
> > On Monday, December 7, 2015 at 8:14:46 PM UTC-5, Robin Koch wrote:
> > > Am 08.12.2015 um 02:05 schrieb Robert:
> > > > Hi,
> > > > When I learn for loop with below link:
> > > >
> > > > http://www.shutupandship.com/2012/01/understanding-python-iterables-and.html
> > > >
> > > > it has such explanation:
> > > >
> > > > \
> > > > for loop under the hood
> > > >
> > > > First let's look at the for loop under the hood. When Python executes 
> > > > the
> > > >   for loop, it first invokes the __iter__() method of the container to 
> > > > get the
> > > >   iterator of the container. It then repeatedly calls the next() method
> > > >   (__next__() method in Python 3.x) of the iterator until the iterator 
> > > > raises a
> > > >   StopIteration exception. Once the exception is raised, the for loop 
> > > > ends.
> > > > \
> > > >
> > > > When I follow a list example from the above link, and one example of 
> > > > myself:
> > > >
> > > > //
> > > > xx=[1,2,3,4,5]
> > > >
> > > > xx.next
> > > > ---
> > > > AttributeErrorTraceback (most recent call 
> > > > last)
> > > >  in ()
> > > > > 1 xx.next
> > > >
> > > > AttributeError: 'list' object has no attribute 'next'
> > > >
> > > > xx.__iter__
> > > > Out[77]:  > > > 0x0A1ACE08>
> > > >
> > > > for c in xx: print c
> > > > 1
> > > > 2
> > > > 3
> > > > 4
> > > > 5
> > > > //
> > > >
> > > > I am puzzled that the list examples have no next method, but it can run 
> > > > as
> > > > a for loop. Could you explain it to me the difference?
> > > 
> > > Lists don't have a next method. Their iterators have:
> > > 
> > > xx.__iter__().__next__()
> > > 
> > > or
> > > 
> > > xxIterator = xx.__iter__()
> > > xxIterator.__next__()
> > > xxIterator.__next__()
> > > xxIterator.__next__()
> > > xxIterator.__next__()
> > > xxIterator.__next__()
> > > 
> > > That's also what your quoted paragraph states:
> > > 
> > > | It then repeatedly calls the next() method
> > > | (__next__() method in Python 3.x) of the iterator
> > > 
> > > -- 
> > > Robin Koch
> > 
> > I use Python 2.7. I have tried these commands:
> > 
> > xx=[1,2,3,4,5]
> > 
> > xx.__iter__
> > Out[2]: 
> > 
> > xx.__iter__().__next__()
> > ---
> > AttributeErrorTraceback (most recent call last)
> >  in ()
> > > 1 xx.__iter__().__next__()
> > 
> > AttributeError: 'listiterator' object has no attribute '__next__' 
> > 
> > xx.__iter__.__next__
> > ---
> > AttributeErrorTraceback (most recent call last)
> >  in ()
> > > 1 xx.__iter__.__next__
> > 
> > AttributeError: 'method-wrapper' object has no attribute '__next__' 
> > 
> > xx.__iter__()
> > Out[5]: 
> > 
> > xx.__iter__().__next__
> > ---
> > AttributeErrorTraceback (most recent call last)
> >  in ()
> > > 1 xx.__iter__().__next__
> > 
> > AttributeError: 'listiterator' object has no attribute '__next__' 
> > 
> > xxIterator = xx.__iter__()
> > 
> > xxIterator
> > Out[8]: 
> > 
> > xxIterator.__next__()
> > ---
> > AttributeErrorTraceback (most recent call last)
> >  in ()
> > > 1 xxIterator.__next__()
> > 
> > AttributeError: 'listiterator' object has no attribute '__next__' 
> > 
> > for c in xx: print c
> > 1
> > 2
> > 3
> > 4
> > 5
> > 
> > 
> > I don't find a way to show __next__ yet. 
> > Can we explicitly get the iterator for a list?
> > Thanks,
> 
> Excuse me. I find it as the following:
> 
> xx.__iter__().next
> Out[16]: 
> 
> xx.__iter__().next()
> Out[17]: 1

One example, see below please, is in the above mentioned link. I don't see
the purpose of the example.


\\\
class MyList(list):
def __iter__(self):
return MyListIter(self)

class MyListIter(object):
""" A sample implementation of a list iterator. NOTE: This is just a 
demonstration of concept!!! YOU SHOULD NEVER IMPLEMENT SOMETHING LIKE THIS!
Even if you have to (for any reason), there are many better ways to 
implement this."""
def __init__(self, lst):
self.lst = lst
self.i = -1
def __iter__(self):
return self
def next(self):
if self.ihttps://mail.python.org/mailman/listinfo/python-list


Re: Help on for loop understanding

2015-12-08 Thread Robert
On Monday, December 7, 2015 at 10:24:09 PM UTC-5, Chris Angelico wrote:
> On Tue, Dec 8, 2015 at 1:36 PM, Erik  wrote:
> > So, you can write your class's iterator to do anything that makes sense when
> > someone says "for i in myclassinstance:".
> >
> > If your class is a subclass of a class ("is-a") that already has a defined
> > iterator (such as a list or a dict) and the behaviour of that is correct for
> > you, then you need to do nothing (you inherit that class's __iter__()
> > method).
> >
> > If your class should iterate over an embedded object ("has-a") that already
> > has a defined iterator, then your __iter__() method can just delegate to
> > that object's iterator using something like:
> >
> > def __iter__(self):
> > return iter(self.embedded_thing)
> 
> Another great way to write an __iter__ method is as a generator.
> 
> def __iter__(self):
> yield "thing"
> yield from self.things
> yield "other thing"
> 
> Like returning an embedded object's iterator, this saves you having to
> write a __next__ method. The less work you do, the less bugs you get.
> 
> ChrisA

Thanks. One more question is here. I see the following code snippet. 
It can run as expected. That is, zeros array is iterable, but I don't see
the iterator of it. And I do see some codes using np.nditer, but the 
following without it.


seq = [ im for im in zeros((20,240,320), int)]

1. What difference for iterate with and without np.nditer?
2. How can I know one object whether it is iterable (only by testing?)

-- 
https://mail.python.org/mailman/listinfo/python-list


Is there a data type rule on 'x+=' and 'x0=x+'?

2015-12-09 Thread Robert
Hi,

I have a numpy np.array data x:

x= np.array([[157, 185], [169, 195], [162, 179], [145, 195], [159, 199],
 [165, 180], [172, 195], [180, 201], [163, 195], [169, 191]])

Here is the original code snippet:

x+=uniform(-8, 8, x.shape)

x
Out[557]: 
array([[163, 192],
   [163, 187],
   [171, 200],
   [165, 186],
   [162, 185],
   [160, 193],
   [156, 194],
   [168, 197],
   [186, 207],
   [184, 208]])


I am curious about the integer data type of result x.
When I use this code:

x0=x+uniform(-8, 8, x.shape)

x0 is a floating format:

x0
Out[555]: 
array([[ 150.84633535,  188.93254259],
   [ 170.29185044,  197.22753051],
   [ 175.65371771,  190.59168953],
   [ 159.61401655,  175.06364015],
   [ 168.35531363,  180.19243277],
   [ 163.79970077,  206.28600694],
   [ 157.89342616,  203.39444556],
   [ 179.86956647,  192.91265609],
   [ 182.48075601,  208.02397713],
   [ 187.59332123,  207.41482024]])


I would like to know what rule makes these two different result formats.

Could you help me?

Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


Why doesn't response pydoc on my Python 2.7?

2015-12-12 Thread Robert
Hi,

I want to use pydoc as some online tutorial shows, but it cannot run as 
below. What is wrong?


Thanks,




>>> import pydoc
>>> pydoc

>>> pydoc sys
SyntaxError: invalid syntax
>>> import sys
>>> pydoc sys
SyntaxError: invalid syntax
>>> help(pydoc)
Help on module pydoc:
..
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why doesn't response pydoc on my Python 2.7?

2015-12-12 Thread Robert
On Saturday, December 12, 2015 at 6:02:11 PM UTC-5, Robert wrote:
> Hi,
> 
> I want to use pydoc as some online tutorial shows, but it cannot run as 
> below. What is wrong?
> 
> 
> Thanks,
> 
> 
> 
> 
> >>> import pydoc
> >>> pydoc
> 
> >>> pydoc sys
> SyntaxError: invalid syntax
> >>> import sys
> >>> pydoc sys
> SyntaxError: invalid syntax
> >>> help(pydoc)
> Help on module pydoc:
> ..

In fact, I wanted to run the following code. When it failed, I moved to
the original question above.

Anyone can help? Thanks,

//
It also changes what pydoc will show:

module1.py

a = "A"
b = "B"
c = "C"
module2.py

__all__ = ['a', 'b']

a = "A"
b = "B"
c = "C"
$ pydoc module1
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why doesn't response pydoc on my Python 2.7?

2015-12-12 Thread Robert
On Saturday, December 12, 2015 at 6:24:25 PM UTC-5, Erik wrote:
> On 12/12/15 23:08, Robert wrote:
> > In fact, I wanted to run the following code. When it failed, I moved to
> > the original question above.
> 
> How did it fail? Tell us what _did_ happen.
> 
> It works fine for me:
> 
> $ pydoc module1
> Help on module module1:
> 
> NAME
>  module1
> 
> FILE
>  /tmp/robert/module1.py
> 
> DATA
>  a = 'A'
>  b = 'B'
>  c = 'C'
> 
> 
> $ pydoc module2
> Help on module module2:
> 
> NAME
>  module2
> 
> FILE
>  /tmp/robert/module2.py
> 
> DATA
>  __all__ = ['a', 'b']
>  a = 'A'
>  b = 'B'
> 
> E.

Excuse me for the incomplete information on previous posts.
Here is the message when I run it on Canopy (module1.py and module2.py 
are in the current folder):

Welcome to Canopy's interactive data-analysis environment!
 with pylab-backend set to: qt
Type '?' for more information.

In [1]: pydoc module1
  File "", line 1
pydoc module1
^
SyntaxError: invalid syntax
 

In [2]: 


The above code snippet is from here:
http://stackoverflow.com/questions/44834/can-someone-explain-all-in-python

Thanks again.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why doesn't response pydoc on my Python 2.7?

2015-12-12 Thread Robert
On Saturday, December 12, 2015 at 7:05:39 PM UTC-5, Robert wrote:
> On Saturday, December 12, 2015 at 6:24:25 PM UTC-5, Erik wrote:
> > On 12/12/15 23:08, Robert wrote:
> > > In fact, I wanted to run the following code. When it failed, I moved to
> > > the original question above.
> > 
> > How did it fail? Tell us what _did_ happen.
> > 
> > It works fine for me:
> > 
> > $ pydoc module1
> > Help on module module1:
> > 
> > NAME
> >  module1
> > 
> > FILE
> >  /tmp/robert/module1.py
> > 
> > DATA
> >  a = 'A'
> >  b = 'B'
> >  c = 'C'
> > 
> > 
> > $ pydoc module2
> > Help on module module2:
> > 
> > NAME
> >  module2
> > 
> > FILE
> >  /tmp/robert/module2.py
> > 
> > DATA
> >  __all__ = ['a', 'b']
> >  a = 'A'
> >  b = 'B'
> > 
> > E.
> 
> Excuse me for the incomplete information on previous posts.
> Here is the message when I run it on Canopy (module1.py and module2.py 
> are in the current folder):
> 
> Welcome to Canopy's interactive data-analysis environment!
>  with pylab-backend set to: qt
> Type '?' for more information.
> 
> In [1]: pydoc module1
>   File "", line 1
> pydoc module1
> ^
> SyntaxError: invalid syntax
>  
> 
> In [2]: 
> 
> 
> The above code snippet is from here:
> http://stackoverflow.com/questions/44834/can-someone-explain-all-in-python
> 
> Thanks again.

Hi,
It turns out that Enthought does not allow pydoc as the link said:
http://stackoverflow.com/questions/12063718/using-help-and-pydoc-to-list-python-modules-not-working

Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why doesn't response pydoc on my Python 2.7?

2015-12-12 Thread Robert
On Saturday, December 12, 2015 at 7:36:21 PM UTC-5, Peter Otten wrote:
> Robert wrote:
> 
> > On Saturday, December 12, 2015 at 7:05:39 PM UTC-5, Robert wrote:
> >> On Saturday, December 12, 2015 at 6:24:25 PM UTC-5, Erik wrote:
> >> > On 12/12/15 23:08, Robert wrote:
> >> > > In fact, I wanted to run the following code. When it failed, I moved
> >> > > to the original question above.
> >> > 
> >> > How did it fail? Tell us what _did_ happen.
> >> > 
> >> > It works fine for me:
> >> > 
> >> > $ pydoc module1
> >> > Help on module module1:
> >> > 
> >> > NAME
> >> >  module1
> >> > 
> >> > FILE
> >> >  /tmp/robert/module1.py
> >> > 
> >> > DATA
> >> >  a = 'A'
> >> >  b = 'B'
> >> >  c = 'C'
> >> > 
> >> > 
> >> > $ pydoc module2
> >> > Help on module module2:
> >> > 
> >> > NAME
> >> >  module2
> >> > 
> >> > FILE
> >> >  /tmp/robert/module2.py
> >> > 
> >> > DATA
> >> >  __all__ = ['a', 'b']
> >> >  a = 'A'
> >> >  b = 'B'
> >> > 
> >> > E.
> >> 
> >> Excuse me for the incomplete information on previous posts.
> >> Here is the message when I run it on Canopy (module1.py and module2.py
> >> are in the current folder):
> >> 
> >> Welcome to Canopy's interactive data-analysis environment!
> >>  with pylab-backend set to: qt
> >> Type '?' for more information.
> >> 
> >> In [1]: pydoc module1
> >>   File "", line 1
> >> pydoc module1
> >> ^
> >> SyntaxError: invalid syntax
> >>  
> >> 
> >> In [2]:
> >> 
> >> 
> >> The above code snippet is from here:
> >> http://stackoverflow.com/questions/44834/can-someone-explain-all-in-python
> >> 
> >> Thanks again.
> > 
> > Hi,
> > It turns out that Enthought does not allow pydoc as the link said:
> > http://stackoverflow.com/questions/12063718/using-help-and-pydoc-to-list-python-modules-not-working
> 
> This is completely unrelated.
> 
> >>> help("modules")
> 
> for the specific string "modules" triggers a scan for all available modules. 
> For other strings like "module1" that represent a module name
> 
> >>> help("module1")
> 
> should work unless
> 
> >>> import module1
> 
> fails, too.

Thanks Peter and others. It previously may not be at the right directory.
After that, import pydoc, it works. Great thanks with the following command.


pydoc.help('module1')
Help on module module1:

NAME
module1

FILE
c:\users\rj\pyprj\module1.py

DATA
a = 'A'
b = 'B'
c = 'C'
-- 
https://mail.python.org/mailman/listinfo/python-list


Help on class understanding in pymc code

2015-12-13 Thread Robert
Hi,

I follow code example at link:

https://users.obs.carnegiescience.edu/cburns/ipynbs/PyMC.html


There is the following code line:

sampler = pymc.MCMC([alpha,betax,betay,eps,model,tau,z_obs,x_true,y_true])


I want to know the detail of pymc.MCMC, then I get help content of it with:

/
help(pymc.MCMC)
Help on class MCMC in module pymc.MCMC:

class MCMC(pymc.Model.Sampler)
 |  This class fits probability models using Markov Chain Monte Carlo. Each 
stochastic variable
 |  is assigned a StepMethod object, which makes it take a single MCMC step 
conditional on the
 |  rest of the model. These step methods are called in turn.
 |  
 |>>> A = MCMC(input, db, verbose=0)
 |  
\\


help('pymc.Model.Sampler')
no Python documentation found for 'pymc.Model.Sampler'


help('pymc.Model')
Help on class Model in pymc:

pymc.Model = class Model(pymc.Container.ObjectContainer)
 |  The base class for all objects that fit probability models. Model is 
initialized with:
 |  
 |>>> A = Model(input, verbose=0)
 |  
 |:Parameters:
 |  - input : module, list, tuple, dictionary, set, object or nothing.
 |  Model definition, in terms of Stochastics, Deterministics, 
Potentials and Containers.
 |  If nothing, all nodes are collected from the base namespace.
 |  
 |  Attributes:
 |- deterministics
 |- stochastics (with observed=False)
 |- data (stochastic variables with observed=True)
 |- variables
 |- potentials
 |- containers
 |- nodes
 |- all_objects
 |- status: Not useful for the Model base class, but may be used by 
subclasses.
 |  
 |  The following attributes only exist after the appropriate method is called:
 |- moral_neighbors: The edges of the moralized graph. A dictionary, keyed 
by stochastic variable,
 |  whose values are sets of stochastic variables. Edges exist between the 
key variable and all variables
 |  in the value. Created by method _moralize.
 |- extended_children: The extended children of self's stochastic 
variables. See the docstring of
 |  extend_children. This is a dictionary keyed by stochastic variable.
 |- generations: A list of sets of stochastic variables. The members of 
each element only have parents in
 |  previous elements. Created by method find_generations.
 |  
 |  Methods:
 | - sample_model_likelihood(iter): Generate and return iter samples of 
p(data and potentials|model).
 |   Can be used to generate Bayes' factors.
 |  
 |  :SeeAlso: Sampler, MAP, NormalApproximation, weight, Container, graph.
 |  
 |  Method resolution order:
 |  Model
 |  pymc.Container.ObjectContainer
 |  pymc.six.NewBase
 |  pymc.Node.ContainerBase
 |  __builtin__.object
 |  
 |  Methods defined here:
 |  
 |  __init__(self, input=None, name=None, verbose=-1)
 |  Initialize a Model instance.
 |  
 |  :Parameters:
 |- input : module, list, tuple, dictionary, set, object or nothing.
 |Model definition, in terms of Stochastics, Deterministics, 
Potentials and Containers.
 |If nothing, all nodes are collected from the base namespace.
 |  
 |  draw_from_prior(self)
 |  Sets all variables to random values drawn from joint 'prior', meaning 
contributions
 |  of data and potentials to the joint distribution are not considered.
 |  
 |  get_node(self, node_name)
 |  Retrieve node with passed name
 |  
 |  seed(self)
 |  Seed new initial values for the stochastics.
 |  
 |  --
 |  Data descriptors defined here:
 |  
 |  generations
 |  
 |  --
 |  Data and other attributes defined here:
 |  
 |  __slotnames__ = []
 |  
 |  register = False
 |  
 |  --
 |  Methods inherited from pymc.Container.ObjectContainer:
 |  
 |  replace(self, item, new_container, key)
 |  
 |  --
 |  Data descriptors inherited from pymc.Container.ObjectContainer:
 |  
 |  value
 |  A copy of self, with all variables replaced by their values.
 |  
 |  --
 |  Methods inherited from pymc.Node.ContainerBase:
 |  
 |  assimilate(self, new_container)
 |  
 |  --
 |  Data descriptors inherited from pymc.Node.ContainerBase:
 |  
 |  __dict__
 |  dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |  list of weak references to the object (if defined)
 |  
 |  logp
 |  The summed log-probability of all stochastic variables (data
 |  or otherwise) and factor potentials in self.
 |  
 |  --
 |  Data and other attributes inherited from pymc.Node.Container

Re: Help on class understanding in pymc code

2015-12-13 Thread Robert
On Sunday, December 13, 2015 at 8:10:25 PM UTC-5, Peter Otten wrote:
> Robert wrote:
> 
> > Hi,
> > 
> > I follow code example at link:
> > 
> > https://users.obs.carnegiescience.edu/cburns/ipynbs/PyMC.html
> > 
> > 
> > There is the following code line:
> > 
> > sampler = pymc.MCMC([alpha,betax,betay,eps,model,tau,z_obs,x_true,y_true])
> > 
> > 
> > I want to know the detail of pymc.MCMC, then I get help content of it
> > with:
> > 
> > /
> > help(pymc.MCMC)
> > Help on class MCMC in module pymc.MCMC:
> > 
> > class MCMC(pymc.Model.Sampler)
> >  |  This class fits probability models using Markov Chain Monte Carlo.
> >  |  Each stochastic variable is assigned a StepMethod object, which makes
> >  |  it take a single MCMC step conditional on the rest of the model. These
> >  |  step methods are called in turn.
> >  |  
> >  |>>> A = MCMC(input, db, verbose=0)
> >  |  
> > \\
> > 
> > 
> > help('pymc.Model.Sampler')
> > no Python documentation found for 'pymc.Model.Sampler'
> > 
> > 
> > help('pymc.Model')
> > Help on class Model in pymc:
> > 
> > pymc.Model = class Model(pymc.Container.ObjectContainer)
> >  |  The base class for all objects that fit probability models. Model is
> >  |  initialized with:
> >  |  
> >  |>>> A = Model(input, verbose=0)
> >  |  
> >  |:Parameters:
> >  |  - input : module, list, tuple, dictionary, set, object or nothing.
> >  |  Model definition, in terms of Stochastics, Deterministics,
> >  |  Potentials and Containers. If nothing, all nodes are collected
> >  |  from the base namespace.
> >  |  
> >  |  Attributes:
> >  |- deterministics
> >  |- stochastics (with observed=False)
> >  |- data (stochastic variables with observed=True)
> >  |- variables
> >  |- potentials
> >  |- containers
> >  |- nodes
> >  |- all_objects
> >  |- status: Not useful for the Model base class, but may be used by
> >  |subclasses.
> >  |  
> >  |  The following attributes only exist after the appropriate method is
> >  |  called:
> >  |- moral_neighbors: The edges of the moralized graph. A dictionary,
> >  |keyed by stochastic variable,
> >  |  whose values are sets of stochastic variables. Edges exist between
> >  |  the key variable and all variables in the value. Created by method
> >  |  _moralize.
> >  |- extended_children: The extended children of self's stochastic
> >  |variables. See the docstring of
> >  |  extend_children. This is a dictionary keyed by stochastic
> >  |  variable.
> >  |- generations: A list of sets of stochastic variables. The members
> >  |of each element only have parents in
> >  |  previous elements. Created by method find_generations.
> >  |  
> >  |  Methods:
> >  | - sample_model_likelihood(iter): Generate and return iter samples
> >  | of p(data and potentials|model).
> >  |   Can be used to generate Bayes' factors.
> >  |  
> >  |  :SeeAlso: Sampler, MAP, NormalApproximation, weight, Container, graph.
> >  |  
> >  |  Method resolution order:
> >  |  Model
> >  |  pymc.Container.ObjectContainer
> >  |  pymc.six.NewBase
> >  |  pymc.Node.ContainerBase
> >  |  __builtin__.object
> >  |  
> >  |  Methods defined here:
> >  |  
> >  |  __init__(self, input=None, name=None, verbose=-1)
> >  |  Initialize a Model instance.
> >  |  
> >  |  :Parameters:
> >  |- input : module, list, tuple, dictionary, set, object or
> >  |nothing.
> >  |Model definition, in terms of Stochastics, Deterministics,
> >  |Potentials and Containers. If nothing, all nodes are
> >  |collected from the base namespace.
> >  |  
> >  |  draw_from_prior(self)
> >  |  Sets all variables to random values drawn from joint 'prior',
> >  |  meaning contributions of data and potentials to the joint
> >  |  distribution are not considered.
> >  |  
> >  |  get_node(self, node_name)
> >  |  Retrieve node with passed name
> >  |  
> >  |  seed(self)
> >  |  Seed new initial values for the stochastics.
> >  |  
> >  |  -

Question about figure plot

2015-12-14 Thread Robert
Hi,

When I run the following code, there is no figure shown in the end.


//
import pymc
import numpy as np

n = 5*np.ones(4,dtype=int)
x = np.array([-.86,-.3,-.05,.73])

alpha = pymc.Normal('alpha',mu=0,tau=.01)
beta = pymc.Normal('beta',mu=0,tau=.01)

@pymc.deterministic
def theta(a=alpha, b=beta):
"""theta = logit^{-1}(a+b)"""
return pymc.invlogit(a+b*x)

d = pymc.Binomial('d', n=n, p=theta, value=np.array([0.,1.,3.,5.]),\
observed=True)

import pymc
import mymodel

S = pymc.MCMC(mymodel, db='pickle')
S.sample(iter=1, burn=5000, thin=2)
pymc.Matplot.plot(S)



I find that the figures are shown after these two lines by myself:
*
import matplotlib.pyplot as plt
plt.show()

I have searched around and have not found some explanation about it.
The plot function here is different from Matlab's. Is there better ways than
my last two lines? (I am not confident whether my last two lines is the 
only choice.

Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


Help on code comprehension from an example project of pymc

2015-12-15 Thread Robert
Hi,

I find the useful small code project for me:
#https://users.obs.carnegiescience.edu/cburns/ipynbs/PyMC.html

It runs as expected.

When I review the code, I find 'data' in the original line:

data = pymc.Normal('data', mu=model, tau=tau, value=z_obs, observed=True)

has not been referenced thereafter.
If I comment out the line as:

#data = pymc.Normal('data', mu=model, tau=tau, value=z_obs, observed=True)

the result is ugly different from the original.

If I change it to:

pymc.Normal('data', mu=model, tau=tau, value=z_obs, observed=True)


it still runs as the original. Reading the last half part 
(after data= line), I cannot see anything uses 'data', though I suspect
below line may use it:
sampler = pymc.MCMC([alpha,betax,betay,eps,model,tau,z_obs,x_true,y_true])

This is quite different from my any other language experience.

Could you help me on this?

Thanks,





--
#https://users.obs.carnegiescience.edu/cburns/ipynbs/PyMC.html
from numpy import *
Nobs = 20
x_true = random.uniform(0,10, size=Nobs)
y_true = random.uniform(-1,1, size=Nobs)
alpha_true = 0.5
beta_x_true = 1.0
beta_y_true = 10.0
eps_true = 0.5
z_true = alpha_true + beta_x_true*x_true + beta_y_true*y_true
z_obs = z_true + random.normal(0, eps_true, size=Nobs)


import pymc
# define the parameters with their associated priors

#alpha = pymc.Normal('alpha',mu=0,tau=.01)
#beta = pymc.Normal('beta',mu=0,tau=.01)

alpha = pymc.Uniform('alpha', -100,100, value=median(z_obs))
betax = pymc.Uniform('betax', -100,100, value=std(z_obs)/std(x_true))
betay = pymc.Uniform('betay', -100,100, value=std(z_obs)/std(y_true))
eps   = pymc.Uniform('eps',  0,100, value=0.01)

# Now define the model
@pymc.deterministic
def model(alpha=alpha, betax=betax, betay=betay, x=x_true, y=y_true):
return alpha + betax*x + betay*y

# pymc parametrizes the width of the normal distribution by tau=1/sigma**2
@pymc.deterministic
def tau(eps=eps):
return power(eps, -2)

# Lastly relate the model/parameters to the data
#data = pymc.Normal('data', mu=model, tau=tau, value=z_obs, observed=True)
pymc.Normal('data0', mu=model, tau=tau, value=z_obs, observed=True)

sampler = pymc.MCMC([alpha,betax,betay,eps,model,tau,z_obs,x_true,y_true])
sampler.use_step_method(pymc.AdaptiveMetropolis, [alpha,betax,betay,eps],
scales={alpha:0.1, betax:0.1, betay:1.0, eps:0.1})
sampler.sample(iter=1)

pymc.Matplot.plot(sampler)

sampler.sample(iter=1)

alpha.summary()

m_alpha = median(alpha.trace())
m_betax = median(betax.trace())
m_betay = median(betay.trace())
m_eps = median(eps.trace())


import matplotlib.pyplot as plt
plt.figure(figsize=(12,6))
plt.subplot(1,2,1)
plt.plot(x_true, z_obs-m_alpha-m_betay*y_true, 'o')
plt.xlabel('X')
plt.ylabel('Z - alpha - beta_y y')
# Now plot the model
xx = array([x_true.min(), x_true.max()])
plt.plot(xx, xx*m_betax)
plt.plot(xx, xx*m_betax + m_eps, '--', color='k')
plt.plot(xx, xx*m_betax - m_eps, '--', color='k')
plt.subplot(1,2,2)
plt.plot(y_true, z_obs-m_alpha-m_betax*x_true, 'o')
plt.xlabel('Y')
plt.ylabel('Z - alpha - beta_x x')
yy = array([y_true.min(), y_true.max()])
plt.plot(yy, yy*m_betay)
plt.plot(yy, yy*m_betay + m_eps, '--', color='k')
plt.plot(yy, yy*m_betay - m_eps, '--', color='k')
plt.show()
-- 
https://mail.python.org/mailman/listinfo/python-list


Help on error " ValueError: For numerical factors, num_columns must be an int "

2015-12-16 Thread Robert
Hi,

When I run the following code, there is an error:

ValueError: For numerical factors, num_columns must be an int 



import numpy as np
import pandas as pd
from patsy import dmatrices
from sklearn.linear_model import LogisticRegression

X = [0.5,0.75,1.0,1.25,1.5,1.75,1.75,2.0,2.25,2.5,2.75,3.0,3.25,
3.5,4.0,4.25,4.5,4.75,5.0,5.5]
y = [0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1]

zipped = list(zip(X,y))
df = pd.DataFrame(zipped,columns = ['study_hrs','p_or_f'])

y, X = dmatrices('p_or_f ~ study_hrs', df, return_type="dataframe")
===

I have check 'df' is this type:
=
type(df)
Out[25]: pandas.core.frame.DataFrame
=

I cannot figure out where the problem is. Can you help me?
Thanks.

Error message:
..


---
ValueErrorTraceback (most recent call last)
C:\Users\rj\pyprj\stackoverflow_logisticregression0.py in ()
 17 df = pd.DataFrame(zipped,columns = ['study_hrs','p_or_f'])
 18 
---> 19 y, X = dmatrices('p_or_f ~ study_hrs', df, return_type="dataframe")
 20 
 21 y = np.ravel(y)

C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\highlevel.pyc
 in dmatrices(formula_like, data, eval_env, NA_action, return_type)
295 eval_env = EvalEnvironment.capture(eval_env, reference=1)
296 (lhs, rhs) = _do_highlevel_design(formula_like, data, eval_env,
--> 297   NA_action, return_type)
298 if lhs.shape[1] == 0:
299 raise PatsyError("model is missing required outcome variables")

C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\highlevel.pyc
 in _do_highlevel_design(formula_like, data, eval_env, NA_action, return_type)
150 return iter([data])
151 design_infos = _try_incr_builders(formula_like, data_iter_maker, 
eval_env,
--> 152   NA_action)
153 if design_infos is not None:
154 return build_design_matrices(design_infos, data,

C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\highlevel.pyc
 in _try_incr_builders(formula_like, data_iter_maker, eval_env, NA_action)
 55   data_iter_maker,
 56   eval_env,
---> 57   NA_action)
 58 else:
 59 return None

C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\build.pyc
 in design_matrix_builders(termlists, data_iter_maker, eval_env, NA_action)
704 factor_states[factor],
705 num_columns=num_column_counts[factor],
--> 706 categories=None)
707 else:
708 assert factor in cat_levels_contrasts

C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\design_info.pyc
 in __init__(self, factor, type, state, num_columns, categories)
 86 if self.type == "numerical":
 87 if not isinstance(num_columns, int):
---> 88 raise ValueError("For numerical factors, num_columns "
 89  "must be an int")
 90 if categories is not None:

ValueError: For numerical factors, num_columns must be an int 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help on error " ValueError: For numerical factors, num_columns must be an int "

2015-12-16 Thread Robert
On Wednesday, December 16, 2015 at 5:44:21 AM UTC-5, Robert wrote:
> Hi,
> 
> When I run the following code, there is an error:
> 
> ValueError: For numerical factors, num_columns must be an int 
> 
> 
> 
> import numpy as np
> import pandas as pd
> from patsy import dmatrices
> from sklearn.linear_model import LogisticRegression
> 
> X = [0.5,0.75,1.0,1.25,1.5,1.75,1.75,2.0,2.25,2.5,2.75,3.0,3.25,
> 3.5,4.0,4.25,4.5,4.75,5.0,5.5]
> y = [0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1]
> 
> zipped = list(zip(X,y))
> df = pd.DataFrame(zipped,columns = ['study_hrs','p_or_f'])
> 
> y, X = dmatrices('p_or_f ~ study_hrs', df, return_type="dataframe")
> ===
> 
> I have check 'df' is this type:
> =
> type(df)
> Out[25]: pandas.core.frame.DataFrame
> =
> 
> I cannot figure out where the problem is. Can you help me?
> Thanks.
> 
> Error message:
> ..
> 
> 
> ---
> ValueErrorTraceback (most recent call last)
> C:\Users\rj\pyprj\stackoverflow_logisticregression0.py in ()
>  17 df = pd.DataFrame(zipped,columns = ['study_hrs','p_or_f'])
>  18 
> ---> 19 y, X = dmatrices('p_or_f ~ study_hrs', df, return_type="dataframe")
>  20 
>  21 y = np.ravel(y)
> 
> C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\highlevel.pyc
>  in dmatrices(formula_like, data, eval_env, NA_action, return_type)
> 295 eval_env = EvalEnvironment.capture(eval_env, reference=1)
> 296 (lhs, rhs) = _do_highlevel_design(formula_like, data, eval_env,
> --> 297   NA_action, return_type)
> 298 if lhs.shape[1] == 0:
> 299 raise PatsyError("model is missing required outcome 
> variables")
> 
> C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\highlevel.pyc
>  in _do_highlevel_design(formula_like, data, eval_env, NA_action, return_type)
> 150 return iter([data])
> 151 design_infos = _try_incr_builders(formula_like, data_iter_maker, 
> eval_env,
> --> 152   NA_action)
> 153 if design_infos is not None:
> 154 return build_design_matrices(design_infos, data,
> 
> C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\highlevel.pyc
>  in _try_incr_builders(formula_like, data_iter_maker, eval_env, NA_action)
>  55   data_iter_maker,
>  56   eval_env,
> ---> 57   NA_action)
>  58 else:
>  59 return None
> 
> C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\build.pyc
>  in design_matrix_builders(termlists, data_iter_maker, eval_env, NA_action)
> 704 factor_states[factor],
> 705 num_columns=num_column_counts[factor],
> --> 706 categories=None)
> 707 else:
> 708 assert factor in cat_levels_contrasts
> 
> C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\design_info.pyc
>  in __init__(self, factor, type, state, num_columns, categories)
>  86 if self.type == "numerical":
>  87 if not isinstance(num_columns, int):
> ---> 88 raise ValueError("For numerical factors, num_columns "
>  89  "must be an int")
>  90 if categories is not None:
> 
> ValueError: For numerical factors, num_columns must be an int

BTW, I use Python 2.7 on Canopy. 

patsy: VERSION0.4.0

Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help on error " ValueError: For numerical factors, num_columns must be an int "

2015-12-16 Thread Robert
On Wednesday, December 16, 2015 at 5:57:04 AM UTC-5, Robert wrote:
> On Wednesday, December 16, 2015 at 5:44:21 AM UTC-5, Robert wrote:
> > Hi,
> > 
> > When I run the following code, there is an error:
> > 
> > ValueError: For numerical factors, num_columns must be an int 
> > 
> > 
> > 
> > import numpy as np
> > import pandas as pd
> > from patsy import dmatrices
> > from sklearn.linear_model import LogisticRegression
> > 
> > X = [0.5,0.75,1.0,1.25,1.5,1.75,1.75,2.0,2.25,2.5,2.75,3.0,3.25,
> > 3.5,4.0,4.25,4.5,4.75,5.0,5.5]
> > y = [0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1]
> > 
> > zipped = list(zip(X,y))
> > df = pd.DataFrame(zipped,columns = ['study_hrs','p_or_f'])
> > 
> > y, X = dmatrices('p_or_f ~ study_hrs', df, return_type="dataframe")
> > ===
> > 
> > I have check 'df' is this type:
> > =
> > type(df)
> > Out[25]: pandas.core.frame.DataFrame
> > =
> > 
> > I cannot figure out where the problem is. Can you help me?
> > Thanks.
> > 
> > Error message:
> > ..
> > 
> > 
> > ---
> > ValueErrorTraceback (most recent call last)
> > C:\Users\rj\pyprj\stackoverflow_logisticregression0.py in ()
> >  17 df = pd.DataFrame(zipped,columns = ['study_hrs','p_or_f'])
> >  18 
> > ---> 19 y, X = dmatrices('p_or_f ~ study_hrs', df, return_type="dataframe")
> >  20 
> >  21 y = np.ravel(y)
> > 
> > C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\highlevel.pyc
> >  in dmatrices(formula_like, data, eval_env, NA_action, return_type)
> > 295 eval_env = EvalEnvironment.capture(eval_env, reference=1)
> > 296 (lhs, rhs) = _do_highlevel_design(formula_like, data, eval_env,
> > --> 297   NA_action, return_type)
> > 298 if lhs.shape[1] == 0:
> > 299 raise PatsyError("model is missing required outcome 
> > variables")
> > 
> > C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\highlevel.pyc
> >  in _do_highlevel_design(formula_like, data, eval_env, NA_action, 
> > return_type)
> > 150 return iter([data])
> > 151 design_infos = _try_incr_builders(formula_like, 
> > data_iter_maker, eval_env,
> > --> 152   NA_action)
> > 153 if design_infos is not None:
> > 154 return build_design_matrices(design_infos, data,
> > 
> > C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\highlevel.pyc
> >  in _try_incr_builders(formula_like, data_iter_maker, eval_env, NA_action)
> >  55   data_iter_maker,
> >  56   eval_env,
> > ---> 57   NA_action)
> >  58 else:
> >  59 return None
> > 
> > C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\build.pyc
> >  in design_matrix_builders(termlists, data_iter_maker, eval_env, NA_action)
> > 704 factor_states[factor],
> > 705 num_columns=num_column_counts[factor],
> > --> 706 categories=None)
> > 707 else:
> > 708 assert factor in cat_levels_contrasts
> > 
> > C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\design_info.pyc
> >  in __init__(self, factor, type, state, num_columns, categories)
> >  86 if self.type == "numerical":
> >  87 if not isinstance(num_columns, int):
> > ---> 88 raise ValueError("For numerical factors, 
> > num_columns "
> >  89  "must be an int")
> >  90 if categories is not None:
> > 
> > ValueError: For numerical factors, num_columns must be an int
> 
> BTW, I use Python 2.7 on Canopy. 
> 
> patsy: VERSION0.4.0
> 
> Thanks,

When I use this code snippet, copied from the wb, it is also wrong:

import numpy as np
import pandas as pd
import patsy

time = np.tile([1, 2, 3, 4], 3)
country = np.repeat(['a', 'b', 'c'], 4)
event_int = np.random.randint(0, 2, size=len(time))

df = pd.DataFrame({'event_int':event_int, 'time_day':time, 'country':country})

f0 = 'event_int ~ C(time_day):C(country) - 1'
y,X0 = patsy.dmatrices(f0, df, return_type='dataframe')
print len(X0.columns)

I am new to these packages. I don't know why it is correct for other users.
Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help on error " ValueError: For numerical factors, num_columns must be an int "

2015-12-16 Thread Robert
On Wednesday, December 16, 2015 at 6:34:21 AM UTC-5, Mark Lawrence wrote:
> On 16/12/2015 10:44, Robert wrote:
> > Hi,
> >
> > When I run the following code, there is an error:
> >
> > ValueError: For numerical factors, num_columns must be an int
> >
> >
> > 
> > import numpy as np
> > import pandas as pd
> > from patsy import dmatrices
> > from sklearn.linear_model import LogisticRegression
> >
> > X = [0.5,0.75,1.0,1.25,1.5,1.75,1.75,2.0,2.25,2.5,2.75,3.0,3.25,
> > 3.5,4.0,4.25,4.5,4.75,5.0,5.5]
> > y = [0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1]
> >
> > zipped = list(zip(X,y))
> > df = pd.DataFrame(zipped,columns = ['study_hrs','p_or_f'])
> >
> > y, X = dmatrices('p_or_f ~ study_hrs', df, return_type="dataframe")
> > ===
> >
> > I have check 'df' is this type:
> > =
> > type(df)
> > Out[25]: pandas.core.frame.DataFrame
> > =
> >
> > I cannot figure out where the problem is. Can you help me?
> > Thanks.
> >
> > Error message:
> > ..
> >
> >
> > ---
> > ValueErrorTraceback (most recent call last)
> > C:\Users\rj\pyprj\stackoverflow_logisticregression0.py in ()
> >   17 df = pd.DataFrame(zipped,columns = ['study_hrs','p_or_f'])
> >   18
> > ---> 19 y, X = dmatrices('p_or_f ~ study_hrs', df, return_type="dataframe")
> >   20
> >   21 y = np.ravel(y)
> >
> > C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\highlevel.pyc
> >  in dmatrices(formula_like, data, eval_env, NA_action, return_type)
> >  295 eval_env = EvalEnvironment.capture(eval_env, reference=1)
> >  296 (lhs, rhs) = _do_highlevel_design(formula_like, data, eval_env,
> > --> 297   NA_action, return_type)
> >  298 if lhs.shape[1] == 0:
> >  299 raise PatsyError("model is missing required outcome 
> > variables")
> >
> > C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\highlevel.pyc
> >  in _do_highlevel_design(formula_like, data, eval_env, NA_action, 
> > return_type)
> >  150 return iter([data])
> >  151 design_infos = _try_incr_builders(formula_like, 
> > data_iter_maker, eval_env,
> > --> 152   NA_action)
> >  153 if design_infos is not None:
> >  154 return build_design_matrices(design_infos, data,
> >
> > C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\highlevel.pyc
> >  in _try_incr_builders(formula_like, data_iter_maker, eval_env, NA_action)
> >   55   data_iter_maker,
> >   56   eval_env,
> > ---> 57   NA_action)
> >   58 else:
> >   59 return None
> >
> > C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\build.pyc
> >  in design_matrix_builders(termlists, data_iter_maker, eval_env, NA_action)
> >  704 factor_states[factor],
> >  705 num_columns=num_column_counts[factor],
> > --> 706 categories=None)
> >  707 else:
> >  708 assert factor in cat_levels_contrasts
> >
> > C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\design_info.pyc
> >  in __init__(self, factor, type, state, num_columns, categories)
> >   86 if self.type == "numerical":
> >   87 if not isinstance(num_columns, int):
> > ---> 88 raise ValueError("For numerical factors, 
> > num_columns "
> >   89  "must be an int")
> >   90 if categories is not None:
> >
> > ValueError: For numerical factors, num_columns must be an int
> >
> 
> Slap the ValueError into a search engine and the first hit is 
> https://groups.google.com/forum/#!topic/pystatsmodels/KcSzNqDxv-Q
> 
> -- 
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
> 
> Mark Lawrence

Hi,
I don't see a solution to my problem. I find the following demo code from 

https://patsy.readthedocs.org/en/v0.1.0/API-reference.html#patsy.dmatrix

It doesn't work either on the Canopy. Does it work on your computer?
Thanks,

/
demo_data("a", "x", nlevels=3)
Out[134]: 
{'a': ['a1', 'a2', 'a3', 'a1', 'a2', 'a3'],
 'x': array([ 1.76405235,  0.40015721,  0.97873798,  2.2408932 ,  1.86755799,
-0.97727788])}

mat = dmatrix("a + x", demo_data("a", "x", nlevels=3))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help on error " ValueError: For numerical factors, num_columns must be an int "

2015-12-16 Thread Robert
On Wednesday, December 16, 2015 at 8:57:30 PM UTC-5, Josef Pktd wrote:
> On Wednesday, December 16, 2015 at 9:50:35 AM UTC-5, Robert wrote:
> > On Wednesday, December 16, 2015 at 6:34:21 AM UTC-5, Mark Lawrence wrote:
> > > On 16/12/2015 10:44, Robert wrote:
> > > > Hi,
> > > >
> > > > When I run the following code, there is an error:
> > > >
> > > > ValueError: For numerical factors, num_columns must be an int
> > > >
> > > >
> > > > 
> > > > import numpy as np
> > > > import pandas as pd
> > > > from patsy import dmatrices
> > > > from sklearn.linear_model import LogisticRegression
> > > >
> > > > X = [0.5,0.75,1.0,1.25,1.5,1.75,1.75,2.0,2.25,2.5,2.75,3.0,3.25,
> > > > 3.5,4.0,4.25,4.5,4.75,5.0,5.5]
> > > > y = [0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1]
> > > >
> > > > zipped = list(zip(X,y))
> > > > df = pd.DataFrame(zipped,columns = ['study_hrs','p_or_f'])
> > > >
> > > > y, X = dmatrices('p_or_f ~ study_hrs', df, return_type="dataframe")
> > > > ===
> > > >
> > > > I have check 'df' is this type:
> > > > =
> > > > type(df)
> > > > Out[25]: pandas.core.frame.DataFrame
> > > > =
> > > >
> > > > I cannot figure out where the problem is. Can you help me?
> > > > Thanks.
> > > >
> > > > Error message:
> > > > ..
> > > >
> > > >
> > > > ---
> > > > ValueErrorTraceback (most recent call 
> > > > last)
> > > > C:\Users\rj\pyprj\stackoverflow_logisticregression0.py in ()
> > > >   17 df = pd.DataFrame(zipped,columns = ['study_hrs','p_or_f'])
> > > >   18
> > > > ---> 19 y, X = dmatrices('p_or_f ~ study_hrs', df, 
> > > > return_type="dataframe")
> > > >   20
> > > >   21 y = np.ravel(y)
> > > >
> > > > C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\highlevel.pyc
> > > >  in dmatrices(formula_like, data, eval_env, NA_action, return_type)
> > > >  295 eval_env = EvalEnvironment.capture(eval_env, reference=1)
> > > >  296 (lhs, rhs) = _do_highlevel_design(formula_like, data, 
> > > > eval_env,
> > > > --> 297   NA_action, return_type)
> > > >  298 if lhs.shape[1] == 0:
> > > >  299 raise PatsyError("model is missing required outcome 
> > > > variables")
> > > >
> > > > C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\highlevel.pyc
> > > >  in _do_highlevel_design(formula_like, data, eval_env, NA_action, 
> > > > return_type)
> > > >  150 return iter([data])
> > > >  151 design_infos = _try_incr_builders(formula_like, 
> > > > data_iter_maker, eval_env,
> > > > --> 152   NA_action)
> > > >  153 if design_infos is not None:
> > > >  154 return build_design_matrices(design_infos, data,
> > > >
> > > > C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\highlevel.pyc
> > > >  in _try_incr_builders(formula_like, data_iter_maker, eval_env, 
> > > > NA_action)
> > > >   55   data_iter_maker,
> > > >   56   eval_env,
> > > > ---> 57   NA_action)
> > > >   58 else:
> > > >   59 return None
> > > >
> > > > C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\patsy\build.pyc
> > > >  in design_matrix_builders(termlists, data_iter_maker, eval_env, 
> > > > NA_action)
> > > >  704 factor_states[factor],
> > > >  705 
> > > > num_columns=num_column_counts[factor],
> > > > --> 706 categories=None)
> > > >  707 else:
> > > >  708 a

How can I get/save Pandas DataFrame help content?

2015-12-16 Thread Robert
Hi,

When I use Enthought/Canopy, help(DataFrame) has so much content that it 
exceeds the display buffer, i.e. its head is cut off as I go up to see it.
I would like to know whether there is a way similar to Linux redirection to
save the help DataFrame content to a file?

I have search on-line Pandas DataFrame web page. Surprisingly, it has much
less content than help(DataFrame) command.

If there is no way to save the content to a file, do you know where I can get
the full help DataFrame content on a web page?


Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How can I get/save Pandas DataFrame help content?

2015-12-16 Thread Robert
On Wednesday, December 16, 2015 at 9:56:23 PM UTC-5, Chris Angelico wrote:
> On Thu, Dec 17, 2015 at 1:48 PM, Robert  wrote:
> > Hi,
> >
> > When I use Enthought/Canopy, help(DataFrame) has so much content that it
> > exceeds the display buffer, i.e. its head is cut off as I go up to see it.
> > I would like to know whether there is a way similar to Linux redirection to
> > save the help DataFrame content to a file?
> 
> If you use command-line Python (by just running 'python' or 'python3'
> in your shell), you should be able to use help() with a More pager.
> 
> ChrisA

Thanks for this useful idea.
I have tried with: 

help(DataFrame) | more
NameError Traceback (most recent call last)
 in ()
> 1 help(DataFrame) | more

NameError: name 'more' is not defined 

It is possible I misunderstand your method. Could you give me a little more
description on it?

-- 
https://mail.python.org/mailman/listinfo/python-list


Can I call a pymc-function directly?

2015-12-20 Thread Robert
Hi,

I am new to pymc package. I come across the following tutorial:


The example is from: www.map.ox.ac.uk/media/PDF/Patil_et_al_2010.pdf

.

import pymc 
import numpy as np 

n = 5*np.ones(4,dtype=int) 
x = np.array([-.86,-.3,-.05,.73]) 

alpha = pymc.Normal('alpha',mu=0,tau=.01) 
beta = pymc.Normal('beta',mu=0,tau=.01) 

@pymc.deterministic 
def theta(a=alpha, b=beta): 
"""theta = logit^{-1}(a+b)""" 
return pymc.invlogit(a+b*x) 

d = pymc.Binomial('d', n=n, p=theta, value=np.array([0.,1.,3.,5.]),\ 
observed=True) 
... 

import pymc 
import pymc.Matplot 
import mymodel 

S = pymc.MCMC(mymodel, db='pickle') 
S.sample(iter=1, burn=5000, thin=2) 
pymc.Matplot.plot(S) 
import matplotlib.pyplot as plt 
plt.show() 
///

Through the above code runs and plots, I would like to know more detail
about function theta. I have tried the following modified code:

import pymc 
import numpy as np 

n = 5*np.ones(4,dtype=int) 
x = np.array([-.86,-.3,-.05,.73]) 

alpha = pymc.Normal('alpha',mu=0,tau=.01) 
beta = pymc.Normal('beta',mu=0,tau=.01) 

@pymc.deterministic 
def theta(a=alpha, b=beta): 
"""theta = logit^{-1}(a+b)""" 
return pymc.invlogit(a+b*x)

p=theta(alpha, beta)
print 'p=', p
//

It has this error:

---
TypeError Traceback (most recent call last)
C:\Users\rj\pyprj\pymc_mymodel0.py in ()
 13 return pymc.invlogit(a+b*x)
 14 
---> 15 p=theta(alpha, beta)
 16 print 'p=', p

C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\CommonDeterministics.pyc
 in __call__(self, *args, **kwargs)
989 {'self': self, 'args': args, 'kwargs': 
kwargs},
990 trace=False,
--> 991 plot=False)
992 Variable.__call__ = UnboundMethodType(__call__, None, Variable)
993 

C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\PyMCObjects.pyc
 in __init__(self, eval, doc, name, parents, dtype, trace, cache_depth, plot, 
verbose, jacobians, jacobian_formats)
441   trace=trace,
442   plot=plot,
--> 443   verbose=verbose)
444 
445 # self._value.force_compute()

C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\Node.pyc 
in __init__(self, doc, name, parents, cache_depth, trace, dtype, plot, verbose)
212 self.extended_children = set()
213 
--> 214 Node.__init__(self, doc, name, parents, cache_depth, 
verbose=verbose)
215 
216 if self.dtype is None:

C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\Node.pyc 
in __init__(self, doc, name, parents, cache_depth, verbose)
127 
128 # Initialize
--> 129 self.parents = parents
130 
131 def _get_parents(self):

C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\Node.pyc 
in _set_parents(self, new_parents)
145 
146 # Get new lazy function
--> 147 self.gen_lazy_function()
148 
149 parents = property(

C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\PyMCObjects.pyc
 in gen_lazy_function(self)
452cache_depth=self._cache_depth)
453 
--> 454 self._value.force_compute()
455 
456 self._jacobians = {}

C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\LazyFunction.pyd
 in pymc.LazyFunction.LazyFunction.force_compute (pymc\LazyFunction.c:2409)()

C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\CommonDeterministics.pyc
 in eval_fun(self, args, kwargs)
981 
982 def eval_fun(self, args=args, kwargs=kwargs):
--> 983 return self(*args, **kwargs)
984 return pm.Deterministic(eval_fun,
985 'A Deterministic returning the value of 
%s(*%s, **%s)' % (

TypeError: 'numpy.ndarray' object is not callable 
///
>From function syntactically, it seems OK for this function calling(random
variables alpha and beta as parameters of theta. 


p=theta(alpha, beta)


Do you know what is wrong 
with my code and idea (call theta by my code, not a random model like:


d = pymc.Binomial('d', n=n, p=theta, value=np.array([0.,1.,3.,5.]),\
observed=True)


Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


What could cause a plot fail in my code?

2015-12-21 Thread Robert
Hi,

I find a useful code snippet on link:

http://stackoverflow.com/questions/25126444/logistic-regression-in-pymc/34400966#34400966

but it has error on plot function. The error message is as following:
-
%run "C:\Users\rj\pyprj\logic_regression0.py"
[-100%-] 1 of 1 complete in 12.6   
secPlotting beta0
Plotting tau
Plotting betaSalad
Plotting sigma
---
UnboundLocalError Traceback (most recent call last)
C:\Users\pyprj\logic_regression0.py in ()
 34 #m.sample(10, 5, 50)
 35 m.sample(1, 5000, 50)
---> 36 pm.Matplot.plot(m)
 37 import matplotlib.pyplot as plt
 38 #plt.plot(m)

C:\Users\AppData\Local\Enthought\Canopy\User\lib\site- 
packages\pymc\Matplot.pyc in wrapper(pymc_obj, *args, **kwargs)
339 if args:
340 name = '%s_%s' % (args[0], variable.__name__)
--> 341 f(data, name, *args, **kwargs)
342 return
343 except AttributeError:

C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\Matplot.pyc 
in plot(data, name, format, suffix, path,  common_scale, datarange, new, last, 
rows, num, fontmap, verbose)
453 num=num * 2,
454 last=last,
--> 455 fontmap=fontmap)
456 
457 if last:

C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\Matplot.pyc 
in wrapper(pymc_obj, *args, **kwargs)
375 
376 # If others fail, assume that raw data is passed
--> 377 f(pymc_obj, *args, **kwargs)
378 
379 wrapper.__doc__ = f.__doc__

C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\Matplot.pyc 
in histogram(data, name, bins, datarange, format, suffix, path, rows, columns, 
num, last, fontmap, verbose)
576 
577 # Generate histogram
--> 578 hist(data.tolist(), bins, histtype='stepfilled')
579 
580 xlim(datarange)

C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\pyplot.py
 in hist(x, bins, range, normed, weights, cumulative, bottom, histtype, align, 
orientation, rwidth, log, color, label, stacked, hold, data, **kwargs)
   2956   histtype=histtype, align=align, 
orientation=orientation,
   2957   rwidth=rwidth, log=log, color=color, label=label,
-> 2958   stacked=stacked, data=data, **kwargs)
   2959 finally:
   2960 ax.hold(washold)

C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\__init__.py
 in inner(ax, *args, **kwargs)
   1809 warnings.warn(msg % (label_namer, func.__name__),
   1810   RuntimeWarning, stacklevel=2)
-> 1811 return func(ax, *args, **kwargs)
   1812 pre_doc = inner.__doc__
   1813 if pre_doc is None:

C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\axes\_axes.py
 in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, 
align, orientation, rwidth, log, color, label, stacked, **kwargs)
   6192 ymin = np.amin(m[m != 0])
   6193 # filter out the 0 height bins
-> 6194 ymin = max(ymin*0.9, minimum) if not input_empty else 
minimum
   6195 ymin = min(ymin0, ymin)
   6196 self.dataLim.intervaly = (ymin, ymax)

UnboundLocalError: local variable 'ymin' referenced before assignment 
/

I have no clue at all on debug it. Could you help me?
Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What could cause a plot fail in my code?

2015-12-21 Thread Robert
On Monday, December 21, 2015 at 12:15:54 PM UTC-5, Robert wrote:
> Hi,
> 
> I find a useful code snippet on link:
> 
> http://stackoverflow.com/questions/25126444/logistic-regression-in-pymc/34400966#34400966
> 
> but it has error on plot function. The error message is as following:
> -
> %run "C:\Users\rj\pyprj\logic_regression0.py"
> [-100%-] 1 of 1 complete in 12.6  
>  secPlotting beta0
> Plotting tau
> Plotting betaSalad
> Plotting sigma
> ---
> UnboundLocalError Traceback (most recent call last)
> C:\Users\pyprj\logic_regression0.py in ()
>  34 #m.sample(10, 5, 50)
>  35 m.sample(1, 5000, 50)
> ---> 36 pm.Matplot.plot(m)
>  37 import matplotlib.pyplot as plt
>  38 #plt.plot(m)
> 
> C:\Users\AppData\Local\Enthought\Canopy\User\lib\site- 
> packages\pymc\Matplot.pyc in wrapper(pymc_obj, *args, **kwargs)
> 339 if args:
> 340 name = '%s_%s' % (args[0], variable.__name__)
> --> 341 f(data, name, *args, **kwargs)
> 342 return
> 343 except AttributeError:
> 
> C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\Matplot.pyc
>  in plot(data, name, format, suffix, path,  common_scale, datarange, new, 
> last, rows, num, fontmap, verbose)
> 453 num=num * 2,
> 454 last=last,
> --> 455 fontmap=fontmap)
> 456 
> 457 if last:
> 
> C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\Matplot.pyc
>  in wrapper(pymc_obj, *args, **kwargs)
> 375 
> 376 # If others fail, assume that raw data is passed
> --> 377 f(pymc_obj, *args, **kwargs)
> 378 
> 379 wrapper.__doc__ = f.__doc__
> 
> C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-packages\pymc\Matplot.pyc
>  in histogram(data, name, bins, datarange, format, suffix, path, rows, 
> columns, num, last, fontmap, verbose)
> 576 
> 577 # Generate histogram
> --> 578 hist(data.tolist(), bins, histtype='stepfilled')
> 579 
> 580 xlim(datarange)
> 
> C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\pyplot.py
>  in hist(x, bins, range, normed, weights, cumulative, bottom, histtype, 
> align, orientation, rwidth, log, color, label, stacked, hold, data, **kwargs)
>2956   histtype=histtype, align=align, 
> orientation=orientation,
>2957   rwidth=rwidth, log=log, color=color, 
> label=label,
> -> 2958   stacked=stacked, data=data, **kwargs)
>2959 finally:
>2960 ax.hold(washold)
> 
> C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\__init__.py
>  in inner(ax, *args, **kwargs)
>1809 warnings.warn(msg % (label_namer, func.__name__),
>1810   RuntimeWarning, stacklevel=2)
> -> 1811 return func(ax, *args, **kwargs)
>1812 pre_doc = inner.__doc__
>1813 if pre_doc is None:
> 
> C:\Users\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\axes\_axes.py
>  in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, 
> align, orientation, rwidth, log, color, label, stacked, **kwargs)
>6192 ymin = np.amin(m[m != 0])
>6193 # filter out the 0 height bins
> -> 6194 ymin = max(ymin*0.9, minimum) if not input_empty else 
> minimum
>6195 ymin = min(ymin0, ymin)
>6196 self.dataLim.intervaly = (ymin, ymax)
> 
> UnboundLocalError: local variable 'ymin' referenced before assignment 
> /
> 
> I have no clue at all on debug it. Could you help me?
> Thanks,

Excuse me. The code link should be this one:

http://nbviewer.ipython.org/gist/aflaxman/8329ec1b9f861469f896

Do you experience such errors?

Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


How to change/set compiler option for weave inline function

2015-12-27 Thread Robert
Hi,
I want to run a few C code inside Python on Windows 7, 64-bit PC, Canopy 
1.5.2.2785. I made a few trials, but I forget the detail commands I made on 
compiler setting. One thing I remember is that I added one file: distutils.cfg, 
whose content is: [build]
compiler=mingw32

And another thing I did was an option to msvc for the compiler, but I don't 
remember where the file was.
Now, it always search for 'msvccompiler', though I think it should use mingw32, 
as the following:

import scipy.weave

In [16]: weave.test()
Running unit tests for scipy.weave
NumPy version 
1.9.2..S.SSSSS.
NumPy is installed in 
C:\Users\jj\AppData\Local\Enthought\Canopy\User\lib\site-packages\numpy
SciPy version 0.16.1
SciPy is installed in 
C:\Users\jj\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy
Python version 2.7.6 | 64-bit | (default, Sep 15 2014, 17:36:35) [MSC v.1500 64 
bit (AMD64)]
nose version 1.3.4

--
Ran 146 tests in 5.652s

OK (SKIP=10)
Out[16]: 

In [17]: a  = 1

In [18]: weave.inline('printf("%d\\n",a);',['a'])
No module named msvccompiler in numpy.distutils; trying from distutils
objdump.exe: C:\Users\jj\AppData\Local\Enthought\Canopy\User\python27.dll: File 
format not recognized
Looking for python27.dll
---
ValueErrorTraceback (most recent call last)
 in ()
> 1 weave.inline('printf("%d\\n",a);',['a'])

C:\Users\rj\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\weave\inline_tools.pyc
 in inline(code, arg_names, local_dict, global_dict, force, compiler, verbose, 
support_code, headers, customize, type_converters, auto_downcast, 
newarr_converter, **kw)
364 type_converters=type_converters,
365 auto_downcast=auto_downcast,
--> 366 **kw)
367 
368 function_catalog.add_function(code,func,module_dir)

C:\Users\jj\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\weave\inline_tools.pyc
 in compile_function(code, arg_names, local_dict, global_dict, module_dir, 
compiler, verbose, support_code, headers, customize, type_converters, 
auto_downcast, **kw)
494 # setting.  All input keywords are passed through to distutils
495 mod.compile(location=storage_dir,compiler=compiler,
--> 496 verbose=verbose, **kw)
497 
498 # import the module and return the function.  Make sure

C:\Users\jj\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\weave\ext_tools.pyc
 in compile(self, location, compiler, verbose, **kw)
371 success = build_tools.build_extension(file, temp_dir=temp,
372   compiler_name=compiler,
--> 373   verbose=verbose, **kw)
374 if not success:
375 raise SystemError('Compilation failed')

C:\Users\jj\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\weave\build_tools.pyc
 in build_extension(module_path, compiler_name, build_dir, temp_dir, verbose, 
**kw)
277 environ = copy.deepcopy(os.environ)
278 try:
--> 279 setup(name=module_name, ext_modules=[ext],verbose=verb)
280 finally:
281 # restore state

C:\Users\jj\AppData\Local\Enthought\Canopy\User\lib\site-packages\numpy\distutils\core.pyc
 in setup(**attr)
167 new_attr['distclass'] = NumpyDistribution
168 
--> 169 return old_setup(**new_attr)
170 
171 def _check_append_library(libraries, item):

C:\Users\jj\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.2.2785.win-x86_64\lib\distutils\core.pyc
 in setup(**attrs)
150 if ok:
151 try:
--> 152 dist.run_commands()
153 except KeyboardInterrupt:
154 raise SystemExit, "interrupted"

C:\Users\jj\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.2.2785.win-x86_64\lib\distutils\dist.pyc
 in run_commands(self)
951 """
952 for cmd in self.commands:
--> 953 self.run_command(cmd)
954 
955 # -- Methods that operate on its Commands --

C:\Users\jj\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.2.2785.win-x86_64\lib\distutils\dist.pyc
 in run_command(self, command)
970 cmd_obj = self.get_command_obj(command)
971 cmd_obj.ensure_finalized()
--> 972 cmd_obj.run()
973 self.have_run[command] = 1
974 

C:\Users\jj\AppData\Local\Enthought\Canopy\User\lib\site-packages\numpy\distutils\command\build_ext.pyc
 in run(self)
 92  verbose=self.verbose,
 93   

What meaning is '[: , None]'?

2016-01-02 Thread Robert
Hi,

I read a code snippet, in which object w_A is:


w_A
Out[48]: array([ 0.10708809,  0.94933575,  0.8412686 ,  0.03280939,  
0.59985308])


Then, I don't know what is '[:  ' below:
vs_A = w_A[:, None] * xs


I don't find the answer after searching around 
Could you explain above code to me?
Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


What use of 'sum' in this line code?

2016-01-03 Thread Robert
Hi,

I find below code snippet on line:


//
m = 10
theta_A = 0.8
theta_B = 0.3
theta_0 = [theta_A, theta_B]

coin_A = bernoulli(theta_A)
coin_B = bernoulli(theta_B)

xs = map(sum, [coin_A.rvs(m), coin_A.rvs(m), coin_B.rvs(m), coin_A.rvs(m), 
coin_B.rvs(m)])
/

I see 
[coin_A.rvs(m), coin_A.rvs(m), coin_B.rvs(m), coin_A.rvs(m), coin_B.rvs(m)]

is simply a list, but I don't know what use of 'sum' in this line.
I replace the random number with a simple list:
///
yy=map(sum, [13, 22, 33, 41])

In [24]: yy
Out[24]: [13, 22, 33, 41]
///

I don't see 'sum' has any effect above.
The code source is from:
#http://people.duke.edu/~ccc14/sta-663/EMAlgorithm.html


What could you help me on the 'sum'?

Thanks,

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What use of 'sum' in this line code?

2016-01-03 Thread Robert
On Sunday, January 3, 2016 at 7:54:13 PM UTC-5, Peter Otten wrote:
> Robert wrote:
> 
> > Hi,
> > 
> > I find below code snippet on line:
> > 
> > 
> > //
> > m = 10
> > theta_A = 0.8
> > theta_B = 0.3
> > theta_0 = [theta_A, theta_B]
> > 
> > coin_A = bernoulli(theta_A)
> > coin_B = bernoulli(theta_B)
> > 
> > xs = map(sum, [coin_A.rvs(m), coin_A.rvs(m), coin_B.rvs(m), coin_A.rvs(m),
> > coin_B.rvs(m)]) /
> > 
> > I see
> > [coin_A.rvs(m), coin_A.rvs(m), coin_B.rvs(m), coin_A.rvs(m),
> > [coin_B.rvs(m)]
> > 
> > is simply a list, but I don't know what use of 'sum' in this line.
> > I replace the random number with a simple list:
> > ///
> > yy=map(sum, [13, 22, 33, 41])
> > 
> > In [24]: yy
> > Out[24]: [13, 22, 33, 41]
> > ///
> > 
> > I don't see 'sum' has any effect above.
> > The code source is from:
> > #http://people.duke.edu/~ccc14/sta-663/EMAlgorithm.html
> > 
> > 
> > What could you help me on the 'sum'?
> 
> >>> import numpy
> >>> values = [13, 22, 33, 41]
> >>> map(numpy.sum, values)
> [13, 22, 33, 41]
> >>> values2 = [[1, 2], [3, 4]]
> >>> map(numpy.sum, values2)
> [3, 7]
> 
> In Python 2 map(sum, values) applies sum to every value in the list and 
> returns the resulting list of sums. Apparently the numpy developers found it 
> convenient that sum(scalar) == scalar holds.

Thanks, all you say are correct in one way or the other. I just notice that
it uses coin_A.rvs(m) (m=10). Thus, it sums 10 random numbers.
-- 
https://mail.python.org/mailman/listinfo/python-list


Is '*args' useful in this example code?

2016-01-04 Thread Robert
Hi,

I find an example code on wrap at this link:
http://stackoverflow.com/questions/308999/what-does-functools-wraps-do

Here is the code:

def logged(func):
def with_logging(*args, **kwargs):
print func.__name__ + " was called"
return func(*args, **kwargs)
return with_logging
///

I understand now, but I feel the args usage is weird. I don't see any way 
to use *args and **kwargs in above code. What is your opinion on it?


Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is '*args' useful in this example code?

2016-01-04 Thread Robert
On Monday, January 4, 2016 at 9:26:47 PM UTC-5, Ben Finney wrote:
> Robert  writes:
> 
> > I understand now, but I feel the args usage is weird. I don't see any way 
> > to use *args and **kwargs in above code. What is your opinion on it?
> 
> Can you show example code that you would expect, and specifically what about
> the actual code doesn't match what you expect?
> 
> -- 
>  \"Of course, everybody says they're for peace. Hitler was for |
>   `\  peace. Everybody is for peace. The question is: what kind of |
> _o__)peace?" --Noam Chomsky, 1984-05-14 |
> Ben Finney

Excuse me for the incomplete info previously. 
If I call it with 
a = f(3) 
the result is 12. It is correct as below message. That is no use of *args 
and **kwargs.

If I put more parameters in f, it will give errors as below.


///
%run "C:/Users/pyprj/ipython0/decor0.py"
f was called
---
TypeError Traceback (most recent call last)
C:\Users\pyprj\ipython0\decor0.py in ()
 11return x + x * x
 12 
---> 13 a = f(3, 4)
 14 

C:\Users\pyprj\ipython0\decor0.py in with_logging(*args, **kwargs)
  3 def with_logging(*args, **kwargs):
  4 print func.__name__ + " was called"
> 5 return func(*args, **kwargs)
  6 return with_logging
  7 

TypeError: f() takes exactly 1 argument (2 given) 

%run "C:/Users/rj/pyprj/ipython0/decor0.py"
f was called

a
Out[13]: 12
///

I don't see *args and **kwargs can be used by other way yet.
Do you think this internal function definition (with *args) is useful?

Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is '*args' useful in this example code?

2016-01-05 Thread Robert
On Monday, January 4, 2016 at 10:28:31 PM UTC-5, Ben Finney wrote:
> Robert <.com> writes:
> 
> > On Monday, January 4, 2016 at 9:26:47 PM UTC-5, Ben Finney wrote:
> > > Can you show example code that you would expect, and specifically what 
> > > about
> > > the actual code doesn't match what you expect?
> >
> > Excuse me for the incomplete info previously. 
> > If I call it with 
> > a = f(3) 
> > the result is 12.
> 
> Can you show a *very* simple example of the 'f' you're talking about?
> Contrive a new one, make it behave the same way while keeping it very
> short, and once you have that, put that code in your message.
> 
> With an actual function to examine it will be much easier to know where
> the confusion lies.
> 
> -- 
>  \   "When a well-packaged web of lies has been sold to the masses |
>   `\over generations, the truth will seem utterly preposterous and |
> _o__)its speaker a raving lunatic." --Dresden James |
> Ben Finney

Excuse me, Ben. I forgot to add the f function. It is as below:
@logged
def f(x):
   """does some math"""
   return x + x * x
   
a = f(3)  
 
My problem has been solved after all of your helpful posts. Thanks.
-- 
https://mail.python.org/mailman/listinfo/python-list


Why is there difference between cmd line and .py file?

2016-01-05 Thread Robert
Hi,

I run below code, which is downloaded from link:

http://stackoverflow.com/questions/15513792/expectation-maximization-coin-toss-examples?rq=1




# represent the experiments
head_counts = np.array([5,9,8,4,7])
tail_counts = 10-head_counts
experiments = zip(head_counts,tail_counts)

# initialise the pA(heads) and pB(heads)
pA_heads = np.zeros(100); pA_heads[0] = 0.60
pB_heads = np.zeros(100); pB_heads[0] = 0.50

# E-M begins!
delta = 0.001  
j = 0 # iteration counter
improvement = float('inf')
while (improvement>delta):
expectation_A = np.zeros((5,2), dtype=float) 
expectation_B = np.zeros((5,2), dtype=float)
for i in range(0,len(experiments)):
e = experiments[i] # i'th experiment
ll_A = get_mn_log_likelihood(e,np.array([pA_heads[j],1-pA_heads[j]])) # 
loglikelihood of e given coin A
ll_B = get_mn_log_likelihood(e,np.array([pB_heads[j],1-pB_heads[j]])) # 
loglikelihood of e given coin B

weightA = math.exp(ll_A) / ( math.exp(ll_A) + math.exp(ll_B) ) # 
corresponding weight of A proportional to likelihood of A 
weightB = math.exp(ll_B) / ( math.exp(ll_A) + math.exp(ll_B) ) # 
corresponding weight of B proportional to likelihood of B   
 

expectation_A[i] = np.dot(weightA, e) 
expectation_B[i] = np.dot(weightB, e)

print "sum(expectation_A)[0]", sum(expectation_A)[0]
pA_heads[j+1] = sum(expectation_A)[0] / sum(sum(expectation_A)); 
pB_heads[j+1] = sum(expectation_B)[0] / sum(sum(expectation_B));
//
The print message is: 
sum(expectation_A)[0] 21.2974818963
sum(expectation_A)[0] 19.2093824201
sum(expectation_A)[0] 19.4102767983
sum(expectation_A)[0] 19.7538328728
sum(expectation_A)[0] 19.9753285438
sum(expectation_A)[0] 20.0879016403
sum(expectation_A)[0] 20.139820175
sum(expectation_A)[0] 20.1628374165



When I check below in detail in interactive mode (Canopy Python shell),
 
sum(expectation_A)[0]

it has error:
/
sum(expectation_A)[0]
---
IndexErrorTraceback (most recent call last)
 in ()
> 1 sum(expectation_A)[0]

IndexError: invalid index to scalar variable. 
//

I can see expectation_A content with:

In[146]:expectation_A
Out[146]: 
array([[ 0.52278641,  0.52278641],
   [ 8.55858656,  0.95095406],
   [ 6.75024946,  1.68756237],
   [ 0.1260128 ,  0.1890192 ],
   [ 4.20520218,  1.80222951]])

It looks like 
sum(expectation_A)[0]

can run in .py file, while it cannot be run in python shell.
Is it true?

Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why is there difference between cmd line and .py file?

2016-01-05 Thread Robert
On Tuesday, January 5, 2016 at 3:26:15 PM UTC-5, Robert wrote:
> Hi,
> 
> I run below code, which is downloaded from link:
> 
> http://stackoverflow.com/questions/15513792/expectation-maximization-coin-toss-examples?rq=1
> 
> 
> 
> 
> # represent the experiments
> head_counts = np.array([5,9,8,4,7])
> tail_counts = 10-head_counts
> experiments = zip(head_counts,tail_counts)
> 
> # initialise the pA(heads) and pB(heads)
> pA_heads = np.zeros(100); pA_heads[0] = 0.60
> pB_heads = np.zeros(100); pB_heads[0] = 0.50
> 
> # E-M begins!
> delta = 0.001  
> j = 0 # iteration counter
> improvement = float('inf')
> while (improvement>delta):
> expectation_A = np.zeros((5,2), dtype=float) 
> expectation_B = np.zeros((5,2), dtype=float)
> for i in range(0,len(experiments)):
> e = experiments[i] # i'th experiment
> ll_A = get_mn_log_likelihood(e,np.array([pA_heads[j],1-pA_heads[j]])) 
> # loglikelihood of e given coin A
> ll_B = get_mn_log_likelihood(e,np.array([pB_heads[j],1-pB_heads[j]])) 
> # loglikelihood of e given coin B
> 
> weightA = math.exp(ll_A) / ( math.exp(ll_A) + math.exp(ll_B) ) # 
> corresponding weight of A proportional to likelihood of A 
> weightB = math.exp(ll_B) / ( math.exp(ll_A) + math.exp(ll_B) ) # 
> corresponding weight of B proportional to likelihood of B 
>
> 
> expectation_A[i] = np.dot(weightA, e) 
> expectation_B[i] = np.dot(weightB, e)
> 
> print "sum(expectation_A)[0]", sum(expectation_A)[0]
> pA_heads[j+1] = sum(expectation_A)[0] / sum(sum(expectation_A)); 
> pB_heads[j+1] = sum(expectation_B)[0] / sum(sum(expectation_B));
> //
> The print message is: 
> sum(expectation_A)[0] 21.2974818963
> sum(expectation_A)[0] 19.2093824201
> sum(expectation_A)[0] 19.4102767983
> sum(expectation_A)[0] 19.7538328728
> sum(expectation_A)[0] 19.9753285438
> sum(expectation_A)[0] 20.0879016403
> sum(expectation_A)[0] 20.139820175
> sum(expectation_A)[0] 20.1628374165
> 
> 
> 
> When I check below in detail in interactive mode (Canopy Python shell),
>  
> sum(expectation_A)[0]
> 
> it has error:
> /
> sum(expectation_A)[0]
> ---
> IndexErrorTraceback (most recent call last)
>  in ()
> > 1 sum(expectation_A)[0]
> 
> IndexError: invalid index to scalar variable. 
> //
> 
> I can see expectation_A content with:
> 
> In[146]:expectation_A
> Out[146]: 
> array([[ 0.52278641,  0.52278641],
>[ 8.55858656,  0.95095406],
>[ 6.75024946,  1.68756237],
>[ 0.1260128 ,  0.1890192 ],
>[ 4.20520218,  1.80222951]])
> 
> It looks like 
> sum(expectation_A)[0]
> 
> can run in .py file, while it cannot be run in python shell.
> Is it true?
> 
> Thanks,

I just wonder that the cmd line function sum may be different from the 
.py file used. One is numpy package, the other is a general one. Then,
how can I further make it clear for this guess?
Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why is there difference between cmd line and .py file?

2016-01-05 Thread Robert
On Tuesday, January 5, 2016 at 3:37:53 PM UTC-5, John Gordon wrote:
> In  Robert 
>  writes:
> 
> > 
> > # represent the experiments
> > head_counts = np.array([5,9,8,4,7])
> 
> The code doesn't define 'np', so this line should produce an error.
> 
> The code you linked contains this import:
> 
> import numpy as np
> 
> However you didn't show it here, so I wonder if you posted the real code.
> 
> > sum(expectation_A)[0]
> > ---
> > IndexErrorTraceback (most recent call last)
> >  in ()
> > > 1 sum(expectation_A)[0]
> 
> > IndexError: invalid index to scalar variable. 
> > //
> 
> The built-in function sum() returns a single value, not a list, so this
> is a reasonable error.
> 
> I suspect the code actually intended to call numpy.sum(), which does
> return a list (or something like a list).
> 
> -- 
> John Gordon   A is for Amy, who fell down the stairs
> gor...@panix.com  B is for Basil, assaulted by bears
> -- Edward Gorey, "The Gashlycrumb Tinies"

Thanks, John. When I typed my new thought, your reply came. You are right.
 The attached message missed numpy import (In my file, it had).

Now, I cannot use np.sum. It has an error, see below please. How can I use
the numpy sum()?

Thanks,
/
import numpy as np

In [154]: np.sum(expectation_A)[0]
---
IndexErrorTraceback (most recent call last)
 in ()
> 1 np.sum(expectation_A)[0]

IndexError: invalid index to scalar variable.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why is there difference between cmd line and .py file?

2016-01-05 Thread Robert
On Tuesday, January 5, 2016 at 3:58:44 PM UTC-5, Joel Goldstick wrote:
> On Tue, Jan 5, 2016 at 3:45 PM, Robert  wrote:
> 
> > On Tuesday, January 5, 2016 at 3:37:53 PM UTC-5, John Gordon wrote:
> > > In  Robert <
> > r...@gmail.com> writes:
> > >
> > > > 
> > > > # represent the experiments
> > > > head_counts = np.array([5,9,8,4,7])
> > >
> > > The code doesn't define 'np', so this line should produce an error.
> > >
> > > The code you linked contains this import:
> > >
> > > import numpy as np
> > >
> > > However you didn't show it here, so I wonder if you posted the real code.
> > >
> > > > sum(expectation_A)[0]
> > > >
> > ---
> > > > IndexErrorTraceback (most recent call
> > last)
> > > >  in ()
> > > > > 1 sum(expectation_A)[0]
> > >
> > > > IndexError: invalid index to scalar variable.
> > > > //
> > >
> > > The built-in function sum() returns a single value, not a list, so this
> > > is a reasonable error.
> > >
> > > I suspect the code actually intended to call numpy.sum(), which does
> > > return a list (or something like a list).
> > >
> > > --
> > > John Gordon   A is for Amy, who fell down the stairs
> > > gor...@panix.com  B is for Basil, assaulted by bears
> > > -- Edward Gorey, "The Gashlycrumb Tinies"
> >
> > Thanks, John. When I typed my new thought, your reply came. You are right.
> >  The attached message missed numpy import (In my file, it had).
> >
> > Now, I cannot use np.sum. It has an error, see below please. How can I use
> > the numpy sum()?
> >
> > Thanks,
> > /
> > import numpy as np
> >
> > In [154]: np.sum(expectation_A)[0]
> > ---
> > IndexErrorTraceback (most recent call last)
> >  in ()
> > > 1 np.sum(expectation_A)[0]
> >
> > IndexError: invalid index to scalar variable.
> >
> 
> I've not used numpy, but you should print expectation_A to see what's in
> it.  It may be empty, causing the index.  It may be that expectation_A is
> an integer, not a list
> 
> >
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
> 
> 
> 
> -- 
> Joel Goldstick
> http://joelgoldstick.com/stats/birthdays

Thanks. It has sum method. OK now.
-- 
https://mail.python.org/mailman/listinfo/python-list


What meaning is 'from . import'

2016-01-07 Thread Robert
Hi,

I see the following code. After searching around, I still don't know the
meaning of '.'. Could you tell me that ? Thanks,





from . import _hmmc
from .utils import normalize
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about a class member

2016-01-07 Thread Robert
On Thursday, January 7, 2016 at 12:24:53 PM UTC-5, Robert wrote:
> Hi,
> 
> I am using a download package. When I read its code, see below please, I 
> don't know what 'sample' is:
> 
> 
> --
> model = hmm.GaussianHMM(n_components=4, covariance_type="full")
> 
> model.startprob_ = startprob
> model.transmat_ = transmat
> model.means_ = means
> model.covars_ = covars
> 
> # Generate samples
> X, Z = model.sample(50)
> -
> 
> When I read its (class) definition, I find the following part (which may not
> be sure 100% the above origination yet, but it is the only line being 
> 'sample').
> //
> self.gmms_ = []
> for x in range(self.n_components):
> if covariance_type is None:
> gmm = GMM(n_mix)
> else:
> gmm = GMM(n_mix, covariance_type=covariance_type)
> self.gmms_.append(gmm)
> 
> def _init(self, X, lengths=None):
> super(GMMHMM, self)._init(X, lengths=lengths)
> 
> for g in self.gmms_:
> g.set_params(init_params=self.init_params, n_iter=0)
> g.fit(X)
> 
> def _compute_log_likelihood(self, X):
> return np.array([g.score(X) for g in self.gmms_]).T
> 
> def _generate_sample_from_state(self, state, random_state=None):
> return self.gmms_[state].sample(1, 
> random_state=random_state).flatten()
> 
> 
> The above code looks like self.gmms is a list, which has an attribute
> 'sample'. But when I play with a list, there is no 'sample' attribute.
> 
> ..
> a=[1, 32.0, 4]
> 
> a
> Out[68]: [1, 32.0, 4]
> 
> type(a)
> Out[69]: list
> 
> a[0].sample(1,5)
> ---
> AttributeErrorTraceback (most recent call last)
>  in ()
> > 1 a[0].sample(1,5)
> 
> AttributeError: 'int' object has no attribute 'sample' 
> 
> a[1].sample(1,5)
> ---
> AttributeErrorTraceback (most recent call last)
>  in ()
> > 1 a[1].sample(1,5)
> 
> AttributeError: 'float' object has no attribute 'sample'
> 
> 
> What is 'sample' do you think?
> 
> Thanks,

The code can be downloaded from:
https://github.com/hmmlearn/hmmlearn/blob/master/examples/plot_hmm_sampling.py

Hope it can help to answer my question. Thanks again.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about a class member

2016-01-07 Thread Robert
On Thursday, January 7, 2016 at 5:06:07 PM UTC-5, Steven D'Aprano wrote:
> On Fri, 8 Jan 2016 04:23 am, Robert wrote:
> 
> > Hi,
> > 
> > I am using a download package. When I read its code, see below please, I
> > don't know what 'sample' is:
> > 
> > 
> > --
> > model = hmm.GaussianHMM(n_components=4, covariance_type="full")
> 
> 
> When I try running that code, I get an error:
> 
> 
> py> model = hmm.GaussianHMM(n_components=4, covariance_type="full")
> Traceback (most recent call last):
>   File "", line 1, in 
> NameError: name 'hmm' is not defined
> 
> What's hmm? Where does it come from? Is it this?
> 
> https://hmmlearn.github.io/hmmlearn/generated/hmmlearn.hmm.GaussianHMM.html
> 
> It has a sample method here:
> 
> https://hmmlearn.github.io/hmmlearn/generated/hmmlearn.hmm.GaussianHMM.html#hmmlearn.hmm.GaussianHMM.sample
> 
> 
> You should try googling for help before asking questions:
> 
> https://duckduckgo.com/html/?q=hmm.GaussianHMM
> 
> or use the search engine of your choice.
> 
> 
> -- 
> Steven

Thanks. I just realized that my list assumption was wrong. I got that 
conclusion was incorrect.
-- 
https://mail.python.org/mailman/listinfo/python-list


What use is '__reduce__'?

2016-01-08 Thread Robert
Hi,

When I try to run the following code:



/
from collections import Counter, OrderedDict

class OrderedCounter(Counter, OrderedDict):
 'Counter that remembers the order elements are first seen'
 def __repr__(self):
 return '%s(%r)' % (self.__class__.__name__,
OrderedDict(self))
 def __reduce__(self):
 return self.__class__, (OrderedDict(self),)

oc = OrderedCounter('abracadabra') 
-

I don't know the use of '__reduce__', even I look it up on Python website.
On that website, it explains 'pickle' module:
https://docs.python.org/2/library/pickle.html

But the above example without import that module. Is it from built-in?
Anyhow, I don't find a built-in explanation about '__reduce__'.

What use of the above two new self methods are in class OrderedCounter?

Thanks,


-- 
https://mail.python.org/mailman/listinfo/python-list


What use is '() here?

2016-01-08 Thread Robert
Hi,

Thanks Ian for replying to my previous post. Here is a further question on
the 'return' line below.




import collections
import pickle
class C(collections.defaultdict):
 def __init__(self):
 collections.defaultdict.__init__(self, list)
 def __reduce__(self):
 t = collections.defaultdict.__reduce__(self)
 return (t[0], ()) + t[2:]
 
 
c=C()
print c
print c.__reduce__()
c[1].append(200)
c[2].append(223)
c2 = pickle.loads(pickle.dumps(c))
c2 == c 
///
>From below command, I see 't' should be a tuple:


c.__reduce__()
Out[103]: (__main__.C, (), None, None, )


Then, I cannot get the idea what the two level parenthesis are for.
Its result is a tuple? Then, it can add 't[2:]'. 

 return (t[0], ()) + t[2:]


It is not a tuple because there is no third level parenthesis, i.e. 

((t[0], ()) + t[2:])


Do you have a simple way for me to make it clear? Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What use is '__reduce__'?

2016-01-08 Thread Robert
On Friday, January 8, 2016 at 12:04:21 PM UTC-5, Ian wrote:
> As you found by searching, __reduce__ is used to determine how
> instances of the class are pickled. If the example you're using
> doesn't do any pickling, then it's not really relevant to the example.
> It's probably included so that it won't be missed when the code is
> copied.
> 
> On Fri, Jan 8, 2016 at 9:42 AM, Robert  wrote:
> > Hi,
> >
> > When I try to run the following code:
> >
> >
> >
> > /
> > from collections import Counter, OrderedDict
> >
> > class OrderedCounter(Counter, OrderedDict):
> >  'Counter that remembers the order elements are first seen'
> >  def __repr__(self):
> >  return '%s(%r)' % (self.__class__.__name__,
> > OrderedDict(self))
> >  def __reduce__(self):
> >  return self.__class__, (OrderedDict(self),)
> >
> > oc = OrderedCounter('abracadabra')
> > -
> >
> > I don't know the use of '__reduce__', even I look it up on Python website.
> > On that website, it explains 'pickle' module:
> > https://docs.python.org/2/library/pickle.html
> >
> > But the above example without import that module. Is it from built-in?
> > Anyhow, I don't find a built-in explanation about '__reduce__'.
> >
> > What use of the above two new self methods are in class OrderedCounter?
> >
> > Thanks,
> >
> >
> > --
> > https://mail.python.org/mailman/listinfo/python-list

Thanks for your reply.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What use is '() here?

2016-01-08 Thread Robert
On Friday, January 8, 2016 at 12:44:01 PM UTC-5, Robert wrote:
> Hi,
> 
> Thanks Ian for replying to my previous post. Here is a further question on
> the 'return' line below.
> 
> 
> 
> 
> import collections
> import pickle
> class C(collections.defaultdict):
>  def __init__(self):
>  collections.defaultdict.__init__(self, list)
>  def __reduce__(self):
>  t = collections.defaultdict.__reduce__(self)
>  return (t[0], ()) + t[2:]
>  
>  
> c=C()
> print c
> print c.__reduce__()
> c[1].append(200)
> c[2].append(223)
> c2 = pickle.loads(pickle.dumps(c))
> c2 == c 
> ///
> From below command, I see 't' should be a tuple:
> 
> 
> c.__reduce__()
> Out[103]: (__main__.C, (), None, None, )
> 
> 
> Then, I cannot get the idea what the two level parenthesis are for.
> Its result is a tuple? Then, it can add 't[2:]'. 
> 
>  return (t[0], ()) + t[2:]
> 
> 
> It is not a tuple because there is no third level parenthesis, i.e. 
> 
> ((t[0], ()) + t[2:])
> 
> 
> Do you have a simple way for me to make it clear? Thanks,

OK. I get my answer for it. It is a tuple. I should get familiar with the
return line. Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


Help on return type(?)

2016-01-09 Thread Robert
Hi,

I see below code snippet. The return line is not as the usual type.



def make_cov(cov_type, n_comp, n_fea):
mincv = 0.1
rand = np.random.random
return {
'spherical': (mincv + mincv * np.dot(rand((n_components, 1)),
 np.ones((1, n_features ** 2,
'tied': (make_spd_matrix(n_features)
 + mincv * np.eye(n_features)),
'diag': (mincv + mincv * rand((n_components, n_features))) ** 2,
'full': np.array([(make_spd_matrix(n_features)
   + mincv * np.eye(n_features))
  for x in range(n_components)])
}[cov_type]

Specifically, could you explain the meaning of 

{
...}[cov_type]

to me?


Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


What use of these _ prefix members?

2016-01-10 Thread Robert
Hi,

When I read a sample file from hmm learn package, which is on top of 
scikit-learn package, I see there are many member functions (prefix with 
'_') have no caller. That is, I don't see anything uses those functions.
Below is a sample part from class GMMHMM(_BaseHMM):





//
def __init__(self, n_components=1, n_mix=1,
 params="stmcw", init_params="stmcw"):
_BaseHMM.__init__(self, n_components,.
  params=params, init_params=init_params)

# XXX: Hotfit for n_mix that is incompatible with the scikit's
# BaseEstimator API
self.n_mix = n_mix
self.covariance_type = covariance_type
self.covars_prior = covars_prior
self.gmms_ = []
for x in range(self.n_components):
if covariance_type is None:
gmm = GMM(n_mix)
else:
gmm = GMM(n_mix, covariance_type=covariance_type)
self.gmms_.append(gmm)

def _init(self, X, lengths=None):
super(GMMHMM, self)._init(X, lengths=lengths)

for g in self.gmms_:
g.set_params(init_params=self.init_params, n_iter=0)
g.fit(X)

def _compute_log_likelihood(self, X):
return np.array([g.score(X) for g in self.gmms_]).T

def _generate_sample_from_state(self, state, random_state=None):
return self.gmms_[state].sample(1, random_state=random_state).flatten()


Some online tutorials tell me '_' prefix is a kind of convention of private 
members. hmm is not a formal package. Do these '_' member functions are
unfinished? Or, they may be used in some way I don't know yet?

Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


Question on pytest example code

2016-01-10 Thread Robert
Hi,

Below is a code snippet from pytest package. It passes pytest, i.e. there is
no failure report.


# content of test_sysexit.py
import pytest

def f():
 raise SystemExit(1)

def test_mytest():
 with pytest.raises(SystemExit):
  f()


I see that f() will generate 'SystemExit(1)'. Then what does function
test_mytest()?

Is it missing some assert line?
The above code is from page 5 (9 of 93) of 'pytest Documentation' 
Release 2.8.2

Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


Confusing on bool data object or class function?

2016-01-10 Thread Robert
Hi,

When I use an on line code snippet, see below please, I find that 
'converged' in class ConvergenceMonitor is seen as a data.






-
class ConvergenceMonitor(object):
"""Monitors and reports convergence to :data:`sys.stderr`.

Parameters
--
tol : double
Convergence threshold. EM has converged either if the maximum
number of iterations is reached or the log probability
improvement between the two consecutive iterations is less
than threshold.

n_iter : int
Maximum number of iterations to perform.

verbose : bool
If ``True`` then per-iteration convergence reports are printed,
otherwise the monitor is mute.

Attributes
--
history : deque
The log probability of the data for the last two training
iterations. If the values are not strictly increasing, the
model did not converge.

iter : int
Number of iterations performed while training the model.
"""
_template = "{iter:>10d} {logprob:>16.4f} {delta:>+16.4f}"

def __init__(self, tol, n_iter, verbose):
self.tol = tol
self.n_iter = n_iter
self.verbose = verbose
self.history = deque(maxlen=2)
self.iter = 0

def __repr__(self):
class_name = self.__class__.__name__
params = dict(vars(self), history=list(self.history))
return "{0}({1})".format(
class_name, _pprint(params, offset=len(class_name)))

def report(self, logprob):
"""Reports convergence to :data:`sys.stderr`.

The output consists of three columns: iteration number, log
probability of the data at the current iteration and convergence
rate.  At the first iteration convergence rate is unknown and
is thus denoted by NaN.

Parameters
--
logprob : float
The log probability of the data as computed by EM algorithm
in the current iteration.
"""

@property
def converged(self):
"""``True`` if the EM algorithm converged and ``False`` otherwise."""
# XXX we might want to check that ``logprob`` is non-decreasing.
return (self.iter == self.n_iter or
(len(self.history) == 2 and
 self.history[1] - self.history[0] < self.tol))
/

Here is except of the online help content:

 |  Data descriptors defined here:
 |  
 |  __dict__
 |  dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |  list of weak references to the object (if defined)
 |  
 |  converged
 |  ``True`` if the EM algorithm converged and ``False`` otherwise.


The data conclusion is verified by the following test code:


from hmmlearn.base import ConvergenceMonitor

class TestMonitor(object):
def test_converged_by_iterations(self):
print 'self test0'
m = ConvergenceMonitor(tol=1e-3, n_iter=2, verbose=False)
print 'self test1', m.converged
assert not m.converged
print 'self test2', m.converged
m.report(-0.01)
assert not m.converged
print 'self test3', m.converged
m.report(-0.1)
assert m.converged
print 'self test4', m.converged

tmp_obj=TestMonitor()
print 'ttt', tmp_obj.test_converged_by_iterations()
mm = ConvergenceMonitor(tol=1e-3, n_iter=2, verbose=False)
print 'self test mm', mm.converged

That is, I can only use:

mm.converged

If I use it in this way:
mm.converged()

it will have error:
TypeError Traceback (most recent call last)
C:\Users\rj\Documents\PythonDocPrj0\hmmlearn-master\hmmlearn\tests\test_base1.py
 in ()
 27 print None
 28 mm = ConvergenceMonitor(tol=1e-3, n_iter=2, verbose=False)
---> 29 print 'self test mm', mm.converged()
 30 

TypeError: 'bool' object is not callable



On the other hand, 'play' in the following class is seen as a member
function:

class Engine(object):
def __init__(self, scene_map):
pass

def play(self):
print 'play'
return True

a_game = Engine(scene_map=0.1)
a_game.play()


Why can 'converged' only be seen as a data, while 'play' can be seen as
a function?

Thanks,
-- 
https://mail.python.org/mailman/listinfo/python-list


It looks like one function not tested by pytest

2016-01-12 Thread Robert
Hi,

I modify a test suite, and simplified to the below content.
I am interested in function:
test_bad_covariance_type()

I think that it tests for wrong type input, i.e. 
'badcovariance_type'
will generate error, which is expected. Thus, it passes test.

When a correct type, such as 'diag', is given, there will be no error 
generated. pytest will declare a failure.


Unfortunately, it will always pass the pytest for both cases.

What is wrong with my code or my understanding?


Thanks,




//
from __future__ import absolute_import

from unittest import TestCase

import numpy as np
import pytest

from hmmlearn import hmm

from . import log_likelihood_increasing, make_covar_matrix, normalized


class GaussianHMMTestMixin(object):
covariance_type = None  # set by subclasses

def setUp(self):
self.prng = prng = np.random.RandomState(10)
self.n_components = n_components = 3
self.n_features = n_features = 3
self.startprob = prng.rand(n_components)
self.startprob = self.startprob / self.startprob.sum()
self.transmat = prng.rand(n_components, n_components)
self.transmat /= np.tile(self.transmat.sum(axis=1)[:, np.newaxis],
 (1, n_components))
self.means = prng.randint(-20, 20, (n_components, n_features))
self.covars = dict(
(cv_type, make_covar_matrix(cv_type, n_components, n_features))
for cv_type in ["spherical", "tied", "diag", "full"])
self.expanded_covars = {
'spherical': [np.eye(n_features) * cov
  for cov in self.covars['spherical']],
'diag': [np.diag(cov) for cov in self.covars['diag']],
'tied': [self.covars['tied']] * n_components,
'full': self.covars['full'],
}

def test_bad_covariance_type(self):
with pytest.raises(ValueError):
#h = hmm.GaussianHMM(20, covariance_type='badcovariance_type')
h = hmm.GaussianHMM(covariance_type='diag')
h.means_ = self.means
h.covars_ = []
h.startprob_ = self.startprob
h.transmat_ = self.transmat
h._check()

def test_fit(self, params='stmc', n_iter=5, **kwargs):
h = hmm.GaussianHMM(self.n_components, self.covariance_type)
h.startprob_ = self.startprob
h.transmat_ = normalized(
self.transmat + np.diag(self.prng.rand(self.n_components)), 1)
h.means_ = 20 * self.means
h.covars_ = self.covars[self.covariance_type]

lengths = [10] * 10
X, _state_sequence = h.sample(sum(lengths), random_state=self.prng)

# Mess up the parameters and see if we can re-learn them.
# TODO: change the params and uncomment the check
h.fit(X, lengths=lengths)




class TestGaussianHMMWithSphericalCovars(GaussianHMMTestMixin, TestCase):
covariance_type = 'spherical'

def test_fit_startprob_and_transmat(self):
self.test_fit('st')

def test_bad_covariance_type0(self):
self.test_bad_covariance_type()
-- 
https://mail.python.org/mailman/listinfo/python-list


  1   2   3   4   5   6   7   8   9   10   >