numpy

timedelta64 and datetime conversion

懵懂的女人 提交于 2021-02-05 08:08:37
问题 I have two datetime (Timestamp) formatted columns in my dataframe, df['start'], df['end'] . I'd like to get the duration between the two dates. So I create the duration column df['duration'] = df['start'] - df['end'] However, now the duration column is formatted as numpy.timedelta64 , instead of datetime.timedelta as I would expect. >>> df['duration'][0] >>> numpy.timedelta64(0,'ns') While >>> df['start'][0] - df['end'][0] >>> datetime.timedelta(0) Can someone explain to me why the array

timedelta64 and datetime conversion

泄露秘密 提交于 2021-02-05 08:06:47
问题 I have two datetime (Timestamp) formatted columns in my dataframe, df['start'], df['end'] . I'd like to get the duration between the two dates. So I create the duration column df['duration'] = df['start'] - df['end'] However, now the duration column is formatted as numpy.timedelta64 , instead of datetime.timedelta as I would expect. >>> df['duration'][0] >>> numpy.timedelta64(0,'ns') While >>> df['start'][0] - df['end'][0] >>> datetime.timedelta(0) Can someone explain to me why the array

timedelta64 and datetime conversion

会有一股神秘感。 提交于 2021-02-05 08:05:30
问题 I have two datetime (Timestamp) formatted columns in my dataframe, df['start'], df['end'] . I'd like to get the duration between the two dates. So I create the duration column df['duration'] = df['start'] - df['end'] However, now the duration column is formatted as numpy.timedelta64 , instead of datetime.timedelta as I would expect. >>> df['duration'][0] >>> numpy.timedelta64(0,'ns') While >>> df['start'][0] - df['end'][0] >>> datetime.timedelta(0) Can someone explain to me why the array

timedelta64 and datetime conversion

流过昼夜 提交于 2021-02-05 08:04:01
问题 I have two datetime (Timestamp) formatted columns in my dataframe, df['start'], df['end'] . I'd like to get the duration between the two dates. So I create the duration column df['duration'] = df['start'] - df['end'] However, now the duration column is formatted as numpy.timedelta64 , instead of datetime.timedelta as I would expect. >>> df['duration'][0] >>> numpy.timedelta64(0,'ns') While >>> df['start'][0] - df['end'][0] >>> datetime.timedelta(0) Can someone explain to me why the array

Trying to play a sound wave on python using pygame

人盡茶涼 提交于 2021-02-05 07:56:27
问题 I'm following this example from the book 'math for programmers' but it doesn't work for me: import pygame, pygame.sndarray pygame.mixer.init(frequency=44100, size=-16, channels=1) import numpy as np arr = np.random.randint(-32768, 32767, size=44100) sound = pygame.sndarray.make_sound(arr) sound.play() It returns these errors: ... in make_sound return numpysnd.make_sound(array)" ... in make_sound return mixer.Sound(array=array) ValueError: Array must be 2-dimensionarl for stereo mixer" The

numpy - why Z[(0,2)] is view but Z[(0, 2), (0)] is copy?

两盒软妹~` 提交于 2021-02-05 07:51:53
问题 Question Why are the numpy tuple indexing behaviors inconsistent? Please explain the rational or design decision behind these behaviors. In my understanding, Z[(0,2)] and Z[(0, 2), (0)] are both tuple indexing and expected the consistent behavior for copy/view. If this is incorrect, please explain, import numpy as np Z = np.arange(36).reshape(3, 3, 4) print("Z is \n{}\n".format(Z)) b = Z[ (0,2) # Select Z[0][2] ] print("Tuple indexing Z[(0,2)] is \n{}\nIs view? {}\n".format( b, b.base is not

Create a frequency matrix for bigrams from a list of tuples, using numpy or pandas

穿精又带淫゛_ 提交于 2021-02-05 07:47:27
问题 I am very new to Python. I have a list of tuples, where I created bigrams. This question is pretty close to my needs my_list = [('we', 'consider'), ('what', 'to'), ('use', 'the'), ('words', 'of')] Now I am trying to convert this into a frequency matrix The desired output is consider of the to use we what words consider 0 0 0 0 0 0 0 0 of 0 0 0 0 0 0 0 0 the 0 0 0 0 0 0 0 0 to 0 0 0 0 0 0 0 0 use 0 0 1 0 0 0 0 0 we 1 0 0 0 0 0 0 0 what 0 0 0 1 0 0 0 0 words 0 1 0 0 0 0 0 0 How to do this,

Outer subtraction with Numpy

放肆的年华 提交于 2021-02-05 07:45:27
问题 I simply want to do: C_i=\Sum_k (A_i -B_k)^2 I saw that this calculation is faster with a simple for loop than with the numpy.subtract.outer ! Anyway I feel that numpy.einsum would be the fastest. I could not understand numpy.einsum that well. Can anyone please help me out? Additionally, it would be great if someone explains how a general summation expression consisting of vector/matrices can be written with numpy.einsum ? I did not find solution for this particular problem on the web. Sorry

squash all consecutive ones in a python list

自古美人都是妖i 提交于 2021-02-05 07:24:06
问题 I have variable lists filled with 0s and 1s , like: l1 = [1,1,1,0,1,1] l2 = [0,1,1,0,1,1,0,0,1] what is the most efficient way to create new lists that squash all consecutive 1s . So the result would be: l1_new = [1,0,1] l2_new = [0,1,0,1,0,0,1] Hint: numpy/vectorization or some logical operation would be great! 回答1: Here's one approach using with np.diff and bitwise operations : l1 = [1,1,1,0,1,1] l2 = [0,1,1,0,1,1,0,0,1] a = np.array(l2) a[~((np.diff(a,prepend=False)==0) & (a==1))] # array(

Scalable solution for dot product of two vectors

蹲街弑〆低调 提交于 2021-02-05 06:55:27
问题 The dot product of two vectors can be computed via numpy.dot. Now I want to compute the dot product of an array of vectors: >>> numpy.arange(15).reshape((5, 3)) array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11], [12, 13, 14]]) The vectors are row vectors and the output should be a 1d-array containing the results from the dot products: array([ 5, 50, 149, 302, 509]) For the cross product (numpy.cross) this can be easily achieved specifying the axis keyword. However numpy.dot doesn't have