numpy

Find the business days between two columns in a pandas dataframe, which contain NaTs

纵饮孤独 提交于 2021-02-08 08:27:19
问题 I have 2 columns in my pandas data frame, and I want to calculate the business dates between them. Data: ID On hold Off Hold 101 09/15/2017 09/16/2017 102 NA NA 103 09/22/2017 09/26/2017 104 10/12/2017 10/30/2017 105 NA NA 106 08/05/2017 08/06/2017 107 08/08/2017 08/03/2017 108 NA NA I tried the below code using busday_count from numpy: df1['On hold'] = pd.to_datetime(df1['On hold']) df1['Off Hold'] = pd.to_datetime(df1['Off Hold']) np.busday_count(df1['On hold'].values.astype('datetime64[D]'

Looping through n x n matrix with a smaller matrix of certain size with numpy arrays

北慕城南 提交于 2021-02-08 08:23:39
问题 I'm currently having the following issue, given an array let's say for simplicity a 4 x 4 array (I'm actually working with 512 x 512 ) X = np.array([[3, 5, 2, 4], [7, 6, 8, 8], [1, 6, 7, 7], [2, 1, 3, 4]]) I would like to loop/slide around the array in a way that I can save new arrays in the form np.array([3,5],[7,6]), np.array([2,4], [8,8]), np.array([1,6],[2,1]), np.array ([7,7],[1,4]) and so on (Ideally that I could choose the step and the size of my "sliding" window). Also I would like to

Reverse a 2D NumPy array with multiple slice objects

女生的网名这么多〃 提交于 2021-02-08 08:20:28
问题 Problem I have a 2D NumPy array, arr , and for each row, I would like to reverse a section of the array. Crucially, for each row, the start and stop indices must be unique. I can achieve this using the following. import numpy as np arr = np.repeat(np.arange(10)[np.newaxis, :], 3, axis=0) reverse = np.sort(np.random.choice(arr.shape[1], [arr.shape[0], 2], False)) # arr # array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]) # reverse #

Reverse a 2D NumPy array with multiple slice objects

有些话、适合烂在心里 提交于 2021-02-08 08:19:57
问题 Problem I have a 2D NumPy array, arr , and for each row, I would like to reverse a section of the array. Crucially, for each row, the start and stop indices must be unique. I can achieve this using the following. import numpy as np arr = np.repeat(np.arange(10)[np.newaxis, :], 3, axis=0) reverse = np.sort(np.random.choice(arr.shape[1], [arr.shape[0], 2], False)) # arr # array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]) # reverse #

Vectorizing operation on numpy array

纵饮孤独 提交于 2021-02-08 08:13:41
问题 I have a numpy array containing many three-dimensional numpy arrays, where each of these sub-elements is a grayscale image. I want to use numpy's vectorize to apply an affine transformation to each image in the array. Here is a minimal example that reproduces the issue: import cv2 import numpy as np from functools import partial # create four blank images data = np.zeros((4, 1, 96, 96), dtype=np.uint8) M = np.array([[1, 0, 0], [0, 1, 0]], dtype=np.float32) # dummy affine transformation matrix

Mapping rows of a Pandas dataframe to numpy array

柔情痞子 提交于 2021-02-08 08:00:46
问题 Sorry, I know there are so many questions relating to indexing, and it's probably starring me in the face, but I'm having a little trouble with this. I am familiar with .loc , .iloc , and .index methods and slicing in general. The method .reset_index may not have been (and may not be able to be) called on our dataframe and therefore index lables may not be in order. The dataframe and numpy array(s) are actually different length subsets of the dataframe, but for this example I'll keep them the

One end clamped and other end free cubic spline using scipy.interpolate.splprep and splev

旧时模样 提交于 2021-02-08 07:25:44
问题 I have the following data: x_old = [ 0.00000000e+00, -5.96880765e-24, -8.04361605e-23, -2.11167774e-22, -2.30386081e-22, -7.86854147e-23, 1.17548440e-22, 1.93009272e-22, 1.49906866e-22, 9.66877465e-23, 1.48495705e-23] y_old = [ 0. , 0.03711505, 0.03780602, 0.02524459, 0.01349815, 0.00964215, 0.00972842, 0.0168793 , 0.02577024, 0.02761626, 0.02141961] z_old = [ 0. , 0.29834302, 0.59805918, 0.89773519, 1.19755092, 1.49749325, 1.79750314, 2.09741402, 2.39727031, 2.69726787, 2.99719479] I want to

One end clamped and other end free cubic spline using scipy.interpolate.splprep and splev

会有一股神秘感。 提交于 2021-02-08 07:25:30
问题 I have the following data: x_old = [ 0.00000000e+00, -5.96880765e-24, -8.04361605e-23, -2.11167774e-22, -2.30386081e-22, -7.86854147e-23, 1.17548440e-22, 1.93009272e-22, 1.49906866e-22, 9.66877465e-23, 1.48495705e-23] y_old = [ 0. , 0.03711505, 0.03780602, 0.02524459, 0.01349815, 0.00964215, 0.00972842, 0.0168793 , 0.02577024, 0.02761626, 0.02141961] z_old = [ 0. , 0.29834302, 0.59805918, 0.89773519, 1.19755092, 1.49749325, 1.79750314, 2.09741402, 2.39727031, 2.69726787, 2.99719479] I want to

Using Pandas to calculate distance between coordinates from imported csv

耗尽温柔 提交于 2021-02-08 07:21:52
问题 I am trying to import a .csv that contains two columns of location data (lat/long), compute the distance between points, write the distance to a new column, loop the function to the next set of coordinates, and write the output data frame to a new .csv. I have the following code written and it import pandas as pd import numpy as np pd.read_csv("input.csv") def dist_from_coordinates(lat1, lon1, lat2, lon2): R = 6371 # Earth radius in km #conversion to radians d_lat = np.radians(lat2-lat1) d

Best way to count Greater Than in numpy 2d array

爷,独闯天下 提交于 2021-02-08 07:01:22
问题 results is 2d numpy array with size 300000 for i in range(np.size(results,0)): if results[i][0]>=0.7: count+=1 it takes me 0.7 second in this python code,but I run this in C++ code,it takes less than 0.07 second. So how to make this python code as fast as possible? 回答1: When doing numerical computation for speed, especially in Python, you never want to use for loops if possible. Numpy is optimized for "vectorized" computation, so you want to pass off the work you'd typically do in for loops