newbie question: how to read back the dictionary from a file?

2007-04-16 Thread lancered
Hi Dear all,

I have some data here in the form of a dictionary, called "vdic". Then
I write them to a data file "f" using   the write function as
f.write(str(vdic)).  The keys of this dictionary are integers and
values are float numbers. Something like this:

{ 1: 0.00951486513347, 2: 0.0388123556019, ... ...}

Now, I want to read these data back in another function.  Of course, I
could parse the string little by little, e.g, first read a "{", then
loop read a int, then read a ":", then a float etc etc... Since it is
written out with standard python builtin functions,  I guess there may
be some more direct method than this, say a function in some modules?
Could someone give me a hint?

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


Re: Newbie Question about sequence multiplication

2007-04-08 Thread lancered
On Apr 5, 12:19 am, "Scott" <[EMAIL PROTECTED]> wrote:
> Alright, so I've been trying to teach myself Python which, when compared to
> my attempt to learn C++, is going pretty well.
> But I've come across an issue that I can't figure out, so I figured I'd ask
> the pro's.
>
> Now it looks pretty weird in this format but it was copied exactly from IDLE
>
> *code follows***
>
> #What this program is suppose to do is print a sentence centered in a box
>
> sentence = raw_input('Sentence: ')
>
> screen_width = 80
> text_width = len(sentence)
> box_width = text_width + 6
> left_margin = (screen_width - box_width) // 2
>
> print
> print ' ' * left_margin + '+'   + '-' * (box_width-2)  +  '+'
> print ' ' * left_margin + '|  ' + ' ' * text_width + ' |'
> print ' ' * left_margin + '|  ' + ' '   sentence   + ' |'
> print ' ' * left_margin + '|  ' + ' ' * text_width + ' |'
> print ' ' * left_margin + '+'   + '-' * (box_width-2)  + ' |'
> print
>
> end code
>
> Now that, even though copied straight from "Beginning Python: From Novice to
> Professional", returns :
> There's an error in your program: invalid syntax
>
> with the word sentence highlighted (not the sentence when I'm defining the
> name, the one in..uhmthe body of the code)
>
> Now if i put * before sentence as it is with the rest of the variables, it
> actually gets to the point where it asks me for the sentence, but after
> inputting my sentence I receive:
> Traceback (most recent call last):
>   File "D:/Programming/Python/sequence string multiplication example", line
> 16, in 
> print ' ' * left_margin + '|  ' + ' ' * sentence   + ' |'
> TypeError: can't multiply sequence by non-int of type 'str'
>
> Why can't I get it to do what it's supposed to do? What am I
> missing/misunderstanding?
> Very simply all its supposed to do is something like this (now bear with me
> formating might distort this a bit lol)
> ++
> ||
> |  Like This|
> ||
> ++
>
> Any help would be greatly appreciated
>
> -Scott

I modified the codes a little bit to get it running, and give the
correct alignment.
Just as a reference for you. Here is it:

*
sentence = raw_input('Sentence: ')

screen_width = 80
text_width = len(sentence)
box_width = text_width + 6
left_margin = (screen_width - box_width) /2

print
print ' ' * left_margin + '+' + '-' * (box_width-2)  + '+'
print ' ' * left_margin + '|' + ' ' * (box_width-2)  + '|'
print ' ' * left_margin + '|' + ' '*2+sentence +' '*2+ '|'
print ' ' * left_margin + '|' + ' ' * (box_width-2)  + '|'
print ' ' * left_margin + '+' + '-' * (box_width-2)  + '+'
print


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


Re: An error of matrix inversion using NumPy

2007-04-04 Thread lancered
Here is the eigenvalues of KK  I obtained:

 >>> linalg.eigvals(KK)
array([  1.11748411e+05,   3.67154458e+04,   3.41580846e+04,
 2.75272440e+04,   2.09790868e+04,   1.86242332e+04,
 8.68628325e+03,   6.66127732e+03,   6.15547187e+03,
 4.68626197e+03,   3.17838339e+03,   2.84888045e+03,
 1.88279736e+03,   1.32427574e+03,   1.04946287e+03,
 5.79303171e+02,   3.83111876e+02,   4.93826556e-12,
 1.50263232e-12])

