Related
During communities' detection I am trying to remove duplicates nodes from lists of lists (aimed to calculate ARI).
What I have – few dozen lists inside one list with different dimensions:
lst_of_lts= [[5192, 32896, 34357, 34976, 36683, 43315], … ,[19, 92585, 94137, 98381, 99041, 100395, 101100, 109759]]
What I am running:
import itertools
Lst_of_lts.sort()
Lst_of_lts_2 = list(k for k,_ in itertools.groupby(Lst_of_lts))
Lst_of_lts_nodops= [list(i) for i in {tuple(sorted(i)) for i in Lst_of_lts_2}]
For some reason, it doesn’t remove duplicates.
The dimensions remain the same-
Any suggestions?
Also tried many options such as:
Remove duplicate items from lists in Python lists and
Remove duplicated lists in list of lists in Python
If you are removing duplicates just in the list itself, you can use set.
a = np.random.randint(0,5,(10,10)).tolist()
a
Out[128]:
[[0, 3, 0, 2, 4, 4, 0, 0, 3, 3],
[2, 4, 0, 2, 4, 2, 2, 4, 3, 1],
[3, 2, 0, 1, 2, 0, 2, 0, 2, 1],
[3, 1, 4, 1, 0, 1, 4, 4, 3, 4],
[2, 0, 1, 1, 0, 4, 1, 4, 2, 3],
[0, 0, 1, 3, 4, 3, 1, 3, 0, 1],
[1, 2, 0, 2, 1, 3, 4, 2, 2, 0],
[3, 3, 2, 2, 0, 4, 1, 1, 0, 0],
[0, 1, 3, 0, 4, 4, 2, 1, 1, 4],
[0, 1, 4, 4, 0, 1, 3, 2, 1, 1]]
[list(set(i)) for i in a]
Out[129]:
[[0, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3],
[0, 1, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]]
Or if you want to preserve the order of the element, you can use dict.fromkeys
[list(dict.fromkeys(i)) for i in a]
Out[133]:
[[0, 3, 2, 4],
[2, 4, 0, 3, 1],
[3, 2, 0, 1],
[3, 1, 4, 0],
[2, 0, 1, 4, 3],
[0, 1, 3, 4],
[1, 2, 0, 3, 4],
[3, 2, 0, 4, 1],
[0, 1, 3, 4, 2],
[0, 1, 4, 3, 2]]
import pandas as pd
import os
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
predictions = model.predict_generator(Br_test_generator, steps=test_steps_per_epoch)
predicted_classes = np.argmax(predictions, axis=1)
predicted_classes
output= array([3, 1, 0, 3, 5, 0, 0, 0, 6, 0, 0, 3, 6, 0, 1, 0, 0, 2, 2, 2, 2, 2,
1, 1, 0, 2, 2, 6, 0, 0, 0, 1, 1, 0, 0, 2, 0, 1, 1, 1, 1, 1, 1, 1,
6, 0, 5, 1, 3, 1, 0, 2, 2, 1, 1, 1, 1, 2, 2, 2, 4, 1, 5, 1, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 2, 2, 5, 2, 5, 5, 5, 2, 2, 2, 2,
1, 3, 5, 5, 2, 2, 5, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 2, 1, 2, 1, 5,
2, 2, 2, 5, 3, 1, 3, 3, 1, 3, 3, 3, 1, 1, 0, 1, 5, 0, 2, 5, 5, 4,
4, 4, 4, 4, 6, 4, 4, 4, 5, 0, 4, 4, 4, 4, 4, 5, 6, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 2, 2, 5, 5, 6, 5,
5, 6, 1, 6, 4, 5, 4, 1, 4, 5, 0, 2, 5, 5, 5, 2, 2, 2, 6, 6, 5, 6,
6, 6, 6, 6, 4, 6, 2, 6, 6, 2, 0, 2, 5, 6, 6, 6, 4, 4, 0, 6])
true_classes = Bre_test_generator.classes
class_labels = list(Bre_test_generator.class_indices.keys())
class_labels
output=['1B', '2B', '3B', 'CA', 'FB', 'MB', 'NB']
I want my predicted_classes to match the corresponding class_labels and I also want to output the result in csv.
I want my csv to have two columns: the image ID and the predicted classs_labels
I have a big matrix A with shape (10, 10)
array([[2, 1, 2, 1, 1, 4, 3, 2, 2, 2],
[3, 2, 1, 2, 3, 3, 2, 3, 2, 4],
[1, 3, 3, 4, 2, 4, 4, 3, 4, 1],
[1, 3, 1, 3, 3, 1, 4, 2, 1, 2],
[3, 3, 1, 3, 3, 2, 3, 4, 3, 2],
[2, 4, 1, 4, 2, 1, 1, 2, 1, 1],
[2, 3, 2, 3, 1, 4, 3, 1, 2, 3],
[3, 1, 3, 2, 2, 4, 2, 3, 3, 3],
[1, 2, 3, 2, 1, 3, 4, 4, 1, 3],
[3, 1, 3, 2, 4, 3, 1, 1, 1, 1]])
and an array of positions B with shape (5, 2)
array([[4, 5], # row 4, column 5
[2, 1],
[2, 5],
[4, 1],
[6, 7]])
and several small matrices C with shape (5, 2, 2)
array([[[7, 9],
[6, 7]],
[[6, 6],
[9, 6]],
[[9, 6],
[8, 9]],
[[8, 7],
[8, 7]],
[[8, 6],
[7, 7]]])
Now, I want to assign these 5 small matrices to the large matrix. The positions are the position for the up-left corner of the small matrix. If there exists overlapping area, we can use the last one, maximum or just sum it up. The effect I want looks like
A[B] += C
A for loop implementation looks like:
for i in range(B.shape[0]):
A[B[i][0]:B[i][0]+2,B[i][1]:B[i][1]+2] += C[i]
The expected result looks like
array([[ 2, 1, 2, 1, 1, 4, 3, 2, 2, 2],
[ 3, 2, 1, 2, 3, 3, 2, 3, 2, 4],
[ 1, 9, 9, 4, 2, 13, 10, 3, 4, 1],
[ 1, 12, 7, 3, 3, 9, 13, 2, 1, 2],
[ 3, 11, 8, 3, 3, 9, 12, 4, 3, 2],
[ 2, 12, 8, 4, 2, 7, 8, 2, 1, 1],
[ 2, 3, 2, 3, 1, 4, 3, 9, 8, 3],
[ 3, 1, 3, 2, 2, 4, 2, 10, 10, 3],
[ 1, 2, 3, 2, 1, 3, 4, 4, 1, 3],
[ 3, 1, 3, 2, 4, 3, 1, 1, 1, 1]])
Is there a solution without for loop?
Your arrays:
In [58]: A = np.array([[2, 1, 2, 1, 1, 4, 3, 2, 2, 2],
...: [3, 2, 1, 2, 3, 3, 2, 3, 2, 4],
...: [1, 3, 3, 4, 2, 4, 4, 3, 4, 1],
...: [1, 3, 1, 3, 3, 1, 4, 2, 1, 2],
...: [3, 3, 1, 3, 3, 2, 3, 4, 3, 2],
...: [2, 4, 1, 4, 2, 1, 1, 2, 1, 1],
...: [2, 3, 2, 3, 1, 4, 3, 1, 2, 3],
...: [3, 1, 3, 2, 2, 4, 2, 3, 3, 3],
...: [1, 2, 3, 2, 1, 3, 4, 4, 1, 3],
...: [3, 1, 3, 2, 4, 3, 1, 1, 1, 1]])
In [59]: B=np.array([[4, 5], # row 4, column 5
...: [2, 1],
...: [2, 5],
...: [4, 1],
...: [6, 7]])
In [60]: C=np.array([[[7, 9],
...: [6, 7]],
...:
...: [[6, 6],
...: [9, 6]],
...:
...: [[9, 6],
...: [8, 9]],
...:
...: [[8, 7],
...: [8, 7]],
...:
...: [[8, 6],
...: [7, 7]]])
Your iteration, cleaned up a bit:
In [72]: for cnt,(i,j) in enumerate(B):
...: A[i:i+2, j:j+2] += C[cnt]
...:
In [73]: A
Out[73]:
array([[ 2, 1, 2, 1, 1, 4, 3, 2, 2, 2],
[ 3, 2, 1, 2, 3, 3, 2, 3, 2, 4],
[ 1, 9, 9, 4, 2, 13, 10, 3, 4, 1],
[ 1, 12, 7, 3, 3, 9, 13, 2, 1, 2],
[ 3, 11, 8, 3, 3, 9, 12, 4, 3, 2],
[ 2, 12, 8, 4, 2, 7, 8, 2, 1, 1],
[ 2, 3, 2, 3, 1, 4, 3, 9, 8, 3],
[ 3, 1, 3, 2, 2, 4, 2, 10, 10, 3],
[ 1, 2, 3, 2, 1, 3, 4, 4, 1, 3],
[ 3, 1, 3, 2, 4, 3, 1, 1, 1, 1]])
And to make the action clearer, lets start with a 0 array:
In [76]: A = np.zeros_like(Acopy)
In [77]: for cnt,(i,j) in enumerate(B):
...: A[i:i+2, j:j+2] += C[cnt]
...:
In [78]: A
Out[78]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 6, 6, 0, 0, 9, 6, 0, 0, 0],
[0, 9, 6, 0, 0, 8, 9, 0, 0, 0],
[0, 8, 7, 0, 0, 7, 9, 0, 0, 0],
[0, 8, 7, 0, 0, 6, 7, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 8, 6, 0],
[0, 0, 0, 0, 0, 0, 0, 7, 7, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
I don't see overlap, so I think we could construct an index array from B, that would allow us to:
A[B1] += C
and if there was a overlap, it would write the last C value.
If we don't like that, there is the np.add.at ufunc that can perform unbuffered addition (or even np.max.at).
But it will take some time to work out the required B1 indices.
edit
Here's a way of using +=. I'm using linspace to construct a multidimensional index, which will be used inplace of the slices. Getting shapes right took a lot of trial and error and testing (in an interactive session). As long as the blocks don't overlap this is fast and correct. But as documented with np.add.at, this won't match the iterative approach when there are duplicate indices.
In [125]: B1 = B+2
In [126]: I = np.linspace(B,B1,2,endpoint=False).astype(int)
In [127]: A1 =np.zeros_like(Acopy)
In [128]: A1[I[:,:,0][:,None], I[:,:,1]] += C.transpose(1,2,0)
In [129]: np.allclose(A1,A)
Out[129]: True
I is a (2,5,2) shape array, where the first 2 in the number of "steps"
In [130]: I
Out[130]:
array([[[4, 5],
[2, 1],
[2, 5],
[4, 1],
[6, 7]],
[[5, 6],
[3, 2],
[3, 6],
[5, 2],
[7, 8]]])
And since the C subarrays are (2,2), this is the same as: np.stack([B,B+1])
The C transpose is needed since this indexing of A1 produces a (2,2,5) array:
In [134]: A1[I[:,:,0][:,None], I[:,:,1]]
Out[134]:
array([[[7, 6, 9, 8, 8],
[9, 6, 6, 7, 6]],
[[6, 9, 8, 8, 7],
[7, 6, 9, 7, 7]]])
In [135]: _.shape
Out[135]: (2, 2, 5)
If some blocks overlap, np.add.at can be used to sum the overlaps:
In [137]: A1 =np.zeros_like(Acopy)
In [138]: np.add.at(A1, (I[:,:,0][:,None], I[:,:,1]), C.transpose(1,2,0))
In [140]: np.allclose(A1,A)
Out[140]: True
or for the largest
In [143]: np.maximum.at(A1, (I[:,:,0][:,None], I[:,:,1]), C.transpose(1,2,0))
In [144]: np.allclose(A1,A)
A simple forloop can solve this:
import numpy as np
initial = np.array([
[2, 1, 2, 1, 1, 4, 3, 2, 2, 2], [3, 2, 1, 2, 3, 3, 2, 3, 2, 4], [1, 3, 3, 4, 2, 4, 4, 3, 4, 1], [1, 3, 1, 3, 3, 1, 4, 2, 1, 2],
[3, 3, 1, 3, 3, 2, 3, 4, 3, 2], [2, 4, 1, 4, 2, 1, 1, 2, 1, 1], [2, 3, 2, 3, 1, 4, 3, 1, 2, 3], [3, 1, 3, 2, 2, 4, 2, 3, 3, 3],
[1, 2, 3, 2, 1, 3, 4, 4, 1, 3], [3, 1, 3, 2, 4, 3, 1, 1, 1, 1],
])
offsets = np.array([[4, 5], [2, 1], [2, 5], [4, 1], [6, 7]])
subarrays = np.array([
[[7, 9], [6, 7]], [[6, 6], [9, 6]], [[9, 6], [8, 9]],
[[8, 7], [8, 7]], [[8, 6], [7, 7]],
])
for subarray, offset in zip(subarrays, offsets):
(a, b), (c, d) = offset, subarray.shape
initial[a:a+c, b:b+d] += subarray
print(initial)
See, what I have tried, without using any kind of loop
import numpy as np
A=np.array([[2, 1, 2, 1, 1, 4, 3, 2, 2, 2],
[3, 2, 1, 2, 3, 3, 2, 3, 2, 4],
[1, 3, 3, 4, 2, 4, 4, 3, 4, 1],
[1, 3, 1, 3, 3, 1, 4, 2, 1, 2],
[3, 3, 1, 3, 3, 2, 3, 4, 3, 2],
[2, 4, 1, 4, 2, 1, 1, 2, 1, 1],
[2, 3, 2, 3, 1, 4, 3, 1, 2, 3],
[3, 1, 3, 2, 2, 4, 2, 3, 3, 3],
[1, 2, 3, 2, 1, 3, 4, 4, 1, 3],
[3, 1, 3, 2, 4, 3, 1, 1, 1, 1]])
B= np.array([[4, 5], # row 4, column 5
[2, 1],
[2, 5],
[4, 1],
[6, 7]])
C=np.array([[[7, 9],
[6, 7]],
[[6, 6],
[9, 6]],
[[9, 6],
[8, 9]],
[[8, 7],
[8, 7]],
[[8, 6],
[7, 7]]])
D= np.array([[ 2, 1, 2, 1, 1, 4, 3, 2, 2, 2], # this is required
[ 3, 2, 1, 2, 3, 3, 2, 3, 2, 4],
[ 1, 9, 9, 4, 2, 13, 10, 3, 4, 1],
[ 1, 12, 7, 3, 3, 9, 13, 2, 1, 2],
[ 3, 11, 8, 3, 3, 9, 12, 4, 3, 2],
[ 2, 12, 8, 4, 2, 7, 8, 2, 1, 1],
[ 2, 3, 2, 3, 1, 4, 3, 9, 8, 3],
[ 3, 1, 3, 2, 2, 4, 2, 10, 10, 3],
[ 1, 2, 3, 2, 1, 3, 4, 4, 1, 3],
[ 3, 1, 3, 2, 4, 3, 1, 1, 1, 1]])
We need A==D.
I created row and column indexes for all value of C.
b_row=np.repeat(np.c_[B[:,0],B[:,0]+1], repeats=2, axis=1).ravel()
b_col=np.repeat(np.c_[B[:,1],B[:,1]+1], repeats=2, axis=0).ravel()
print(np.c_[bx,by]) # to see indexes
A[b_row,b_col]+=C.ravel()
Now you can check
print(A==D)
False in (A==D)
Suppose I have a function that can calculate the distance between two lists.
def get_distance(list_a, list_b):
"""returns euclidian distance between 2 lists"""
return sum((p-q)**2 for p, q in zip(list_a, list_b)) ** 0.5
Suppose I have another function with 4 parameters. my_data is a matrix(list of lists) with the same number of columns as my_list(single list). I am trying to find the k rows of my_data which have the k lowest distances to my_list and I have to use the above function to do so. After finding these k rows, it should find and return the related rows in the matrix labels. This is what I tried but was unsure of the next step
For example, if k = 4, the function finds the 4 rows with the lowest distances to my_list in my_data. If these rows were 5, 11, 14, 17, it should return a list of lists containing solely the rows 5, 11, 14, 17 from the matrix labels.
def get_k_nearest_distance(my_list, my_data, labels, k):
matrix = my_data
for x in matrix:
for i in range(k):
my_list = [5, 3, 2, 8, 5, 10, 8, 1, 2]
labels = [[0], [0], [0], [1], [0], [0], [0], [1], [0], [0], [0], [1], [0], [1], [1], [0], [0], [0], [1], [1]]
my_data = [[1, 3, 3, 2, 2, 1, 7, 2, 1], [4, 3, 1, 1, 2, 1, 4, 8, 1], [3, 1, 3, 1, 2, 1, 2, 1, 1], [5, 4, 6, 6, 4, 10, 4, 3, 1], [5, 1, 2, 1, 2, 1, 2, 1, 1], [3, 1, 1, 1, 2, 1, 2, 1, 1], [1, 1, 1, 1, 2, 1, 1, 1, 1], [10, 10, 9, 3, 7, 5, 3, 5, 1], [4, 1, 1, 1, 2, 1, 3, 1, 1], [5, 1, 1, 1, 1, 1, 1, 1, 1], [3, 1, 4, 1, 2, 1, 1, 1, 1], [5, 7, 10, 10, 5, 10, 10, 10, 1], [5, 1, 1, 1, 2, 1, 1, 1, 1], [7, 4, 7, 4, 3, 7, 7, 6, 1], [10, 6, 6, 3, 4, 5, 3, 6, 1], [4, 1, 1, 1, 2, 1, 1, 1, 1], [1, 1, 1, 1, 2, 1, 3, 1, 1], [5, 1, 1, 2, 2, 1, 2, 1, 1], [10, 10, 10, 10, 3, 10, 10, 6, 1], [5, 10, 10, 10, 5, 2, 8, 5, 1]]
You want to create a BallTree and query it for k=4
my_list = [[5, 3, 2, 8, 5, 10, 8, 1, 2]]
labels = [[0], [0], [0], [1], [0], [0], [0], [1], [0], [0], [0], [1], [0], [1], [1], [0], [0], [0], [1], [1]]
my_data = [[1, 3, 3, 2, 2, 1, 7, 2, 1],
[4, 3, 1, 1, 2, 1, 4, 8, 1],
[3, 1, 3, 1, 2, 1, 2, 1, 1],
[5, 4, 6, 6, 4, 10, 4, 3, 1],
[5, 1, 2, 1, 2, 1, 2, 1, 1],
[3, 1, 1, 1, 2, 1, 2, 1, 1],
[1, 1, 1, 1, 2, 1, 1, 1, 1],
[10, 10, 9, 3, 7, 5, 3, 5, 1],
[4, 1, 1, 1, 2, 1, 3, 1, 1],
[5, 1, 1, 1, 1, 1, 1, 1, 1],
[3, 1, 4, 1, 2, 1, 1, 1, 1],
[5, 7, 10, 10, 5, 10, 10, 10, 1],
[5, 1, 1, 1, 2, 1, 1, 1, 1],
[7, 4, 7, 4, 3, 7, 7, 6, 1],
[10, 6, 6, 3, 4, 5, 3, 6, 1],
[4, 1, 1, 1, 2, 1, 1, 1, 1],
[1, 1, 1, 1, 2, 1, 3, 1, 1],
[5, 1, 1, 2, 2, 1, 2, 1, 1],
[10, 10, 10, 10, 3, 10, 10, 6, 1],
[5, 10, 10, 10, 5, 2, 8, 5, 1]]
I changed the data of my_list to make sure the dimensions are as sklearn BallTree expects.
convert to numpy
np_data = np.array( my_data )
np_mylist = np.array( my_list )
Create a BallTree
import numpy as np
from sklearn.neighbors import BallTree
tree = BallTree(np_data, leaf_size=10)
And query it with k=4
distances, indici = tree.query( my_list ,k=4)
And you have
print(indici)
with output
[[ 3 13 0 14]]
About the default Metric
the distance metric to use for the tree. Default=’minkowski’ with p=2 (that is, a euclidean metric). See the documentation of the DistanceMetric class for a list of available metrics. ball_tree.valid_metrics gives a list of the metrics which are valid for BallTree.
I add a answer only using numpy.
import numpy as np
my_list = [5, 3, 2, 8, 5, 10, 8, 1, 2]
labels = [[0], [0], [0], [1], [0], [0], [0], [1], [0], [0], [0], [1], [0], [1], [1], [0], [0], [0], [1], [1]]
my_data = [[1, 3, 3, 2, 2, 1, 7, 2, 1],
[4, 3, 1, 1, 2, 1, 4, 8, 1],
[3, 1, 3, 1, 2, 1, 2, 1, 1],
[5, 4, 6, 6, 4, 10, 4, 3, 1],
[5, 1, 2, 1, 2, 1, 2, 1, 1],
[3, 1, 1, 1, 2, 1, 2, 1, 1],
[1, 1, 1, 1, 2, 1, 1, 1, 1],
[10, 10, 9, 3, 7, 5, 3, 5, 1],
[4, 1, 1, 1, 2, 1, 3, 1, 1],
[5, 1, 1, 1, 1, 1, 1, 1, 1],
[3, 1, 4, 1, 2, 1, 1, 1, 1],
[5, 7, 10, 10, 5, 10, 10, 10, 1],
[5, 1, 1, 1, 2, 1, 1, 1, 1],
[7, 4, 7, 4, 3, 7, 7, 6, 1],
[10, 6, 6, 3, 4, 5, 3, 6, 1],
[4, 1, 1, 1, 2, 1, 1, 1, 1],
[1, 1, 1, 1, 2, 1, 3, 1, 1],
[5, 1, 1, 2, 2, 1, 2, 1, 1],
[10, 10, 10, 10, 3, 10, 10, 6, 1],
[5, 10, 10, 10, 5, 2, 8, 5, 1]]
Here we make them numpy arrays
np_data = np.array( my_data )
np_mylist = np.array( my_list )
We use numpy's strength to calculate the distance
distances = np.sum( (np_data - np_mylist)**2, axis=1)
Left to do is a argsort, and get only the first k=4
sorted_indici = np.argsort( distances )
sorted_indici[:4]
Which returns
array([ 3, 13, 0, 14])
How would I go about going converting a list of lists of ints into a matrix plot in Python?
The example data set is:
[[3, 5, 3, 5, 2, 3, 2, 4, 3, 0, 5, 0, 3, 2],
[5, 2, 2, 0, 0, 3, 2, 1, 0, 5, 3, 5, 0, 0],
[2, 5, 3, 1, 1, 3, 3, 0, 0, 5, 4, 4, 3, 3],
[4, 1, 4, 2, 1, 4, 5, 1, 2, 2, 0, 1, 2, 3],
[5, 1, 1, 1, 5, 2, 5, 0, 4, 0, 2, 4, 4, 5],
[5, 1, 0, 4, 5, 5, 4, 1, 3, 3, 1, 1, 0, 1],
[3, 2, 2, 4, 3, 1, 5, 5, 0, 4, 3, 2, 4, 1],
[4, 0, 1, 3, 2, 1, 2, 1, 0, 1, 5, 4, 2, 0],
[2, 0, 4, 0, 4, 5, 1, 2, 1, 0, 3, 4, 3, 1],
[2, 3, 4, 5, 4, 5, 0, 3, 3, 0, 2, 4, 4, 5],
[5, 2, 4, 3, 3, 0, 5, 4, 0, 3, 4, 3, 2, 1],
[3, 0, 4, 4, 4, 1, 4, 1, 3, 5, 1, 2, 1, 1],
[3, 4, 2, 5, 2, 5, 1, 3, 5, 1, 4, 3, 4, 1],
[0, 1, 1, 2, 3, 1, 2, 0, 1, 2, 4, 4, 2, 1]]
To give you an idea of what I'm looking for, the function MatrixPlot in Mathematica gives me this image for this data set:
Thanks!
You may try
from pylab import *
A = rand(5,5)
figure(1)
imshow(A, interpolation='nearest')
grid(True)
source
Perhaps matshow() from matplotlib is what you need.
You can also use pyplot from matplotlib, follows the code:
from matplotlib import pyplot as plt
plt.imshow(
[[3, 5, 3, 5, 2, 3, 2, 4, 3, 0, 5, 0, 3, 2],
[5, 2, 2, 0, 0, 3, 2, 1, 0, 5, 3, 5, 0, 0],
[2, 5, 3, 1, 1, 3, 3, 0, 0, 5, 4, 4, 3, 3],
[4, 1, 4, 2, 1, 4, 5, 1, 2, 2, 0, 1, 2, 3],
[5, 1, 1, 1, 5, 2, 5, 0, 4, 0, 2, 4, 4, 5],
[5, 1, 0, 4, 5, 5, 4, 1, 3, 3, 1, 1, 0, 1],
[3, 2, 2, 4, 3, 1, 5, 5, 0, 4, 3, 2, 4, 1],
[4, 0, 1, 3, 2, 1, 2, 1, 0, 1, 5, 4, 2, 0],
[2, 0, 4, 0, 4, 5, 1, 2, 1, 0, 3, 4, 3, 1],
[2, 3, 4, 5, 4, 5, 0, 3, 3, 0, 2, 4, 4, 5],
[5, 2, 4, 3, 3, 0, 5, 4, 0, 3, 4, 3, 2, 1],
[3, 0, 4, 4, 4, 1, 4, 1, 3, 5, 1, 2, 1, 1],
[3, 4, 2, 5, 2, 5, 1, 3, 5, 1, 4, 3, 4, 1],
[0, 1, 1, 2, 3, 1, 2, 0, 1, 2, 4, 4, 2, 1]], interpolation='nearest')
plt.show()
The output would be: