Multiply a series of matrices - python-3.x

I have a matrix like below:
[0 0 1 1]
[0 0 1 1]
[0 0 0 0]
[0 0 0 0]
I need to divide it into multiple 3x3 matrices starting from top left through right. It's sort of a 3x3 slide across the matrix. In this example, we would have 4 3x3 matrices like so:
[0 0 1] [0 1 1]
1 = [0 0 1] 2 = [0 1 1]
[0 0 0] [0 0 0]
[0 0 1] [0 1 1]
3 = [0 0 0] 4 = [0 0 0]
[0 0 0] [0 0 0]
I've tried this using tf.extract_image_patch and got the 4 matrices, but I'm still not sure how I can do a sort-of running product for these matrices in Tensorflow. Or, better could achieve the running product without having to pre-calculate the separate matrices.
With running product I mean this: I need to multiple above 1-4 matrices element-wise and need to get 1 3x3 matrix. For example, 1 & 2 matrices would be multiplied, the result would be multiplied with matrix 3, and the result again would be multiplied with matrix 4.This operation should give me start of the patch([[1 1], [1 1]]) in my original matrix, a matrix like below:
[0 0 1]
res = [0 0 0]
[0 0 0]
Once done, I need to make this operation part of my network, a Tensorflow layer perhaps.
I'd appreciate if someone could help me achieve this. Thanks.
EDIT
This seems to be one way to multiply matrices in a list, but I'm still looking for 1) slice matrices into multiple parts and multiply them in a better way and 2) to add this as a layer to a network:
tf.scan(lambda a, b: tf.multiply(tf.squeeze(a), tf.squeeze(b)), original)

you could use tf.nn.conv2d, manipulating a matrix like this is called a convolution
see tensorflow.org/api_docs/python/tf/nn/conv2d

You can use numpy array slicing
import numpy as np
A = np.array([[0, 0, 1, 1],
[0, 0, 1, 1],
[0, 0, 0, 0],
[0, 0, 0, 0]])
res = A[:-1, :-1] * A[:-1, 1:] * A[1:, :-1] * A[1:, 1:]
and then, perhaps, convert the numpy array to a Tensor object by
tf.convert_to_tensor(res)

Related

Show two connected triangles as rectangle

I have a question regarding open3d. Is it possible to display two triangles as a rectangle?
I try to explain it with the following code example, what I would like as a result. (in principle I would like to display rectangle elements, and I think this is only possible if I create two rectangles)
import open3d as o3d
import open3d.visualization.gui as gui
import open3d.visualization.rendering as rendering
import numpy as np
node_array = np.zeros(shape=(4, 3))
shell_array = np.zeros(shape=(2, 3))
"""
Nodes
0 0 0 0
1 10 0 0
2 10 10 0
3 0 10 0
Shells
0 0 1 3
1 3 1 2
"""
node_array[0] = [0, 0, 0]
node_array[1] = [10, 0, 0]
node_array[2] = [10, 10, 0]
node_array[3] = [0, 10, 0]
shell_array[0] = [0, 1, 3]
shell_array[1] = [3, 1, 2]
mesh = o3d.geometry.TriangleMesh()
mesh.vertices = o3d.utility.Vector3dVector(node_array)
mesh.triangles = o3d.utility.Vector3iVector(shell_array)
o3d.visualization.draw_geometries([mesh], mesh_show_wireframe=True)
And I would like to have it displayed that way.
Is this even possible with open3d?
Thanks a lot for your help.

sklearn ROC curver

