Re: uWISGI with Qt for Python

2019-03-14 Thread Thomas Jollans
On 13/03/2019 22:42, Israel Brewster wrote:
> 1) Is there a “better way”? This GitHub repo: 
> https://github.com/unbit/uwsgi-qtloop seems to indicate that it should be 
> possible to run a Qt event loop from within a uWSGI app, thus eliminating the 
> extra “subprocess” spinoff, but it hasn’t been updated in 5 years and I have 
> been unable to get it to work with my current Qt/Python/OS setup
> 
> 2) Baring any “better way”, is there a way to at least ensure that the 
> subprocess is killed in the event of parent death, or alternately to look for 
> and kill any such lingering processes on application startup?
> 
> P.S. The purpose of running the web server is to be able to load and use 
> Plotly charts in my app (via a QWebEngineView). So a “better way” may be 
> using a different plotting library that can essentially “cut out” the middle 
> man. I’ve tried Matplotlib, but I found its performance to be worse than 
> Plotly - given the size of my data sets, performance matters. Also I had some 
> glitches with it when using a lasso selector (plot going black). Still, with 
> some work, it may be an option.

In a similar situation (web component in a Qt app) we simply use
werkzeug's built-in web server in a Python/Qt thread. This works very
nicely, but the performance probably isn't perfect. Our use case is
allowing a small part of our experiments to be controlled from a phone
in the local network, so the performance requirements aren't severe at all.

As for live plotting in a GUI app, you're right, matplotlib isn't really
suitable. But pyQtgraph is: http://www.pyqtgraph.org/
I've had no problems with it at all for streaming live measurement data
to screen and interacting with reasonable-sized 2d images. I expect
that's your best option.

Despite what the description says, it does PyQt5 as well as PyQt4.

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


asyncio Question

2019-03-14 Thread Simon Connah

Hi,

Hopefully this isn't a stupid question. For the record I am using Python 
3.7 on Ubuntu Linux.


I've decided to use asyncio to write a TCP network server using Streams 
and asyncio.start_server(). I can handle that part of it without many 
problems as the documentation is pretty good. I have one problem though 
that I am not sure how to solve. Each request to the server will be a 
JSON string and one of the components of the JSON string will be the 
latitude and longitude. What I want to do is associate a latitude and 
longitude with the client connection. Every time the latitude and 
longitude changes then the two values will be updated. There will only 
ever be one latitude and longitude for each client connection (since a 
device can only ever be in one place). I'm just not sure what the best 
method to achieve this would be when using asyncio.start_server().


Any help would be greatly appreciated.

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


Re: Generator question

2019-03-14 Thread Pierre Reinbold
Wow, thank you Ian for this very detailed answer, and thank you for taking the
time for that! Much appreciated!

If I get this right, I have to somehow fix the value of a_list during the loop,
like when you use the classic default value argument trick with lambdas (damn, I
should have thought of that!). So if I "unfold" the generator expression, using
default values for both iterables, I get this :

def flat_gen_cat_prod(lists):
solutions = [[]]
for a_list in lists:
def new_solutions(l=a_list, s=solutions):
for part_sol in s:
for el in l:
yield part_sol+[el]
solutions = new_solutions()
return solutions

With this I get the right behavior! Thanks again!

Doest that mean that there is no possibility to use a generator expression in
this case ? (gen. exp. are sooo much more elegant :-))

Thanks again for the answer and helpful comments!


πr

Le 14/03/19 à 02:44, Ian Kelly a écrit :
> You're basically running into this:
> https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result
> 
> To see why, let's try disassembling your function. I'm using Python 3.5
> here, but it shouldn't make much of a difference.
> 
> py> import dis
> py> dis.dis(flat_genexp_cat_prod)
>   2   0 BUILD_LIST   0
>   3 BUILD_LIST   1
>   6 STORE_FAST   1 (solutions)
> 
>   3   9 SETUP_LOOP  39 (to 51)
>  12 LOAD_FAST0 (lists)
>  15 GET_ITER
> >>   16 FOR_ITER31 (to 50)
>  19 STORE_DEREF  0 (a_list)
> 
>   4  22 LOAD_CLOSURE 0 (a_list)
>  25 BUILD_TUPLE  1
>  28 LOAD_CONST   1 ( at
> 0x73f31571ac90, file "", line 4>)
>  31 LOAD_CONST   2
> ('flat_genexp_cat_prod..')
>  34 MAKE_CLOSURE 0
>  37 LOAD_FAST1 (solutions)
>  40 GET_ITER
>  41 CALL_FUNCTION1 (1 positional, 0 keyword pair)
>  44 STORE_FAST   1 (solutions)
>  47 JUMP_ABSOLUTE   16
> >>   50 POP_BLOCK
> 
>   5 >>   51 LOAD_FAST1 (solutions)
>  54 RETURN_VALUE
> 
> Now, take a look at the difference between the instruction at address 22
> and the one at address 37:
> 
>   4  22 LOAD_CLOSURE 0 (a_list)
>  37 LOAD_FAST1 (solutions)
> 
> The value of solutions is passed directly to the generator as an argument,
> which is the reason why building the generator up iteratively like this
> works at all: although the nested generators are evaluated lazily, each new
> generator that is constructed contains as its input a reference to the
> previous generator.
> 
> By contrast, the value of a_list is a closure. The contents of the closure
> are just whatever the value of a_list is when the generator gets evaluated,
> not when the generator was created. Since the entire nested generated
> structure is evaluated lazily, it doesn't get evaluated until list() is
> called after the function has returned. The value of the a_list closure at
> that point is the last value that was assigned to it: the list [5, 6] from
> the last iteration of the for loop. This same list value then gets used for
> all three nested generators.
> 
> So now why do solutions and a_list get treated differently like this? To
> answer this, look at this paragraph about generator expressions from the
> language reference:
> 
> """
> Variables used in the generator expression are evaluated lazily when the
> __next__() method is called for the generator object (in the same fashion
> as normal generators). However, the iterable expression in the leftmost for
> clause is immediately evaluated, so that an error produced by it will be
> emitted at the point where the generator expression is defined, rather than
> at the point where the first value is retrieved. Subsequent for clauses and
> any filter condition in the leftmost for clause cannot be evaluated in the
> enclosing scope as they may depend on the values obtained from the leftmost
> iterable. For example: (x*y for x in range(10) for y in range(x, x+10)).
> """
> 
> So, it's simply because the iterable expression in the leftmost for clause
> is treated differently from every other value in the generator expression.
> 
> On Wed, Mar 13, 2019 at 3:49 PM Pierre Reinbold  wrote:
> 
>> Dear all,
>>
>> I want to implement a function computing the Cartesian product if the
>> elements
>> of a list of lists, but using generator expressions. I know that it is
>> already
>> available in itertools but it is for the sake of understanding how things
>> work.
>>
>> I already have a working recursive version, and I'm quite sure that this
>> iterative version used to wo

