How to calculate a Fourier series in Numpy?

In the end, the most simple thing (calculating the coefficient with a riemann sum) was the most portable/efficient/robust way to solve my problem: import numpy as np def cn(n): c = y*np.exp(-1j*2*n*np.pi*time/period) return c.sum()/c.size def f(x, Nh): f = np.array([2*cn(i)*np.exp(1j*2*i*np.pi*x/period) for i in range(1,Nh+1)]) return f.sum() y2 = np.array([f(t,50).real for t in time]) plot(time, y) … Read more

CUDA initialization: CUDA unknown error – this may be due to an incorrectly set up environment

Had the same issue and in my case solution was very easy, however it wasn’t easy to find it. I had to remove and insert nvidia_uvm module. So: > sudo rmmod nvidia_uvm > sudo modprobe nvidia_uvm That’s all. Just before these command collect_env.py reported “Is CUDA available: False”. After: “Is CUDA available: True”

How to print all columns in SQLAlchemy ORM

This is an old post, but I ran into a problem with the actual database column names not matching the mapped attribute names on the instance. We ended up going with this: from sqlalchemy import inspect inst = inspect(model) attr_names = [c_attr.key for c_attr in inst.mapper.column_attrs] Hope that helps somebody with the same problem!

How to drop a row whose particular column is empty/NaN?

Use dropna with parameter subset for specify column for check NaNs: data = data.dropna(subset=[‘sms’]) print (data) id city department sms category 1 2 lhr revenue good 1 Another solution with boolean indexing and notnull: data = data[data[‘sms’].notnull()] print (data) id city department sms category 1 2 lhr revenue good 1 Alternative with query: print (data.query(“sms … Read more

Why list comprehensions create a function internally?

The main logic of creating a function is to isolate the comprehension’s iteration variablepeps.python.org. By creating a function: Comprehension iteration variables remain isolated and don’t overwrite a variable of the same name in the outer scope, nor are they visible after the comprehension However, this is inefficient at runtime. Due to this reason, python-3.12 implemented … Read more