Python 2d array - python

I want to make 2D array, which is 50X75.
Computer has to make random coordinates inside the array, about 15 to 20 coordinates.
What should I do TT
I stopped with the first step, making 50X75 2D array, so help meTT

You can generate 2D array using random runmbers
from random import randint
coordinates = [[randint(1, 100), randint(1, 100)] for i in range(20)]
Output: [[81, 52], [12, 79], [24, 90], [93, 53], [98, 17], [40, 44], [31, 1], [1, 40], [8, 34], [81, 31], [87, 50], [45, 72], [86, 70], [43, 78], [64, 80], [85, 76], [28, 43], [81, 78], [80, 55], [82, 58]]

A 50 x 75 2D array can be made using a np.reshape function. Here is an example, hope this helps.
import numpy as np
np.arange(3750).reshape(50, 75) # the array has 50 rows and 75 cols
array([[ 0, 1, 2, ..., 72, 73, 74],
[ 75, 76, 77, ..., 147, 148, 149],
[ 150, 151, 152, ..., 222, 223, 224],
...,
[3525, 3526, 3527, ..., 3597, 3598, 3599],
[3600, 3601, 3602, ..., 3672, 3673, 3674],
[3675, 3676, 3677, ..., 3747, 3748, 3749]])

Related

calculating kurtosis and skew for every array in a 4d array

I have a 4d array of arrays where for example, a[0] looks like this :
array([[[135, 105, 95],
[109, 78, 60],
[101, 78, 54],
...,
[ 32, 21, 22],
[ 32, 21, 23],
[ 35, 28, 31]],
[[144, 119, 107],
[117, 87, 68],
[115, 94, 74],
...,
[ 32, 21, 22],
[ 33, 22, 24],
[ 33, 22, 26]],
[[145, 127, 113],
[140, 116, 102],
[128, 104, 87],
...,
[ 29, 22, 20],
[ 28, 21, 19],
[ 33, 23, 20]],
...,
[[105, 70, 62],
[109, 81, 75],
[142, 123, 117],
...,
[ 52, 41, 39],
[ 62, 49, 47],
[ 52, 38, 33]],
[[ 90, 55, 50],
[ 96, 67, 65],
[133, 111, 108],
...,
[ 45, 37, 34],
[ 48, 36, 32],
[ 48, 37, 30]],
[[129, 111, 106],
[124, 103, 101],
[116, 94, 90],
...,
[ 50, 40, 35],
[ 53, 39, 35],
[ 48, 37, 32]]], dtype=uint8)
Every array in the 4d array of arrays represents an image (pixels). I want to calculate kurtosis for every array in the 4d array by using a loop. So, could someone please help me with this?
Thanks in advance for your help
Without having an example, you could try something similar to this:
from scipy.stats import kurtosis
k = []
for elem in a:
k.append(kurtosis(elem))
This will output an array. If you want to output a single number, you should set axis=None when calling kurtosis().

How to calculate which Voronoi cells are in specific area in Python?

I have some points which illustrates heads of pedestrians during an experiment in each frame. I need to calculate which Voronoi Cells are in specific area - measurement square:
x_range = (-0.4, 0.4)
y_range = (0.5, 1.3)
So I've adapted a sample of generating Voronoi cells from points + I've added measurement area (blue lines) and walls (black), here is result for frame 0:
And here is part of code (adapted from the sample):
entries_for_frame = get_entries_at_frame(entries, frame)
points = points_from_entries(entries_for_frame)
vor = scipy.spatial.Voronoi(points)
scipy.spatial.voronoi_plot_2d(vor)
plt.show()
As I know in order to calculate which cells are inside the measurement area I need to check which lines of cells are crossing with the measurement square or are inside its.
So according to the documentation of scipy.spatial.Voronoi the interesting attributes are: vertices, which is returning those orange vertices. I need also to have edges of vertices inside the measurement area, the attribute according to the documentation of scipy.spatial.Voronoi is ridge_vertices, but unfortunately it is returning something strange:
[[0, 19], [0, 2], [1, 17], [1, 3], [2, 3], [17, 19], [-1, 22], [-1, 15], [15, 16], [16, 21], [21, 22], [-1, 0], [2, 23], [22, 23], [28, 32], [28, 29], [29, 30], [30, 31], [31, 32], [12, 13], [12, 28], [13, 25], [25, 29], [-1, 24], [-1, 31], [24, 30], [-1, 26], [26, 27], [27, 32], [-1, 33], [19, 20], [20, 34], [33, 34], [35, 36], [-1, 35], [36, 37], [-1, 37], [-1, 4], [4, 5], [5, 35], [6, 37], [6, 7], [7, 36], [38, 39], [38, 40], [39, 41], [40, 41], [-1, 40], [-1, 8], [8, 38], [-1, 9], [9, 10], [10, 41], [10, 43], [39, 42], [42, 43], [52, 53], [52, 57], [53, 54], [54, 55], [55, 56], [56, 57], [13, 52], [25, 57], [48, 49], [48, 54], [49, 55], [9, 50], [24, 56], [49, 50], [17, 59], [18, 61], [18, 20], [59, 61], [11, 46], [11, 60], [18, 47], [46, 47], [60, 61], [58, 63], [58, 60], [59, 62], [62, 63], [26, 64], [27, 65], [64, 65], [21, 67], [23, 68], [67, 68], [42, 45], [43, 69], [44, 45], [44, 72], [69, 72], [50, 70], [69, 70], [48, 71], [70, 71], [4, 76], [5, 75], [75, 76], [33, 77], [76, 77], [34, 78], [77, 78], [47, 79], [78, 79], [80, 82], [80, 81], [81, 83], [82, 84], [83, 84], [14, 53], [14, 80], [71, 82], [72, 84], [14, 51], [51, 87], [81, 85], [85, 87], [88, 90], [88, 89], [89, 93], [90, 91], [91, 92], [92, 93], [44, 88], [83, 89], [85, 86], [86, 93], [11, 91], [58, 92], [94, 95], [94, 97], [95, 96], [96, 98], [97, 99], [98, 99], [12, 94], [51, 95], [65, 97], [101, 104], [101, 102], [102, 103], [103, 105], [104, 105], [15, 101], [16, 104], [64, 102], [99, 103], [66, 67], [66, 105], [1, 106], [3, 107], [106, 107], [68, 108], [107, 108], [8, 73], [45, 109], [73, 110], [109, 110], [111, 115], [111, 113], [112, 113], [112, 114], [114, 115], [46, 74], [74, 111], [79, 113], [75, 112], [7, 114], [116, 117], [116, 118], [117, 120], [118, 119], [119, 121], [120, 121], [96, 118], [98, 100], [100, 116], [87, 119], [86, 121], [63, 120], [122, 127], [122, 123], [123, 124], [124, 125], [125, 126], [126, 127], [100, 127], [117, 122], [62, 123], [106, 124], [108, 125], [66, 126], [128, 129], [128, 130], [129, 132], [130, 131], [131, 133], [132, 134], [133, 134], [90, 128], [109, 129], [74, 130], [110, 132], [115, 131], [6, 133], [73, 134]]
In documentation I don't see how to understand returned numbers. Also in tutorials in explaining how to solve my problem. So my question is: how to calculate which Voronoi cells are inside the measurement area with at least single point?
I believe that your best bet is to use some kind of multiple polygon intersection algorithm using the cell vertices to describe the polygons.
You can whittle down the number of polygons by discarding those whose rightmost vertex is left of the blue rectangle, those whose leftmost vertex is to the right, and so on for up and down. This leaves you with the yellow polygons only.
You can also quickly eliminate (only, in this case you mark them as "intersecting") all those whose center or vertex lies inside the rectangle. This also is very quick.
In this example this is enough to locate all cells.
In other cases (for example, in the figure below, if the bottom-left yellow cell was shifted slightly upwards) you will have cells that have all vertices and the Delaunay center outside the rectangle, and yet one edge crosses it, so there is an intersection. To recognize those, you can exploit the fact that a rectangle is a convex figure, and check whether, among the cells you've left, there is one that contains at least one of the rectangle's corners. This is a slightly more complex check ("whether a point lies inside a convex polygon"), but not too complex since the cell is also convex and can be trivially decomposed in triangles.
The pseudo algorithm would be:
for all Voronoi cells:
get list of vertices.
are they all left/below/above/right of the rectangle?
YES: this cell does not intersect. Continue.
for all the vertices plus the cell center:
is this point inside the rectangle?
YES: we have intersection. Report this cell and continue.
decompose the cell in a list of triangles with vertex in the
Delaunay center, taking ordered vertex pairs.
for each triangle
for each vertex of the rectangle
is the vertex inside the triangle?
YES: we have intersection. Report and continue
this cell does not intersect the rectangle.

How to convert pixels stored in a list into an image with python?