Re: Generator question

2019-03-14 Thread Peter Otten
Pierre Reinbold wrote:

> Wow, thank you Ian for this very detailed answer, and thank you for taking
> the time for that! Much appreciated!
> 
> If I get this right, I have to somehow fix the value of a_list during the
> loop, like when you use the classic default value argument trick with
> lambdas (damn, I should have thought of that!). So if I "unfold" the
> generator expression, using default values for both iterables, I get this
> :
> 
> def flat_gen_cat_prod(lists):
> solutions = [[]]
> for a_list in lists:
> def new_solutions(l=a_list, s=solutions):
> for part_sol in s:
> for el in l:
> yield part_sol+[el]
> solutions = new_solutions()
> return solutions
> 
> With this I get the right behavior! Thanks again!
> 
> Doest that mean that there is no possibility to use a generator expression
> in this case ? (gen. exp. are sooo much more elegant :-))

The obvious approach is to put the genexpr into a function:

def prod(lists):
solutions = [[]]
def new_solutions(a_list):
return (part_sol + [el] for part_sol in solutions for el in a_list)

for a_list in lists:
solutions = new_solutions(a_list)
return solutions

If you want to avoid the function you can take advantage of the fact that 
the outermost iterable is bound early:

def prod(lists):
solutions = [[]]

for a_list in lists:
solutions = (
part_sol + [el]
for solutions, a_list in [(solutions, a_list)]
for part_sol in solutions for el in a_list
)
return solutions


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


Re: Generator question

2019-03-14 Thread Pierre Reinbold
Le 14/03/19 à 10:45, Peter Otten a écrit :
> Pierre Reinbold wrote:
> 
>> Wow, thank you Ian for this very detailed answer, and thank you for taking
>> the time for that! Much appreciated!
>>
>> If I get this right, I have to somehow fix the value of a_list during the
>> loop, like when you use the classic default value argument trick with
>> lambdas (damn, I should have thought of that!). So if I "unfold" the
>> generator expression, using default values for both iterables, I get this
>> :
>>
>> def flat_gen_cat_prod(lists):
>> solutions = [[]]
>> for a_list in lists:
>> def new_solutions(l=a_list, s=solutions):
>> for part_sol in s:
>> for el in l:
>> yield part_sol+[el]
>> solutions = new_solutions()
>> return solutions
>>
>> With this I get the right behavior! Thanks again!
>>
>> Doest that mean that there is no possibility to use a generator expression
>> in this case ? (gen. exp. are sooo much more elegant :-))
> 
> The obvious approach is to put the genexpr into a function:
> 
> def prod(lists):
> solutions = [[]]
> def new_solutions(a_list):
> return (part_sol + [el] for part_sol in solutions for el in a_list)
> 
> for a_list in lists:
> solutions = new_solutions(a_list)
> return solutions
> 
> If you want to avoid the function you can take advantage of the fact that 
> the outermost iterable is bound early:
> 
> def prod(lists):
> solutions = [[]]
> 
> for a_list in lists:
> solutions = (
> part_sol + [el]
> for solutions, a_list in [(solutions, a_list)]
> for part_sol in solutions for el in a_list
> )
> return solutions
> 
> 

Thank you Peter, very clever suggestions !


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


Not Defined error in basic code

2019-03-14 Thread Jack Dangler

Just getting started with tutorials and such, and don't understand this -



class weapon:
    weaponId
    manufacturerName

    def printWeaponInfo(self):
    infoString = "ID: %d Mfg: %s Model: %s" % (self.weaponId, 
self.manufacturerName)

    return infoString



import class_weapon

MyWeapon=weapon()
MyWeapon.weaponId = 100
MyWeapon.manufacturerName = "Glock"

print(MyWeapon.printWeaponInfo)

executing 'python3 weaponTrack.py' results in this bailing on the first 
element in the class with "not defined". I've been staring at templates 
of this exact structure for about an hour trying to figure out why this 
isn't running at all. Is it simply because it isn't all in one file? 
Thanks for any guidance. Really appreciate the help.



Thanks.

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


Re: Not Defined error in basic code

2019-03-14 Thread Calvin Spealman
Where are you seeing something like this? The two lines under `class
weapon:` are not correct because they are variable names that you've never
defined.

Maybe you intended this to "declare" the attributes for the class? That
isn't something you need to do in Python. If you simply remove these lines
your example should work.

On Thu, Mar 14, 2019 at 10:05 AM Jack Dangler  wrote:

> Just getting started with tutorials and such, and don't understand this -
>
> 
>
> class weapon:
>  weaponId
>  manufacturerName
>
>  def printWeaponInfo(self):
>  infoString = "ID: %d Mfg: %s Model: %s" % (self.weaponId,
> self.manufacturerName)
>  return infoString
>
> 
>
> import class_weapon
>
> MyWeapon=weapon()
> MyWeapon.weaponId = 100
> MyWeapon.manufacturerName = "Glock"
>
> print(MyWeapon.printWeaponInfo)
>
> executing 'python3 weaponTrack.py' results in this bailing on the first
> element in the class with "not defined". I've been staring at templates
> of this exact structure for about an hour trying to figure out why this
> isn't running at all. Is it simply because it isn't all in one file?
> Thanks for any guidance. Really appreciate the help.
>
>
> Thanks.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>


-- 

CALVIN SPEALMAN

SENIOR QUALITY ENGINEER

[email protected]  M: +1.336.210.5107

TRIED. TESTED. TRUSTED. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Not Defined error in basic code

2019-03-14 Thread Dan Sommers

On 3/14/19 9:05 AM, Jack Dangler wrote:

Just getting started with tutorials and such, and don't understand this -



class weapon:
      weaponId
      manufacturerName


