numpy

Cube root of negative numbers in a numpy array returns nan

左心房为你撑大大i 提交于 2021-02-10 22:48:10
问题 From Numpy documentation: >>> a = np.arange(10)**3 >>> a array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729]) >>> a[2] 8 >>> a[2:5] array([ 8, 27, 64]) >>> a[:6:2] = -1000 # equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000 >>> a array([-1000, 1, -1000, 27, -1000, 125, 216, 343, 512, 729]) >>> a[ : :-1] # reversed a array([ 729, 512, 343, 216, 125, -1000, 27, -1000, 1, -1000]) >>> for i in a: ... print(i**(1/3.)) ... nan 1.0 nan 3.0 nan 5.0 6.0

Cube root of negative numbers in a numpy array returns nan

旧时模样 提交于 2021-02-10 22:47:12
问题 From Numpy documentation: >>> a = np.arange(10)**3 >>> a array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729]) >>> a[2] 8 >>> a[2:5] array([ 8, 27, 64]) >>> a[:6:2] = -1000 # equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000 >>> a array([-1000, 1, -1000, 27, -1000, 125, 216, 343, 512, 729]) >>> a[ : :-1] # reversed a array([ 729, 512, 343, 216, 125, -1000, 27, -1000, 1, -1000]) >>> for i in a: ... print(i**(1/3.)) ... nan 1.0 nan 3.0 nan 5.0 6.0

Efficient way to assign values into random blocks of an array of images?

蓝咒 提交于 2021-02-10 20:50:53
问题 I have a 3d numpy array. It's an array of 2d square images, all of the same size. my task is to block out a random square patch of the images (set all the pixel values to 0). I can figure out how to do it in the case where i just have 1 image as follows x = np.random.randint(image_width - size) y = np.random.randint(image_width - size) image[x:x + size, y:y + size] = 0 where size is the size of the blocked out area. I'm not sure how to efficiently do this technique to an array of 2d images,

Efficient way to assign values into random blocks of an array of images?

安稳与你 提交于 2021-02-10 20:48:55
问题 I have a 3d numpy array. It's an array of 2d square images, all of the same size. my task is to block out a random square patch of the images (set all the pixel values to 0). I can figure out how to do it in the case where i just have 1 image as follows x = np.random.randint(image_width - size) y = np.random.randint(image_width - size) image[x:x + size, y:y + size] = 0 where size is the size of the blocked out area. I'm not sure how to efficiently do this technique to an array of 2d images,

using allocatable arrays from modules in f2py

纵然是瞬间 提交于 2021-02-10 20:47:57
问题 I'm having issues with allocatable arrays in f2py . In the code below (stored in mymod.f90 ), I created two modules, vars and worker : vars stores and allocates the array b worker contains subroutines to work with this array from vars . The first worker -subroutine adds a scalar to b . This works as expected. The problem is with the next routine that should add a vector of matching first dimension to the array b . The implemented subroutine add_vector2 works, but needs the dimension of the

using allocatable arrays from modules in f2py

感情迁移 提交于 2021-02-10 20:43:36
问题 I'm having issues with allocatable arrays in f2py . In the code below (stored in mymod.f90 ), I created two modules, vars and worker : vars stores and allocates the array b worker contains subroutines to work with this array from vars . The first worker -subroutine adds a scalar to b . This works as expected. The problem is with the next routine that should add a vector of matching first dimension to the array b . The implemented subroutine add_vector2 works, but needs the dimension of the

TypeError: only size-1 arrays can be converted to Python scalars arrives during a simple program

[亡魂溺海] 提交于 2021-02-10 20:38:52
问题 When I tried this code, from math import exp import numpy as np w1=2 b1=0.5 b2=0.75 X=[[0, 1, 1, 1], [1, 1, 1, 1]] y=(np.dot(w1,X)-b1) tanh=np.vectorize((1-exp(-2*y))/(1+exp(-2*y))) y_out=1/(1+np.exp(-tanh))-b2 print(y_out) I got this error: TypeError: only size-1 arrays can be converted to Python scalars.. Where am I making a mistake? 回答1: In [269]: import math In [270]: w1=2 ...: b1=0.5 ...: b2=0.75 ...: X=[[0, 1, 1, 1], [1, 1, 1, 1]] ...: y=(np.dot(w1,X)-b1) In [271]: X Out[271]: [[0, 1, 1

TypeError: only size-1 arrays can be converted to Python scalars arrives during a simple program

孤街醉人 提交于 2021-02-10 20:31:05
问题 When I tried this code, from math import exp import numpy as np w1=2 b1=0.5 b2=0.75 X=[[0, 1, 1, 1], [1, 1, 1, 1]] y=(np.dot(w1,X)-b1) tanh=np.vectorize((1-exp(-2*y))/(1+exp(-2*y))) y_out=1/(1+np.exp(-tanh))-b2 print(y_out) I got this error: TypeError: only size-1 arrays can be converted to Python scalars.. Where am I making a mistake? 回答1: In [269]: import math In [270]: w1=2 ...: b1=0.5 ...: b2=0.75 ...: X=[[0, 1, 1, 1], [1, 1, 1, 1]] ...: y=(np.dot(w1,X)-b1) In [271]: X Out[271]: [[0, 1, 1

numpy.float128 doesn't exist in windows, but is called from OpenGL

青春壹個敷衍的年華 提交于 2021-02-10 20:22:49
问题 I decided to try using OpenGL VBO in Python to improve FPS. I found code, that worked perfectly fine in Linux OS (Ubuntu), but when I tried launching in Windows OS, the code resulted in a message: "GLUT Display callback with (),{} failed: returning None module 'numpy' has no attribute 'float128'" So, I can't run the code specifically on Windows, but because I want to create a cross-platform application, I really need to solve this. I've done a lot of research and only found that numpy

How to get N random integer numbers whose sum is equal to M

依然范特西╮ 提交于 2021-02-10 20:21:58
问题 I want to make a list of N random INTEGER numbers whose sum is equal to M number. I have used numpy and dirichlet function in Python, but this generate double random number array, I would like to generate integer random number. import numpy as np np.random.dirichlet(np.ones(n))*m The solution can use other distribution the sense is resolve the problem. 回答1: The problem with using dirichlet for this is that it is a distribution over real numbers. It will yield a vector of numbers in the range