I'm trying to interpolate a 2d unstructured grid using scipy.interpolate.SmoothBivariateSpline. I'm afraid I have not understood how it is supposed to work.
I've tried with a very simple example:
from scipy import interpolate
x = [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2]
y = [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]
z = [-0.07453796, -0.10857792, -0.07307213, 0.01813757, -0.31634891, -0.47235507, -0.33198942, -0.28530956, -0.26995915, -0.40618327, -0.0950876, -0.18871505]
xy_func = interpolate.SmoothBivariateSpline(x, y, z, kx=1, ky=1, s=0)
print(xy_func.ev(0, 1), xy_func.ev(1, 0), xy_func.ev(1, 3))
I've visualized the result and it is obvious that it is incorrect. I also evaluated the result on some of the data points since it should be clear what the output should be. From the print I expected to get the output "-0.10857792, -0.31634891, -0.28530956", but I got the result "-0.1390947215 -0.272092075 -0.16190767".
Where am I off?
I think there are two problems:
If you allow a curvier shape by increasing i.e. kx=2 and ky=3, you already get a much better fit of your predictions.
However, because SmoothBivariateSpline doesnt like the vertical nature of your test data, you will not get very good results anyway. If you change x so that it increments more evenly i.e. (x=range(len(x)), it looks much better.
Related
I tried to multiply two polynomials g(x), h(x) ∈ GF(2)[x]. And I got the result as c(x). I would like to get the vector representation of c(x).
Here is the code I am sharing.
import galois
GF = galois.GF(2)
g = galois.Poly([1, 0, 1, 1], field=GF)
h = galois.Poly([1, 1, 0], field=GF)
c = g * h
print(c)
The output I am getting is
x^5 + x^4 + x^3 + x
but I would like to get output in vector form, i.e.,
[1, 1, 1, 0, 1, 0]
Any suggestion about how I can get the answer in vector form?
I tried the using calling the function
GF("c").vector()
but it is giving me the wrong answer.
Use c.coeffs. This gives:
GF([1, 1, 1, 0, 1, 0], order=2)
This form may be satisfactory for whatever you are trying to do.
If not, you can (among other things) turn it into a normal Python list[int] with [int(i) for i in c.coeffs], yielding:
[1, 1, 1, 0, 1, 0]
I have this function which works for single vector:
def vec_to_board(vector, player, dim, reverse=False):
player_board = np.zeros(dim * dim)
player_pos = np.argwhere(vector == player)
if not reverse:
player_board[mapping[player_pos.T]] = 1
else:
player_board[reverse_mapping[player_pos.T]] = 1
return np.reshape(player_board, [dim, dim])
However, I want it to work for a batch of vectors.
What I have tried so far:
states = jnp.array([[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2], [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2]])
batch_size = 1
b_states = vmap(vec_to_board)((states, 1, 4), batch_size)
This doesn't work. However, if I understand correctly vmap should be able to handle this transformation for batches?
There are a couple issues you'll run into when trying to vmap this function:
This function is defined in terms of numpy arrays, not jax arrays. How do I know? JAX arrays are immutable, so things like arr[idx] = 1 will raise errors. You need to replace these with equivalent JAX operations (see JAX Sharp Bits: in-place updates) and ensure your function works with JAX array operations rather than numpy array operations.
Your function makes used of dynamically-shaped arrays; e.g. player_pos, has a shape dependent on the number of nonzero entries in vector == player. You'll have to rewrite your function in terms of statically-shaped arrays. There is some discussion of this in the jnp.argwhere docstring; for example, if you know a priori how many True entries you expect in the array, you can specify the size to make this work.
Good luck!
I have made a morse code translator and I want it to be able to record a flashing light and make it into morse code. I think I will need OpenCV or a light sensor, but I don't know how to use either of them. I haven't got any code for it yet, as I couldn't find any solutions anywhere else.
The following is just a concept of what you could try. Yes, you could also train a neural network for this but if your setup is simple enough, some engineering will do.
We first create a "toy-video" to work with:
import numpy as np
import matplotlib.pyplot as plt
# Create a toy "video"
image = np.asarray([
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 1, 2, 2, 1],
[0, 0, 2, 4, 4, 2],
[0, 0, 2, 4, 4, 2],
[0, 0, 1, 2, 2, 1],
])
signal = np.asarray([0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0])
x = list(range(len(signal)))
signal = np.interp(np.linspace(0, len(signal), 100), x, signal)[..., None]
frames = np.einsum('tk,xy->txyk', signal, image)[..., 0]
Plot a few frames:
fig, axes = plt.subplots(1, 12, sharex='all', sharey='all')
for i, ax in enumerate(axes):
ax.matshow(frames[i], vmin=0, vmax=1)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax.set_title(i)
plt.show()
Now that you have this kind of toy video, it's pretty straight forward to convert it back to some sort of binary signal. You'd simply compute the average brightness of each frame:
reconstructed = frames.mean(1).mean(1)
reconstructed_bin = reconstructed > 0.5
plt.plot(reconstructed, label='original')
plt.plot(reconstructed_bin, label='binary')
plt.title('Reconstructed Signal')
plt.legend()
plt.show()
From here we only have to determine the length of each flash.
# This is ugly, I know. Just for understanding though:
# 1. Splits the binary signal on zero-values
# 2. Filters out the garbage (accept only lists where len(e) > 1)
# 3. Gets the length of the remaining list == the duration of each flash
tmp = np.split(reconstructed_bin, np.where(reconstructed_bin == 0)[0][1:])
flashes = list(map(len, filter(lambda e: len(e) > 1, tmp)))
We can now take a look at how long flashes take:
print(flashes)
gives us
[5, 5, 5, 10, 9, 9, 5, 5, 5]
So.. "short" flashes seem to take 5 frames, "long" around 10. With this we can classify each flash as either being "long" or "short" by defining a sensible threshold of 7 like so:
# Classify each flash-duration
flashes_classified = list(map(lambda f: 'long' if f > 7 else 'short', flashes))
And let's repeat for pauses
# Repeat for pauses
tmp = np.split(reconstructed_bin, np.where(reconstructed_bin != False)[0][1:])
pauses = list(map(len, filter(lambda e: len(e) > 1, tmp)))
pauses_classified = np.asarray(list(map(lambda f: 'w' if f > 6 else 'c', pauses)))
pauses_indices, = np.where(np.asarray(pauses_classified) == 'w')
Now we can visualize the results.
fig = plt.figure()
ax = fig.gca()
ax.bar(range(len(flashes)), flashes, label='Flash duration')
ax.set_xticks(list(range(len(flashes_classified))))
ax.set_xticklabels(flashes_classified)
[ax.axvline(idx-0.5, ls='--', c='r', label='Pause' if i == 0 else None) for i, idx in enumerate(pauses_indices)]
plt.legend()
plt.show()
It somewhat depends on your environment. You might try inexpensively with a Raspberry Pi Zero (£9) or even a Pico (£4) or Arduino and an attached LDR - Light Dependent Resistor for £1 rather than a £100 USB camera.
Your program would then come down to repeatedly measuring the resistance (which depends on the light intensity) and making it into long and short pulses.
This has the benefit of being cheap and not requiring you to learn OpenCV, but Stefan's idea is far more fun and has my vote!
Take the probability distribution of a XOR gate in which every configuration is equally probable (configurations are given by outcomes_sub; the probability mass function by pmf_xor_sub):
import numpy as np
import itertools as it
outcomes_sub = [list(item) for item in list(it.product([0,1], repeat=3))]
pmf_xor_sub = np.array([1/4, 0, 0, 1/4, 0, 1/4, 1/4, 0])
Now take the probability distribution corresponding to two uncorrelated such XORs:
outcomes = [outcome1 + outcome2 for (outcome1, outcome2)
in it.product(outcomes_sub, outcomes_sub)]
pmf_xor = [pmf1 * pmf2 for (pmf1, pmf2)
in it.product(pmf_xor_sub, pmf_xor_sub)]
And create some data based on it:
indices = np.random.choice(len(outcomes), 10000, p=pmf_xor)
data_xor = np.array([outcomes[index] for index in indices])
data_xor looks like this:
array([[1, 1, 0, 0, 0, 0],
[1, 0, 1, 0, 0, 0],
[0, 1, 1, 1, 1, 0],
...,
[0, 1, 1, 1, 1, 0],
[1, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0]])
I.e., two independent XORs back to back. What's the right way to perform dimensionality reduction on it? PCA won't work (because the dependence is non-linear, right?):
from sklearn import decomposition
pca_xor = decomposition.PCA()
pca_xor.fit(data_xor)
Now, pca_xor.explained_variance_ratio_ gives:
array([ 0.17145045, 0.17018817, 0.16758773, 0.16575979, 0.16410862,
0.16090524], dtype=float32)
No two components stand out. I understand that a non-linear method such as kernel PCA should work here, but I am struggling to find pointers to ways of applying it to my problem.
To give a bit more context: what I am actually after is ways to bring out the structure in data_xor: two big XOR blobs, each of which is composed of some finer-grained stuff. If I am going about it all wrong, feel free to point that out too.
When I try running the following code with my data (from this example)
X_new = LinearSVC(C=0.01, penalty="l1", dual=False).fit_transform(X, y)
I get:
"Invalid threshold: all features are discarded"
I tried specifying my own threshold:
clf = LinearSVC(C=0.01, penalty="l1", dual=False)
clf.fit(X,y)
X_new = clf.transform(X, threshold=my_threshold)
but I either get:
An array X_new of the same size as X, this is whenever my_threshold is one of:
'mean'
'median'
Or the "Invalid threshold" error (e.g. when passing scalar values to threshold)
I can't post the entire matrix X, but below are a few stats of the data:
> X.shape
Out: (29,312)
> np.mean(X, axis=1)
Out:
array([-0.30517191, -0.1147345 , 0.03674294, -0.15926932, -0.05034101,
-0.06357734, -0.08781186, -0.12865185, 0.14172452, 0.33640029,
0.06778798, -0.00217696, 0.09097335, -0.17915627, 0.03701893,
-0.1361117 , 0.13132006, 0.14406628, -0.05081956, 0.20777349,
-0.06028931, 0.03541849, -0.07100492, 0.05740661, -0.38585413,
0.31837905, 0.14076042, 0.1182338 , -0.06903557])
> np.std(X, axis=1)
Out:
array([ 1.3267662 , 0.75313658, 0.81796146, 0.79814621, 0.59175161,
0.73149726, 0.8087903 , 0.59901198, 1.13414141, 1.02433752,
0.99884428, 1.11139231, 0.89254901, 1.92760784, 0.57181158,
1.01322265, 0.66705546, 0.70248779, 1.17107696, 0.88254386,
1.06930436, 0.91769016, 0.92915593, 0.84569395, 1.59371779,
0.71257806, 0.94307434, 0.95083782, 0.88996455])
y = array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
0, 0, 0, 0, 0, 0])
This is all with scikit-learn 0.14.
You should first analyze if your SVM model is training well before trying to use it as a transformation base. It is possible, that you are using too small C parameter, which is causing sklearn to train a trivial model which leads to the removal of all features. You can check it by either performing classification tests on your data, or at least printing the found coefficients (clf.coef_)
It would be a good idea to run a grid search technique, for the best C in terms of generalization properties, and then use it for transformation.