You are right. The ratio of max/min eigenvalues is 7.4368432669e+016
Maybe this exceed the of precision of my machine?

Is there any tricks for me to be able to deal with this matrix
correctly with
NumPy?




On Apr 4, 3:58 pm, Robin Becker <[EMAIL PROTECTED]> wrote:
> lancered wrote:
> > Hi dear all,
> ..
> > matrices are correct.
>
> >So,  can you tell me what goes wrong?  Is this a bug in
> > Numpy.linalg? How to deal with this situation?  If you need, I can
> > post the matrix I used below, but it is so long,so not at the moment.
>
> ...
>
> presumably the matrix KK is actually some kind of normal matrix obtained from
> the data. So you have say n variables and m observations the data matrix is 
> than
> an n x m real valued thing say D then you want the inverse of something like 
> D'D
> ie an n by n thing. Typically the data D is de-meaned and normalized by the
> column norms so that you end up with a fairly well scaled problem.
>
> A long time ago I used Numeric+python to do exactly this sort of calculation
> with excellent results and the matrices were as large or larger eg 100 x 100 
> and
> above. I don't think the underlying numeric routines have changed that much. 
> If
> your matrix is symmetric then you should certainly be using
>
> Even if you can't post the matrix, perhaps you should indicate how you proceed
> from data to matrix. Another problem is that a large determinant is no 
> guarantee
> of stability for the inversion. If the largest eigenvalue is 10**100 and the
> smallest 10**-200 I probably have an ill determined problem; surprisingly easy
> to achieve :(
> --
> Robin Becker


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


An error of matrix inversion using NumPy

2007-04-04 Thread lancered
Hi dear all,
I am using Python2.4.2+NumPy1.0.1 to deal with a parameter
estimation problem with the least square methods. During the
calculations, I use  NumPy package to deal with matrix operations,
mostly matrix inversion and trasposition. The dimentions of the
matrices used are about 29x19,19x19 and 29x29.

During the calculation, I noticed an apparent error of
inverion of a 19x19 matrix. Denote this matrix as KK, U=KK^ -1, I
found the product of U and KK is not equivalent to unit matrix! This
apparently violate the definition of inversion. The inversion is
through the function linalg.inv().

   I have checked that det(KK)=-1.2E+40. At first, I thought the
error may be caused by such a large determinant, so I scaled it as
LL=KK/100, then invert LL. Since det(LL)=11.5 and all its elements are
within -180.0 to 850.0, this seems easier. But the result is still not
correct, the product of LL^-1 thus obtained and LL still not unit
matrix ... At the same time, the inversion results of some 29x19
matrices are correct.

   So,  can you tell me what goes wrong?  Is this a bug in
Numpy.linalg? How to deal with this situation?  If you need, I can
post the matrix I used below, but it is so long,so not at the moment.

   Thanks in advance!

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


An error of matrix inversion using NumPy

2007-04-04 Thread lancered
Hi dear all,
I am using Python2.4.2+NumPy1.0.1 to deal with a parameter
estimation problem with the least square methods. During the
calculations, I use  NumPy package to deal with matrix operations,
mostly matrix inversion and trasposition. The dimentions of the
matrices used are about 29x19,19x19 and 29x29.

During the calculation, I noticed an apparent error of
inverion of a 19x19 matrix. Denote this matrix as KK, U=KK^ -1, I
found the product of U and KK is not equivalent to unit matrix! This
apparently violate the definition of inversion. The inversion is
through the function linalg.inv().

   I have checked that det(KK)=-1.2E+40. At first, I thought the
error may be caused by such a large determinant, so I scaled it as
LL=KK/100, then invert LL. Since det(LL)=11.5 and all its elements are
within -180.0 to 850.0, this seems easier. But the result is still not
correct, the product of LL^-1 thus obtained and LL still not unit
matrix ... At the same time, the inversion results of some 29x19
matrices are correct.

   So,  can you tell me what goes wrong?  Is this a bug in
Numpy.linalg? How to deal with this situation?  If you need, I can
post the matrix I used below, but it is so long,so not at the moment.

   Thanks in advance!

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