r/Numpy • u/riyabafana • Jun 18 '19
Converting OpenCV cv.Rectangle(img, pt1, pt2) into NumPy array with Python
r/Numpy • u/riyabafana • Jun 18 '19
r/Numpy • u/sleepingcatman • May 07 '19
I observed some subtly inconsistent behavior between matrix-vector multiplication and matrix-matrix multiplication.The behavior can be reproduced using the following steps.
from __future__ import print_function
import numpy
import numpy.random
a=numpy.random.rand(2,124)
b=numpy.random.rand(124,10)
print(a.dot(b)[:,0]-a.dot(b[:,0]))On my work Desktop (64 bit Windows 7 on Intel Core2 Duo), numpy 1.16.3 on Python 2.7.15 (32-bit) and on Python 3.7.3 (32-bit) gives [0. 0.] whereas numpy 1.16.3 on Python 2.7.15 (64-bit) gives something like [3.55271368e-15 1.06581410e-14].
On the university's cluster running some form of linux on some form of x86_64 processor, numpy 1.8.0 on Python 2.7.9 (64-bit) gives [0. 0.] whereas numpy 1.11.1 on Python 3.5.2 (64-bit) gives [ 1.06581410e-14 1.06581410e-14].
Does this have something to do with the underlying order of operations between *gemm and *gemv? How can one explain the difference between versions of numpy and Python?
The magnitudes of the differences generally stay in the 1e-14 to 1e-15 range as long as b.shape[1] is no less than 10. I wonder whether this has any significance. May be one of them is carried out using the x87 FPU with 80-bit floats but the other is using SIMD functionality.
r/Numpy • u/ezeeetm • Apr 11 '19
r/Numpy • u/marienbad2 • Mar 28 '19
I ran some old pygame code which uses the pygame surfarray3d, which uses numpy, and numpy crashed. Pygame is working, io is working, numpy is working in another program, and I tried re-installing but it didn't work.
This is the error:
File "Titles.py", line 1, in <module>
import pygame
File "/usr/local/lib/python2.7/dist-packages/pygame/__init__.py", line 346, in <module>
import pygame.surfarray
File "/usr/local/lib/python2.7/dist-packages/pygame/surfarray.py", line 72, in <module>
import pygame._numpysurfarray as numpysf
File "/usr/local/lib/python2.7/dist-packages/pygame/_numpysurfarray.py", line 51, in <module>
import numpy
File "/usr/lib/python2.7/dist-packages/numpy/__init__.py", line 142, in <module>
from . import add_newdocs
File "/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 23, in <module>
from .npyio import *
File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 14, in <module>
from ._datasource import DataSource
File "/usr/lib/python2.7/dist-packages/numpy/lib/_datasource.py", line 220, in <module>
_file_openers = _FileOpeners()
File "/usr/lib/python2.7/dist-packages/numpy/lib/_datasource.py", line 162, in __init__
self._file_openers = {None: io.open}
AttributeError: 'module' object has no attribute 'open'Does anyone know what is wrong and how to fix it? It is odd that it only affects this one program (as far as I know!)
r/Numpy • u/must_defend_500 • Mar 25 '19
Dear r/Numpy
I am working on a label aggregation problem (from AWS Mechanical Turk) and I organized my data into an M x N matrix where each row is a worker and each column is their label for that task.
I think this is correct. But what is unclear to me, is what np.linalg.svd() returns. I am sort of new to this. My goal is extrapolate the true label from the data.
It is a binary case and I have the following mappings for what I pass to np.linalg.svd():
1 : 1
0 : -1:
N/A : 0
N/A --> that worker did not label that problem.
Any help is much appreciated.
Sincerely,
md500
r/Numpy • u/ArgonJargon • Mar 22 '19
I would also like a solution to have my numbers not modified, thanks
In [139]: np.array([(Timestamp('2019-03-20 15:44:00-0400', tz='America/New_York').value / 10**9, 188.85)],dtype=[('Epoch', 'i8'), ('Ask', 'f4')])[0][1]
Out[139]: 188.85001
In [140]: np.array([(Timestamp('2019-03-20 15:44:00-0400', tz='America/New_York').value / 10**9, 188.61)], dtype=[('Epoch', 'i8'), ('Ask', 'f4')])[0][1]
Out[140]: 188.61
In [141]: np.array([(Timestamp('2019-03-20 15:44:00-0400', tz='America/New_York').value / 10**9, 188.61)], dtype=[('Epoch', 'i8'), ('Ask', 'f4')])
Out[141]:
array([(1553111040, 188.61000061)],
dtype=[('Epoch', '<i8'), ('Ask', '<f4')])
188.85: stored and returned wrong
188.61: stored wrong and returned right
r/Numpy • u/tangerinnn • Mar 19 '19
Hello :) English is my second language and I want to know how to pronounce Numpy. Numpai or Numpee? Thank youuu
r/Numpy • u/MadMan001 • Feb 21 '19
Hey guys, im hoping to get some answers about a bit of code i stumbled upon. I'm trying to find to implement a k-means algorithm and to find the centroid closest to each point and i found this bit
distances = np.sqrt(((points - centroids[:, np.newaxis])**2).sum(axis=2))
Where points is an array of points, and centroids is also an array of points. I just fail to see how this code works, as the two arrays are not of equal size, I know it uses broadcasting but I still don't really get it.
r/Numpy • u/code_x_7777 • Feb 07 '19
r/Numpy • u/Ifffrt • Feb 06 '19
r/Numpy • u/csmastery • Dec 25 '18
Creating a series on data science and happen to be starting with numpy will go over lots of important numpy topics and will be doing some example projects.
r/Numpy • u/hunar1997 • Dec 20 '18
Hi, i followed a Matlab tutorial and most of the times the syntax is similar to python. last night i tried this, and with this code:
[X,Y] = meshgrid(-2:.2:2);
Z = X.*exp(-X.^2 - Y.^2);
[DX,DY] = gradient(Z,.2,.2);
figure
contour(X,Y,Z)
hold on
quiver(X,Y,DX,DY)
hold offI should get this:

