Re: Recoding Categorical to Numerical
On 14/08/2021 00:33, John Griner wrote: Hello, and thanks in advance, I am trying to recode categorical variable to numeric. Despite using lambda, If else, dummy-recoding, and so forth, what I get is from ONE column that has 4 variables (City, Town, Suburb, Rural), I get FOUR columns: localeDummy_City localeDummy_Town localeDummy_Suburb localeDummy_Rural locale_recode with the corresponding numeric variable. What I want is the recode to have ONE column that has the numeric conversion. For instance: local_recode 2 4 4 6 2 8 6 2 8 2 2 4 6 4 8 and so forth, where I have set City to 2, and Town to 4, etc. Again, thanks, John My crystal ball says you want import pandas df = pandas.DataFrame( [ [("City", "Suburb")], [("Town", "City", "Suburb")], [("Rural",)] ], columns=["before"] ) flags = dict( City=1, Town=2, Suburb=4, Rural=8 ) df["after"] = df["before"].apply( lambda names: sum(flags[name] for name in set(names)) ) print(df) If that's not it show us your failing code, preferably as a small self-contained script that also generates the required input data. Use cut and paste, and include it into the message body as attachments are usually removed. -- https://mail.python.org/mailman/listinfo/python-list
Re: Empty list as a default param - the problem, and my suggested solution
I don't know if it is useful but it is an interesting metaprogramming/reflection challenge. You used `inspect` but you didn't take its full potential. Try to see if you can simplify your code and see if you can come with a decorator that does not require special parameters. from new import NEW @NEW ... def new_func(a=[]): ... a.append('new appended') ... return a ... new_func() ['new appended'] new_func() ['new appended'] Spoiler - My solution is at https://book-of-gehn.github.io/articles/2021/08/14/Fresh-Python-Defaults.html On Fri, Aug 13, 2021 at 03:44:20PM -0700, guruyaya wrote: I am fairly sure all of us know about this python quirk: def no_new_func(a=[]): ...a.append('new') ...return a no_new_func() ['new'] no_new_func() ['new', 'new'] For some time I was bothered about that there's no elegant way to use empty list or dict as a default parameter. While this can be solved like this: def no_new_func(a=None): ...if a == None: a = [] ...a.append('new') ...return a I have to say I find this solution very far from the spirit of python. Kinda ugly, and not explicit. So I've decided to try and create a new module, that will try and make, what I think, is a more beautiful and explicit: from new import NEW @NEW.parse ... def new_func(a=NEW.new([])): ... a.append('new appended') ... return a ... new_func() ['new appended'] new_func() ['new appended'] I'd like to hear your thoughts on my solution and code. You can find and give your feedback in this project https://github.com/guruyaya/new If I see that people like this, I will upload it to pip. I'm not fully sure about the name I choose (I thought about the "new" keyword used in JAVA, not sure it applies here as well) Thanks in advance for your feedback Yair -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Regarding inability of Python Module Winsound to produce beep in decimal frequency
On Fri, 13 Aug 2021 17:41:05 +0100 MRAB wrote: > On 2021-08-13 17:17, Chris Angelico wrote: > > On Sat, Aug 14, 2021 at 2:11 AM Terry Reedy > > wrote: > >> > >> On 8/13/2021 6:53 AM, Umang Goswami wrote: > >> > Hi There, Hope you find this mail in good health. > >> > > >> > I am Umang Goswami, a Python developer and student working on a > >> > huge project for automation of music instruments. I am producing > >> > the musical notes using the Beep function of Winsound Module( > >> > https://docs.python.org/3/library/winsound.html) by passing > >> > frequency as a argument to the function. > >> > > >> > Now whenever i provide frequency of any note in decimal(for > >> > example 277.1826 for C4 note) it shows following error: > >> > Traceback (most recent call last): > >> >File "C:\Users\Umang Goswami\Desktop\Umang Goswami\test.py", > >> > line 2, in > >> > winsound.Beep(111.11,11) > >> > TypeError: integer argument expected, got float > >> > > >> > Now I have to round up the frequencies. This is hurting the > >> > quality, accuracy ,authenticity and future of the project. > >> > Almost all the notes have the frequencies in decimal parts. > >> > Rounding up means changing semitones and quatertones thus whole > >> > note itself. This problem is technically making my program > >> > useless. > >> > > > > > Is it really? In my experience, no human ear can distinguish 277Hz > > from 277.1826Hz when it's played on a one-bit PC speaker, which the > > Beep function will be using. > > > I've just tried it on my PC and I couldn't hear the difference, > except that odd frequencies had a momentary break in them during > longer notes whereas even frequencies didn't. Very odd... Rounding to integer frequencies will produce disastrously out-of-tune notes in a musical context! Particularly for low notes, where a whole semitone is only a couple of Hz difference. Even for higher notes, when they're played together any inaccuracies are much more apparent. To the OP, there aren't too many options in pure python for playing audio - to do anything complicated I've used applications such as Fluidsynth and sox as python subprocesses. But for playing purely generated tones, here's an example that doesn't limit frequency precision, using pyaudio to play a numpy array representing a sine-wave: import pyaudio, numpy as np f = 555.555 #frequency d = 1.0 # duration in seconds rate = 44100 #sample rate p = pyaudio.PyAudio() stream = p.open(format=pyaudio.paFloat32, channels=1, rate=rate, output=True) samples = np.sin(2*np.pi*np.arange(rate*d)*f/rate).astype(np.float32) stream.write(samples) Hope this helps John -- https://mail.python.org/mailman/listinfo/python-list
Re: Regarding inability of Python Module Winsound to produce beep in decimal frequency
On Sun, Aug 15, 2021 at 1:02 PM John O'Hagan wrote: > > > On 2021-08-13 17:17, Chris Angelico wrote: > > > Is it really? In my experience, no human ear can distinguish 277Hz > > > from 277.1826Hz when it's played on a one-bit PC speaker, which the > > > Beep function will be using. > > Rounding to integer frequencies will produce disastrously out-of-tune > notes in a musical context! Particularly for low notes, where a whole > semitone is only a couple of Hz difference. Even for higher notes, when > they're played together any inaccuracies are much more apparent. But before you advocate that too hard, check to see the *real* capabilities of a one-bit PC speaker. You go on to give an example that uses PyAudio and a sine wave, not the timer chip's "beep" functionality. Try getting some recordings of a half dozen or so computers making a beep at 440Hz. Then do some analysis on the recordings and see whether they're actually within 1Hz of that. (And that's aside from the fact that quite a number of computers will show up completely silent, due to either not having an internal speaker, or not letting you use it.) ChrisA -- https://mail.python.org/mailman/listinfo/python-list