difference between numpy.convolve and scipy signal.convolve - python

now I have a numpy 2D array and want to make a convolution with a 2D kernel. I have tried using numpy.convolve and the out put was :
ValueError: object too deep for desired array
when trying signal.convolve it works well .
so is there any way to fix np.convolve??
and is the result of signal.convolve will be the same as np.convolve
?
here is my simple code:
import numpy as np
from scipy import signal
a=np.array([[1,2,3],[4,5,6]])
b=np.array([[1,1],[1,1]])
A=np.convolve(a,b,'same')
out:ValueError: object too deep for desired array
B=signal.convolve(a,b,'same')
Out[53]:
array([[ 1, 3, 5],
[ 5, 12, 16]])

Related

Extracting from tensor using indices like numpy

I have a tensor for example called tensor1 of shape (1,20,4). I am trying to create a tensor using certain indices (1,4,5) from this tensor. I could do this form numpy for example using: tensor[:,[1,4,5],:]. From what I understand this could be done using "tf.gather_nd" but I don't really see how it could be done.
What you want can be done with tf.gather:
tensor2 = tf.gather(tensor1, [1, 4, 5], axis=1)

Sum pattern across array

I'm having trouble finding the proper way to do something I think should be trivial using numpy. I have an array (1000x1000) and I want to calculate the sum of a specific pattern across the array.
For example:
If I have this array and want to calculate the sum of a two-cell-right diagonal I would expect [7,12,11,8,12,6,11,7] (a total of 8 sums).
How can I do this?
This operation is called a 2-dimensional convolution:
>>> import numpy as np
>>> from scipy.signal import convolve2d
>>> kernel = np.eye(2, dtype=int)
>>> a = np.array([[5,3,7,1,2],[3,2,9,4,7],[8,9,4,2,3]])
>>> convolve2d(a, kernel, mode='valid')
array([[ 7, 12, 11, 8],
[12, 6, 11, 7]])
Should you want to generalize it to arbitrary dimensions, there is also scipy.ndimage.convolve available. It will also work for this 2d case, but does not offer the mode='valid' convenience.
l = [[5,3,7,1,2],[3,2,9,4,7],[8,9,4,2,3]]
[q+l[w+1][t+1] for w,i in enumerate(l[:-1]) for t,q in enumerate(i[:-1])]
then you can avoid using numpy :) and the output is
[7,12,11,8,12,6,11,7]

Dataframe vs Numpy array in Python

I have a questions regarding dataframe and numpy arrays in Python. When we read any csv file using pandas, it is stored in a dataframe. Dataframe is useful when it comes to data manipulations, viewing data in columns etc. However some preprocessing functions such as Imputer do not work on Dataframes. For these functions we have to get the data in numpy arrays which makes the data manipulation difficult
In following code I while y is stored as int64 array, X is ndarray object of numpy module. I can not use append function on X. Can anyone suggest how to correct this
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('titanic.csv')
y = dataset.iloc[:,1].values
X= dataset.iloc[:,2:12].values
You really should give us more information about what you want/expect, but here's my guess:
In [6]: Y=np.arange(3) # 1d
In [7]: X=np.arange(12).reshape(3,4) # 2d
In [8]: np.column_stack([Y,X])
Out[8]:
array([[ 0, 0, 1, 2, 3],
[ 1, 4, 5, 6, 7],
[ 2, 8, 9, 10, 11]])
This should be the same as
dataset.iloc[:,[1,2,3,...12]].values
though why you didn't do dataset.iloc[:,1:12]?

calculate a 2d array in python using numpy

I want to create a 2d array in python using numpy.
In the following, I just create the 2d array with numbers 1,2,3,...
I should calculate an expression to produce each element of it.
Thank you
import numpy as np
my_2darray = np.array([[1,2,3],[4,5,6]])
you can create a 2d array of size 2 x 3 using numpy in that way:
x = np.array([[1, 2, 3], [4, 5, 6]], np.int32)

Creating inverse and diagonal of numpy matrices

I am trying to do inverse of numpy array,
from numpy import mat
from numpy import *
from numpy import matrix
from numpy import linalg
d =array ([ (0, 1, 2, 3, 4),
( 5, 6, 7, 8, 9),
(10, 11, 12, 13, 14)])
print d.T
print d.I
print d.diagonal
#above line gives <built-in method diagonal of numpy.ndarray object at 0x7fdf40a263f0>
print numpy.linalg.inv(d)
I am getting
AttributeError: 'numpy.ndarray' object has no attribute 'I'
any suggestion for this to get inverse and diagonal?
I would suggest changing all these imports:
from numpy import mat
from numpy import *
from numpy import matrix
from numpy import linalg
to just one:
import numpy as np
Then you can do
d = np.array(...)
# d = np.arange(15).reshape(3,5)
M = np.matrix(d)
M.I # a matrix has an I property, but an array does not
d.diagonal() # diagonal is a method, not a property
np.diagonal(d) # diagonal is also a function
np.linalg.inv(d) does not work - it gives an error objecting that the array is not square. So evidently M.I is returning a different kind of inverse. See my note below about pinv.
numpy (and Python in general) has functions, methods, and properties (attributes). Are the distinctions clear?
Many numpy functions end up calling the corresponding method for the main array object. Usually that doesn't matter much, except as a calling convenience.
np.matrix is one subclass that has many of its own methods. Note, for example
In [817]: M.diagonal()
Out[817]: matrix([[ 0, 6, 12]])
In [818]: d.diagonal()
Out[818]: array([ 0, 6, 12])
diagonal has returned the same numbers, but for M, it returns an object of the same class, and which by class definition is 2d.
d.T, M.I access properties. These don't require the () that a method does, but in many ways they are the same. np.matrix has defined I, but np.array has not.
If you are used to working with matrices in MATLAB, the np.matrix class may ease the transition. But if this is your first experience with arrays like this, I'd suggest sticking the np.array. The np.matrix class will just add confusion.
M.I is the same as M.getI().
It's code is (use help(M.getI) to read its docs)
def getI(self):
M, N = self.shape
if M == N:
from numpy.dual import inv as func
else:
from numpy.dual import pinv as func
return asmatrix(func(self))
So that means that M.I use pinv rather than inv. np.linalg.pinv(d) works.
You will get these methods if you transform d to a np.matrix, by doing:
d = np.matrix(d)
then:
d.I
d.diagonal()
will work.
A.diagonal is a method of numpy.ndarray, just as the print out suggests. Therefore, the solution of #Saullo Castro works for numpy arrays as well, without the need to convert to np.matrix.
import numpy as np
A = np.arange(25).reshape((5,5))
diag = A.diagonal()
# array([ 0, 6, 12, 18, 24])
Numpy Arrays have no method to calculate the inverse of a matrix, but you can easily do that with numpy.linalg.inv, just as you already tried according to your code example.

Categories