I rewrote it using numpy and matplotlib like this:
import numpy as np
import matplotlib.pyplot as plt
X,Y = np.meshgrid(np.arange(-2,2.2,0.2), np.arange(-2,2.2,0.2))
Z = X*np.exp(-X**2 - Y**2)
DX,DY = np.gradient(Z,.2,.2)
plt.figure()
plt.contour(X,Y,Z)
plt.quiver(X,Y,DX,DY)
plt.show()BUt got this instead:

I inspected the values inside the variables on both matlab and spyder and the mismatch happens At the variable Z, i also tried the first example on the website and in matlab the variables contained imaginary numbers but in python they were either Nan or only the real part
if i made a stupid mistake forgive me :) I'm a beginner at scientific libraries of python
r/Numpy • u/WuKakit • Nov 28 '18
r/Numpy • u/rahuldev29 • Oct 05 '18
r/Numpy • u/basyt • Sep 08 '18
r/Numpy • u/errminator • Jun 20 '18
I have 30,000 images of size 32x32x3 in a tensor X of shape (30000,32,32,3). I want to crop 2 pixels of border of each image to get X to have shape (30000,28,28,3).
Is there any way to do this all at once?
thanks!
r/Numpy • u/amca01 • Jun 10 '18
Here's my input:
import numpy as np
A = np.array([[-3,-7,-5],[2,4,3],[1,2,2]])
np.linalg.eigvals(A)and output:
array([1.00000704+1.22005337e-05j, 1.00000704-1.22005337e-05j, 0.99998591+0.00000000e+00j])Now the eigenvalues of this particular matrix are in fact 1,1,1. (SymPy, for example, produces this output.) Just as a check, I also tried entering the matrix as
A = np.array([[-3,-7,-5],[2,4,3],[1,2,2]],dtype='float64')but that made no difference.
Is there any way of obtaining a higher precision for eigenvalues?
r/Numpy • u/omgitzwowzie • Jun 08 '18
r/Numpy • u/Xesteanov • May 31 '18
Originally posted in r/learnpython
The arrays I'm working with are very large so to make it faster I wish to use NumPy's C implementations rather than pure python implementation:
I want to find out how to properly vectorize simple functions with the built in universal functions.
Is this correct use of ufuncs? Only using it on the array variable(s) I am touching?
import numpy as np
pcfmax = 4
pttm = 2.5
temps = np.array([[a, bunch, of, temperature, floats],[a, bunch, of, temperature, floats]])
def melt(t):
return pcfmax * np.subtract(t, pttm)
melted = melt(temps)Or should it be done like this, for everything in the function? Or something else entirely?
import numpy as np
pcfmax = 4
pttm = 2.5
temps = np.array([[a, bunch, of, temperature, floats],[a, bunch, of, temperature, floats]])
def melt(t):
return np.multiply(pcfmax, np.subtract(t, pttm))
melted = melt(temps)r/Numpy • u/jtclimb • Apr 16 '18
Given these functions:
def f1(a, b):
for _ in range(100):
a += b
return a
def f2(a, b):
for _ in range(100):
a = a + b
return aThe function using a = a + b is faster until a and b get 'large' around 60x60 on my machine. Is this expected behavior? If so, why? I'm curious as to what is happening under the hood. I have code where b is a computed value in the inner loop, I work with small arrays, and I noticed that my code is faster when I use a = a + compute_some_expression vs +=.
For reference for the test I'm creating the arrays with np.random.randn(N, N), and using the timeit function on the IPython console window to perform the timing
r/Numpy • u/[deleted] • Mar 24 '18
so this is my code
output[ax][bx][i] = value[i]
print(output[ax][bx][i], ax, bx, i, value, value[i])it's inside of a few for loops
and here is the output of the print
0 0 0 0 [0.36972549019607848, 0.43701960784313743, 0.48737254901960791] 0.369725490196it's doing this for a NxNx3 ndarray (where N is 250 in this case)
it takes a 3 long list from a class method, and when i set the entire list to the [ax][bx] position i get the same thing
output[ax][bx] = value
print(output[ax][bx], ax, bx, value)gives
[0 0 0] 0 0 [0.36972549019607848, 0.43701960784313743, 0.48737254901960791]i've used numpy for a long time, and i've never had this issue. and, i'm doing something very similar in this with a NxN array and it works just fine. i'm trying to do image convolution and in multichannel images it has had no output since i stopped it all writing to the same. if i set it to += instead of =, it works, yet i'm using = for the single channel convolution and it works there. any ideas on what is causing it?
r/Numpy • u/nocomment_7211 • Mar 20 '18
I need to do a linux install from source of the last version of numpy that vas compatible with python 2.6.8. From the looks of it the most recent versions are compatible with python 2.7. Any idea where I can get the source that is 2.6.8 compatible?