numpy

Strange issue when storing FFT periods in Pandas dataframe

白昼怎懂夜的黑 提交于 2021-02-10 19:57:51
问题 I am trying to store the results of FFT calculations in a Pandas data frame: ft = pd.DataFrame(index=range(90)) ft['y'] = ft.index.map(lambda x: np.sin(2*x)) ft['spectrum'] = np.fft.fft(ft['y']) ft['freq'] = np.fft.fftfreq(len(ft.index)).real ft['T'] = ft['freq'].apply(lambda f: 1/f if f != 0 else 0) Everything seems to be working fine until the last line: the column T which is supposed to store periods has for some reason all the columns of the frame, ie.: In [499]: ft.T[0] Out[499]: y 0j

Strange issue when storing FFT periods in Pandas dataframe

家住魔仙堡 提交于 2021-02-10 19:57:33
问题 I am trying to store the results of FFT calculations in a Pandas data frame: ft = pd.DataFrame(index=range(90)) ft['y'] = ft.index.map(lambda x: np.sin(2*x)) ft['spectrum'] = np.fft.fft(ft['y']) ft['freq'] = np.fft.fftfreq(len(ft.index)).real ft['T'] = ft['freq'].apply(lambda f: 1/f if f != 0 else 0) Everything seems to be working fine until the last line: the column T which is supposed to store periods has for some reason all the columns of the frame, ie.: In [499]: ft.T[0] Out[499]: y 0j

Numpy : difference between np.repeat and np.broadcast_to

ⅰ亾dé卋堺 提交于 2021-02-10 19:53:52
问题 1° Why is the following code returning False ? I thought that np.broadcast_to will increase the dimensin of the array the same way np.repeat does. 2° Can i reproduce the result given by np.repeat with np.broadcast_to ? import numpy as np n = 100 d = 10 A = np.random.uniform(size=(n,d)) np.all(np.broadcast_to(A.reshape(n,1,d),(n,d-1,d))==np.repeat(A,d-1).reshape(n,d-1,d)) 3° More generaly, for a given aray A of shape (n,d), how can i reproduce np.repeat(A,k).reshape((n,k,d)) with np.broadcast

Numpy : difference between np.repeat and np.broadcast_to

南楼画角 提交于 2021-02-10 19:53:17
问题 1° Why is the following code returning False ? I thought that np.broadcast_to will increase the dimensin of the array the same way np.repeat does. 2° Can i reproduce the result given by np.repeat with np.broadcast_to ? import numpy as np n = 100 d = 10 A = np.random.uniform(size=(n,d)) np.all(np.broadcast_to(A.reshape(n,1,d),(n,d-1,d))==np.repeat(A,d-1).reshape(n,d-1,d)) 3° More generaly, for a given aray A of shape (n,d), how can i reproduce np.repeat(A,k).reshape((n,k,d)) with np.broadcast

Numpy array - stack multiple columns into one using reshape

走远了吗. 提交于 2021-02-10 18:54:30
问题 For a 2D array like this: table = np.array([[11,12,13],[21,22,23],[31,32,33],[41,42,43]]) Is it possible to use np.reshape on table to get an array single_column where each column of table is stacked vertically? This can be accomplished by splitting table and combining with vstack . single_column = np.vstack(np.hsplit(table , table .shape[1])) Reshape can combine all the rows into a single row, I'm wondering if it can combine the columns as well to make the code cleaner and possibly faster.

Numpy array - stack multiple columns into one using reshape

旧时模样 提交于 2021-02-10 18:53:35
问题 For a 2D array like this: table = np.array([[11,12,13],[21,22,23],[31,32,33],[41,42,43]]) Is it possible to use np.reshape on table to get an array single_column where each column of table is stacked vertically? This can be accomplished by splitting table and combining with vstack . single_column = np.vstack(np.hsplit(table , table .shape[1])) Reshape can combine all the rows into a single row, I'm wondering if it can combine the columns as well to make the code cleaner and possibly faster.

Fixing no return function for numpy.nextafter

别说谁变了你拦得住时间么 提交于 2021-02-10 18:48:39
问题 I'm trying to find the next floating point value after a towards a + 1 . My code basically looks like this: if a == 0: a = numpy.nextafter(a,a+1) I expect to get a new a that's the next floating point value, as stated above, but all I get is "Assigning result of a function call, where the function has no return (assignment-from-no-return)". I've double checked my versions of NumPy and Python, and cannot figure out what's wrong. 来源: https://stackoverflow.com/questions/54548783/fixing-no-return

Fixing no return function for numpy.nextafter

冷暖自知 提交于 2021-02-10 18:48:36
问题 I'm trying to find the next floating point value after a towards a + 1 . My code basically looks like this: if a == 0: a = numpy.nextafter(a,a+1) I expect to get a new a that's the next floating point value, as stated above, but all I get is "Assigning result of a function call, where the function has no return (assignment-from-no-return)". I've double checked my versions of NumPy and Python, and cannot figure out what's wrong. 来源: https://stackoverflow.com/questions/54548783/fixing-no-return

random.randint()与np.random.randint()的区别

点点圈 提交于 2021-02-10 17:52:52
一.比较两个函数 先来看看random.randint() import random for n in range(5 ): for i in range(10 ): print (random.randint(1,5),end= ' ' ) print () # 运行结果 1 5 5 3 3 1 3 1 5 2 4 4 4 4 4 4 3 1 5 2 3 2 3 1 1 5 5 1 4 3 3 4 4 2 5 5 3 4 4 4 3 5 4 5 4 5 4 5 2 4 Process finished with exit code 0 再来看看numpy.random.randint()方法: import numpy as np for n in range(5 ): for i in range(10 ): print (np.random.randint(1, 5), end= ' ' ) print () # 运行结果 2 4 1 1 1 1 2 2 2 4 3 4 3 2 3 4 3 2 2 4 2 2 1 2 1 1 3 3 3 4 4 1 4 2 4 1 3 4 3 2 2 3 3 2 3 4 4 3 4 4 Process finished with exit code 0 看出有什么不同了吗? random.randint()方法里面的取值区间是前闭后闭区间

Merge rows together who have the same value in a column

大兔子大兔子 提交于 2021-02-10 17:44:30
问题 I have a CSV file like this (which parsed by using the pandas by using read_csv): Filename f1 f2 f3 1.jpg 1 0.2 0.3 1.jpg 0 0.8 0.7 2.jpg 1 0.3 0.2 How would I use this dataset and change it into a numpy array which will look like this: [ [[1,0.2,0.3],[0,0.8.0.7]], [[1,0.3,0.2]] ] 回答1: You can create nested lists by GroupBy.apply with lambda function, DataFrame.set_index is for avoid convert column Filename to lists: df = pd.read_csv(file) L = (df.set_index('Filename') .groupby('Filename')