chi square test in sklearn printing NAN values for most of the columns
Hi i am trying to use chi-square Test to select most important columns among 5501 columns. But for most of the columns i am getting NAN value as a Chi test value import pandas as pd from sklearn.preprocessing import LabelEncoder from sklearn.feature_selection import chi2 cols =[] cols.append(int(0)) #for i in range(1, 5502): cols.append(int(10)) df = pd.read_csv("D:\PHD\obranking\\demo.csv", usecols=cols) df.apply(LabelEncoder().fit_transform) X = df.drop(labels='label', axis=1) Y = df['label'] chi_scores = chi2(X, Y) print(chi_scores) in this code i printed chi value for 10th column but for most of the columns it is behaving like below "C:\Users\Rahul Gupta\PycharmProjects\CSVLearn\venv\Scripts\python.exe" "C:/Users/Rahul Gupta/PycharmProjects/CSVLearn/ChiSq_learn.py" (array([nan]), array([nan])) Process finished with exit code 0 -- https://mail.python.org/mailman/listinfo/python-list
please help me while installing pyttsx3 it shows error
PS C:\Users\amanb\OneDrive\Desktop\jarvis> pip install pyttsx3 Collecting pyttsx3 Using cached pyttsx3-2.87-py3-none-any.whl (39 kB) Collecting comtypes; platform_system == "Windows" Using cached comtypes-1.1.7.zip (180 kB) Installing collected packages: comtypes, pyttsx3 Running setup.py install for comtypes ... error ERROR: Command errored out with exit status 1: command: 'C:\Users\amanb\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\amanb\\AppData\\Local\\Temp\\pip-install-uwazge93\\comtypes\\setup.py'"'"'; __file__='"'"'C:\\Users\\amanb\\AppData\\Local\\Temp\\pip-install-uwazge93\\comtypes\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\amanb\AppData\Local\Temp\pip-record-3lktlb2m\install-record.txt' --single-version-externally-managed --user --prefix= --compile --install-headers 'C:\Users\amanb\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\Include\comtypes' cwd: C:\Users\amanb\AppData\Local\Temp\pip-install-uwazge93\comtypes\ Complete output (276 lines): running install running build running build_py creating build creating build\lib creating build\lib\comtypes copying comtypes\automation.py -> build\lib\comtypes copying comtypes\connectionpoints.py -> build\lib\comtypes copying comtypes\errorinfo.py -> build\lib\comtypes copying comtypes\git.py -> build\lib\comtypes copying comtypes\GUID.py -> build\lib\comtypes copying comtypes\hresult.py -> build\lib\comtypes copying comtypes\logutil.py -> build\lib\comtypes copying comtypes\messageloop.py -> build\lib\comtypes copying comtypes\npsupport.py -> build\lib\comtypes copying comtypes\patcher.py -> build\lib\comtypes copying comtypes\persist.py -> build\lib\comtypes copying comtypes\safearray.py -> build\lib\comtypes copying comtypes\shelllink.py -> build\lib\comtypes copying comtypes\typeinfo.py -> build\lib\comtypes copying comtypes\util.py -> build\lib\comtypes copying comtypes\viewobject.py -> build\lib\comtypes copying comtypes\_comobject.py -> build\lib\comtypes copying comtypes\_meta.py -> build\lib\comtypes copying comtypes\_safearray.py -> build\lib\comtypes copying comtypes\__init__.py -> build\lib\comtypes creating build\lib\comtypes\client copying comtypes\client\dynamic.py -> build\lib\comtypes\client copying comtypes\client\lazybind.py -> build\lib\comtypes\client copying comtypes\client\_code_cache.py -> build\lib\comtypes\client copying comtypes\client\_events.py -> build\lib\comtypes\client copying comtypes\client\_generate.py -> build\lib\comtypes\client copying comtypes\client\__init__.py -> build\lib\comtypes\client creating build\lib\comtypes\server copying comtypes\server\automation.py -> build\lib\comtypes\server copying comtypes\server\connectionpoints.py -> build\lib\comtypes\server copying comtypes\server\inprocserver.py -> build\lib\comtypes\server copying comtypes\server\localserver.py -> build\lib\comtypes\server copying comtypes\server\register.py -> build\lib\comtypes\server copying comtypes\server\w_getopt.py -> build\lib\comtypes\server copying comtypes\server\__init__.py -> build\lib\comtypes\server creating build\lib\comtypes\tools copying comtypes\tools\codegenerator.py -> build\lib\comtypes\tools copying comtypes\tools\tlbparser.py -> build\lib\comtypes\tools copying comtypes\tools\typedesc.py -> build\lib\comtypes\tools copying comtypes\tools\typedesc_base.py -> build\lib\comtypes\tools copying comtypes\tools\__init__.py -> build\lib\comtypes\tools creating build\lib\comtypes\test copying comtypes\test\find_memleak.py -> build\lib\comtypes\test copying comtypes\test\runtests.py -> build\lib\comtypes\test copying comtypes\test\setup.py -> build\lib\comtypes\test copying comtypes\test\TestComServer.py -> build\lib\comtypes\test copying comtypes\test\TestDispServer.py -> build\lib\comtypes\test copying comtypes\test\test_agilent.py -> build\lib\comtypes\test copying comtypes\test\test_avmc.py -> build\lib\comtypes\test copying comtypes\test\test_basic.py -> build\lib\comtypes\test copying comtypes\test\test_BSTR.py -> build\lib\comtypes\test copying comtypes\test\test_casesensitivity.py -> build\lib\comtypes\test copying comtypes\test\test_client.py -> build\lib\comtypes\test copying comtypes\test\test_collections.py -> build\lib\comtypes\test copying comtypes\test\test_comserver.py -> build\lib\comtypes\test copying comtypes\test\test_createwrappers.py -> build\lib\comtypes\test copying comtypes\te
unable to write content in csv filw
FOLLWOING IS MY CODE import pandas as pd import csv from sklearn.preprocessing import LabelEncoder from sklearn.feature_selection import chi2 with open("D:\PHD\obranking\\test_chi.csv", 'w') as csvfilew1: fields = ['index', 'feature name', 'p_value'] csvwriter1 = csv.DictWriter(csvfilew1, fieldnames=fields) csvwriter1.writeheader() for i in range(1, 5502): csv_data = dict().fromkeys(fields) csv_data['index'] = i cols =[] cols.append(int(0)) cols.append(int(i)) df = pd.read_csv("D:\PHD\obranking\\demo.csv", usecols=cols) df.apply(LabelEncoder().fit_transform) X = df.drop(labels='label', axis=1) Y = df['label'] chi_scores = chi2(X, Y) if(chi_scores[1] < 0.05): f_name = str(X.columns) f_name = f_name[8:-19] csv_data['feature name'] = f_name p_val = str(chi_scores[1]) p_val = p_val[1:-1] csv_data['p_value'] = p_val print(csv_data) csvwriter1.writerow(csv_data) #print(csv_data) #print(f_name + p_val) #print(str(X.col + str(chi_scores[1])) test_chi.csv is created but it remains empty after execution of the code. although when i am printing csv_data it gets printed but not written in csv using writerow(csv_data). Also there are no field names in the csv even writeheader() seems to not work. I am confused what is wrong. Could someone help -- https://mail.python.org/mailman/listinfo/python-list
Re: Function to avoid a global variable
Thanks Chris! At least my code isn't (quite!) as bad as the xkcd example :) Guess my "concern" is using the initialized array in the function: def myfunct(a, b, c=array[0,1,2,3] ) always feels like an abuse. Has anyone seriously considered implementing a true static variable in a function? Is there a PEP? Best, On Sun, Apr 26, 2020 at 8:47 PM Chris Angelico wrote: > On Mon, Apr 27, 2020 at 1:39 PM Bob van der Poel wrote: > > > > Does this make as much sense as anything else? I need to track calls to a > > function to make sure it doesn't get called to too great a depth. I had a > > global which I inc/dec and then check in the function. Works fine, but I > do > > need to keep a global around just for this. > > > > So ... instead I wrote a short function: > > > > def parseStack(inc, depth=[0]): > > if depth[0] > 50: > > ... report error and die nicely > > depth[0] += inc > > > > This gets rid of the global, and it moves my error check out of the main > > code as well. But, I never feel all that great about using a list in the > > function call for static storage. > > That's not really any different from a global. If globals bother you, > this should as well. But it's also reasonable to NOT be bothered by it > - it's not that big a problem. > > If this is directly recursive (if there's a call to parseStack inside > parseStack itself, as opposed to being mutually recursive with one or > more other functions), the easiest way to avoid the global is to just > pass the parameter down - something like this: > > def spaminate(x, y, depth=0): > ... > spaminate(newx, newy, depth+1) > ... > > But that can be harder if you have other things happening, eg if your > function calls something else, which calls something else, which calls > your function again. If it's that complex, I would just accept the > global, honestly - globals (module-level variables) aren't the worst > thing that can happen. You won't end up with code reviews like > https://xkcd.com/1513/ just because of one global (or if you do, you > REALLY need to find better code reviewers!). > > ChrisA > -- > https://mail.python.org/mailman/listinfo/python-list > -- Listen to my FREE CD at http://www.mellowood.ca/music/cedars Bob van der Poel ** Wynndel, British Columbia, CANADA ** EMAIL: b...@mellowood.ca WWW: http://www.mellowood.ca -- https://mail.python.org/mailman/listinfo/python-list
Re: unable to write content in csv filw
1: Does the code run to completion without errors? If there's an exception then the data may not get to the file because I/O is normally buffered in memory and written out in larger chunks as the buffer fills i.e. at a time later than your .writerow() call. 2: If you delete the CSV file before running your code, does it get recreated? If not, maybe your filename is incorrect. As a matter of practice you should write filenames using "raw" strings. Instead of writing this: with open("D:\PHD\obranking\\test_chi.csv", 'w') as csvfilew1: it is better to write this: with open(r'D:\PHD\obranking\test_chi.csvr', 'w') as csvfilew1: In a raw string the backslash is not special and does not need to be doubled. More importantly, the reason the backslash is special is that in ordinary string syntax it is used to introduce various special characters, such as "\n" to specify a newline character. If you have an unfortunate filename such as: D:\PHD\new_files then you might easily produce a string with an embedded newline instead of a file separator and an "n". Likewise for the various other escapes. With a raw string like: r'D:\PHD\new_files' you do not run this risk. Cheers, Cameron Simpson On 27Apr2020 09:14, Rahul Gupta wrote: FOLLWOING IS MY CODE import pandas as pd import csv from sklearn.preprocessing import LabelEncoder from sklearn.feature_selection import chi2 with open("D:\PHD\obranking\\test_chi.csv", 'w') as csvfilew1: fields = ['index', 'feature name', 'p_value'] csvwriter1 = csv.DictWriter(csvfilew1, fieldnames=fields) csvwriter1.writeheader() for i in range(1, 5502): csv_data = dict().fromkeys(fields) csv_data['index'] = i cols =[] cols.append(int(0)) cols.append(int(i)) df = pd.read_csv("D:\PHD\obranking\\demo.csv", usecols=cols) df.apply(LabelEncoder().fit_transform) X = df.drop(labels='label', axis=1) Y = df['label'] chi_scores = chi2(X, Y) if(chi_scores[1] < 0.05): f_name = str(X.columns) f_name = f_name[8:-19] csv_data['feature name'] = f_name p_val = str(chi_scores[1]) p_val = p_val[1:-1] csv_data['p_value'] = p_val print(csv_data) csvwriter1.writerow(csv_data) #print(csv_data) #print(f_name + p_val) #print(str(X.col + str(chi_scores[1])) test_chi.csv is created but it remains empty after execution of the code. although when i am printing csv_data it gets printed but not written in csv using writerow(csv_data). Also there are no field names in the csv even writeheader() seems to not work. I am confused what is wrong. Could someone help -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Function to avoid a global variable
On 4/27/20 10:39 AM, Bob van der Poel wrote: > Thanks Chris! > > At least my code isn't (quite!) as bad as the xkcd example :) > > Guess my "concern" is using the initialized array in the function: > >def myfunct(a, b, c=array[0,1,2,3] ) > > always feels like an abuse. > > Has anyone seriously considered implementing a true static variable in a > function? Is there a PEP? There's actually no need. You can create attributes on the function itself, just like a regular object: def foo(): if not 'bar' in foo.__dict__: foo.bar = 5 print (foo.bar) -- https://mail.python.org/mailman/listinfo/python-list
Re: Function to avoid a global variable
Oh my, that is very cool! So, I can do this: def foo(i): if not 'bar' in foo.__dict__: foo.bar = 5 foo.bar += i for a in range(10): foo(1) print (foo.bar) Thanks. I will have to play more with this. On Mon, Apr 27, 2020 at 5:31 PM Michael Torrie wrote: > On 4/27/20 10:39 AM, Bob van der Poel wrote: > > Thanks Chris! > > > > At least my code isn't (quite!) as bad as the xkcd example :) > > > > Guess my "concern" is using the initialized array in the function: > > > >def myfunct(a, b, c=array[0,1,2,3] ) > > > > always feels like an abuse. > > > > Has anyone seriously considered implementing a true static variable in a > > function? Is there a PEP? > > There's actually no need. You can create attributes on the function > itself, just like a regular object: > > def foo(): > if not 'bar' in foo.__dict__: > foo.bar = 5 > > print (foo.bar) > > > -- > https://mail.python.org/mailman/listinfo/python-list > -- Listen to my FREE CD at http://www.mellowood.ca/music/cedars Bob van der Poel ** Wynndel, British Columbia, CANADA ** EMAIL: b...@mellowood.ca WWW: http://www.mellowood.ca -- https://mail.python.org/mailman/listinfo/python-list
Re: Function to avoid a global variable
bvdp於 2020年4月28日星期二 UTC+8上午9時46分35秒寫道: > Oh my, that is very cool! So, I can do this: > > def foo(i): > if not 'bar' in foo.__dict__: > foo.bar = 5 > foo.bar += i You can have function attribute created this way if you like: def foo(i): foo.bar += i foo.bar = 5 --Jach -- https://mail.python.org/mailman/listinfo/python-list
matplotlib: Difference between Axes and AxesSubplot
Hello It's not clear to me what the differences between Axes and AxesSubplot classes are AxesSubplot is a sub class of Axes It is possible to draw in objects of both type with plot, scatter ... Axes object seems to be able to be inserted in AxesSubplot object (It is strange because AxesSubplot is a sub class of Axes) Here is a piece of code using both. import matplotlib.pyplot as pyplot figure = pyplot.figure() axes = figure.add_subplot(111) # Renvoie un objet AxesSubplot, sous classe de Axes axes.scatter(range(5), [x ** 2 for x in range(5)]) axes.set_xlim(0, 4) axes.set_xlabel('axe des x') axes2 = figure.add_axes([0.3, 0.5, 0.3, 0.3]) # renvoie un objet Axes axes2.patch.set_color('lightyellow') axes2.plot(range(5), range(5)) and the figure it produces: http://www.python-simple.com/img/img32.png -- https://mail.python.org/mailman/listinfo/python-list
Re: Function to avoid a global variable
Le 27/04/2020 à 04:46, Bob van der Poel a écrit : Does this make as much sense as anything else? I need to track calls to a function to make sure it doesn't get called to too great a depth. I had a global which I inc/dec and then check in the function. Works fine, but I do need to keep a global around just for this. So ... instead I wrote a short function: def parseStack(inc, depth=[0]): if depth[0] > 50: ... report error and die nicely depth[0] += inc This gets rid of the global, and it moves my error check out of the main code as well. But, I never feel all that great about using a list in the function call for static storage. Using a callable object is the best way to define a function with a memory class ParseStack: def __init__(self): self.depth=0 def __call__(self, inc, reset=True): if reset: self.depth = 0 if self.depth > 50: ... report error self.depth += 1 ... do your stuff parse_stack = ParseStack() and use "function" parse_stack parse_stack(43) ... -- https://mail.python.org/mailman/listinfo/python-list