Plot not working
I am new to Python and using it to learn machine learning. Below is a sample program I am running to plot IRIS data set. The code runs but no plot comes up. I am not sure what the issue is with the code. # Imports from matplotlib import pyplot as plt from sklearn.datasets import load_iris import numpy as np # load the data with load_iris from sklearn data = load_iris() features = data['data'] feature_names = data['feature_names'] target = data['target'] #print("features\n",features) #print("feature names\n",feature_names) #print("target\n",target); #print("data\n",data) for t,marker,c in zip(range(3),">ox","rgb"): # plot each class on its own to get different colored markers plt.scatter(features[target == t,0], features[target == t,1], marker=marker, c=c) -- https://mail.python.org/mailman/listinfo/python-list
Re: Plot not working
On Saturday, 5 May 2018 10:59:13 UTC+5:30, John Ladasky wrote: > On Friday, May 4, 2018 at 9:13:02 PM UTC-7, Sharan Basappa wrote: > > I am new to Python and using it to learn machine learning. > > > > Below is a sample program I am running to plot IRIS data set. > > The code runs but no plot comes up. I am not sure what the issue is with > > the code. > > > > # Imports > > from matplotlib import pyplot as plt > > from sklearn.datasets import load_iris > > import numpy as np > > > > # load the data with load_iris from sklearn > > data = load_iris() > > features = data['data'] > > feature_names = data['feature_names'] > > target = data['target'] > > > > #print("features\n",features) > > #print("feature names\n",feature_names) > > #print("target\n",target); > > #print("data\n",data) > > > > for t,marker,c in zip(range(3),">ox","rgb"): > > # plot each class on its own to get different colored markers > > plt.scatter(features[target == t,0], > > features[target == t,1], > > marker=marker, > > c=c) > > Try adding a call to plt.show() at the end. Thanks a lot. That was the issue -- https://mail.python.org/mailman/listinfo/python-list
ipython install
I am trying to install ipython. I already have python installed. When I type "pip install ipython" I get the following messages: Requirement already satisfied: setuptools>=18.5 in d:\users\sharanb\appdata\local\programs\python\python36-32\lib\site-packages (from ipython) Requirement already satisfied: jedi>=0.10 in d:\users\sharanb\appdata\roaming\python\python36\site-packages (from ipython) Requirement already satisfied: colorama; sys_platform == "win32" in d:\users\sharanb\appdata\roaming\python\python36\site-packages (from ipython) Requirement already satisfied: decorator in d:\users\sharanb\appdata\roaming\python\python36\site-packages (from ipython) Requirement already satisfied: traitlets>=4.2 in d:\users\sharanb\appdata\roaming\python\python36\site-packages (from ipython) After this, when I type ipython on command prompt, I don't get anything on my windows. Am I making any mistakes installing ipython? -- https://mail.python.org/mailman/listinfo/python-list
Re: ipython install
On Saturday, 5 May 2018 15:02:33 UTC+5:30, Steven D'Aprano wrote: > On Sat, 05 May 2018 00:20:26 -0700, Sharan Basappa wrote: > > > After this, when I type ipython on command prompt, I don't get anything > > on my windows. > > You don't get *anything*? Not even an error? > > > > -- > Steve like typical behaviour when you don't have the application installed on your computer, it actually open up a browser and gives results for ipython using bing -- https://mail.python.org/mailman/listinfo/python-list
Re: ipython install
On Saturday, 5 May 2018 15:39:32 UTC+5:30, Sharan Basappa wrote: > On Saturday, 5 May 2018 15:02:33 UTC+5:30, Steven D'Aprano wrote: > > On Sat, 05 May 2018 00:20:26 -0700, Sharan Basappa wrote: > > > > > After this, when I type ipython on command prompt, I don't get anything > > > on my windows. > > > > You don't get *anything*? Not even an error? > > > > > > > > -- > > Steve > > like typical behaviour when you don't have the application installed on your > computer, it actually open up a browser and gives results for ipython using > bing by the way, i listed all the installed modules and don't see ipython at all. I see the following: autoexpand imaplib re winreg autoreload imghdr redirector winsound base64 imp replace wsgiref bdb importlib reprlib xdrlib binasciiinspect requestsxml binhex io rlcompleter xmlrpc bisect iomenu rmagic xxsubtype bleach ipaddress rpc zipapp botoipykernel rstrip zipfile boto3 ipykernel_launcher run zipimport botocoreipython_genutilsrunpy zlib browser ipywidgets runscript zmq builtinsitertools s3transfer zoomheight bz2 jedisched zzdummy -- https://mail.python.org/mailman/listinfo/python-list
write to file
In my program, I have print statements for debugging. However, these are cluttering the display. So, I am trying to save these to a file but somehow I cant seem to get it correct. For example, the following are the print statement in my program: print("target_names\n",target_names) To print to a file, I have opened the file using - fh = open("ML_PY_2.log","w+") Beyond this, I see that fh.write has to be used but most of the examples I see only show how strings are saved. Can I get input how to covert the above print statement to save into the file? Thanks in advance -- https://mail.python.org/mailman/listinfo/python-list
Re: write to file
On Saturday, 5 May 2018 19:00:12 UTC+5:30, Steven D'Aprano wrote: > On Sat, 05 May 2018 04:25:50 -0700, Sharan Basappa wrote: > > > In my program, I have print statements for debugging. However, these are > > cluttering the display. So, I am trying to save these to a file but > > somehow I cant seem to get it correct. > > There are lots of way to solve this problem. > > The best way is to use the logging module. Here are a couple of > introductions to it: > > https://docs.python.org/3/howto/logging.html#logging-basic-tutorial > > https://pymotw.com/3/logging/index.html > > and there are many more: > > https://duckduckgo.com/?q=python+logging+tutorial > > > but we can start with something much simpler, just using print. > > You have already run: > > fh = open("ML_PY_2.log","w+") > > at the start of your program, now call print: > > print("target_names:",target_names, file=fh) > > Finally, at the end of your program, call: > > fh.close() > > > -- > Steve Steve, Thanks a lot. I have actually tried print with file handle as a parameter (the last option). I see that the file is created but nothing is logged. So, I assumed, I am doing something incorrect. Anyway, I will try this again and let you know how it goes. -- https://mail.python.org/mailman/listinfo/python-list
Re: write to file
On Saturday, 5 May 2018 22:05:53 UTC+5:30, Mark Lawrence wrote: > On 05/05/18 12:25, Sharan Basappa wrote: > > In my program, I have print statements for debugging. > > However, these are cluttering the display. So, I am trying to save > > these to a file but somehow I cant seem to get it correct. > > Use the logging module https://docs.python.org/3/library/logging.html > for debugging, it's far easier in the longer term. > > > > > For example, the following are the print statement in my program: > > print("target_names\n",target_names) > > > > To print to a file, I have opened the file using - fh = > > open("ML_PY_2.log","w+") > > Beyond this, I see that fh.write has to be used but most of the examples I > > see only show how strings are saved. > > > > Can I get input how to covert the above print statement to save into the > > file? > > You'll need to do some string formatting. Take your pick from :- > > The old but still heavilly used C style > https://docs.python.org/3/library/stdtypes.html#old-string-formatting > > The new style > https://docs.python.org/3/library/string.html#format-string-syntax > > If you have Python 3.6 f-strings > > > > > Thanks in advance > > > > -- > My fellow Pythonistas, ask not what our language can do for you, ask > what you can do for our language. > > Mark Lawrence Mark, Thanks a lot. I will take a look at logging module. I have used logger module in perl which really takes out a lot of coding effort -- https://mail.python.org/mailman/listinfo/python-list
Re: write to file
On Saturday, 5 May 2018 21:47:33 UTC+5:30, Steven D'Aprano wrote: > On Sat, 05 May 2018 08:45:39 -0700, Sharan Basappa wrote: > > > Thanks a lot. I have actually tried print with file handle as a > > parameter (the last option). I see that the file is created but nothing > > is logged. > > That could be a file buffer issue. Nothing will actually be written to > the disk until either the buffer is full, or you close the file. Try > calling fh.flush() from time to time, or use: > > print(msg, file=fh, flush=True) > > > although things may be different on Windows. (For example, you may not be > able to open the file while it is still open in Python.) > > > > -- > Steve Steve, I agree that flushing could be an issue but I assume when the program closes, buffered data should be flushed even if I am explicitly flushing. This I did not see happening. Probably, I have to spend some additional time. -- https://mail.python.org/mailman/listinfo/python-list
Re: write to file
On Sunday, 6 May 2018 10:48:12 UTC+5:30, Chris Angelico wrote: > On Sun, May 6, 2018 at 3:10 PM, Sharan Basappa > wrote: > > On Saturday, 5 May 2018 21:47:33 UTC+5:30, Steven D'Aprano wrote: > >> On Sat, 05 May 2018 08:45:39 -0700, Sharan Basappa wrote: > >> > >> > Thanks a lot. I have actually tried print with file handle as a > >> > parameter (the last option). I see that the file is created but nothing > >> > is logged. > >> > >> That could be a file buffer issue. Nothing will actually be written to > >> the disk until either the buffer is full, or you close the file. Try > >> calling fh.flush() from time to time, or use: > >> > >> print(msg, file=fh, flush=True) > >> > >> > >> although things may be different on Windows. (For example, you may not be > >> able to open the file while it is still open in Python.) > >> > >> > >> > >> -- > >> Steve > > > > Steve, > > > > I agree that flushing could be an issue but I assume when the program > > closes, buffered data should be flushed even if I am explicitly flushing. > > This I did not see happening. Probably, I have to spend some additional > > time. > > -- > > Can you post a complete (but preferably small) example of a program > that creates a file but doesn't properly write to it? > > ChrisA Thanks, Everyone. I was probably making some mistake. I re-checked again and added file handle to the prints. Everything works fine. Below is the code ... # Imports from matplotlib import pyplot as plt from sklearn.datasets import load_iris import numpy as np import pickle fh = open("ML_PY_2.log","w+") # load the data with load_iris from sklearn data = load_iris() features = data['data'] feature_names = data['feature_names'] target = data['target'] target_names = data['target_names'] labels = target_names[target] plength = features[:,2] # use numpy operations to get setosa features is_setosa = (labels == 'setosa') # this the important step max_setosa = plength[is_setosa].max() min_non_setosa = plength[~is_setosa].min() print("data\n",data,file=fh) print("target\n",target, file=fh) print("target_names\n",target_names,file=fh) print("labels\n",labels,file=fh) print("is_setosa\n",is_setosa,file=fh) print("plength\n",plength,file=fh) print('maximum of setosa: {0}.'.format(max_setosa)) print('minimum of others: {0}.'.format(min_non_setosa)) fh.close() -- https://mail.python.org/mailman/listinfo/python-list
Module, Package
I am a bit confused between module and package in Python. Does a module contain package or vice versa? When we import something in Python, do we import a module or a package? Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: Module, Package
On Monday, 7 May 2018 23:09:41 UTC+5:30, Chris Angelico wrote: > On Tue, May 8, 2018 at 2:53 AM, Sharan Basappa > wrote: > > I am a bit confused between module and package in Python. > > Does a module contain package or vice versa? > > When we import something in Python, do we import a module or a package? > > You import a module. > > A package is one particular form of module, which is built out of > other modules (usually a directory full of .py files). There are a > number of different sorts of modules; regardless, you 'import X' and > get module X. > > ChrisA Thank you very much. Much appreiated -- https://mail.python.org/mailman/listinfo/python-list
Re: Module, Package
MRAB, ChirisA, One question. So, we can import the entire package or just a module in a given package. Is this correct? For example, import nltk import nltk.stem -- https://mail.python.org/mailman/listinfo/python-list
Re: Module, Package
On Tuesday, 8 May 2018 13:05:58 UTC+5:30, Steven D'Aprano wrote: > On Mon, 07 May 2018 09:53:45 -0700, Sharan Basappa wrote: > > > I am a bit confused between module and package in Python. Does a module > > contain package or vice versa? When we import something in Python, do we > > import a module or a package? > > The term "module" in Python has multiple meanings: > > - a particular kind of object, types.ModuleType > > - a single importable .py, .pyc etc file > > A package is a logical collection of importable .py etc files, usually > collected inside a single directory. When you import a module of a > package, that gives you a module object. > > Normally we would say that packages contain modules. For example, if you > have this file structure: > > > library/ > +-- __init__.py # special file which defines a package > +-- widgets.py > +-- stuff/ > +-- __init__.py > +-- things.py > > > then we have a package "library", which in turn contains a submodule > "library.widgets", and a subpackage "library.stuff", which in turn > contains a submodule "library.stuff.things". > > Each of these lines imports a module object: > > import library > import library.stuff > import library.stuff.things > import library.widgets > > from library import widgets > from library.stuff import things > > > Effectively, "packages" relates to how you arrange the files on disk; > "modules" relates to what happens when you import them. > > > -- > Steve Wow! Thanks a lot. -- https://mail.python.org/mailman/listinfo/python-list
PIP install
I have installed Python recently. Do I need to install PIP separately or this would be part of default installation. When I run pip install <>, windows complains that no such command exists -- https://mail.python.org/mailman/listinfo/python-list
issue runing ipython
I see an issue while running ipython. Can anyone help please. D:\Users\sharanb>ipython notebook [TerminalIPythonApp] WARNING | Subcommand `ipython notebook` is deprecated and will be removed in future versions. [TerminalIPythonApp] WARNING | You likely want to use `jupyter notebook` in the future Traceback (most recent call last): File "d:\users\sharanb\appdata\local\programs\python\python37-32\lib\runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "d:\users\sharanb\appdata\l -- https://mail.python.org/mailman/listinfo/python-list
Re: PIP install
On Saturday, 12 May 2018 08:32:04 UTC+5:30, MRAB wrote: > On 2018-05-12 03:47, Sharan Basappa wrote: > > I have installed Python recently. Do I need to install PIP separately or > > this would be part of default installation. When I run pip install <>, > > windows complains that no such command exists > > > That means that pip isn't on the Windows search path. > > It might be better to call the pip module via the py launcher: > > py -m pip install <> thanks. i uninstalled and re-ininstalled with option to add python to my path. It works now. I could have simply added the python path to env variable but i did not realize the issue then. Thanks a lot -- https://mail.python.org/mailman/listinfo/python-list
Numpy array
This is regarding numpy array. I am a bit confused how parts of the array are being accessed in the example below. 1 import scipy as sp 2 data = sp.genfromtxt("web_traffic.tsv", delimiter="\t") 3 print(data[:10]) 4 x = data[:,0] 5 y = data[:,1] Apparently, line 3 prints the first 10 entries in the array line 4 & 5 is to extract all rows but only 1st and second columns alone for x and y respectively. I am confused as to how data[:10] gives the first 10 rows while data[:,0] gives all rows -- https://mail.python.org/mailman/listinfo/python-list
syntax question
Can anyone please tell me what the following line in a python program does: line = lambda x: x + 3 I have pasted the entire code below for reference: from scipy.optimize import fsolve import numpy as np line = lambda x: x + 3 solution = fsolve(line, -2) print solution -- https://mail.python.org/mailman/listinfo/python-list
user defined modules
Is there a specific location where user defined modules need to be kept? If not, do we need to specify search location so that Python interpreter can find it? Also, when does Python interpreter compile the module code? When it is imported? -- https://mail.python.org/mailman/listinfo/python-list
mutable sequences
The term mutable appears quite often in Python. Can anyone explain what is meant by mutable and immutable sequences. For example, Python lists are mutable. BTW, is the below explanation correct (it is taken from a book I am reading) Python lists are mutable sequences. They are very similar to tuples, but they don't have the restrictions due to immutability. It says lists are mutable and then says they are immutable??? -- https://mail.python.org/mailman/listinfo/python-list
pattern
Can anyone explain to me the purpose of "pattern" in the line below: documents.append((w, pattern['class'])) documents is declared as a list as follows: documents.append((w, pattern['class'])) -- https://mail.python.org/mailman/listinfo/python-list
Re: mutable sequences
Thanks, All, for the responses. -- https://mail.python.org/mailman/listinfo/python-list
Re: pattern
> >Can anyone explain to me the purpose of "pattern" in the line below: > > > >documents.append((w, pattern['class'])) > > > >documents is declared as a list as follows: > >documents.append((w, pattern['class'])) > > Not without a lot more context. Where did you find this code? > > Cheers, I am sorry that partial info was not sufficient. I am actually trying to implement my first text classification code and I am referring to the below URL for that: https://machinelearnings.co/text-classification-using-neural-networks-f5cd7b8765c6 I hope this helps. -- https://mail.python.org/mailman/listinfo/python-list
Re: pattern
Dear Cameron, This is so kind of you. Thanks for spending time to explain the code. It did help a lot. I did go back and brush up lists & dictionaries. At this point, I think, I need to go back and brush up Python from the start. So, I will do that first. On Friday, 15 June 2018 09:12:22 UTC+5:30, Cameron Simpson wrote: > On 14Jun2018 20:01, Sharan Basappa wrote: > >> >Can anyone explain to me the purpose of "pattern" in the line below: > >> > > >> >documents.append((w, pattern['class'])) > >> > > >> >documents is declared as a list as follows: > >> >documents.append((w, pattern['class'])) > >> > >> Not without a lot more context. Where did you find this code? > > > >I am sorry that partial info was not sufficient. > >I am actually trying to implement my first text classification code and I am > >referring to the below URL for that: > > > >https://machinelearnings.co/text-classification-using-neural-networks-f5cd7b8765c6 > > Ah, ok. It helps to include some cut/paste of the relevant code, though the > URL > is a big help. > > The wider context of the code you recite looks like this: > > words = [] > classes = [] > documents = [] > ignore_words = ['?'] > # loop through each sentence in our training data > for pattern in training_data: > # tokenize each word in the sentence > w = nltk.word_tokenize(pattern['sentence']) > # add to our words list > words.extend(w) > # add to documents in our corpus > documents.append((w, pattern['class'])) > > and the training_data is defined like this: > > training_data = [] > training_data.append({"class":"greeting", "sentence":"how are you?"}) > training_data.append({"class":"greeting", "sentence":"how is your day?"}) > ... lots more ... > > So training data is a list of dicts, each dict holding a "class" and > "sentence" > key. The "for pattern in training_data" loop iterates over each item of the > training_data. It calls nltk.word_tokenize on the 'sentence" part of the > training item, presumably getting a list of "word" strings. The documents > list > gets this tuple: > > (w, pattern['class']) > > added to it. > > In this way the documents list ends up with tuples of (words, > classification), > with the words coming from the sentence via nltk and the classification > coming > straight from the train item's "class" value. > > So at the end of the loop the documents array will look like: > > documents = [ > ( ['how', 'are', 'you'], 'greeting' ), > ( ['how', 'is', 'your', 'day', 'greeting' ), > ] > > and so forth. > > Cheers, > Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
syntax difference
Is there a difference between these prints. The first one looks a bit complex. So, why should it be used? my_age = 35 # not a lie print "my age %s." % my_age print "my age ", my_age Output: %run "D:/Projects/Initiatives/machine learning/programs/five.py" my age 35. my age 35 -- https://mail.python.org/mailman/listinfo/python-list
Re: syntax difference
On Sunday, 17 June 2018 07:25:57 UTC+5:30, Ben Bacarisse wrote: > Cameron Simpson writes: > > > ... In Python 3 we have "format strings", which let you write: > > > > name = "Sharon" > > age = 35 > > print(f"The person named {name|r} is {age} years old.") > > You meant {name!r} I think there. > > -- > Ben. thanks, everyone. I think I am now confused with format options in Python. I tried an example as below and both print proper value: age = 35 print "age is %s" % age print "age is %d" % age %run "D:/Projects/Initiatives/machine learning/programs/six.py" age is 35 age is 35 I other languages I know the format specifier should be same as the variable type. For example, in the above case, it has to be %d and not %s -- https://mail.python.org/mailman/listinfo/python-list
Re: syntax difference
On Sunday, 17 June 2018 11:00:50 UTC+5:30, Ben Finney wrote: > Sharan Basappa writes: > > > I think I am now confused with format options in Python. > > You should refer to the documentation for string formatting > https://docs.python.org/3/library/stdtypes.html#str.format> > https://docs.python.org/3/library/string.html#formatstrings> > > (or, if you want to continue with the older less-flexible style, > ) > > > I tried an example as below and both print proper value: > > > > age = 35 > > > > print "age is %s" % age > > The ‘s’ format specifier says “Ask the object for its text > representation, and put that text here”. > > Every object has a text representation, so ‘s’ works with any object. > > > print "age is %d" % age > > The ‘d’ format specifier says “Format the integer as a decimal text > representation, and put that text here”. > > Only objects that are integers (or that implement the format-as-decimal > API) will work with ‘d’. > > > I other languages I know the format specifier should be same as the > > variable type. For example, in the above case, it has to be %d and not > > %s > > Because you are explicitly specifying which formatting to use, there's > no ambiguity. With an object that is an integer, either of the above > makes sense in different use cases. > > -- > \“A right is not what someone gives you; it's what no one can | > `\ take from you.” —Ramsey Clark | > _o__) | > Ben Finney Thanks a lot. -- https://mail.python.org/mailman/listinfo/python-list
Re: syntax difference
On Sunday, 17 June 2018 11:42:03 UTC+5:30, Jim Lee wrote: > On 06/16/2018 10:13 PM, Sharan Basappa wrote: > > I think I am now confused with format options in Python. > > I tried an example as below and both print proper value: > > > > age = 35 > > > > print "age is %s" % age > > print "age is %d" % age > > > > %run "D:/Projects/Initiatives/machine learning/programs/six.py" > > age is 35 > > age is 35 > > > > I other languages I know the format specifier should be same as the > > variable type. For example, in the above case, it has to be %d and not %s > > Python is not like other languages. For one thing, there are no > "variables" in Python (in the way you think of them). There are only > objects and names. Names can be thought of like void pointers in C, for > example. They don't have a "type" themselves - they only refer to > objects, and the type of object they refer to can change at will. Also, > there are no integers in Python. Scalar values are actually objects. > The number 35 is not an integer, it is an object that has an integer > type. If you do a "dir(35)" in Python, you'll see that the object "35" > has more than 70 methods associated with it. I'll stop there to avoid > information overload, but these are some of the key things a Python > newcomer needs to wrap their head around > > -Jim Jim, thanks a lot. Somehow, not one book I referred to brought out very clearly that everything in Python is an object including basic data types. Probably, they did and not so explicitly. -- https://mail.python.org/mailman/listinfo/python-list
curve_fit in scipy
Hi All, I am working out an exercise on curve_fit function available scipy package. While I understand in general about curve_fit, I am unable to understand the following: params, params_covariance = optimize.curve_fit(test_func, x_data, y_data, p0=[2, 2]) Firstly, I don't understand why test_func is passed as an argument to cur_fit Secondly, I don't understand how curve_fit knows the number of arguments that test_func takes. Full code is available below for reference: import numpy as np # Seed the random number generator for reproducibility np.random.seed(0) x_data = np.linspace(-5, 5, num=50) y_data = 2.9 * np.sin(1.5 * x_data) + np.random.normal(size=50) # And plot it import matplotlib.pyplot as plt plt.figure(figsize=(6, 4)) plt.scatter(x_data, y_data) from scipy import optimize def test_func(x, a, b): return a * np.sin(b * x) params, params_covariance = optimize.curve_fit(test_func, x_data, y_data, p0=[2, 2]) print(params) plt.figure(figsize=(6, 4)) plt.scatter(x_data, y_data, label='Data') plt.plot(x_data, test_func(x_data, params[0], params[1]), label='Fitted function') plt.legend(loc='best') plt.show() -- https://mail.python.org/mailman/listinfo/python-list
Re: curve_fit in scipy
> > Secondly, I don't understand how curve_fit knows the number of arguments > > that test_func takes. > > Part of the dynamic nature of Python is that a function carries with it > the number of parameters (as just one among many such properties). We > call it "introspection" when we examine such properties of objects. The > curve_fit function usees such an introspection to find that > test_function has two parameters (a and b) defining the family of curves. Thanks a lot. The above feature where a given function is able to inspect another function is really cool. This gives me an idea to go a read it further. -- https://mail.python.org/mailman/listinfo/python-list
nltk related issue
Folks, I am trying to run a simple example associated with nltk. I get some error and I don't know what the issue is. I need some guidance please. I am using python canopy distribution The following is the code: inputstring = ' This is an example sent. The sentence splitter will split on sent markers. Ohh really !!' from nltk.tokenize import sent_tokenize sentences = sent_tokenize(inputstring) print sentences The following is the error report: LookupErrorTraceback (most recent call last) D:\Projects\Initiatives\machine learning\programs\nltk_1.py in () 2 from nltk.tokenize import sent_tokenize 3 > 4 sentences = sent_tokenize(inputstring) 5 6 print sentences D:\Users\sharanb\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\nltk\tokenize\__init__.pyc in sent_tokenize(text, language) 94 :param language: the model name in the Punkt corpus 95 """ ---> 96 tokenizer = load('tokenizers/punkt/{0}.pickle'.format(language)) 97 return tokenizer.tokenize(text) 98 D:\Users\sharanb\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\nltk\data.pyc in load(resource_url, format, cache, verbose, logic_parser, fstruct_reader, encoding) 812 813 # Load the resource. --> 814 opened_resource = _open(resource_url) 815 816 if format == 'raw': D:\Users\sharanb\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\nltk\data.pyc in _open(resource_url) 930 931 if protocol is None or protocol.lower() == 'nltk': --> 932 return find(path_, path + ['']).open() 933 elif protocol.lower() == 'file': 934 # urllib might not use mode='rb', so handle this one ourselves: D:\Users\sharanb\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\nltk\data.pyc in find(resource_name, paths) 651 sep = '*' * 70 652 resource_not_found = '\n%s\n%s\n%s' % (sep, msg, sep) --> 653 raise LookupError(resource_not_found) 654 655 LookupError: ** Resource u'tokenizers/punkt/english.pickle' not found. Please use the NLTK Downloader to obtain the resource: >>> nltk.download() Searched in: - 'D:\\Users\\sharanb/nltk_data' - 'C:\\nltk_data' - 'D:\\nltk_data' - 'E:\\nltk_data' - 'D:\\Users\\sharanb\\AppData\\Local\\Enthought\\Canopy\\edm\\envs\\User\\nltk_data' - 'D:\\Users\\sharanb\\AppData\\Local\\Enthought\\Canopy\\edm\\envs\\User\\lib\\nltk_data' - 'D:\\Users\\sharanb\\AppData\\Roaming\\nltk_data' - u'' ** -- https://mail.python.org/mailman/listinfo/python-list
sigmoid function and derivative
Folks, I know this is not a machine learning forum but I wanted to see if anyone can explain this to me. In artificial neural network, I can understand why sigmoid is used but I see that derivative of sigmoid output function is used. I am not able to understand why. For example: # convert output of sigmoid function to its derivative def sigmoid_output_to_derivative(output): return output*(1-output) -- https://mail.python.org/mailman/listinfo/python-list
error in os.chdir
0 down vote favorite I need to change directory to my local working directory in windows and then open a file for processing. Its just a 3 lines code, as below: import csv import os os.chdir('D:\Projects\Initiatives\machine learning\programs\assertion') The error is as follows: WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: 'D:\Projects\Initiatives\machine learning\programs\x07ssertion' Notice x07 character that has replaced character x07. I have a similar code but that goes through fine: import csv import os os.chdir('D:\Projects\Initiatives\machine learning\programs') with open('example.csv') as csvfile: readCSV = csv.reader(csvfile, delimiter=',') The only difference is directory assertion in the problematic code. I have tried single quoting, double quoting etc. for the chdir directive but nothing helps. I have also tried escaping as \assertion but that is not the issue -- https://mail.python.org/mailman/listinfo/python-list
Re: error in os.chdir
On Saturday, 30 June 2018 16:51:53 UTC+5:30, Karsten Hilbert wrote: > On Sat, Jun 30, 2018 at 04:05:22AM -0700, Sharan Basappa wrote: > > > I need to change directory to my local working directory in windows and > > then open a file for processing. > > Its just a 3 lines code, as below: > > import csv > > import os > > os.chdir('D:\Projects\Initiatives\machine learning\programs\assertion') > > The error is as follows: > > WindowsError: [Error 123] The filename, directory name, or volume label > > syntax is incorrect: 'D:\Projects\Initiatives\machine > > learning\programs\x07ssertion' > > Notice x07 character that has replaced character x07. > > I have a similar code but that goes through fine: > > import csv > > import os > > os.chdir('D:\Projects\Initiatives\machine learning\programs') > > > > with open('example.csv') as csvfile: > > readCSV = csv.reader(csvfile, delimiter=',') > > The only difference is directory assertion in the problematic code. > > I have tried single quoting, double quoting etc. for the chdir directive > > but nothing helps. I have also tried escaping as \assertion but that is not > > the issue > > The quick fix: > > put an r in front of the directory string: r'...' > > Karsten > -- > GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B thanks. That works -- https://mail.python.org/mailman/listinfo/python-list
list initialize with ?
Can anyone explain to me what the ? does here: ignore_words = ['?'] -- https://mail.python.org/mailman/listinfo/python-list
Re: error in os.chdir
On Saturday, 30 June 2018 18:05:02 UTC+5:30, Steven D'Aprano wrote: > On Sat, 30 Jun 2018 04:47:44 -0700, Sharan Basappa wrote: > > >> The quick fix: > >> > >> put an r in front of the directory string: r'...' > > > > thanks. That works > > Please don't do that. It's the wrong solution -- all you are doing is > postponing failure. It will *seem* to work, until one day you will write > something like this: > > directory = r'D:\directory\' > > and you will get a mysterious failure. Chris gave you the right solution: > use forward slashes instead of backslashes for all paths. > > os.chdir('D:/Projects/Initiatives/machine learning/programs') > > > > > -- > Steven D'Aprano > "Ever since I learned about confirmation bias, I've been seeing > it everywhere." -- Jon Ronson alright. I will do that but still I don't have an answer why I got the error in the first place. -- https://mail.python.org/mailman/listinfo/python-list
Re: list initialize with ?
On Saturday, 30 June 2018 17:48:05 UTC+5:30, Steven D'Aprano wrote: > On Sat, 30 Jun 2018 04:50:10 -0700, Sharan Basappa wrote: > > > Can anyone explain to me what the ? does here: > > > > ignore_words = ['?'] > > Its a question mark inside a string, which is inside a list. You can put > anything you like in strings: > > 'a' > > 'abcde' > > even punctuation like '?'. Then you can put the string inside a list. > > What you do with it, I have no idea -- presumably its your code. > > > -- > Steven D'Aprano > "Ever since I learned about confirmation bias, I've been seeing > it everywhere." -- Jon Ronson oh, ok. I assumed that ? has some special meaning. I should be able to find out more. PS: Actually, its not my code. I am using an online code as reference for my research. -- https://mail.python.org/mailman/listinfo/python-list
is my interpreation correct
A code I am using as reference has the following line: from nltk.stem.lancaster import LancasterStemmer I am inferring the following based on above: 1) nltk is a package 2) nltk itself may have module because I see - nltk.word_tokenize(pattern['sentence']) in the code 3) nltk has a package named stem which has another package named Lancaster that has LancasterStemmer package Somehow, I get confused with what is the package, module and function in third party packages like above. -- https://mail.python.org/mailman/listinfo/python-list
Re: error in os.chdir
On Saturday, 30 June 2018 18:55:53 UTC+5:30, Karsten Hilbert wrote: > On Sat, Jun 30, 2018 at 05:46:59AM -0700, Sharan Basappa wrote: > > > > >> The quick fix: > > > >> > > > >> put an r in front of the directory string: r'...' > > > > > > Please don't do that. It's the wrong solution -- all you are doing is > > > postponing failure. It will *seem* to work, until one day you will write > > > something like this: > > > > > > directory = r'D:\directory\' > > > > > > and you will get a mysterious failure. Chris gave you the right solution: > > > use forward slashes instead of backslashes for all paths. > > > > alright. I will do that but still I don't have an answer why I got the > > error in the first place. > > For that you'll have to read up on strings and escaping. > > https://docs.python.org/2/tutorial/introduction.html#strings > > Karsten > -- > GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B sorry. I mean why my code worked in one case but did not in the other one. This worked - os.chdir('D:\Projects\Initiatives\machine learning\programs') This did not work - os.chdir('D:\Projects\Initiatives\machine learning\programs\assertion') only difference is, I added an additional directory in the problematic case. -- https://mail.python.org/mailman/listinfo/python-list
using trace module in enthought
I am using enthought for python. Trace module seems to be very useful for my work but somehow I am unable to make it work. When I use the following option, I get the following error: %run -m trace --trace "D:/Projects/Initiatives/machine learning/programs/debug_1.py" UsageError: option --trace not recognized ( allowed: "nidtN:b:pD:l:rs:T:em:G" ) %run -m trace --trace "D:/Projects/Initiatives/machine learning/programs/debug_1.py" --trace UsageError: option --trace not recognized ( allowed: "nidtN:b:pD:l:rs:T:em:G" ) %run -m trace "D:/Projects/Initiatives/machine learning/programs/debug_1.py" --trace D:\Users\sharanb\AppData\Local\Enthought\Canopy\edm\envs\User\lib\trace.py: must specify one of --trace, --count, --report, --listfuncs, or --trackcalls SystemExitTraceback (most recent call last) D:\Users\sharanb\AppData\Local\Enthought\Canopy\edm\envs\User\lib\runpy.pyc in run_module(mod_name, init_globals, run_name, alter_sys) 186 if alter_sys: 187 return _run_module_code(code, init_globals, run_name, --> 188 fname, loader, pkg_name) 189 else: 190 # Leave the sys module alone D:\Users\sharanb\AppData\Local\Enthought\Canopy\edm\envs\User\lib\runpy.pyc in _run_module_code(code, init_globals, mod_name, mod_fname, mod_loader, pkg_name) 80 mod_globals = temp_module.module.__dict__ 81 _run_code(code, mod_globals, init_globals, ---> 82 mod_name, mod_fname, mod_loader, pkg_name) 83 # Copy the globals of the temporary module, as they 84 # may be cleared when the temporary module goes away D:\Users\sharanb\AppData\Local\Enthought\Canopy\edm\envs\User\lib\runpy.pyc in _run_code(code, run_globals, init_globals, mod_name, mod_fname, mod_loader, pkg_name) 70__loader__ = mod_loader, 71__package__ = pkg_name) ---> 72 exec code in run_globals 73 return run_globals 74 D:\Users\sharanb\AppData\Local\Enthought\Canopy\edm\envs\User\lib\trace.py in () 817 818 if __name__=='__main__': --> 819 main() D:\Users\sharanb\AppData\Local\Enthought\Canopy\edm\envs\User\lib\trace.py in main(argv) 770 771 if not (count or trace or report or listfuncs or countcallers): --> 772 _err_exit("must specify one of --trace, --count, --report, " 773 "--listfuncs, or --trackcalls") 774 D:\Users\sharanb\AppData\Local\Enthought\Canopy\edm\envs\User\lib\trace.py in _err_exit(msg) 655 def _err_exit(msg): 656 sys.stderr.write("%s: %s\n" % (sys.argv[0], msg)) --> 657 sys.exit(1) 658 659 def main(argv=None): SystemExit: 1 I don't know where I am going wrong. -- https://mail.python.org/mailman/listinfo/python-list
Logger option
Can anyone tell me if there are some good logger modules in Python. I have tried both logging and trace in Canopy and both are not working. Any help is appreciated. -- https://mail.python.org/mailman/listinfo/python-list
$s and %d in python
Is there any difference in %d and %s below. I get the same result: my_name = 'Zed A. Shaw' my_age = 35 # not a lie my_height = 74 # inches print "Let's talk about %s." % my_name print "He's %d inches tall." % my_height print "He's %s inches tall." % my_height Let's talk about Zed A. Shaw. He's 74 inches tall. He's 74 inches tall. -- https://mail.python.org/mailman/listinfo/python-list
logging module
Folks, I am trying to use logging module and somehow I cannot make it work. A simple code that I am trying is below. The commented code line 5,6 are other options I have tried but don't work #importing module import logging #Create and configure logger #logging.basicConfig(filename="D:/Projects/Initiatives/machine learning/programs/newfile.log", #logging.basicConfig(filename="newfile.log", logging.basicConfig(format='%(asctime)s %(message)s') #Creating an object logger=logging.getLogger() #Setting the threshold of logger to DEBUG logger.setLevel(logging.DEBUG) #Test messages logger.debug("Harmless debug Message") logger.info("Just an information") logger.warning("Its a Warning") logger.error("Did you try to divide by zero") logger.critical("Internet is down") PS: I am running this under Enthought Canopy The following is the output %run "D:/Projects/Initiatives/machine learning/programs/debug_4.py" -- https://mail.python.org/mailman/listinfo/python-list
testing code
Hi All, I am new to Python though not new to programming. I have implemented my first program in python that uses ML to do some classification task. The whole code is in a single file currently. It contains executable code as well as functions. At the end of the program, I have series of calls that is used to test my code. Now, I would like to re-structure this to separate test code from the program. As I have not done this in Python, I am a bit lost. Please let me know if the following understanding of mine is correct. I need to put the program code in a separate file and organize every executable code in some form of function. If any code exists outside of function then it is not executable by importing. Import this in my test program (file/module) and then initiate calls present in the program. If there is some simple example, it would help a lot. -- https://mail.python.org/mailman/listinfo/python-list
Re: testing code
On Friday, 6 July 2018 09:32:08 UTC+5:30, Cameron Simpson wrote: > On 05Jul2018 19:56, Sharan Basappa wrote: > >I have implemented my first program in python that uses ML to do some > >classification task. The whole code is in a single file currently. > >It contains executable code as well as functions. > > I presume when you write "executable code" you mean some kind of "main > program" > that just runs when you run the .py file? > > >At the end of the program, I have series of calls that is used to test my > >code. > >Now, I would like to re-structure this to separate test code from the > >program. > >As I have not done this in Python, I am a bit lost. > > > >Please let me know if the following understanding of mine is correct. > >I need to put the program code in a separate file and organize every > >executable code in some form of function. If any code exists outside of > >function then it is not executable by importing. > > This is not quite right. Because Python is a dynamic language, importing a > file > actually runs it. That is how the functions etc get defined. > > So what you need to do is arrange that your "series of calls that is used to > test my code" live in their own function, and that that function only gets > run > if the file is directly run. > > Fortunately, this is a very common, almost universal, requirement and there > is > a standard idom for arranging it. > > Support you have your code in the file "foo.py" (because I need a concrete > filename for the example). It might look like this at present: > > def func1(...): > > def func2(...): > > x = func1(...) > y = func2(...) > print(x + y) > > i.e. some function definitions and then you testing code. > > Now, you can write another file "foo_tests.py" which starts like this: > > import foo > ... run some tests of foo.func1, foo.func2 etc ... > > The issue is that as written, foo.py will run your test calls during the > import. Restructure foo.py like this: > > def main(): > x = func1(...) > y = func2(...) > print(x + y) > > def func1(...): > > def func2(...): > > if __name__ == '__main__': > main() > > This is very standard. When you run a python file directly the built in > __name__ variable contains the string "__main__". This tells you that you're > running it as a "main program" i.e. directly. > > If you import the file instead, as from your planned testing file, the > __name__ > variable will contain the string "foo" because that is the name of the module. > > So that "main" function and the "if" at the bottom is standard Python > boilerplate code for what you're trying to do. > > >Import this in my test program (file/module) and then initiate calls > >present > >in the program. > >If there is some simple example, it would help a lot. > > Now you can do this part. > > Cheers, > Cameron Simpson Cameron. thanks. this is much more easier than I thought. -- https://mail.python.org/mailman/listinfo/python-list
Re: testing code
On Friday, 6 July 2018 09:22:31 UTC+5:30, Chris Angelico wrote: > On Fri, Jul 6, 2018 at 12:56 PM, Sharan Basappa > wrote: > > Please let me know if the following understanding of mine is correct. > > I need to put the program code in a separate file and organize every > > executable code in some form of function. If any code exists outside of > > function then it is not executable by importing. > > > > Kinda. It's actually the other way around: if any code exists outside > of functions, it will be executed immediately when you import. So > you're correct in that it would be hard (maybe impossible) to > unit-test that; and yes, the normal way to do it is to put all your > important code into functions. > > ChrisA thanks a lot -- https://mail.python.org/mailman/listinfo/python-list
Re: testing code
On Friday, 6 July 2018 09:22:31 UTC+5:30, Chris Angelico wrote: > On Fri, Jul 6, 2018 at 12:56 PM, Sharan Basappa > wrote: > > Please let me know if the following understanding of mine is correct. > > I need to put the program code in a separate file and organize every > > executable code in some form of function. If any code exists outside of > > function then it is not executable by importing. > > > > Kinda. It's actually the other way around: if any code exists outside > of functions, it will be executed immediately when you import. So > you're correct in that it would be hard (maybe impossible) to > unit-test that; and yes, the normal way to do it is to put all your > important code into functions. > > ChrisA Chris, Things do work as per expected with one exception. You mentioned that as soon as a file is imported, it executes immediately. Please see the example below: file: test_2.py x = 10 y = 20 c = x-y print c def func1(): return x+y test_2_test.py x = 10 y = 20 c = x-y print c def func1(): return x+y this is the output: %run "D:/Projects/Initiatives/machine learning/programs/test_2_test.py" 30 As you can see, print c in test_2 file was not executed upon import as there is no corresponding output -- https://mail.python.org/mailman/listinfo/python-list
Re: testing code
On Saturday, 7 July 2018 18:22:23 UTC+5:30, Chris Angelico wrote: > On Sat, Jul 7, 2018 at 10:02 PM, Sharan Basappa > wrote: > > On Friday, 6 July 2018 09:22:31 UTC+5:30, Chris Angelico wrote: > >> On Fri, Jul 6, 2018 at 12:56 PM, Sharan Basappa > >> wrote: > >> > Please let me know if the following understanding of mine is correct. > >> > I need to put the program code in a separate file and organize every > >> > executable code in some form of function. If any code exists outside of > >> > function then it is not executable by importing. > >> > > >> > >> Kinda. It's actually the other way around: if any code exists outside > >> of functions, it will be executed immediately when you import. So > >> you're correct in that it would be hard (maybe impossible) to > >> unit-test that; and yes, the normal way to do it is to put all your > >> important code into functions. > >> > >> ChrisA > > > > Chris, > > > > Things do work as per expected with one exception. > > You mentioned that as soon as a file is imported, it executes immediately. > > > > Please see the example below: > > > > file: test_2.py > > > > x = 10 > > y = 20 > > > > c = x-y > > > > print c > > > > def func1(): > > return x+y > > > > test_2_test.py > > > > x = 10 > > y = 20 > > > > c = x-y > > > > print c > > > > def func1(): > > return x+y > > > > this is the output: > > %run "D:/Projects/Initiatives/machine learning/programs/test_2_test.py" > > 30 > > > > As you can see, print c in test_2 file was not executed upon import as > > there is no corresponding output > > I don't think you import your other module anywhere. > > ChrisA sorry. there was a copy paste error when i posted. I pasted test_2.py for both the files: here are the files again. The issue remains. test_2.py: x = 10 y = 20 c = x-y print c def func1(): return x+y test_2_test.py: import test_2 x = test_2.func1() print x output: %run "D:/Projects/Initiatives/machine learning/programs/test_2_test.py" 30 -- https://mail.python.org/mailman/listinfo/python-list
Re: testing code
On Sunday, 8 July 2018 11:52:39 UTC+5:30, Jim Lee wrote: > On 07/07/18 21:21, Sharan Basappa wrote: > > > > sorry. there was a copy paste error when i posted. I pasted test_2.py for > > both the files: > > > > here are the files again. The issue remains. > > [...] > > > > output: > > %run "D:/Projects/Initiatives/machine learning/programs/test_2_test.py" > > 30 > > [11:24 PM jlee@kerndev ~] $cat test_2.py > x = 10 > y = 20 > > c = x-y > > print c > > def func1(): > return x+y > > [11:24 PM jlee@kerndev ~] $cat test_2_test.py > import test_2 > > x = test_2.func1() > print x > [11:24 PM jlee@kerndev ~] $python test_2_test.py > -10 > 30 > [11:24 PM jlee@kerndev ~] $ > > > As you can see, the code from the import is indeed executed by the > python interpreter. > > I'm not familiar with the "%run" prefix in your command - some sort of > windows-ism? > > -Jim i think I figured out the issue. I am running this in Canopy. When I use Canopy Gui to run this, it appears that it loads the imported file only once. So, when I re-start canopy then everything goes fine the first time. When I use command prompt then it works all well. PS: run is nothing but the front end command that Canopy uses to run Python in the background. -- https://mail.python.org/mailman/listinfo/python-list
Re: testing code
On Sunday, 8 July 2018 12:42:07 UTC+5:30, Christian Gollwitzer wrote: > Am 08.07.18 um 06:21 schrieb Sharan Basappa: > > sorry. there was a copy paste error when i posted. I pasted test_2.py for > > both the files: > > > > here are the files again. The issue remains. > > > output: > > %run "D:/Projects/Initiatives/machine learning/programs/test_2_test.py" > > 30 > > > > Jim spotted it... '%run' is in IPython, right? "import" statements are > executed only once for a given module (that's the purpose) - you have > probably modified your imported file, and not restarted the interpreter. > The second time you then import it, nothing happens. > > If you restart your interpreter, you should see both lines printed. > > Christian yes, exactly. this is the issue although execution is in Canopy and not in iPython -- https://mail.python.org/mailman/listinfo/python-list
functions vs methods
Is there a difference between functions and methods in Python. For example, this is the text from tutorialpoint on Python: Python includes the following list functions - cmp, len etc. Python includes following list methods - append, count A related question. Refer to the following lines: 1) len(list) 2) list.append() The first line passes list to a len function while the second line calls append method in list. So, how to interpret this? In the first case, len is a function that python provides to which list can be passed and in the second case, append is a method within list class? If my interpretation is correct, why not make len also as a part of list class itself? Thanks in advance -- https://mail.python.org/mailman/listinfo/python-list
Re: functions vs methods
Thanks a lot. On Sunday, 22 July 2018 04:02:23 UTC+5:30, Rick Johnson wrote: > On Saturday, July 21, 2018 at 2:06:21 PM UTC-5, Sharan Basappa wrote: > > Is there a difference between functions and methods in Python. > > Generally speaking, functions and methods are basically two > words describing the exact same thing: "small packages of > reusable code". > > An easy way to think about it for the > uninitiated is that, functions came first, and methods are a > special kind of function which is owned by what we > programmers call "Objects". > > You can learn more about methods > and objects (and a wholelotta other stuff too) by reading > up on "Object Oriented Programming" (aka: OOP). There are > tons of tutorials and examples on the web. Ask Google. > > "What???" o_O > > "Can you repeat that?..." > > [listening carefully] > > "Her???" > > "No, not _her_!" > > "To your left a bit..." > > "..." > > "No, not _that_ left..." (/ロ°)/ > > "Your _other_ left!" \(°ロ\) > > "..." > > "Oh... for guido's sake, man..." (-‸ლ) > > "THE ONE WITH THE _PURPLE_ HAIR!!!" (屮゚Д゚)屮 > > "..." > > "BINGO!!!" :-) > > "Now, ask her..." > > > > [...] > > > > A related question. > > > > Refer to the following lines: > > 1) len(list) > > 2) list.append() > > > > The first line passes list to a len function while the > > second line calls append method in list. So, how to > > interpret this? In the first case, len is a function that > > python provides to which list can be passed and in the > > second case, append is a method within list class? > > > First, get yer terminology correct... > > list _object_. > > Not _class_. > > _Object_!!! > > "Class" refers to the syntactical representation of an OOP > _object_ -- aka: meaning that "class" is all the code > written to _define_ an object -- while _object_, refers to > the "live thing" that actually does stuff. For instance: in > the case of a python list _object_ (1) the _object_ acts as > a container for other "live objects", and (2) the _object_ > exposes an interface so we can call methods that perform > certain predefined tasks. > > > If my interpretation is correct, why not make len also as a > > part of list class itself? > > Simple Answer: because Python is not a _pure_ OOP language. > > Yes, python allows us to write code in the OOP paradigm (and > many others), but unlike languages such as, say, Ruby and > Java (which are pure OOP languages), python decided to go > down a different route. And there are good reasons to _not_ > be a pure OOP language, and of course, there are certain > "unintuitive seeming effects" because of that design choice. > And you've stumbled upon one here. > > Congrats! > > Once you familiarize yourself with Python's multi-paradigm > design, and you study more about OOP and experience the > onerous of purely OOP languages, you will understand why > python's designers decided to go this route. > > Until then, i'd worry about something more important. Like > actually mastering the language. And once you've mastered > the language, then you earn the right to sit around here all > day and complain about everything. Like any good Pythinistas > would do. -- https://mail.python.org/mailman/listinfo/python-list
print & string formatting
Folks, I get a lot confused while using print functions in Python. For example, I get the same results for the following code: str = "one two three" print str print "%s" %(str) So, what is the need to use the second method which I see being used in many programs I am referring to -- https://mail.python.org/mailman/listinfo/python-list
Re: functions vs methods
On Sunday, 22 July 2018 13:32:16 UTC+5:30, Ben Finney wrote: > Sharan Basappa writes: > > > Is there a difference between functions and methods in Python. > > Python's documentation includes a useful Glossary. See the terms > https://docs.python.org/3/glossary.html#term-method> > https://docs.python.org/3/glossary.html#term-function>. > > Every method is a function; but there are functions that are not > methods. > > What distinguishes a method is that it is associated with a specific > class. A method is always a method *of* some class or object. > > > For example, this is the text from tutorialpoint on Python: > > Python includes the following list functions - cmp, len etc. > > The functions ‘cmp’, ‘len’, are not associated with any particular > class. They can be called without being bound to any object. > > > Python includes following list methods - append, count > > That means the functions it is referring to are each methods of ‘list’. > Any instance of ‘list’ has methods ‘append’ and ‘count’, bound to that > instance. > > > In the first case, len is a function that python provides to which > > list can be passed and in the second case, append is a method within > > list class? > > Yes, that's correct. > > > If my interpretation is correct, why not make len also as a part of > > list class itself? > > Because ‘len’ works with *any* sequence, not only lists. To implement it > as a method of each sequence type, it would have to be implemented on > each type separately, which is a design that is needlessly more complex. > > This is common in Python: it uses so-called “duck typing”, where the way > an object behaves is more important than its type. Because “what is the > length of this object” is a question valid for a broad variety of types, > the design decision was made to allow it to accept any type for which > that query makes sense. > > https://docs.python.org/3/glossary.html#term-duck-typing> > > Your particular question is itself a FAQ > https://docs.python.org/3/faq/design.html#why-does-python-use-methods-for-some-functionality-e-g-list-index-but-functions-for-other-e-g-len-list>. > > -- > \ “All progress has resulted from people who took unpopular | > `\ positions.” —Adlai Stevenson | > _o__) | > Ben Finney Thanks, Ben -- https://mail.python.org/mailman/listinfo/python-list
Re: print & string formatting
On Sunday, 22 July 2018 10:24:55 UTC+5:30, Cameron Simpson wrote: > On 21Jul2018 21:33, Sharan Basappa wrote: > >I get a lot confused while using print functions in Python. > > > >For example, I get the same results for the following code: > > > >str = "one two three" > > Pleasetry not to name variables after builtin classes ("str" is the name of > Python's string class). > > >print str > >print "%s" %(str) > > > >So, what is the need to use the second method which I see being used in many > >programs I am referring to > > For a bare "%s", one would normally just write str(s) where "s" is your > string > variable. > > The % formatting is usually for (a) more complex messages or (b) separating > the > message format from the values. Example: > > print("The time is %s and the place is %s." % (when, where)) > > Instead of the much harder to read and maintain: > > print("The time is", str(when), "and the place is", str(where), ".") > > Cheers, > Cameron Simpson Thanks. I thin I understand. -- https://mail.python.org/mailman/listinfo/python-list
list of lists
I am using a third party module that is returning list of lists. I am using the example below to illustrate. 1 results = [['1', 0.99921393753233001]] 2 k = results[0] 3 print k[0] 4 print k[1] Assume the line 1 is what is returned. I am assigning that to another list (k on line 2) and then accessing the 1st and 2nd element in the list (line 3 and 4). How can I access elements of 1 and 0.99 without assigning it to another list? -- https://mail.python.org/mailman/listinfo/python-list
Re: list of lists
Thanks. This works in my example. Can you tell me how this works? > You can simply unpack the inner list: > > a, b = results[0] > > > Iwo Herka > > ‐‐‐ Original Message ‐‐‐ > > On 22 July 2018 11:47 AM, Sharan Basappa wrote: > > > > > > > I am using a third party module that is returning list of lists. > > > > I am using the example below to illustrate. > > > > 1 results = [['1', 0.99921393753233001]] > > > > 2 k = results[0] > > > > 3 print k[0] > > > > 4 print k[1] > > > > Assume the line 1 is what is returned. > > > > I am assigning that to another list (k on line 2) and then accessing the > > 1st and 2nd element in the list (line 3 and 4). > > > > How can I access elements of 1 and 0.99 without assigning it to another > > list? > > > > > > -- > > > > https://mail.python.org/mailman/listinfo/python-list Thanks -- https://mail.python.org/mailman/listinfo/python-list
coding style - where to declare variables
In other programming languages (e.g. C, C++), as a good practice, variables are declared right at the start of the program, irrespective of where it is normally used. What is the practice in Python? I see that, most of the code, declare variables where it is used and not at the start of the program. -- https://mail.python.org/mailman/listinfo/python-list
Re: list of lists
On Sunday, 22 July 2018 18:15:23 UTC+5:30, Frank Millman wrote: > "Sharan Basappa" wrote in message > news:8e261f75-03f7-4f80-a516-8318dd138...@googlegroups.com... > > > > I am using a third party module that is returning list of lists. > > I am using the example below to illustrate. > > > > 1 results = [['1', 0.99921393753233001]] > > 2 k = results[0] > > 3 print k[0] > > 4 print k[1] > > > > Assume the line 1 is what is returned. > > I am assigning that to another list (k on line 2) and then accessing the > > 1st and 2nd element in the list (line 3 and 4). > > > > How can I access elements of 1 and 0.99 without assigning it to another > > list? > > > > As you say, results is a list of lists. > > results[0] returns the first inner list. There could potentially be more > than one, in which case results[1] would return the second one. Use > len(results) to find out how many inner lists there are. > > results[0][0] returns the first element of the first inner list. > > results[0][1] returns the second element of the first inner list. > > HTH > > Frank Millman Thanks. I initially thought about this but did not know if this is legal syntax. The following is the updated code taking into account what you suggested and the previous posted. results = [['1', 0.99921393753233001]] k = results[0] print k[0] print k[1] a,b = results[0] print a print b x = results[0][0] y = results[0][1] print x print y -- https://mail.python.org/mailman/listinfo/python-list
Re: list of lists
On Sunday, 22 July 2018 18:34:41 UTC+5:30, Iwo Herka wrote: > > Can you tell me how this works? > > "results[0]" returns a list with two elements. Let's call it "pair" > > pair = results[0] > # ['1', 0.99921393753233001] > > Now, we can use regular sequence unpacking to retrieve first and second > argument: > > a, b = pair > > which is equivalent to this: > > a = pair[0] > b = pair[1] > > If you're not sure how many items you have in a list, you can use an asterisk > operator: > > li = [1, 2, 3, 4] > a, b, *c = li > > which is equivalent to: > >a = li[0] >b = li[1] >c = li[2:] > > > Iwo Herka > https://github.com/IwoHerka > > ‐‐‐ Original Message ‐‐‐ > > On 22 July 2018 12:40 PM, Sharan Basappa wrote: > > > > > > > Thanks. This works in my example. Can you tell me how this works? > > > > > You can simply unpack the inner list: > > > > > > a, b = results[0] > > > > > > > > > Iwo Herka > > > > > > ‐‐‐ Original Message ‐‐‐ > > > > > > On 22 July 2018 11:47 AM, Sharan Basappa sharan.basa...@gmail.com wrote: > > > > > > > I am using a third party module that is returning list of lists. > > > > > > > > I am using the example below to illustrate. > > > > > > > > 1 results = [['1', 0.99921393753233001]] > > > > > > > > 2 k = results[0] > > > > > > > > 3 print k[0] > > > > > > > > 4 print k[1] > > > > > > > > Assume the line 1 is what is returned. > > > > > > > > I am assigning that to another list (k on line 2) and then accessing > > > > the 1st and 2nd element in the list (line 3 and 4). > > > > > > > > How can I access elements of 1 and 0.99 without assigning it to another > > > > list? > > > > > > > > https://mail.python.org/mailman/listinfo/python-list > > > > Thanks > > > > > > -- > > > > https://mail.python.org/mailman/listinfo/python-list Thanks a lot. I have seen this syntax earlier but did not understand this fully. Thanks a lot. -- https://mail.python.org/mailman/listinfo/python-list
Re: list of lists
On Sunday, 22 July 2018 21:07:17 UTC+5:30, Thomas Jollans wrote: > On 22/07/18 14:53, Sharan Basappa wrote: > > Thanks. I initially thought about this but did not know if this is legal > > syntax. > > In this kind of situation – you think you know how to do something but > you're not quite sure if it'll work as intended – just try it! Start up > an interactive interpreter, or write a test script (as you appear to > have done). If it doesn't work, that's fine. No harm done. > > Also, please use Python 3. Agree. That is one advantage of interpreted language. For quick trial and error, not much overhead and we can print lot of variables once script is run. -- https://mail.python.org/mailman/listinfo/python-list
machine learning forums
I am quite new to Python. I am learning Python as I am interested in machine learning. The issue is, I have not found any ML forum where novices like me can get help. I have tried reddit and each of my posts have gone unanswered. Looks like reddit forum prefers either abstract topics on ML or very complex issues for discussions. I have tried stackoverflow also but there only programming issues are entertained not to mention people trying to outwit each other at the expense of users like me. I would like to know if there are any ML forums where input can be provided for new users like me. -- https://mail.python.org/mailman/listinfo/python-list
Re: machine learning forums
> > I am quite new to Python. I am learning Python as I am interested in > > machine learning. The issue is, I have not found any ML forum where > > novices like me can get help. I have tried reddit and each of my posts > > have gone unanswered. > > Which subreddits have you posted to? its called machine learning > > Looks like reddit forum prefers either abstract > > topics on ML or very complex issues for discussions. > > > > I have tried stackoverflow also but there only programming issues are > > entertained > > I believe Stackoverflow has a dedicated machine-learning site, "Cross > Validated": > > https://meta.stackexchange.com/questions/130524/which-stack-exchange- > website-for-machine-learning-and-computational-algorithms > > https://meta.stackexchange.com/questions/227757/where-to-ask-basic- > questions-about-machine-learning Thanks. This looks like a very good sight. I hope folks are friendlier than stackoverflow. -- https://mail.python.org/mailman/listinfo/python-list
Re: machine learning forums
On Monday, 6 August 2018 09:21:03 UTC+5:30, downtime wrote: > You might also try courses on Udemy and Udacity. I know Udemy is always > having sales on courses for like $10.99. They have beginner/novice > courses for all kinds of topics and in my experience, there are some > pretty good ones. Actually, I have already completed machine learning course by Andrew NG. And then I spent close to 3 months learning Python as I quickly realized than without Python, it is nearly impossible to do any coding in ML area. But the issue is, from time to time, I do get doubts. For Python, I have this forum but for ML, not much options. Now I know about cross validated though. -- https://mail.python.org/mailman/listinfo/python-list
plot not showing up
What is wrong with the following code. Python does not show the plot. from sklearn.datasets import load_digits from sklearn.cluster import KMeans import matplotlib.pyplot as plt digits = load_digits() digits.data.shape kmeans = KMeans(n_clusters=10,random_state=0) clusters = kmeans.fit_predict(digits.data) kmeans.cluster_centers_.shape fig, ax = plt.subplots(2,5,figsize=(8,3)) centers = kmeans.cluster_centers_.reshape(10,8,8) for axi, center in zip(ax.flat, centers): axi.set(xticks=[], yticks=[]) axi.imshow(center, interpolation='nearest', cmap=plt.cm.binary) -- https://mail.python.org/mailman/listinfo/python-list
Synax error in string assignment
I am not sure what the issue is with the 2nd file that assigns string to text variable. Here is the code: # -*- coding: utf-8 -*- text = “this’s a sent tokenize test. this is sent two. is this sent three? sent 4 is cool! Now it’s your turn.” from nltk.tokenize import sent_tokenize sent_tokenize_list = sent_tokenize(text) len(sent_tokenize_list) sent_tokenize_list Here is the error: text = “this’s a sent tokenize test. this is sent two. is this sent three? sent 4 is cool! Now it’s your turn.” ^ SyntaxError: invalid syntax BTW, the ^ operator pointing to the error is at text = and not for cool word. This is due to formatting issue in google group. -- https://mail.python.org/mailman/listinfo/python-list
Module not found
I am running a program that I got as reference from GitHub. I am running on windows OS. Here is a snippet of the code (initial few lines). #!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = 'Shilin He' import sys sys.path.insert(0, 'D:\Projects\Initiatives\machine learning\loglizer-master\loglizer-master\utils\evaluation') sys.path.insert(0, 'D:\Projects\Initiatives\machine learning\loglizer-master\loglizer-master\utils\evaluation') print sys.path import numpy as np import math from scipy.special import expit from numpy import linalg as LA from scipy.cluster.hierarchy import linkage from scipy.cluster.hierarchy import fcluster from scipy.spatial.distance import pdist import utils.evaluation as ev Here is the error: D:\Projects\Initiatives\machine learning\loglizer-master\loglizer-master\models\log_clustering.py in () 14 from scipy.cluster.hierarchy import fcluster 15 from scipy.spatial.distance import pdist ---> 16 import utils.evaluation as ev 17 18 ImportError: No module named utils.evaluation In the utils directory, I clearly can see evaluation file. Can someone give some directions? -- https://mail.python.org/mailman/listinfo/python-list
Re: Module not found
On Monday, 27 August 2018 22:45:47 UTC+5:30, Peter Otten wrote: > Sharan Basappa wrote: > > > I am running a program that I got as reference from GitHub. > > I am running on windows OS. > > > > Here is a snippet of the code (initial few lines). > > > > #!/usr/bin/env python > > # -*- coding: utf-8 -*- > > __author__ = 'Shilin He' > > > > import sys > > sys.path.insert(0, 'D:\Projects\Initiatives\machine > > learning\loglizer-master\loglizer-master\utils\evaluation') > > sys.path.insert(0, 'D:\Projects\Initiatives\machine > > learning\loglizer-master\loglizer-master\utils\evaluation') print sys.path > > import numpy as np import math > > from scipy.special import expit > > from numpy import linalg as LA > > from scipy.cluster.hierarchy import linkage > > from scipy.cluster.hierarchy import fcluster > > from scipy.spatial.distance import pdist > > import utils.evaluation as ev > > > > Here is the error: > > D:\Projects\Initiatives\machine > > learning\loglizer-master\loglizer-master\models\log_clustering.py in > > () > > 14 from scipy.cluster.hierarchy import fcluster > > 15 from scipy.spatial.distance import pdist > > ---> 16 import utils.evaluation as ev > > 17 > > 18 > > ImportError: No module named utils.evaluation > > > > > > In the utils directory, I clearly can see evaluation file. > > > > Can someone give some directions? > > (1) The parent directory of the utils directory has to be in sys.path. > (2) If you are using Python 2 you need an __init__.py file in the utils > directory. Thank you very much. I am able to get this working. -- https://mail.python.org/mailman/listinfo/python-list
perplexing error
I am running a small python code. The main module calls another data loader module. I keep this getting this error. AttributeError: 'str' object has no attribute 'raw_data' I tried few things in the code and finally realized that no matter the line number is 68 of data loader module. Of course, this was after a few iterations. I then commented this line itself but I still get this error. Below is a little longer code snippet. AttributeErrorTraceback (most recent call last) D:\Projects\Initiatives\machine learning\Log analyzer\log_analyzer.py in () 32 model = para['models'] 33 assert model in ['DT', 'LR', 'SVM'] ---> 34 raw_data, event_mapping_data = data_loader.bgl_data_loader(para) 35 36 logger.debug("raw data %s \n", raw_data) D:\Projects\Initiatives\machine learning\loglizer-master\loglizer-master\utils\data_loader.py in bgl_data_loader(para) 66 # get the label for each log 67 data_df['label'] = (data_df['label'] != '-').astype(int) ---> 68 #logger.debug("data frame %s \n", data_df) 69 logger.debug("\n") 70 raw_data = data_df[['label','seconds_since']].as_matrix() AttributeError: 'str' object has no attribute 'raw_data' If you notice, line 68 is commented. I am not sure what I am doing wrong. Need some inputs ... -- https://mail.python.org/mailman/listinfo/python-list
Re: perplexing error
On Sunday, 9 September 2018 00:02:49 UTC+5:30, Peter Otten wrote: > Sharan Basappa wrote: > > > I am running a small python code. The main module calls another data > > loader module. I keep this getting this error. > > > > AttributeError: 'str' object has no attribute 'raw_data' > > > > I tried few things in the code and finally realized that no matter the > > line number is 68 of data loader module. Of course, this was after a few > > iterations. I then commented this line itself but I still get this error. > > > > Below is a little longer code snippet. > > > > AttributeErrorTraceback (most recent call last) > > D:\Projects\Initiatives\machine learning\Log analyzer\log_analyzer.py in > > () > > 32 model = para['models'] > > 33 assert model in ['DT', 'LR', 'SVM'] > > ---> 34 raw_data, event_mapping_data = > > data_loader.bgl_data_loader(para) > > 35 > > 36 logger.debug("raw data %s \n", raw_data) > > D:\Projects\Initiatives\machine > > learning\loglizer-master\loglizer-master\utils\data_loader.py in > > bgl_data_loader(para) > > 66 # get the label for each log > > 67 data_df['label'] = (data_df['label'] != '-').astype(int) > > ---> 68 #logger.debug("data frame %s \n", data_df) > > 69 logger.debug("\n") > > 70 raw_data = data_df[['label','seconds_since']].as_matrix() > > AttributeError: 'str' object has no attribute 'raw_data' > > > > If you notice, line 68 is commented. > > I am not sure what I am doing wrong. > > > > Need some inputs ... > > The source code in the traceback is read from the file when the exception is > raised. If you edit a module after importing it the traceback may pick lines > that caused the code raising the exception, but now contain something > completely different. A little demo: > > >>> with open("tmp.py", "w") as f: f.write("def f():\n return 1/0\n") > ... > 21 > >>> import tmp > >>> with open("tmp.py", "w") as f: f.write("def f():\n return 1/2\n") > ... > 21 > >>> tmp.f() > Traceback (most recent call last): > File "", line 1, in > File "/home/peter/tmp.py", line 2, in f > return 1/2 > ZeroDivisionError: division by zero > > Therefore you must ensure that the interpreter is restarted after changing > the source code. This may include restarting a server or an editor. Understood. Now I know why the error is stuck on that line. This gives me a clue. -- https://mail.python.org/mailman/listinfo/python-list
logging output
I am loading a csv file using Pandas and then logging it back into another file using logging module. Here is one sample line from input file: - 1117838570 2005.06.03 R02-M1-N0-C:J12-U11 2005-06-03-15.42.50.675872 R02-M1-N0-C:J12-U11 RAS KERNEL INFO instruction cache parity error corrected Here the sample line from logging output 0 - 1117838570 2005.06.03 R02-M1-N0-C:J12-U11 20... The lines seems to be same but logging module seems to be cutting down additional columns towards the end (see "..." characters). Am I missing something? below is the full code listing: import sys import pandas as pd import numpy as np import logging #Create and configure logger logging.basicConfig(filename="debug.log", filemode='w', format='%(asctime)s %(message)s') logger = logging.getLogger() logger.setLevel(logging.DEBUG) data_df = pd.read_csv("BGL_MERGED.log", nrows=100, header=None, delimiter=r'\s"') #data_df = pd.read_csv("BGL_MERGED.log") logger.debug("data frame %s \n", data_df) -- https://mail.python.org/mailman/listinfo/python-list
pandas read_csv
are there any requirements about the format of the CSV file when using read_csv from pandas? For example, is it necessary that the csv file has to have same number of columns in every line etc. I am trying to load a csv file and I get the following error. I really don't know what the issue is ... Any help is appreciated ... ParserErrorTraceback (most recent call last) D:\Projects\Initiatives\machine learning\programs\log\log_analysis_1.py in () 10 11 #data_df = pd.read_csv("BGL_MERGED.log", nrows=100, header=None, delimiter=r'\s"') ---> 12 data_df = pd.read_csv("d:\Projects\Initiatives\machine learning\programs\log\BGL_MERGED.log", nrows=100, header=None) 13 logger.debug("data frame %s \n", data_df) 14 print('Cols %d' %(len(data_df.columns))) D:\Users\sharanb\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\pandas\io\parsers.pyc in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, skip_footer, doublequote, delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, buffer_lines, memory_map, float_precision) 653 skip_blank_lines=skip_blank_lines) 654 --> 655 return _read(filepath_or_buffer, kwds) 656 657 parser_f.__name__ = name D:\Users\sharanb\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\pandas\io\parsers.pyc in _read(filepath_or_buffer, kwds) 409 410 try: --> 411 data = parser.read(nrows) 412 finally: 413 parser.close() D:\Users\sharanb\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\pandas\io\parsers.pyc in read(self, nrows) 1003 raise ValueError('skipfooter not supported for iteration') 1004 -> 1005 ret = self._engine.read(nrows) 1006 1007 if self.options.get('as_recarray'): D:\Users\sharanb\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\pandas\io\parsers.pyc in read(self, nrows) 1746 def read(self, nrows=None): 1747 try: -> 1748 data = self._reader.read(nrows) 1749 except StopIteration: 1750 if self._first_chunk: pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.read (pandas\_libs\parsers.c:10862)() pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._read_low_memory (pandas\_libs\parsers.c:11343)() pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._read_rows (pandas\_libs\parsers.c:11884)() pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._tokenize_rows (pandas\_libs\parsers.c:11755)() pandas/_libs/parsers.pyx in pandas._libs.parsers.raise_parser_error (pandas\_libs\parsers.c:28765)() ParserError: Error tokenizing data. C error: Expected 1 fields in line 8, saw 3 -- https://mail.python.org/mailman/listinfo/python-list
error while importing a module
I have a design file that I am importing in a test file to run some tests. The design file compiles fine when I run it standalone but when I import it in the test file, I see error on the very first line where I am importing the design file. This is the line from test file: """ test my design """ import assertion_design as asd Here is the error: WindowsErrorTraceback (most recent call last) D:\Users\sharanb\OneDrive - HCL Technologies Ltd\Projects\MyBackup\Projects\Initiatives\machine learning\programs\assertion\assertion_design_test.py in () 4 provided by the user 5 """ > 6 import assertion_design as asd Am I missing something? -- https://mail.python.org/mailman/listinfo/python-list
Re: error while importing a module
On Tuesday, 19 March 2019 08:34:25 UTC+5:30, Sharan Basappa wrote: > I have a design file that I am importing in a test file to run some tests. > The design file compiles fine when I run it standalone but when I import it > in the test file, I see error on the very first line where I am importing the > design file. > > This is the line from test file: > > """ > test my design > """ > import assertion_design as asd > > Here is the error: > WindowsErrorTraceback (most recent call last) > D:\Users\sharanb\OneDrive - HCL Technologies > Ltd\Projects\MyBackup\Projects\Initiatives\machine > learning\programs\assertion\assertion_design_test.py in () > 4 provided by the user > 5 """ > > 6 import assertion_design as asd > > Am I missing something? folks, i think i figured it out. The following 2 lines were missing before the import import sys sys.path.append('D:\Users\sharanb\OneDrive - HCL Technologies Ltd\Projects\MyBackup\Projects\Initiatives\machine learning\programs\assertion') -- https://mail.python.org/mailman/listinfo/python-list
log file
I am running a program and even though the program runs all fine, the log file is missing. I have pasted first few lines of the code. Any suggestions where I maybe going wrong? import os import csv import logging import assertion_design as asd import random #Create and configure logger logging.basicConfig(filename="test_1.log", filemode='w', format='%(asctime)s %(message)s') import os import csv import logging import assertion_design as asd import random #Create and configure logger logging.basicConfig(filename="test_1.log", filemode='w', format='%(asctime)s %(message)s') -- https://mail.python.org/mailman/listinfo/python-list
Re: log file
On Friday, 22 March 2019 09:09:14 UTC+5:30, DL Neil wrote: > On 22/03/19 4:25 PM, Sharan Basappa wrote: > > I am running a program and even though the program runs all fine, the log > > file is missing. I have pasted first few lines of the code. > > > > Any suggestions where I maybe going wrong? > > > > import os > > import csv > > import logging > > import assertion_design as asd > > import random > > > > #Create and configure logger > > logging.basicConfig(filename="test_1.log", filemode='w', > > format='%(asctime)s %(message)s') > > import os > > import csv > > import logging > > import assertion_design as asd > > import random > > > > #Create and configure logger > > logging.basicConfig(filename="test_1.log", filemode='w', > > format='%(asctime)s %(message)s') > > > > > Do all of these lines actually appear, or is it an accidental > copy-paste+paste? > > What do you use to actually record data in the log? eg > > logging.info("Informational message") > > Any error message from Python? > > When you say "the log file is missing" do you mean that there is no such > file, that there is an empty file, or what? Which directory do you run > the code from, and in which directory do you expect to find the log file? > > -- > Regards =dn There is no log file at all in the directory where I am running. I expected the log file to be in the directory where I am running the test from. Let me describe it in more detail. This is the directory - "D:\Users\sharanb\OneDrive - HCL Technologies Ltd\Projects\MyBackup\Projects\Initiatives\machine learning\programs\assertion\CNN" The test program imports the design and runs some test on it. Both the design file and test file are in the above directory. -- https://mail.python.org/mailman/listinfo/python-list
Re: log file
On Friday, 22 March 2019 09:09:16 UTC+5:30, adam@gmail.com wrote: > On Thursday, March 21, 2019 at 10:26:14 PM UTC-5, Sharan Basappa wrote: > > I am running a program and even though the program runs all fine, the log > > file is missing. I have pasted first few lines of the code. > > > I am thinking--hoping, rather--that you just kind of double pasted there. > Anyways, you needed to specify the logging level in basicConfig: > > import os > import csv > import logging > import random > > #Create and configure logger > # Check out level=logging.INFO > logging.basicConfig(filename="test_1.log", filemode='w', format='%(asctime)s > %(message)s', level=logging.INFO) > log = logging.getLogger() > log.info("Yay!") Adam, I don't understand. The logger is set to DEBUG level using the following line. logger.setLevel(logging.DEBUG) Do you see anything incorrect? -- https://mail.python.org/mailman/listinfo/python-list
Re: log file
On Friday, 22 March 2019 09:13:18 UTC+5:30, MRAB wrote: > On 2019-03-22 03:25, Sharan Basappa wrote: > > I am running a program and even though the program runs all fine, the log > > file is missing. I have pasted first few lines of the code. > > > > Any suggestions where I maybe going wrong? > > > > import os > > import csv > > import logging > > import assertion_design as asd > > import random > > > > #Create and configure logger > > logging.basicConfig(filename="test_1.log", filemode='w', > > format='%(asctime)s %(message)s') > > import os > > import csv > > import logging > > import assertion_design as asd > > import random > > > > #Create and configure logger > > logging.basicConfig(filename="test_1.log", filemode='w', > > format='%(asctime)s %(message)s') > > > Are you sure that you know where it's putting the log file? You have a > relative path there, but relative to where? Try it with an absolute path. > > Are you sure that it's logging anything? Log a simple message just after > configuring to double-check. Would the file not get logged using the current directory? BTW, I changed the file location as follows and still I don't see it: logging.basicConfig(filename="D:\Users\sharanb\OneDrive - HCL Technologies Ltd\Projects\MyBackup\Projects\Initiatives\machine learning\programs\assertion\CNN\test_1.log", filemode='w', format='%(asctime)s %(message)s') -- https://mail.python.org/mailman/listinfo/python-list
Re: log file
On Sunday, 24 March 2019 10:57:13 UTC+5:30, Cameron Simpson wrote: > On 23Mar2019 21:47, Sharan Basappa wrote: > >On Friday, 22 March 2019 09:13:18 UTC+5:30, MRAB wrote: > >> On 2019-03-22 03:25, Sharan Basappa wrote: > >> > I am running a program and even though the program runs all fine, the > >> > log file is missing. I have pasted first few lines of the code. > >> > Any suggestions where I maybe going wrong? > [...] > >> > #Create and configure logger > >> > logging.basicConfig(filename="test_1.log", filemode='w', > >> > format='%(asctime)s %(message)s') > >> > > >> Are you sure that you know where it's putting the log file? You have a > >> relative path there, but relative to where? Try it with an absolute path. > >> > >> Are you sure that it's logging anything? Log a simple message just after > >> configuring to double-check. > > > >Would the file not get logged using the current directory? > > It should be. > > >BTW, I changed the file location as follows and still I don't see it: > > > >logging.basicConfig(filename="D:\Users\sharanb\OneDrive - HCL > >Technologies Ltd\Projects\MyBackup\Projects\Initiatives\machine > >learning\programs\assertion\CNN\test_1.log", filemode='w', > >format='%(asctime)s %(message)s') > > Did you read my other recent post? You should define strings with > backslashes in them such are your file path as 'raw strings', which > start with r' or r" instead of a plain quote character. Raw strings do > not interpret \x escapes. In particular your path above has a '\a' in > it, which is not a backslash followed by an "a", it is a BEL character. > > That said, I can't see what's wrong with your original example which has > no backslashes. > > Cheers, > Cameron Simpson Ah. I finally solved the issue though I don't know what the problem itself it. This is how the new code looks like: for handler in logging.root.handlers[:]: logging.root.removeHandler(handler) #Create and configure logger filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_1.log') logging.basicConfig(filename=filename, filemode='w', format='%(asctime)s %(message)s') I found the above tip from the following discussion: https://stackoverflow.com/questions/30861524/logging-basicconfig-not-creating-log-file-when-i-run-in-pycharm -- https://mail.python.org/mailman/listinfo/python-list
Re: log file
On Sunday, 24 March 2019 14:20:36 UTC+5:30, Sharan Basappa wrote: > On Sunday, 24 March 2019 10:57:13 UTC+5:30, Cameron Simpson wrote: > > On 23Mar2019 21:47, Sharan Basappa wrote: > > >On Friday, 22 March 2019 09:13:18 UTC+5:30, MRAB wrote: > > >> On 2019-03-22 03:25, Sharan Basappa wrote: > > >> > I am running a program and even though the program runs all fine, the > > >> > log file is missing. I have pasted first few lines of the code. > > >> > Any suggestions where I maybe going wrong? > > [...] > > >> > #Create and configure logger > > >> > logging.basicConfig(filename="test_1.log", filemode='w', > > >> > format='%(asctime)s %(message)s') > > >> > > > >> Are you sure that you know where it's putting the log file? You have a > > >> relative path there, but relative to where? Try it with an absolute path. > > >> > > >> Are you sure that it's logging anything? Log a simple message just after > > >> configuring to double-check. > > > > > >Would the file not get logged using the current directory? > > > > It should be. > > > > >BTW, I changed the file location as follows and still I don't see it: > > > > > >logging.basicConfig(filename="D:\Users\sharanb\OneDrive - HCL > > >Technologies Ltd\Projects\MyBackup\Projects\Initiatives\machine > > >learning\programs\assertion\CNN\test_1.log", filemode='w', > > >format='%(asctime)s %(message)s') > > > > Did you read my other recent post? You should define strings with > > backslashes in them such are your file path as 'raw strings', which > > start with r' or r" instead of a plain quote character. Raw strings do > > not interpret \x escapes. In particular your path above has a '\a' in > > it, which is not a backslash followed by an "a", it is a BEL character. > > > > That said, I can't see what's wrong with your original example which has > > no backslashes. > > > > Cheers, > > Cameron Simpson > > Ah. I finally solved the issue though I don't know what the problem itself it. > This is how the new code looks like: > > for handler in logging.root.handlers[:]: > logging.root.removeHandler(handler) > > #Create and configure logger > filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), > 'test_1.log') > logging.basicConfig(filename=filename, filemode='w', format='%(asctime)s > %(message)s') > > I found the above tip from the following discussion: > https://stackoverflow.com/questions/30861524/logging-basicconfig-not-creating-log-file-when-i-run-in-pycharm I think I got some more idea about the issue though I still don't know the root cause. So, my test program is importing design file. Both test and design file are have the following lines: #Create and configure logger filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_5.log') logging.basicConfig(filename=filename, filemode='w', format='%(asctime)s %(message)s') #Creating an object logger = logging.getLogger() I think this is causing the issue. Anyway, I am able to log from the test program. However, I am not able to log anything from design program. I will open a separate thread on this topic. -- https://mail.python.org/mailman/listinfo/python-list
Multiple log files using logging module
I have a test program that imports a design program. Both the programs need to log messages. I have tried the following: 1) Both the programs have the following lines: for handler in logging.root.handlers[:]: logging.root.removeHandler(handler) #Create and configure logger filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), '<>') logging.basicConfig(filename=filename, filemode='w', format='%(asctime)s %(message)s') #Creating an object logger = logging.getLogger() #Setting the threshold of logger to DEBUG logger.setLevel(logging.DEBUG) replace <> above with respective log file names for test and design programs. However, the test program logs the messages but not the design program. 2) I removed the above lines from design program altogether hoping that the messages will appear in the same log file. There is no error, however, no message is logged from the design program. I would like to get comment from members here as well as some simple programs to illustrate this ... -- https://mail.python.org/mailman/listinfo/python-list
issue in handling CSV data
I am trying to read a log file that is in CSV format. The code snippet is below: ### import matplotlib.pyplot as plt import seaborn as sns; sns.set() import numpy as np import pandas as pd import os import csv from numpy import genfromtxt # read the CSV and get into X array os.chdir(r'D:\Users\sharanb\OneDrive - HCL Technologies Ltd\Projects\MyBackup\Projects\Initiatives\machine learning\programs\constraints') X = [] #with open("constraints.csv", 'rb') as csvfile: #reader = csv.reader(csvfile) #data_as_list = list(reader) #myarray = np.asarray(data_as_list) my_data = genfromtxt('constraints.csv', delimiter = ',', dtype=None) print (my_data) my_data_1 = np.delete(my_data, 0, axis=1) print (my_data_1) my_data_2 = np.delete(my_data_1, 0, axis=1) print (my_data_2) my_data_3 = my_data_2.astype(np.float) Here is how print (my_data_2) looks like: ## [['"\t"81' '"\t5c'] ['"\t"04' '"\t11'] ['"\t"e1' '"\t17'] ['"\t"6a' '"\t6c'] ['"\t"53' '"\t69'] ['"\t"98' '"\t87'] ['"\t"5c' '"\t4b'] ## Finally, I am trying to get rid of the strings and get array of numbers using Numpy's astype function. At this stage, I get an error. This is the error: my_data_3 = my_data_2.astype(np.float) could not convert string to float: " "81 As you can see, the string "\t"81 is causing the error. It seems to be due to char "\t". I don't know how to resolve this. Thanks for your help. -- https://mail.python.org/mailman/listinfo/python-list
Re: issue in handling CSV data
On Saturday, 7 September 2019 21:18:11 UTC-4, MRAB wrote: > On 2019-09-08 01:19, Sharan Basappa wrote: > > I am trying to read a log file that is in CSV format. > > > > The code snippet is below: > > > > ### > > import matplotlib.pyplot as plt > > import seaborn as sns; sns.set() > > import numpy as np > > import pandas as pd > > import os > > import csv > > from numpy import genfromtxt > > > > # read the CSV and get into X array > > os.chdir(r'D:\Users\sharanb\OneDrive - HCL Technologies > > Ltd\Projects\MyBackup\Projects\Initiatives\machine > > learning\programs\constraints') > > X = [] > > #with open("constraints.csv", 'rb') as csvfile: > > #reader = csv.reader(csvfile) > > #data_as_list = list(reader) > > #myarray = np.asarray(data_as_list) > > > > my_data = genfromtxt('constraints.csv', delimiter = ',', dtype=None) > > print (my_data) > > > > my_data_1 = np.delete(my_data, 0, axis=1) > > print (my_data_1) > > > > my_data_2 = np.delete(my_data_1, 0, axis=1) > > print (my_data_2) > > > > my_data_3 = my_data_2.astype(np.float) > > > > > > Here is how print (my_data_2) looks like: > > ## > > [['"\t"81' '"\t5c'] > > ['"\t"04' '"\t11'] > > ['"\t"e1' '"\t17'] > > ['"\t"6a' '"\t6c'] > > ['"\t"53' '"\t69'] > > ['"\t"98' '"\t87'] > > ['"\t"5c' '"\t4b'] > > ## > > > > Finally, I am trying to get rid of the strings and get array of numbers > > using Numpy's astype function. At this stage, I get an error. > > > > This is the error: > > my_data_3 = my_data_2.astype(np.float) > > could not convert string to float: " "81 > > > > As you can see, the string "\t"81 is causing the error. > > It seems to be due to char "\t". > > > > I don't know how to resolve this. > > > > Thanks for your help. > > > Are you sure it's CSV (Comma-Separated Value) and not TSV (Tab-Separated > Value)? > > Also the values look like hexadecimal to me. I think that > .astype(np.float) assumes that the values are decimal. > > I'd probably start by reading them using the csv module, convert the > values to decimal, and then pass them on to numpy. yes. it is CSV. The commas are gone once csv.reader processed the csv file. The tabs seem to be there also which seem to be causing the issue. Thanks for your response -- https://mail.python.org/mailman/listinfo/python-list
Re: issue in handling CSV data
On Sunday, 8 September 2019 04:56:29 UTC-4, Andrea D'Amore wrote: > On Sun, 8 Sep 2019 at 02:19, Sharan Basappa wrote: > This is the error: > > my_data_3 = my_data_2.astype(np.float) > > could not convert string to float: " "81 > > > As you can see, the string "\t"81 is causing the error. > > It seems to be due to char "\t". > > It is not clear what format do you expect to be in the file. > You say "it is CSV" so your actual payload seems to be a pair of three > bytes (a tab and two hex digits in ASCII) per line. > > Can you paste a hexdump of the first three lines of the input file and > say what you expect to get once the data has been processed? Andrea, The issue seems to be presence of tabs along with the numbers in a single string. So, when I try to convert strings to numbers, it fails due to presence of tabs. Here is the hex dump: 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 38 31 2c 22 09 35 63 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 30 34 2c 22 09 31 31 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 65 31 2c 22 09 31 37 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 36 61 2c 22 09 36 63 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 35 33 2c 22 09 36 39 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 39 38 2c 22 09 38 37 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 35 63 2c 22 09 34 62 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 32 38 2c 22 09 33 36 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 36 33 2c 22 09 35 30 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 32 34 2c 22 09 32 31 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 64 66 2c 22 09 39 61 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 61 62 2c 22 09 62 39 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 -- https://mail.python.org/mailman/listinfo/python-list
numpy array - convert hex to int
I have a numpy array that has data in the form of hex. I would like to convert that into decimal/integer. Need suggestions please. -- https://mail.python.org/mailman/listinfo/python-list
Re: issue in handling CSV data
On Sunday, 8 September 2019 12:45:45 UTC-4, Peter J. Holzer wrote: > On 2019-09-08 05:41:07 -0700, Sharan Basappa wrote: > > On Sunday, 8 September 2019 04:56:29 UTC-4, Andrea D'Amore wrote: > > > On Sun, 8 Sep 2019 at 02:19, Sharan Basappa > > > wrote: > > > > As you can see, the string "\t"81 is causing the error. > > > > It seems to be due to char "\t". > > > > > > It is not clear what format do you expect to be in the file. > > > You say "it is CSV" so your actual payload seems to be a pair of three > > > bytes (a tab and two hex digits in ASCII) per line. > > > > The issue seems to be presence of tabs along with the numbers in a single > > string. So, when I try to convert strings to numbers, it fails due to > > presence of tabs. > > > > Here is the hex dump: > > > > 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 > > 74 68 2c 22 09 22 38 31 2c 22 09 35 63 0d 0a 22 > > 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 > ... > > This looks like this: > > "address," "length," "81," 5c > "address," "length," "04," 11 > "address," "length," "e1," 17 > "address," "length," "6a," 6c > ... > > Note that the commas are within the quotes. I'd say Andrea is correct: > This is a tab-separated file, not a comma-separated file. But for some > reason all fields except the last end with a comma. > > I would > > a) try to convince the person producing the file to clean up the mess > > b) if that is not successful, use the csv module to read the file with >separator tab and then discard the trailing commas. > Hi Peter, I respectfully disagree that it is not a comma separated. Let me explain why. If you look the following line in the code, it specifies comma as the delimiter: my_data = genfromtxt('constraints.csv', delimiter = ',', dtype=None) Now, if you see the print after getting the data, it looks like this: ## [['"\t"81' '"\t5c'] ['"\t"04' '"\t11'] ['"\t"e1' '"\t17'] ['"\t"6a' '"\t6c'] ['"\t"53' '"\t69'] ['"\t"98' '"\t87'] ['"\t"5c' '"\t4b'] ## if you observe, the commas have disappeared. That, I think, is because it actually treated this as a CSV file. Anyway, I am checking to see if I can discard the tabs and process this. I will keep everyone posted. -- https://mail.python.org/mailman/listinfo/python-list
Re: numpy array - convert hex to int
On Sunday, 8 September 2019 11:16:52 UTC-4, Luciano Ramalho wrote: > >>> int('C0FFEE', 16) > 12648430 > > There you go! > > On Sun, Sep 8, 2019 at 12:02 PM Sharan Basappa > wrote: > > > > I have a numpy array that has data in the form of hex. > > I would like to convert that into decimal/integer. > > Need suggestions please. > > -- I am sorry. I forgot to mention that I have the data in a numpy array. So, when I try to convert to int, I get the following error. sample code here # my_data_3 = int(my_data_2) my_data_4 = my_data_3.astype(np.float) # Error here ### #np.core.defchararray.replace(my_data_2,",'') 27 ---> 28 my_data_3 = int(my_data_2) 29 30 my_data_4 = my_data_3.astype(np.float) TypeError: only length-1 arrays can be converted to Python scalars # -- https://mail.python.org/mailman/listinfo/python-list
kmeans clustering
Can someone please help me to clarify the different between fit and predict functions of kmeans? -- https://mail.python.org/mailman/listinfo/python-list