numpy

How to convert array into special items of structured array and revert it back?

 ̄綄美尐妖づ 提交于 2021-01-29 07:11:50
问题 I want to perform some numpy methods on items of structured array instead of numbers. So, for example, while working with array of integers of shape (4, 3), I need to convert it to array of items of length 3 and perform some operations as it were a single one dimensional array of shape (4,). These conversions itself, unfortunately, looks really complicated for me. Let's take another example: n, m, r = 2, 3, 4 array = np.arange(n*m).reshape((n,m)) dt = np.dtype(','.join('i'*m)) arr1 = np.array

Ideas for manual copy-paste from excel to python code in spyder

不羁的心 提交于 2021-01-29 07:11:35
问题 I often work with excel sheets of which I want to copy just one specific column of numbers into my python script for plotting purposes. This has to be done manually since it is always a different file, columns and rows. To use numpy arrays, I need the data with a trailing comma to create a python array in this way (I had to add spaces, otherwise stackexchange would post it in a line): myArray=np.array([ 1, 2, 3, 4, 6 )] So after copying the column with the numbers from excel, I have to add

Is there a numpy function like np.fill(), but for arrays as fill value?

情到浓时终转凉″ 提交于 2021-01-29 07:11:28
问题 I'm trying to build an array of some given shape in which all elements are given by another array. Is there a function in numpy which does that efficiently, similar to np.full() , or any other elegant way, without simply employing for loops? Example: Let's say I want an array with shape (dim1,dim2) filled with a given, constant scalar value. Numpy has np.full() for this: my_array = np.full((dim1,dim2),value) I'm looking for an analog way of doing this, but I want the array to be filled with

MemoryError: Unable to allocate 8.27 GiB for an array with shape (323313, 3435) and data type float64

前提是你 提交于 2021-01-29 07:09:35
问题 I have extension(example .exe,.py,.xml,.doc etc) table in my dataframe. after running on terminal I am getting above error on large data set. encoder = OneHotEncoder(handle_unknown='ignore') encoder.fit(features['Extension'].values.reshape(-1, 1)) temp = encoder.transform(features['Extension'].values.reshape(-1, 1)).toarray() #GETTING ERROR on this print("Size of array in bytes",getsizeof(temp)) print("Array :-",temp) print("Shape :- ",features.shape, temp.shape) features.drop(columns=[

What is the use of the data type intp in numpy?

蹲街弑〆低调 提交于 2021-01-29 07:08:45
问题 I've seen the data types in the numpy package of the python, but I found the description of the data type intp a bit confusing, its description is like: intp: Integer used for indexing (same as C ssize_t; normally either int32 or int64) My question is why the word indexing is used here if it is same as the ssize_t of C, as ssize_t is use to store the size of a variable? 来源: https://stackoverflow.com/questions/63584359/what-is-the-use-of-the-data-type-intp-in-numpy

how to find the template matching accuracy

别来无恙 提交于 2021-01-29 06:55:50
问题 i am doing a template matching now,what i want to do is find the accuracy of template matching I have done template matching, but how do i get the accuracy i think i have to subtract the matched region and template image. how do i achieve this CODE import cv2 as cv import numpy as np import matplotlib.pyplot as plt img = cv.imread('image.jpg',0) img1 = img.copy() template = cv.imread('template.jpg',0) w, h = template.shape[::-1] method = ['cv.TM_CCOEFF_NORMED','cv.TM_CCORR_NORMED'] for meth

How to create 'billiard ball' reflection boundary condition in python?

岁酱吖の 提交于 2021-01-29 06:51:13
问题 According to Erwin Schrodinger (in What is Life?), diffusion can be explained entirely by the random motion of particles. I want to test this myself by create a program the creates a time-step visualization of the diffusion of "gas molecules" in a closed container. The initial conditions would have two partitions, one with low and one with high concentration. After t0 the partition is removed and the gas is allowed to diffuse. The only mechanism I want to use is adding displacement random

For loop using np.where

余生长醉 提交于 2021-01-29 06:50:34
问题 I'm trying to create a new column in a dataframe that labels animals that are domesticated with a 1. I'm using a for loop, but for some reason, the loop only picks up the last item in the pets list. dog , cat , and gerbil should all be assigned a 1 under the domesticated column. Anyone have a fix for this or a better approach? df = pd.DataFrame( {'creature': ['dog', 'cat', 'gerbil', 'mouse', 'donkey'] }) pets = ['dog', 'cat', 'gerbil'] for pet in pets: df['domesticated'] = np.where(df[

numpy genfromtxt - how to detect bad int input values

跟風遠走 提交于 2021-01-29 06:49:30
问题 Here is a trivial example of a bad int value to numpy.genfromtxt . For some reason, I can't detect this bad value, as it's showing up as a valid int of -1. >>> bad = '''a,b 0,BAD 1,2 3,4'''.splitlines() My input here has 2 columns of ints, named a and b. b has a bad value, where we have a string "BAD" instead of an integer. However, when I call genfromtxt , I cannot detect this bad value. >>> out = np.genfromtxt(bad, delimiter=',', dtype=(numpy.dtype('int64'), numpy.dtype('int64')), names

RGB to HSV in numpy

偶尔善良 提交于 2021-01-29 06:44:51
问题 I'm trying to implement RGB to HSV conversion from opencv in pure numpy using formula from here: def rgb2hsv_opencv(img_rgb): img_hsv = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2HSV) return img_hsv def rgb2hsv_np(img_rgb): assert img_rgb.dtype == np.float32 height, width, c = img_rgb.shape r, g, b = img_rgb[:,:,0], img_rgb[:,:,1], img_rgb[:,:,2] t = np.min(img_rgb, axis=-1) v = np.max(img_rgb, axis=-1) s = (v - t) / (v + 1e-6) s[v==0] = 0 # v==r hr = 60 * (g - b) / (v - t + 1e-6) # v==g hg = 120 +