numpy

pandas outer product of two dataframes with same index

萝らか妹 提交于 2021-02-18 07:40:47
问题 Consider the following dataframes d1 and d1 d1 = pd.DataFrame([ [1, 2, 3], [2, 3, 4], [3, 4, 5], [1, 2, 3], [2, 3, 4], [3, 4, 5] ], columns=list('ABC')) d2 = pd.get_dummies(list('XYZZXY')) d1 A B C 0 1 2 3 1 2 3 4 2 3 4 5 3 1 2 3 4 2 3 4 5 3 4 5 d2 X Y Z 0 1 0 0 1 0 1 0 2 0 0 1 3 0 0 1 4 1 0 0 5 0 1 0 I need to get a new dataframe with a multi-index columns object that has the product of every combination of columns from d1 and d2 So far I've done this... from itertools import product pd

numpy stride_tricks.as_strided vs list comprehension for rolling window

妖精的绣舞 提交于 2021-02-18 07:26:11
问题 When dealing with rolling windows, I wrote my functions in the way like list comprehension [np.std(x[i:i+framesize]) for i in range(0, len(x)-framesize, hopsize)])] Recently I discovered numpy.lib.stride_tricks.as_strided and found it is used widely for rolling windows (for example, this post), even though it is a "hidden" function. In this issue concerning why stride_tricks.as_strided is undocumented, it's mentioned that Intentionally! It's dangerous! It was just low-level plumbing to help

Detecting start and end point of line in image (numpy array)

佐手、 提交于 2021-02-18 07:07:01
问题 I have an image like the following: What I would like is to get the coordinates of the start and end point of each segment. Actually what I thought was to consider the fact that each extreme point should have just one point belonging to the segment in its neighborhood, while all other point should have at least 2. Unfortunately the line does not have thickness equal to one pixel so this reasoning does not hold. 回答1: Here's a fairly simple way to do it: load the image and discard the

Detecting start and end point of line in image (numpy array)

守給你的承諾、 提交于 2021-02-18 07:04:13
问题 I have an image like the following: What I would like is to get the coordinates of the start and end point of each segment. Actually what I thought was to consider the fact that each extreme point should have just one point belonging to the segment in its neighborhood, while all other point should have at least 2. Unfortunately the line does not have thickness equal to one pixel so this reasoning does not hold. 回答1: Here's a fairly simple way to do it: load the image and discard the

np.argsort which excludes zero values

徘徊边缘 提交于 2021-02-18 06:47:21
问题 I have an array [0.2,0,0,0,0.3,0,0,0,0.4] . I'm using np.argsort to sort values and get that indexes. So, for my example, it will be something like [1,5,9,2,3,4,6...] . However, I would like to get array of indexes only for non zero values . In my example only [1,5,9] . How do I implement it in python with pandas and numpy ? 回答1: Using np.nonzero and indexing trick def sparse_argsort(arr): indices = np.nonzero(arr)[0] return indices[np.argsort(arr[indices])] sparse_argsort(a) array([0, 4, 8])

Fast Interpolation / Resample of Numpy Array - Python

冷暖自知 提交于 2021-02-18 05:25:09
问题 Currently, I have written some Python code that is inserted into a pipeline. The incoming data comes in in a numpy array of shape (1,512,19,25). I use the scipy.ndimage.interpolation.zoom to bring the array up to shape (1,512,38,50). This can be accomplished with one call to the function. Basically, it resizes each (19,25) piece to size (38,50). Later in the code, when the data is moving the other way, different data is again resized the in the other direction (38,50) to (19,25). Everything

Interpreting (and comparing) output from numpy.correlate

自闭症网瘾萝莉.ら 提交于 2021-02-18 03:04:58
问题 I have looked at this question but it hasn't really given me any answers. Essentially, how can I determine if a strong correlation exists or not using np.correlate ? I expect the same output as I get from matlab's xcorr with the coeff option which I can understand (1 is a strong correlation at lag l and 0 is no correlation at lag l ), but np.correlate produces values greater than 1, even when the input vectors have been normalised between 0 and 1. Example input import numpy as np x = np

Interpreting (and comparing) output from numpy.correlate

只愿长相守 提交于 2021-02-18 03:04:50
问题 I have looked at this question but it hasn't really given me any answers. Essentially, how can I determine if a strong correlation exists or not using np.correlate ? I expect the same output as I get from matlab's xcorr with the coeff option which I can understand (1 is a strong correlation at lag l and 0 is no correlation at lag l ), but np.correlate produces values greater than 1, even when the input vectors have been normalised between 0 and 1. Example input import numpy as np x = np

Print predict ValueError: Expected 2D array, got 1D array instead

ぃ、小莉子 提交于 2021-02-17 07:19:29
问题 The error shows in my last two codes. ValueError: Expected 2D array, got 1D array instead: array=[0 1]. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample. import numpy as np import pandas as pd from sklearn.model_selection import ShuffleSplit %matplotlib inline df = pd.read_csv('.......csv') df.drop(['Company'], 1, inplace=True) x = pd.DataFrame(df.drop(['R&D Expense'],1)) y = pd.DataFrame(df['R&D

Numpy: how to select items in numpy and assign its value

泄露秘密 提交于 2021-02-17 07:10:12
问题 I have got a problem in assigning new value by list. I want to change 12 items values in s numpy by numpy array's index ,and i hope every index i choose is different. so i made a list random.sample(range(0,len(s),12) to select 12 different index.And through this index change some of values in numpy array s() However, I'm getting the error: SyntaxError: can't assign to function call import numpy as np import random N = 20 s = np.zeros([N]) alist = random.sample(range(0,20),12) alist for i in