I tried the example for LMS algorithm:
import numpy as np
from neupy import algorithms
input_data = np.array([[1, 0], [2, 2], [3, 3], [0, 0]])
target_data = np.array([[1], [0], [0], [1]])
lmsnet = algorithms.LMS((2, 1), step=0.5)
lmsnet.train(input_data, target_data, epochs=200)
lmsnet.predict(np.array([[4, 4], [0, 0]]))
But I get "OverflowError: cannot convert float infinity to integer" error in this line (file:summary_info.py):
scale = math.ceil(self.delay_limit / average_delay)
I can't relate the input parameters from the example to the error, I know that a division by zero get there but I can't figure out how to fix this. I don't want to modify library files to fix the problem.
Your example works perfectly fine for me
You can overcome this issue, if you train your network in a loop, like this
import numpy as np
from neupy import algorithms
input_data = np.array([[1, 0], [2, 2], [3, 3], [0, 0]])
target_data = np.array([[1], [0], [0], [1]])
# Used smaller step since 0.5 is too big
lmsnet = algorithms.LMS((2, 1), step=0.1)
for _ in range(200):
lmsnet.train(input_data, target_data, epochs=1)
lmsnet.predict(np.array([[4, 4], [0, 0]]))
Related
I have a 3x2 array where each row represents a vertex of a triangle. I would like to reshape it in order to obtain a new array where each row represents a side.
I'm currently trying the following approach:
points = np.array([[0,0], [0,1], [1,0]])
sides = np.array([
[points[0], points[1]],
[points[1], points[2]],
[points[2], points[0]]
])
Is there any build in function to do that in a more elegant way?
Elegance is a matter of definition, if you find the following solution more elegant, is up to you. I use np.roll to shift the indices from [0], [1], [2] to [1] [2] [0] and then pair the shifted and unshifted arrays using np.stack, similar to what you do in your manual code (watch the index pairs you create, they are the same).
import numpy as np
points = np.array([[0,0], [0,1], [1,0]])
print(points)
#array([[0, 0],
# [0, 1],
# [1, 0]])
sides = np.stack([
points,
np.roll(points, -1, axis=-1)
], axis=-1)
print(sides)
#array([[[0, 0],
# [0, 1]],
#
# [[0, 1],
# [1, 0]],
#
# [[1, 0],
# [0, 0]]])
Keep in mind that this solution does not work for an arbitrary amount of vertices, but just three.
I am trying to figure out how to do this with numpy, so I can then convert it to c++ from scratch. I have figured out how to do it when the mode is constant. The way that is done is shown below.
import numpy as np
from scipy import signal
a = np.array([[1, 2, 0, 0], [5, 3, 0, 4], [0, 0, 0, 7], [9, 3, 0, 0]])
k = np.array([[1,0,0],[0,1,0],[0,0,0]])
a = np.pad(a, 1)
k = np.flip(k)
output = signal.convolve(a, k, 'valid')
Which then comes out to the same output as scipy.ndimage.filters.convolve(a, k, mode='constant) So I thought that when the mode was reflect it would work the same way. Except, that the line a = np.pad(a, 1) would be changed to a = np.pad(a, 1, mode='reflect'). However, that does not seem to be the case. Could someone explain how it would work from scratch using numpy and scipy.signal.convolve? Thank you.
Maybe this question is basic. But it does confuse me and caused an error.
import numpy as np
list1 = [np.matrix([[0, 1]]), np.matrix([[0,1]])]
np.stack(list1) # This gives matrix([[0, 1], [0, 1]]), which is expected
list2 = [np.matrix([[0.]]), np.matrix([[0.]])]
np.stack(list2) # This gives matrix([[0., 0.]]).
# But I thought this would output matrix([[0.], [0.]])
What does it have different behaviors?
I am using the following example from :
from scipy import spatial
x, y = np.mgrid[0:5, 2:8]
tree = spatial.KDTree(list(zip(x.ravel(), y.ravel())))
pts = np.array([[0, 0], [2.1, 2.9]])
idx = tree.query(pts)[1]
data = tree.data[??????????]
If I input two arbitrary points (see variable pts), I am looking to return all pairs of coordinates that lie within the rectangle defined by the two points (KDTree finds the closest neighbour). So in this case:
array([[0, 0],
[0, 1],
[0, 2],
[1, 0],
[1, 1],
[1, 2],
[2, 0],
[2, 1],
[2, 2]])
How can I achieve that from the tree data?
Seems that I found a solution:
from scipy import spatial
import numpy as np
x, y = np.mgrid[0:5, 0:5]
tree = spatial.KDTree(list(zip(x.ravel(), y.ravel())))
pts = np.array([[0, 0], [2.1, 2.2]])
idx = tree.query(pts)[1]
data = tree.data[[idx[0], idx[1]]]
rectangle = tree.data[np.where((tree.data[:,0]>=min(data[:,0])) & (tree.data[:,0]<=max(data[:,0])) & (tree.data[:,1]>=min(data[:,1])) & (tree.data[:,1]<=max(data[:,1])))]
However, I would love to see a solution using the query option!
Trying to write a simple lowpass filter in python to run against lena. Then I'd like to run an inverse filter to run against the lowpass and try to get the original back (well, as close to original). I'm new to programming in python and not quite sure where to start.
I tried rearranging a highpass filter code but it doesn't look right.
import matplotlib.pyplot as plt
import numpy as np
import scipy.misc
from scipy import ndimage
import Image
#lowpass
def plot(data, title):
plot.i += 1
plt.subplot(2,2,plot.i)
plt.imshow(data)
plt.gray()
plt.title(title)
plot.i = 0
# Load the data...
img = scipy.misc.lena()
data = np.array(img, dtype=float)
plot(data, 'Original')
#narrow lowpass filter
kernel = np.array([[1, 1, 1],
[1, -8, 1],
[1, 1, 1]])
lp_3 = ndimage.convolve(data, kernel)
plot(lp_3, '3x3 Lowpass')
# A slightly "wider" lowpass filter
kernel = np.array([[1, 1, 1, 1, 1],
[1, -1, -2, -1, 1],
[1, -2, -4, -2, 1],
[1, -1, -2, -1, 1],
[1, 1, 1, 1, 1]])
lp_5 = ndimage.convolve(data, kernel)
plot(lp_5, '5x5 Lowpass')
plt.show()
You should definitely check your kernel first. It does not look like a lowpass (averaging) kernel at all. Try first something like
kernel = np.ones((n,n))
if you want to do a very simple lowpass filter n by n (i.e. blurring):