pix = [
[[90, 94, 6], [126, 108, 24], [180, 116, 42], [166, 116, 46], [72, 94, 31]],
[[101, 96, 14], [190, 165, 84], [202, 134, 63], [170, 115, 50], [40, 50, 0]],
[[145, 125, 53], [150, 112, 40], [148, 73, 6], [156, 90, 31], [25, 11, 1]],
[[133, 124, 57], [165, 142, 75], [195, 142, 77], [169, 120, 62], [82, 74, 28]],
[[73, 105, 40], [56, 77, 10], [138, 135, 67], [97, 95, 34], [45, 69, 21]],
]
I have a bunch of pixels stored in the list and now I want to convert it to an image. How can I turn that list into an image? Thank you
Using PIL, you can create an image using an array:
from PIL import Image
import numpy as np
img = Image.fromarray(np.array(pix).astype(np.uint8))
Now, you may look at the image:
img.show()
Good thing is, from now on, you can benefit from all of PIL's toolcase for image processing (resize, thumbnail, filters, ...).
Here's how to do it using OpenCV. By default, OpenCV uses Numpy arrays to display images so you can simply convert the list into a <class 'numpy.ndarray'>.
Result:
import numpy as np
import cv2
pix = [
[[90, 94, 6], [126, 108, 24], [180, 116, 42], [166, 116, 46], [72, 94, 31]],
[[101, 96, 14], [190, 165, 84], [202, 134, 63], [170, 115, 50], [40, 50, 0]],
[[145, 125, 53], [150, 112, 40], [148, 73, 6], [156, 90, 31], [25, 11, 1]],
[[133, 124, 57], [165, 142, 75], [195, 142, 77], [169, 120, 62], [82, 74, 28]],
[[73, 105, 40], [56, 77, 10], [138, 135, 67], [97, 95, 34], [45, 69, 21]],
]
# Convert to ndarray
img = np.array(pix).astype(np.uint8)
# Save image
cv2.imwrite('img.png', img)
# Display image
cv2.imshow('img', img)
cv2.waitKey()
The answer above transforms your list into a PIL Image. If you just want to see the image, you can do this:
import matplotlib.pyplot as plt
plt.imshow(pix)

Python numpy 2D array minimum values

In the array:
np.random.randint(100, size=(10, 2))
array([[ 8, 31],
[96, 97],
[26, 31],
[81, 70],
[47, 97],
[95, 84],
[11, 93],
[31, 77],
[25, 45],
[79, 22]])
I´d like to obtain [8, 22], the minimum values of each column.
How can I get it?
I'm just putting #gtlambert's comment into an answer, since it's probably the best choice. Use the array.min function
x = array([[ 8, 31],
[96, 97],
[26, 31],
[81, 70],
[47, 97],
[95, 84],
[11, 93],
[31, 77],
[25, 45],
[79, 22]])
In [6]: x.min(axis=0)
Out[6]: array([ 8, 22])
Probably not the most efficient, but...
left = np.array([])
right = np.array([])
for n in aaa:
left = np.append(left,n[0])
right = np.append(right,n[1])
sol = [np.min(left), np.min(right)]

Summing up n number matrix in python

I have a matrix of 40*2000 a vector of of dimension 1500. I used numpy.outer to compute
outer product of vector with each column of matrix as:
np.outer(vector, matrix)
It showed memory error so I splitted matrix as matrix[:,:10] and computed separatly. It resulted into a matrix M of 1500*(40*10). I need sum each of the matrix here i'e first 40 columns with another next 40 column and so on..... If i used loop, it is going to be slow.
M[:,:40]+ M[:,40:80] + .....
Could someone help me to do this operation efficiently?
A bit of reshaping is all you need to be able to sum over the axis:
import numpy
M = numpy.arange(100).reshape(5, 20)
M[:, :4]
#>>> array([[ 0, 1, 2, 3],
#>>> [20, 21, 22, 23],
#>>> [40, 41, 42, 43],
#>>> [60, 61, 62, 63],
#>>> [80, 81, 82, 83]])
M[:, 4:8]
#>>> array([[ 4, 5, 6, 7],
#>>> [24, 25, 26, 27],
#>>> [44, 45, 46, 47],
#>>> [64, 65, 66, 67],
#>>> [84, 85, 86, 87]])
...
M[:, 16:20]
#>>> array([[16, 17, 18, 19],
#>>> [36, 37, 38, 39],
#>>> [56, 57, 58, 59],
#>>> [76, 77, 78, 79],
#>>> [96, 97, 98, 99]])
M.reshape(M.shape[0], -1, 4).sum(axis=1)
#>>> array([[ 40, 45, 50, 55],
#>>> [140, 145, 150, 155],
#>>> [240, 245, 250, 255],
#>>> [340, 345, 350, 355],
#>>> [440, 445, 450, 455]])

Categories