The previous two lines attempt to refer to existing names,
but neither name exists.


      def printWeaponInfo(self):
      infoString = "ID: %d Mfg: %s Model: %s" % (self.weaponId,
self.manufacturerName >       return infoString



import class_weapon

MyWeapon=weapon()
MyWeapon.weaponId = 100
MyWeapon.manufacturerName = "Glock"

print(MyWeapon.printWeaponInfo)

executing 'python3 weaponTrack.py' results in this bailing on the first
element in the class with "not defined". I've been staring at templates
of this exact structure for about an hour trying to figure out why this
isn't running at all. Is it simply because it isn't all in one file?
Thanks for any guidance. Really appreciate the help.


I'm curious about the tutorial you're following.  Depending
on how faithfully you're following along, it seems to
violate some Python conventions, such as CamelCasing class
names and snake_casing (or lowercasing) other names.

Also, when you encounter an error, please copy and paste
the traceback instead of saying "this bailed" so that we
have something to go on rather that having to study your
example from top to bottom to try to figure out what's
wrong.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Not Defined error in basic code

2019-03-14 Thread Jason Friedman
On Thu, Mar 14, 2019 at 8:07 AM Jack Dangler  wrote:

> 
>
> class weapon:
>  weaponId
>  manufacturerName
>
>  def printWeaponInfo(self):
>  infoString = "ID: %d Mfg: %s Model: %s" % (self.weaponId,
> self.manufacturerName)
>  return infoString
>
> 
>
> import class_weapon
>
> MyWeapon=weapon()
> MyWeapon.weaponId = 100
> MyWeapon.manufacturerName = "Glock"
>
> print(MyWeapon.printWeaponInfo)
>
> executing 'python3 weaponTrack.py' results in this bailing on the first
> element in the class with "not defined".


Try running your module file by itself, that will give you a clue:

$ python3 class_weapon.py
Traceback (most recent call last):
  File "class_weapon.py", line 1, in 
class weapon:
  File "class_weapon.py", line 2, in weapon
weaponId
NameError: name 'weaponId' is not defined

You have another error in the file which will be revealed when you get it
to compile.

Also, your printWeapon method could renamed to __repr__ or __str__ (see
https://docs.python.org/3/reference/datamodel.html#object.__repr__)

Also most Python programmers would spell MyWeapon as my_weapon (see
https://www.python.org/dev/peps/pep-0008/).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Not Defined error in basic code

2019-03-14 Thread Jack Dangler


On 3/14/19 10:11 AM, Calvin Spealman wrote:
Where are you seeing something like this? The two lines under `class 
weapon:` are not correct because they are variable names that you've 
never defined.


Maybe you intended this to "declare" the attributes for the class? 
That isn't something you need to do in Python. If you simply remove 
these lines your example should work.


On Thu, Mar 14, 2019 at 10:05 AM Jack Dangler > wrote:


Just getting started with tutorials and such, and don't understand
this -



class weapon:
 weaponId
 manufacturerName

 def printWeaponInfo(self):
 infoString = "ID: %d Mfg: %s Model: %s" % (self.weaponId,
self.manufacturerName)
 return infoString



import class_weapon

MyWeapon=weapon()
MyWeapon.weaponId = 100
MyWeapon.manufacturerName = "Glock"

print(MyWeapon.printWeaponInfo)

executing 'python3 weaponTrack.py' results in this bailing on the
first
element in the class with "not defined". I've been staring at
templates
of this exact structure for about an hour trying to figure out why
this
isn't running at all. Is it simply because it isn't all in one file?
Thanks for any guidance. Really appreciate the help.


Thanks.

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




--

CALVIN SPEALMAN

SENIOR QUALITY ENGINEER

[email protected]  M: +1.336.210.5107 




TRIED. TESTED. TRUSTED. 


Calvin

Thank you for the reply. I tried defining them in the form of 'int 
weaponId' but that didn't help. I finally put it in this form 
'weaponId=0" and it liked that. So, i updated the class file to be as 
follows -




class weapon:
 weaponId=0
 manufacturerName=""

 def printWeaponInfo(self):
 infoString = "ID: %d Mfg: %s " % (self.weaponId, 
self.manufacturerName)

 return infoString

The second file now looks like this -



import class_weapon
MyWeapon=class_weapon.weapon
MyWeapon.weaponId = 100
MyWeapon.manufacturerName = "Glock"

print(MyWeapon.printWeaponInfo)

so now, when I run 'python3 weaponTrack.py', I get weapon.printWeaponInfo at 0x7f2bd3ae7510>, but am expecting


ID: 100 Mfg: Glock ...

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


Re: Not Defined error in basic code

2019-03-14 Thread Calvin Spealman
A few notes, Jack:

On Thu, Mar 14, 2019 at 10:32 AM Jack Dangler  wrote:

>
> On 3/14/19 10:11 AM, Calvin Spealman wrote:
>
> Where are you seeing something like this? The two lines under `class
> weapon:` are not correct because they are variable names that you've never
> defined.
>
> Maybe you intended this to "declare" the attributes for the class? That
> isn't something you need to do in Python. If you simply remove these lines
> your example should work.
>
> On Thu, Mar 14, 2019 at 10:05 AM Jack Dangler  wrote:
>
>> Just getting started with tutorials and such, and don't understand this -
>>
>> 
>>
>> class weapon:
>>  weaponId
>>  manufacturerName
>>
>>  def printWeaponInfo(self):
>>  infoString = "ID: %d Mfg: %s Model: %s" % (self.weaponId,
>> self.manufacturerName)
>>  return infoString
>>
>> 
>>
>> import class_weapon
>>
>> MyWeapon=weapon()
>> MyWeapon.weaponId = 100
>> MyWeapon.manufacturerName = "Glock"
>>
>> print(MyWeapon.printWeaponInfo)
>>
>> executing 'python3 weaponTrack.py' results in this bailing on the first
>> element in the class with "not defined". I've been staring at templates
>> of this exact structure for about an hour trying to figure out why this
>> isn't running at all. Is it simply because it isn't all in one file?
>> Thanks for any guidance. Really appreciate the help.
>>
>>
>> Thanks.
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
>
> --
>
> CALVIN SPEALMAN
>
> SENIOR QUALITY ENGINEER
>
> [email protected]  M: +1.336.210.5107
> 
> TRIED. TESTED. TRUSTED. 
>
> Calvin
>
> Thank you for the reply. I tried defining them in the form of 'int
> weaponId' but that didn't help. I finally put it in this form 'weaponId=0"
> and it liked that. So, i updated the class file to be as follows -
>
> 
>
> class weapon:
>  weaponId=0
>  manufacturerName=""
>
Technically this will work, but it won't always work. You're assigning
these values directly to the class (or type) and not to individual objects
of that type.

This will break very badly if you try to do this with any type of value
that can be changed (like a list, which you can add things to) because
you'll accidentally
modify values shared between ALL objects of the same type. Instead, you
want to define a __init__ method, which is called when all objects of this
type are
created, and assign the attributes in there. Like this:

def __init__(self):
self.weaponId = 0
self.manufacturerName = ""

Of course, you could make it easier to create the specific objects you want
by passing parameters at the creation of the object:

def __init__(self, weaponId, manufacturerName):
self.weaponId = weaponId
self.manufacturerName = manufacturerName

 def printWeaponInfo(self):
>  infoString = "ID: %d Mfg: %s " % (self.weaponId,
> self.manufacturerName)
>  return infoString
>
> The second file now looks like this -
>
> 
>
> import class_weapon
> MyWeapon=class_weapon.weapon
> MyWeapon.weaponId = 100
> MyWeapon.manufacturerName = "Glock"
>
If you follow my advice above, you won't need to override the values here.

But you aren't actually creating anything here, because this line:

MyWeapon = class_weapon.weapon

Doesn't create anything. It just assigns the class you made to a new name.
What you probably meant to do, and can do with the
__init__ I suggest above, is create an instance of your weapon class like
this:

MyWeapon = class_weapon.weapon(100, "Glock")

>
> print(MyWeapon.printWeaponInfo)
>
Similarly, you probably meant to call this method but without parenthesis
on the method you just printed the object representing
the method itself, rather than calling it and printing the value it returns.

print(MyWeapon.printWeaponInfo())

> so now, when I run 'python3 weaponTrack.py', I get  weapon.printWeaponInfo at 0x7f2bd3ae7510>, but am expecting
>
> ID: 100 Mfg: Glock ...
>
I hope this helps.

-- 

CALVIN SPEALMAN

SENIOR QUALITY ENGINEER

[email protected]  M: +1.336.210.5107

TRIED. TESTED. TRUSTED. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Not Defined error in basic code

2019-03-14 Thread Jack Dangler


On 3/14/19 10:39 AM, Calvin Spealman wrote:

A few notes, Jack:

On Thu, Mar 14, 2019 at 10:32 AM Jack Dangler > wrote:



On 3/14/19 10:11 AM, Calvin Spealman wrote:

Where are you seeing something like this? The two lines under
`class weapon:` are not correct because they are variable names
that you've never defined.

Maybe you intended this to "declare" the attributes for the
class? That isn't something you need to do in Python. If you
simply remove these lines your example should work.

On Thu, Mar 14, 2019 at 10:05 AM Jack Dangler mailto:[email protected]>> wrote:

Just getting started with tutorials and such, and don't
understand this -



class weapon:
 weaponId
 manufacturerName

 def printWeaponInfo(self):
 infoString = "ID: %d Mfg: %s Model: %s" %
(self.weaponId,
self.manufacturerName)
 return infoString



import class_weapon

MyWeapon=weapon()
MyWeapon.weaponId = 100
MyWeapon.manufacturerName = "Glock"

print(MyWeapon.printWeaponInfo)

executing 'python3 weaponTrack.py' results in this bailing on
the first
element in the class with "not defined". I've been staring at
templates
of this exact structure for about an hour trying to figure
out why this
isn't running at all. Is it simply because it isn't all in
one file?
Thanks for any guidance. Really appreciate the help.


Thanks.

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




-- 


CALVIN SPEALMAN

SENIOR QUALITY ENGINEER

[email protected]  M:
+1.336.210.5107 


TRIED. TESTED. TRUSTED. 


Calvin

Thank you for the reply. I tried defining them in the form of 'int
weaponId' but that didn't help. I finally put it in this form
'weaponId=0" and it liked that. So, i updated the class file to be
as follows -



class weapon:
 weaponId=0
 manufacturerName=""

Technically this will work, but it won't always work. You're assigning 
these values directly to the class (or type) and not to individual 
objects of that type.


This will break very badly if you try to do this with any type of 
value that can be changed (like a list, which you can add things to) 
because you'll accidentally
modify values shared between ALL objects of the same type. Instead, 
you want to define a __init__ method, which is called when all objects 
of this type are

created, and assign the attributes in there. Like this:

def __init__(self):
    self.weaponId = 0
    self.manufacturerName = ""

Of course, you could make it easier to create the specific objects you 
want by passing parameters at the creation of the object:


def __init__(self, weaponId, manufacturerName):
    self.weaponId = weaponId
    self.manufacturerName = manufacturerName

 def printWeaponInfo(self):
 infoString = "ID: %d Mfg: %s " % (self.weaponId,
self.manufacturerName)
 return infoString

The second file now looks like this -



import class_weapon
MyWeapon=class_weapon.weapon
MyWeapon.weaponId = 100
MyWeapon.manufacturerName = "Glock"

If you follow my advice above, you won't need to override the values here.

But you aren't actually creating anything here, because this line:

MyWeapon = class_weapon.weapon
Doesn't create anything. It just assigns the class you made to a new 
name. What you probably meant to do, and can do with the
__init__ I suggest above, is create an instance of your weapon class 
like this:


MyWeapon = class_weapon.weapon(100, "Glock")


print(MyWeapon.printWeaponInfo)

Similarly, you probably meant to call this method but without 
parenthesis on the method you just printed the object representing
the method itself, rather than calling it and printing the value it 
returns.


print(MyWeapon.printWeaponInfo())

so now, when I run 'python3 weaponTrack.py', I get , but am expecting

ID: 100 Mfg: Glock ...

I hope this helps.

--

CALVIN SPEALMAN

SENIOR QUALITY ENGINEER

[email protected]  M: +1.336.210.5107 




TRIED. TESTED. TRUSTED. 



Calvin

Really great explanation! Giving me a lot to go on. I changed the files 
as suggested (I think), and now have this -


The first file now looks like this -



class weapon:
    weaponId
    manufacturerName

    # Creation/Instantiation
    def __init__(self, weaponId, manufacturerName):
    self.weaponId = weaponId
    self.manufacturerName = manufacturerName

    # Print the class data
    def printWeaponInfo(self):
    infoString = "ID: %d Mfg: %s ." % (self.weaponId, 
self.manufacturerName)

    return i

Re: Not Defined error in basic code

2019-03-14 Thread Calvin Spealman
You still need to get of the two lines at the start of your class, they are
unnecessary and reference variables you never defined:

class weapon:
weaponId  # Get rid of this line!
manufacturerName  # And this one, too!

On Thu, Mar 14, 2019 at 12:43 PM Jack Dangler  wrote:

>
> On 3/14/19 10:39 AM, Calvin Spealman wrote:
>
> A few notes, Jack:
>
> On Thu, Mar 14, 2019 at 10:32 AM Jack Dangler  wrote:
>
>>
>> On 3/14/19 10:11 AM, Calvin Spealman wrote:
>>
>> Where are you seeing something like this? The two lines under `class
>> weapon:` are not correct because they are variable names that you've never
>> defined.
>>
>> Maybe you intended this to "declare" the attributes for the class? That
>> isn't something you need to do in Python. If you simply remove these lines
>> your example should work.
>>
>> On Thu, Mar 14, 2019 at 10:05 AM Jack Dangler  wrote:
>>
>>> Just getting started with tutorials and such, and don't understand this -
>>>
>>> 
>>>
>>> class weapon:
>>>  weaponId
>>>  manufacturerName
>>>
>>>  def printWeaponInfo(self):
>>>  infoString = "ID: %d Mfg: %s Model: %s" % (self.weaponId,
>>> self.manufacturerName)
>>>  return infoString
>>>
>>> 
>>>
>>> import class_weapon
>>>
>>> MyWeapon=weapon()
>>> MyWeapon.weaponId = 100
>>> MyWeapon.manufacturerName = "Glock"
>>>
>>> print(MyWeapon.printWeaponInfo)
>>>
>>> executing 'python3 weaponTrack.py' results in this bailing on the first
>>> element in the class with "not defined". I've been staring at templates
>>> of this exact structure for about an hour trying to figure out why this
>>> isn't running at all. Is it simply because it isn't all in one file?
>>> Thanks for any guidance. Really appreciate the help.
>>>
>>>
>>> Thanks.
>>>
>>> --
>>> https://mail.python.org/mailman/listinfo/python-list
>>>
>>
>>
>> --
>>
>> CALVIN SPEALMAN
>>
>> SENIOR QUALITY ENGINEER
>>
>> [email protected]  M: +1.336.210.5107
>> 
>> TRIED. TESTED. TRUSTED. 
>>
>> Calvin
>>
>> Thank you for the reply. I tried defining them in the form of 'int
>> weaponId' but that didn't help. I finally put it in this form 'weaponId=0"
>> and it liked that. So, i updated the class file to be as follows -
>>
>> 
>>
>> class weapon:
>>  weaponId=0
>>  manufacturerName=""
>>
> Technically this will work, but it won't always work. You're assigning
> these values directly to the class (or type) and not to individual objects
> of that type.
>
> This will break very badly if you try to do this with any type of value
> that can be changed (like a list, which you can add things to) because
> you'll accidentally
> modify values shared between ALL objects of the same type. Instead, you
> want to define a __init__ method, which is called when all objects of this
> type are
> created, and assign the attributes in there. Like this:
>
> def __init__(self):
> self.weaponId = 0
> self.manufacturerName = ""
>
> Of course, you could make it easier to create the specific objects you
> want by passing parameters at the creation of the object:
>
> def __init__(self, weaponId, manufacturerName):
> self.weaponId = weaponId
> self.manufacturerName = manufacturerName
>
>  def printWeaponInfo(self):
>>  infoString = "ID: %d Mfg: %s " % (self.weaponId,
>> self.manufacturerName)
>>  return infoString
>>
>> The second file now looks like this -
>>
>> 
>>
>> import class_weapon
>> MyWeapon=class_weapon.weapon
>> MyWeapon.weaponId = 100
>> MyWeapon.manufacturerName = "Glock"
>>
> If you follow my advice above, you won't need to override the values here.
>
> But you aren't actually creating anything here, because this line:
>
> MyWeapon = class_weapon.weapon
>
> Doesn't create anything. It just assigns the class you made to a new name.
> What you probably meant to do, and can do with the
> __init__ I suggest above, is create an instance of your weapon class like
> this:
>
> MyWeapon = class_weapon.weapon(100, "Glock")
>
>>
>> print(MyWeapon.printWeaponInfo)
>>
> Similarly, you probably meant to call this method but without parenthesis
> on the method you just printed the object representing
> the method itself, rather than calling it and printing the value it
> returns.
>
> print(MyWeapon.printWeaponInfo())
>
>> so now, when I run 'python3 weaponTrack.py', I get > weapon.printWeaponInfo at 0x7f2bd3ae7510>, but am expecting
>>
>> ID: 100 Mfg: Glock ...
>>
> I hope this helps.
>
> --
>
> CALVIN SPEALMAN
>
> SENIOR QUALITY ENGINEER
>
> [email protected]  M: +1.336.210.5107
> 
> TRIED. TESTED. TRUSTED. 
>
>
> Calvin
>
> Really great explanation! Giving me a lot to go on. I changed the files as
> suggested (I think), and now have this -
>
> The first file now looks like this -
>
> 
>
> class weapon:
> weaponId
> manufacturerName
>
> # Creation/Instantiation
> def __init__(self, weaponId, manufacturerName):
> self.weaponId =

Re: Not Defined error in basic code

2019-03-14 Thread Jack Dangler


On 3/14/19 12:50 PM, Calvin Spealman wrote:
You still need to get of the two lines at the start of your class, 
they are unnecessary and reference variables you never defined:


class weapon:
    weaponId  # Get rid of this line!
    manufacturerName  # And this one, too!

On Thu, Mar 14, 2019 at 12:43 PM Jack Dangler > wrote:



On 3/14/19 10:39 AM, Calvin Spealman wrote:

A few notes, Jack:

On Thu, Mar 14, 2019 at 10:32 AM Jack Dangler mailto:[email protected]>> wrote:


On 3/14/19 10:11 AM, Calvin Spealman wrote:

Where are you seeing something like this? The two lines
under `class weapon:` are not correct because they are
variable names that you've never defined.

Maybe you intended this to "declare" the attributes for the
class? That isn't something you need to do in Python. If you
simply remove these lines your example should work.

On Thu, Mar 14, 2019 at 10:05 AM Jack Dangler
mailto:[email protected]>> wrote:

Just getting started with tutorials and such, and don't
understand this -



class weapon:
 weaponId
 manufacturerName

 def printWeaponInfo(self):
 infoString = "ID: %d Mfg: %s Model: %s" %
(self.weaponId,
self.manufacturerName)
 return infoString



import class_weapon

MyWeapon=weapon()
MyWeapon.weaponId = 100
MyWeapon.manufacturerName = "Glock"

print(MyWeapon.printWeaponInfo)

executing 'python3 weaponTrack.py' results in this
bailing on the first
element in the class with "not defined". I've been
staring at templates
of this exact structure for about an hour trying to
figure out why this
isn't running at all. Is it simply because it isn't all
in one file?
Thanks for any guidance. Really appreciate the help.


Thanks.

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




-- 


CALVIN SPEALMAN

SENIOR QUALITY ENGINEER

[email protected]  M:
+1.336.210.5107 


TRIED. TESTED. TRUSTED. 


Calvin

Thank you for the reply. I tried defining them in the form of
'int weaponId' but that didn't help. I finally put it in this
form 'weaponId=0" and it liked that. So, i updated the class
file to be as follows -



class weapon:
 weaponId=0
 manufacturerName=""

Technically this will work, but it won't always work. You're
assigning these values directly to the class (or type) and not to
individual objects of that type.

This will break very badly if you try to do this with any type of
value that can be changed (like a list, which you can add things
to) because you'll accidentally
modify values shared between ALL objects of the same type.
Instead, you want to define a __init__ method, which is called
when all objects of this type are
created, and assign the attributes in there. Like this:

def __init__(self):
    self.weaponId = 0
self.manufacturerName = ""

Of course, you could make it easier to create the specific
objects you want by passing parameters at the creation of the object:

def __init__(self, weaponId, manufacturerName):
    self.weaponId = weaponId
self.manufacturerName = manufacturerName

 def printWeaponInfo(self):
 infoString = "ID: %d Mfg: %s " % (self.weaponId,
self.manufacturerName)
 return infoString

The second file now looks like this -



import class_weapon
MyWeapon=class_weapon.weapon
MyWeapon.weaponId = 100
MyWeapon.manufacturerName = "Glock"

If you follow my advice above, you won't need to override the
values here.

But you aren't actually creating anything here, because this line:

MyWeapon = class_weapon.weapon
Doesn't create anything. It just assigns the class you made to a
new name. What you probably meant to do, and can do with the
__init__ I suggest above, is create an instance of your weapon
class like this:

MyWeapon = class_weapon.weapon(100, "Glock")


print(MyWeapon.printWeaponInfo)

Similarly, you probably meant to call this method but without
parenthesis on the method you just printed the object representing
the method itself, rather than calling it and printing the value
it returns.

print(MyWeapon.printWeaponInfo())

so now, when I run 'python3 weaponTrack.py', I get , but am expecting

ID: 100 Mfg: Glock ...

I hop

Re: Not a python question, just programming logic trap?

2019-03-14 Thread jonas . thornvall
Den torsdag 14 mars 2019 kl. 06:09:02 UTC+1 skrev Rick Johnson:
> [email protected] wrote:
> > Nah i can't see the error is due to the "If clause" branching possibly it 
> > should been an elseif at instead of separate if. No there is something 
> > going wrong with the variables.
> 
> Well, then you'd be wise to unpack those variables and inspect them.

I've done that there is nothing wrong with them the error comes when writing 
out to the canvas, and i rounded them so it is integer pixels. To be honest i 
think there maybe something with the canvas that goes wrong. A block of fixed 
length can't expand. I run console.logg()  barLength=xFrontStart-xBackEnd and 
it is constant at every call within if clause.  It is just that when canvas 
write out the values apparently the draw rectangle function somehow hold other 
values.

Well i restart the whole render programming, doing it right from start this 
time reading values from the record buffer and see if the animation problem 
magically dissapear. It could be something undefined affect the the variables.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: UG Announcement - Python Mauritius User-Group (pymug)

2019-03-14 Thread DL Neil

On 14/03/19 6:53 PM, Abdur-Rahmaan Janhangeer wrote:

As per requirements, i'm announcing the existence of the Python User-Group
for Mauritius, an island in the Indian Ocean. Below are some info.


Congratulations!

--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: Not Defined error in basic code

2019-03-14 Thread DL Neil

Jack,


On 15/03/19 3:05 AM, Jack Dangler wrote:

Just getting started with tutorials and such, and don't understand this -



Did you answer the post asking which tutorial you were following/copying?

--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: Not Defined error in basic code

2019-03-14 Thread Jack Dangler



On 3/14/19 2:28 PM, DL Neil wrote:

Jack,


On 15/03/19 3:05 AM, Jack Dangler wrote:
Just getting started with tutorials and such, and don't understand 
this -



Did you answer the post asking which tutorial you were following/copying?


Sorry - it is this -

https://www.learnpython.org/en/ ..

The section is on classes and objects - 
https://www.learnpython.org/en/Classes_and_Objects

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


Re: Not Defined error in basic code

2019-03-14 Thread DL Neil
Just getting started with tutorials and such, and don't understand 
this -

Did you answer the post asking which tutorial you were following/copying?

Sorry - it is this -
https://www.learnpython.org/en/ ..
The section is on classes and objects - 
https://www.learnpython.org/en/Classes_and_Objects



Wanted to check: Someone publishing the 'first code' in this thread as 
training material, would have been 'unhelpful'.


Those guys have a good reputation - if the code camp approach suits the 
learner, rapid progress can be made.


The examples they provide illustrate attributes being 'defined' with a 
value. (unlike the first code 'here' which only listed attributes)


This feature of Python's dynamic nature often confuses folk who come 
from other, more rigidly defined, languages - there is no need to 
declare "id" as an integer per-se, passing it an integer constant as its 
value performs two tasks for the price of one!


Advice (if I may): don't be too literal in attempting to write Python 
the way you currently write xyz-language code.


You may find it helpful to combine those tutorials with similar 
information from additional sources, eg the Python docs site's tutorial, 
other on-line books, 'dead-tree' sources, other on-line training, etc. 
Whilst it seems like more work (it's certainly more reading/scanning), 
two non-identical statements of 'the same thing', will express things 
differently. Often something from one source that is initially puzzling, 
when compared with another presentation of the 'same', helps learners' 
minds go 'click' or to enjoy that 'ahah moment'.


All the best...
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: Not Defined error in basic code

2019-03-14 Thread Jack Dangler



On 3/14/19 3:49 PM, DL Neil wrote:
Just getting started with tutorials and such, and don't understand 
this -
Did you answer the post asking which tutorial you were 
following/copying?

Sorry - it is this -
https://www.learnpython.org/en/ ..
The section is on classes and objects - 
https://www.learnpython.org/en/Classes_and_Objects



Wanted to check: Someone publishing the 'first code' in this thread as 
training material, would have been 'unhelpful'.


Those guys have a good reputation - if the code camp approach suits 
the learner, rapid progress can be made.


The examples they provide illustrate attributes being 'defined' with a 
value. (unlike the first code 'here' which only listed attributes)


This feature of Python's dynamic nature often confuses folk who come 
from other, more rigidly defined, languages - there is no need to 
declare "id" as an integer per-se, passing it an integer constant as 
its value performs two tasks for the price of one!


Advice (if I may): don't be too literal in attempting to write Python 
the way you currently write xyz-language code.


You may find it helpful to combine those tutorials with similar 
information from additional sources, eg the Python docs site's 
tutorial, other on-line books, 'dead-tree' sources, other on-line 
training, etc. Whilst it seems like more work (it's certainly more 
reading/scanning), two non-identical statements of 'the same thing', 
will express things differently. Often something from one source that 
is initially puzzling, when compared with another presentation of the 
'same', helps learners' minds go 'click' or to enjoy that 'ahah moment'.


All the best...
Thank you sir. I think you may be on to something there. I've done 
mainframe, machine, 3GL, and 4GL languages, but the last couple I've 
taken on have given me headaches. I guess I'll have to just read a bunch 
first and then try and write something simpler than what I'm attempting 
to take on... Thanks for your help and understanding.

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


Re: Not Defined error in basic code

2019-03-14 Thread DL Neil
Thank you sir. I think you may be on to something there. I've done 
mainframe, machine, 3GL, and 4GL languages, but the last couple I've 
taken on have given me headaches. I guess I'll have to just read a bunch 
first and then try and write something simpler than what I'm attempting 
to take on... Thanks for your help and understanding.


Apologies for any misunderstanding. Don't 'stop'!

Keep reading one 'chunk' of learning at a time, and then proving it has 
lodged 'between the ears' by experimenting.


However, if it doesn't make sense/come together for you, 'read around'. 
The frequent practice is vital to effective and efficient learning (in 
my professional opinion).


In fact, as you have probably noted throughout your career, one of the 
best things a professional can do, is to keep reading, even re-reading, 
about the chosen topic.



PS us 'silver surfers' have to stick together...
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: Generator question

2019-03-14 Thread Pierre Reinbold
Wow, thank you Ian for this very detailed answer, and thank you for taking the
time for that! Much appreciated!

If I get this right, I have to somehow fix the value of a_list during the loop,
like when you use the classic default value argument trick with lambdas (damn, I
should have thought of that!). So if I "unfold" the generator expression, using
default values for both iterables, I get this :

def flat_gen_cat_prod(lists):
solutions = [[]]
for a_list in lists:
def new_solutions(l=a_list, s=solutions):
for part_sol in s:
for el in l:
yield part_sol+[el]
solutions = new_solutions()
return solutions

With this I get the right behavior! Thanks again!

Doest that mean that there is no possibility to use a generator expression in
this case ? (gen. exp. are sooo much more elegant :-))

Thanks again for the answer and helpful comments!


πr

Le 14/03/19 à 02:44, Ian Kelly a écrit :
> You're basically running into this:
> https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result
> 
> To see why, let's try disassembling your function. I'm using Python 3.5
> here, but it shouldn't make much of a difference.
> 
> py> import dis
> py> dis.dis(flat_genexp_cat_prod)
>   2   0 BUILD_LIST   0
>   3 BUILD_LIST   1
>   6 STORE_FAST   1 (solutions)
> 
>   3   9 SETUP_LOOP  39 (to 51)
>  12 LOAD_FAST0 (lists)
>  15 GET_ITER
> >>   16 FOR_ITER31 (to 50)
>  19 STORE_DEREF  0 (a_list)
> 
>   4  22 LOAD_CLOSURE 0 (a_list)
>  25 BUILD_TUPLE  1
>  28 LOAD_CONST   1 ( at
> 0x73f31571ac90, file "", line 4>)
>  31 LOAD_CONST   2
> ('flat_genexp_cat_prod..')
>  34 MAKE_CLOSURE 0
>  37 LOAD_FAST1 (solutions)
>  40 GET_ITER
>  41 CALL_FUNCTION1 (1 positional, 0 keyword pair)
>  44 STORE_FAST   1 (solutions)
>  47 JUMP_ABSOLUTE   16
> >>   50 POP_BLOCK
> 
>   5 >>   51 LOAD_FAST1 (solutions)
>  54 RETURN_VALUE
> 
> Now, take a look at the difference between the instruction at address 22
> and the one at address 37:
> 
>   4  22 LOAD_CLOSURE 0 (a_list)
>  37 LOAD_FAST1 (solutions)
> 
> The value of solutions is passed directly to the generator as an argument,
> which is the reason why building the generator up iteratively like this
> works at all: although the nested generators are evaluated lazily, each new
> generator that is constructed contains as its input a reference to the
> previous generator.
> 
> By contrast, the value of a_list is a closure. The contents of the closure
> are just whatever the value of a_list is when the generator gets evaluated,
> not when the generator was created. Since the entire nested generated
> structure is evaluated lazily, it doesn't get evaluated until list() is
> called after the function has returned. The value of the a_list closure at
> that point is the last value that was assigned to it: the list [5, 6] from
> the last iteration of the for loop. This same list value then gets used for
> all three nested generators.
> 
> So now why do solutions and a_list get treated differently like this? To
> answer this, look at this paragraph about generator expressions from the
> language reference:
> 
> """
> Variables used in the generator expression are evaluated lazily when the
> __next__() method is called for the generator object (in the same fashion
> as normal generators). However, the iterable expression in the leftmost for
> clause is immediately evaluated, so that an error produced by it will be
> emitted at the point where the generator expression is defined, rather than
> at the point where the first value is retrieved. Subsequent for clauses and
> any filter condition in the leftmost for clause cannot be evaluated in the
> enclosing scope as they may depend on the values obtained from the leftmost
> iterable. For example: (x*y for x in range(10) for y in range(x, x+10)).
> """
> 
> So, it's simply because the iterable expression in the leftmost for clause
> is treated differently from every other value in the generator expression.
> 
> On Wed, Mar 13, 2019 at 3:49 PM Pierre Reinbold  wrote:
> 
>> Dear all,
>>
>> I want to implement a function computing the Cartesian product if the
>> elements
>> of a list of lists, but using generator expressions. I know that it is
>> already
>> available in itertools but it is for the sake of understanding how things
>> work.
>>
>> I already have a working recursive version, and I'm quite sure that this
>> iterative version used to wo

How to modify Adaline Stochastic gradient descent

2019-03-14 Thread vokoyov
Dear 

May I know how to modify my own Python programming so that I will get the 
same picture as refer to the attached file - Adaline Stochastic gradient descent

(I am using the Anaconda Python 3.7)

Prayerfully 
 
Tron Orino Yeong  
[email protected] 
0916643858




from matplotlib.colors import ListedColormap
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import seed
import pandas as pd

# Stochastic Gradient Descent
class SGD(object):
   def __init__(self, rate = 0.01, niter = 10,
shuffle=True, random_state=None):
  self.rate = rate
  self.niter = niter
  self.weight_initialized = False

  # If True, Shuffles training data every epoch
  self.shuffle = shuffle

  # Set random state for shuffling and initializing the weights.
  if random_state:
 seed(random_state)

   def fit(self, X, y):
  """Fit training data
  X : Training vectors, X.shape : [#samples, #features]
  y : Target values, y.shape : [#samples]
  """

  # weights
  self.initialize_weights(X.shape[1])

  # Cost function
  self.cost = []

  for i in range(self.niter):
 if self.shuffle:
X, y = self.shuffle_set(X, y)
 cost = []
 for xi, target in zip(X, y):
cost.append(self.update_weights(xi, target))
 avg_cost = sum(cost)/len(y)
 self.cost.append(avg_cost)
  return self

   def partial_fit(self, X, y):
  """Fit training data without reinitializing the weights"""
  if not self.weight_initialized:
 self.initialize_weights(X.shape[1])
  if y.ravel().shape[0] > 1:
 for xi, target in zip(X, y):
self.update_weights(xi, target)
  else:
 self.up
  return self

   def shuffle_set(self, X, y):
  """Shuffle training data"""
  r = np.random.permutation(len(y))
  return X[r], y[r]

   def initialize_weights(self, m):
  """Initialize weights to zeros"""
  self.weight = np.zeros(1 + m)
  self.weight_initialized = True

   def update_weights(self, xi, target):
  """Apply SGD learning rule to update the weights"""
  output = self.net_input(xi)
  error = (target - output)
  self.weight[1:] += self.rate * xi.dot(error)
  self.weight[0] += self.rate * error
  cost = 0.5 * error**2
  return cost

   def net_input(self, X):
  """Calculate net input"""
  return np.dot(X, self.weight[1:]) + self.weight[0]

   def activation(self, X):
  """Compute linear activation"""
  return self.net_input(X)

   def predict(self, X):
  """Return class label after unit step"""
  return np.where(self.activation(X) >= 0.0, 1, -1)

def plot_decision_regions(X, y, classifier, resolution=0.02):
   # setup marker generator and color map
   markers = ('s', 'x', 'o', '^', 'v')
   colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
   cmap = ListedColormap(colors[:len(np.unique(y))])

   # plot the decision surface
   x1_min, x1_max = X[:,  0].min() - 1, X[:, 0].max() + 1
   x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
   xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
   np.arange(x2_min, x2_max, resolution))
   Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
   Z = Z.reshape(xx1.shape)
   plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap)
   plt.xlim(xx1.min(), xx1.max())
   plt.ylim(xx2.min(), xx2.max())

   # plot class samples
   for idx, cl in enumerate(np.unique(y)):
  plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1],
  alpha=0.8, c=cmap(idx),
  marker=markers[idx], label=cl)