I have 10 classes, and my y_test has shape (1000, 10) and it looks like this:
array([[0, 0, 1, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 1],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=int64)
If I use the following where i is the class number
fpr[i], tpr[i], _ = roc_curve(y_test[:, i], y_pred[:, i])
should y_pred be
y_pred = model.predict(x_test)
OR
y_pred = np.argmax(model.predict(x_test), axis=1)
lb = LabelBinarizer()
lb.fit(y_test)
y_pred = lb.transform(y_pred)
The first option gives me something like this:
[[6.87280996e-11 6.28617670e-07 9.96915460e-01 ... 3.08361766e-03
3.47333212e-14 2.83545876e-09]
[7.04240659e-30 1.51786850e-07 8.49807921e-28 ... 6.62584656e-33
6.97696034e-19 1.01019222e-20]
[2.97537670e-14 2.67199534e-24 2.85646610e-19 ... 2.19898160e-15
7.03626012e-22 7.56072279e-18]
...
[1.63774752e-15 1.32784101e-06 1.23182635e-05 ... 3.60217566e-14
6.01247484e-05 2.61179358e-01]
[2.09420733e-35 6.94865276e-10 1.14242395e-22 ... 5.08080394e-22
1.20934697e-19 1.77760468e-17]
[1.68334747e-13 8.53335252e-04 4.40571597e-07 ... 1.70050384e-06
1.48684137e-06 2.93400045e-03]]
with shape (1000,10).
where the latter option gives
[[0 0 1 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
with shape (1000,10)
Which way is the correct approach? in other words, what would this y_pred be when passing to sklearn.metrics.roc_curve().
Forget to mention, using the first option gives me extremely high (almost 1) AUC values for all classes, whereas the second option seems to generate reasonable AUC values.
The ROC curves using the two options are below, which one looks more correct?
There is nothing wrong with the first option, and that's what the documentation asks for:
y_scorendarray of shape (n_samples,)
Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers).
Also, the first graph looks like a ROC curve, while the second is weird.
And finally, ROC curves intend to study "different classification thresholds". That means you need predictions "as probabilities" (confidences), not as 0's and 1's.
When you take an argmax, you throw away the probabilities/confidences, making it impossible to study thresholds.

How to extract elements in an array with regard to an index?

I have a row A = [0 1 2 3 4] and an index I = [0 0 1 0 1]. I would like to extract the elements in A indexed by I, i.e. [2, 4].
My attempt:
import numpy as np
A = np.array([0, 1, 2, 3, 4])
index = np.array([0, 0, 1, 0, 1])
print(A[index])
The result is not as I expected:
[0 0 1 0 1]
Could you please elaborate on how to achieve my goal?
I think you want boolean indexing:
A[index.astype(bool)]
# array([2, 4])
A non-numpy way to achieve this, in case its useful - it uses zip to combine each pair of elements, and returns the first if the second is true:
[x[0] for x in zip(a, i) if x[1]]

Create a matrix 11x3 with 0's and 1' s on each entry depending on the result of the condition, using python

I want to create a matrix 11x3 with 0 and 1 and save it as the variable TIME. Each entry in the matrix will execute the following if u[i] < t then will give 1 as an entry else 0.
My code is the following:
u = [1, 3, 9]
ts = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
time = np.zeros((len(ts), len(u))) # the size of the matrix
for i in range(len(ts)):
for j in range(len(u)):
for t in ts:
if u[j] < t:
time [i,j] += 1
else:
time[i,j] += 0
print(time[i,j])
However I do not get the result I want. If anyone could help I would be grateful. I want to create a function out of it because I want to use it again later on my code but it does not work .If anyone could help I would be grateful.
You can try using nested list comprehensions:
import numpy as np
u = [1, 3, 9]
ts = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
l = [[int(x < y) for x in u] for y in ts]
time = np.array(l)
print(time)
Output will look like this:
[[0 0 0]
[0 0 0]
[1 0 0]
[1 0 0]
[1 1 0]
[1 1 0]
[1 1 0]
[1 1 0]
[1 1 0]
[1 1 0]
[1 1 1]]

Shortcut for all the possible permutation of a set of numbers for m digits

I have been working on finite field. Suppose I have a prime number p=7. So I get a list q=[0,1,2,3,4,5,6]. Now I want all the possible permutation of the elements of set q for 7 places. For example [1,1,1,4,6,3,1] is one of the possible permutation. Is there any inbuilt command in python for doing that? Actually I am working with bigger field where P is 127 (p=127).
Those aren't permutations because elements are repeated, this looks more like a product.
you can use itertools.product on repeated q lists (here for 3 elements):
import itertools
q=[0,1,2] # or q = list(range(3))
for z in itertools.product(*(q,)*len(q)): # using arg unpacking like if it was (q,q,q)
z = list(z) # to convert as list
print(z)
prints:
[0, 0, 0]
[0, 0, 1]
[0, 0, 2]
[0, 1, 0]
[0, 1, 1]
[0, 1, 2]
...snip...
[2, 2, 0]
[2, 2, 1]
[2, 2, 2]
for p=3 it prints 3**3 = 27 values. If p=127 well... sounds not reasonable.

Resources