df = 
pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data',
 header=None)

y = df.iloc[0:100, 4].values
y = np.where(y == 'Iris-setosa', -1, 1)
X = df.iloc[0:100, [0, 2]].values

# standardize
X_std = np.copy(X)
X_std[:,0] = (X[:,0] - X[:,0].mean()) / X[:,0].std()
X_std[:,1] = (X[:,1] - X[:,1].mean()) / X[:,1].std()

sgd1 = SGD(niter=100, rate=0.01, random_state=1)
sgd2 = SGD(niter=50, rate=0.01, random_state=1)
sgd3 = SGD(niter=10, rate=0.01, random_state=1)

sgd1.fit(X_std, y)
sgd2.fit(X_std, y)
sgd3.fit(X_std, y)

plt.plot(range(1, len(sgd1.cost) + 1), sgd1.cost, 
 marker='o', linestyle='oo', label='batch=1')
plt.plot(range(1, len(sgd2.cost_) + 1), np.array(sgd2.cost_) / len(y_train), 
 marker='o', linestyle='--', label='batch=2')
plt.plot(range(1, len(sgd3.cost_) + 1), np.array(sgd3.cost_) / len(y_train), 
 marker='o', linestyle='xx', label='batch=3')

plt.xlabel('Epochs')
plt.ylabel('Average Cost')
plt.show()






Please refer to the link -

https://www.scienceforums.net/topic/118410-how-to-modify-adaline-stochastic-gradient-descent/?tab=comments#comment-1097272















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


Re: How to modify Adaline Stochastic gradient descent

2019-03-14 Thread MRAB

On 2019-03-15 02:48, [email protected] wrote:

Dear

May I know how to modify my own Python programming so that I will get the
same picture as refer to the attached file - Adaline Stochastic gradient descent


[snip]
This newsgroup is text-only, and all other attachments, including 
pictures, are removed. If you want to refer to a picture, you will need 
to use some kind of file-sharing site, such as DropBox, and provide a 
link to it.

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