Inconsistent shape between the condition and the input while using seaborn - python

I am trying to plot a heatmap with seaborn. Here is the list that I am trying to plot:
b = [5, 4, 4, 4, 13, 4, 4, 1, 9, 4, 3, 9, 1, 4, 4, 1, 7, 1, 5, 3, 7, 1, 9, 4, 3, 9, 5, 4, 2, 1, 4, 1, 9, 4, 3, 9, 4, 8, 1, 7, 1, 9, 4, 8, 1, 7, 1, 4, 8, 1, 7, 1, 4, 1, 7, 1, 4, 10, 4, 3, 4, 7, 1, 8, 5, 10, 8, 9, 4, 1, 3, 9, 4, 1, 9, 4, 3, 7, 7, 1, 1, 3, 4, 9, 5, 5, 4, 1, 1, 9, 4, 9, 4, 7, 1, 9, 4, 10, 9, 4, 4, 4, 8, 10, 3, 9, 5, 4, 4, 1, 3, 9, 4, 10, 5, 4, 1, 1, 8, 1, 7, 5, 1, 8, 8, 5, 3, 1, 8, 8, 8, 1, 3, 4, 2, 1, 2, 9, 4, 10, 1, 5, 3, 9, 5, 4, 4, 4, 1, 1, 7, 1, 8, 2, 1, 8, 5, 9, 5, 10, 9, 5, 4, 1, 10, 7, 1, 8, 5, 2, 1, 3, 4, 7, 1, 2, 1, 7, 1, 4, 4, 8, 5, 3, 7, 1, 2, 1, 10, 9, 4, 1, 2, 1, 3, 9, 4, 10, 9, 1, 9, 5, 4, 3, 9, 4, 1, 8, 5, 9, 4, 1, 1, 3, 9, 4, 9, 5, 4, 1, 1, 9, 4, 3, 4, 10, 1, 9, 4, 3, 4, 10, 7, 1, 7, 1, 9, 4, 3, 4, 4, 1, 1, 9, 5, 4, 3, 5, 4, 1, 8, 5, 7, 1, 3, 9, 4, 10, 9, 4, 9, 1, 8, 5, 3, 9, 4, 1, 3, 9, 5, 3, 9, 4, 1, 3, 4, 4, 4, 8, 8, 3, 9, 5, 4, 3, 5, 4, 10, 4, 7, 1, 5, 7, 1, 1, 3, 9, 4, 10, 4, 4, 1, 9, 4, 1, 1, 5, 4, 4, 3, 5, 4, 3, 5, 4, 3, 4, 4, 1, 1, 2, 1, 4, 3, 5, 4, 3, 4, 4, 4, 4, 4, 1, 1, 8, 8, 9, 5, 8, 4, 7, 1, 2, 4, 3, 9, 4, 10, 1, 1, 3, 4, 7, 1, 4, 1, 1, 8, 5, 3, 9, 5, 4, 10, 8, 3, 4, 4, 1, 1, 3, 9, 4, 2, 5, 5, 4, 4, 1, 1, 2, 1, 7, 3, 4, 9, 1, 4, 10, 9, 4, 9, 6, 4, 11, 5, 4, 10, 4, 4, 1, 9, 5, 4, 3, 9, 4, 3, 9, 5, 12, 4, 4, 4, 1, 1, 3, 9, 5, 4, 1, 3, 5, 4, 4, 4, 10, 1, 4, 4, 10, 4, 1, 5, 3, 5, 4, 4, 7, 1, 8, 4, 1, 2, 1, 9, 4, 3, 7, 1, 9, 5, 4, 4, 10, 9, 5, 4, 4, 3, 5, 10, 5, 4, 4, 1, 9, 4, 7, 1, 5, 3, 1, 4, 3, 4, 5, 3, 1, 5, 4, 5, 3, 4, 10, 8, 5, 3, 9, 4, 3, 4, 3, 7, 9, 1, 9, 4, 4, 3, 9, 4, 4, 4, 8, 9, 4, 3, 9, 5, 4, 4, 2, 5, 4, 1, 8, 3, 9, 4, 4, 10, 7, 1, 1, 9, 4, 3, 4, 9, 4, 1, 2, 1, 10, 1, 9, 4, 2, 1, 4, 1, 8, 5, 4, 3, 9, 4, 1, 9, 4, 3, 9, 3, 9, 4, 1, 4, 4, 1, 7, 7, 1, 2, 1, 3, 4, 2, 1, 4, 10, 1, 7, 1, 3, 7, 1, 11, 1, 3, 9, 4, 1, 9, 4, 7, 1, 1, 4, 2, 1, 9, 4, 3, 4, 1, 8, 1, 9, 4, 3, 4, 1, 8, 4, 1, 7, 7, 1, 7, 1, 4, 3, 9, 5, 4, 7, 1, 8, 9, 5, 4, 7, 1, 3, 9, 4, 3, 4, 7, 1, 1]
This is the code that I am trying to use to plot:
import numpy as np
import seaborn as sns
from matplotlib.colors import ListedColormap
data = np.asarray(b)
sns.heatmap( data,cmap=ListedColormap(['green', 'yellow', 'red']))
After running the above code, this is the error I am getting:
IndexError: Inconsistent shape between the condition and the input (got (633, 1) and (633,))
I did check some other answers but none of them answered my concerns.
I am not quite sure as to where the problem lies. Here is the result that I get when run data.shape:
(633,)
Any help will be appreciated. Thanks!

import numpy as np
import seaborn as sns
from matplotlib.colors import ListedColormap
import matplotlib.pyplot as plt
data = np.asarray(b).reshape(633,1)
sns.heatmap(data,cmap=ListedColormap(['green', 'yellow', 'red']))
plt.show()
heatmap requires 2D dataset
https://seaborn.pydata.org/generated/seaborn.heatmap.html

From the docs:
data : rectangular dataset 2D dataset that can be coerced into an
ndarray. If a Pandas DataFrame is provided, the index/column
information will be used to label the columns and rows.
You have to transform your data into a 2D dataset.
One way to do so is:
sns.heatmap(data[:, np.newaxis], cmap=ListedColormap(['green', 'yellow', 'red']))

Related

Confusion matrix: ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets

I'm working on a multiclass classification problem (12 classes), but I can't create the confusion matrix.
What I'm trying to do is:
from sklearn.metrics import multilabel_confusion_matrix
pred = model_BiLSTM.predict(X_val)
y_unique = np.unique(y_val)
mcm =confusion_matrix(y_val, pred, labels = y_unique)
But as per the title, the returned error is: "ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets", and the same happens when using confusion_matrix().
Instead, this is the output of
y_val, pred, y_val.shape, pred.shape
16/16 [==============================] - 0s 6ms/step
(array([11, 8, 3, 3, 9, 9, 9, 3, 3, 1, 3, 6, 6, 6, 2, 9, 1,
3, 10, 6, 9, 2, 9, 4, 3, 9, 6, 9, 3, 3, 3, 3, 8, 1,
9, 9, 9, 9, 2, 1, 9, 3, 9, 3, 9, 9, 2, 10, 3, 2, 9,
3, 8, 6, 8, 9, 9, 6, 6, 9, 8, 7, 9, 2, 9, 9, 2, 3,
9, 9, 2, 6, 3, 7, 11, 9, 9, 2, 11, 6, 7, 11, 6, 9, 2,
6, 9, 2, 9, 9, 3, 6, 9, 1, 11, 4, 4, 2, 6, 2, 9, 3,
10, 3, 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, 2, 5, 7, 9, 3, 9, 9, 1, 2, 9, 6, 8, 2,
3, 4, 2, 3, 3, 2, 6, 5, 9, 5, 2, 2, 2, 9, 9, 6, 2,
9, 4, 9, 9, 2, 3, 8, 11, 9, 9], dtype=int32),
array([[5.7949889e-03, 2.5301890e-03, 5.9659913e-05, ..., 2.7534673e-03,
1.8798949e-03, 4.0977496e-01],
[2.1629781e-04, 1.0219574e-02, 1.2285617e-03, ..., 4.0498661e-04,
3.6948815e-04, 8.3618681e-04],
[8.1547890e-03, 1.1354284e-04, 1.3678521e-04, ..., 3.6535120e-01,
1.1546685e-03, 3.5349184e-03],
...,
[1.1976730e-03, 6.8558909e-02, 8.7605380e-03, ..., 1.7384565e-01,
5.4570078e-04, 2.0005915e-02],
[2.1097453e-02, 7.7744485e-03, 2.5690982e-01, ..., 5.4854238e-01,
3.9467164e-03, 1.6034273e-02],
[2.0812787e-03, 1.6885218e-05, 4.7070305e-05, ..., 4.3611538e-01,
3.6522493e-04, 1.4385413e-02]], dtype=float32),
(486,),
(486, 12))
If it helps, this is my model:
def build_BiLSTM_classifier(input_shape, classes):
input_layer = tfkl.Input(shape=input_shape, name='Input')
bilstm = tfkl.Bidirectional(tfkl.LSTM(128, return_sequences=True))(input_layer)
bilstm = tfkl.Bidirectional(tfkl.LSTM(128))(bilstm)
dropout = tfkl.Dropout(.5, seed=seed)(bilstm)
classifier = tfkl.Dense(128, activation='tanh')(dropout)
output_layer = tfkl.Dense(classes, activation='softmax')(classifier)
model = tfk.Model(inputs=input_layer, outputs=output_layer, name='model')
model.compile(loss=tfk.losses.SparseCategoricalCrossentropy(), optimizer=tfk.optimizers.Adam(), metrics='accuracy')
return model
What could I try to do?
Your pred array appears to contain class scores rather than predictions. Try pred.argmax(axis=1) instead.

Python: How to efficiently create all possible 2 element swaps of an array?

I try to generate all possible 2-element swaps of a given array.
For example:
candidate = [ 5, 9, 1, 8, 3, 7, 10, 6, 4, 2]
result = [[ 9, 5, 1, 8, 3, 7, 10, 6,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 2]
[ 5, 9, 1, 8, 6, 7, 10, 3, 4, 2]
[ 5, 9, 1, 8, 4, 7, 10, 6, 3, 2]
[ 5, 9, 1, 8, 2, 7, 10, 6, 4, 3]
[ 5, 9, 1, 8, 3, 10, 7, 6, 4, 2]
[ 5, 9, 1, 8, 3, 6, 10, 7, 4, 2]
[ 5, 9, 1, 8, 3, 4, 10, 6, 7, 2]
[ 5, 9, 1, 8, 3, 2, 10, 6, 4, 7]
[ 5, 9, 1, 8, 3, 7, 6, 10, 4, 2]
[ 5, 9, 1, 8, 3, 7, 4, 6, 10, 2]
[ 5, 9, 1, 8, 3, 7, 2, 6, 4, 10]
[ 5, 9, 1, 8, 3, 7, 10, 4, 6, 2]
[ 5, 9, 1, 8, 3, 7, 10, 2, 4, 6]
[ 5, 9, 1, 8, 3, 7, 10, 6, 2, 4]]
I currently achive this by using two nested for-loops:
neighborhood = []
for node1 in range(candidate.size - 1):
for node2 in range(node1 + 1, candidate.size):
neighbor = np.copy(candidate)
neighbor[node1] = candidate[node2]
neighbor[node2] = candidate[node1]
neighborhood.append(neighbor)
The larger the array gets, the more inefficient and slower it becomes. Is there a more efficient way here that can also process arrays with three-digit contents?
Thank you!
You can use a generator if you need to use those arrays one by one (in this way, you don't need to memorize them all, you need very little space):
from itertools import combinations
def gen(lst):
for i, j in combinations(range(len(lst)), 2):
yield lst[:i] + lst[j] + lst[i:j] + lst[i] + lst[j:]
And then you can use it in this way:
for lst in gen(candidate):
# do something with your list with two swapped elements
This is going to save much space, but it's probably going to be still slow overall.
Here is a solution using NumPy. This is not space efficient (because it's memorizing all possible lists with swapped elements), but it might be much faster because of NumPy optimizations. Give it a try!
from itertools import combinations
from math import comb
arr = np.tile(candidate, (comb(len(candidate), 2), 1))
indices = np.array(list(combinations(range(len(candidate)), 2)))
arr[np.arange(arr.shape[0])[:, None], indices] = arr[np.arange(arr.shape[0])[:, None], np.flip(indices, axis=-1)]
Example (with candidate = [0, 1, 2, 3]):
>>> arr
array([[1, 0, 2, 3],
[2, 1, 0, 3],
[3, 1, 2, 0],
[0, 2, 1, 3],
[0, 3, 2, 1],
[0, 1, 3, 2]])
Notice that math.comb (which gives you the total number of possible lists with 2 swapped elements) is available only with python >= 3.8. Please have a look at this question to know how to replace math.comb in case you're using an older python version.
To swap only two items in any given list, I'd recommend using range with itertools.combinations. It is probably good to use a generator with the yield statement too, though if you are getting all results at once, it probably doesn't matter much.
from itertools import combinations
def swap2(l):
for pair in combinations(range(len(l)), 2):
l2 = l[:]
l2[pair[0]], l2[pair[1]] = l2[pair[1]], l2[pair[0]]
yield l2
if __name__ == "__main__":
candidate = [5, 9, 1, 8, 3, 7, 10, 6, 4, 2]
result = [l for l in swap2(candidate)]

Automatically reformat Python code in VS Code

Let's assume I copied some comma-separated data from somewhere and want to create a Python list. The data might look like this:
x = [1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9]
In PyCharm, I select this line and run "Code - Reformat Code", which produces the desired result (wrapped at column 79, PEP8):
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7,
8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5,
6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3,
4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1,
2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8,
9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6,
7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4,
5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2,
3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9,
1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9]
How can achieve this in VS Code? I tried the Rewrap extension (only works for comments) as well as "Format Document" using autopep8 and black (via the Python extension), but none of these methods work.
Use yapf and use format on save.
editor.formatOnSave
python.formatting.provider:
vs code setting
result:
x = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7,
8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5,
6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3,
4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1,
2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8,
9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6,
7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4,
5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2,
3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9,
1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9
]

How to add a list to a list of lists?

I'm coding a sudoku puzzle solver and I want to create a 9x9 grid where every cell is a list of it's own. The list sudokuGrid is the unsolved puzzle. It's a 2d list only. The list availableNumbers should be a 3d list where every empty cell (represented with a 0 in sudokuGrid) should have a list with the numbers 1-9.
How do I add the list?
sudokuGrid = []
sudokuGrid.append([0, 0, 8, 0, 0, 0, 0, 1, 0])
sudokuGrid.append([0, 9, 0, 0, 0, 0, 0, 0, 0])
sudokuGrid.append([3, 4, 0, 5, 9, 0, 0, 0, 7])
sudokuGrid.append([6, 8, 0, 0, 0, 0, 4, 0, 0])
sudokuGrid.append([0, 0, 0, 0, 7, 0, 0, 0, 0])
sudokuGrid.append([0, 0, 4, 8, 0, 0, 1, 0, 0])
sudokuGrid.append([0, 0, 6, 0, 8, 0, 0, 0, 5])
sudokuGrid.append([0, 5, 1, 0, 0, 0, 0, 2, 0])
sudokuGrid.append([0, 0, 0, 0, 2, 0, 0, 9, 0])
availableNumbers = []
for i in range (9):
for j in range(9):
if sudokuGrid[i][j] == 0:
availableNumbers[i][j][k] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
else:
availableNumbers[i][j][k] = sudokuGrid[i][j]
break
I get an error saying list index is out of range.
You need to initialize your availableNumbers list to have the same dimensions that you are trying to index to in order to add the values. You're getting an index error because availableNumbers[i][j][k] does not exist on an empty list. Also, you do not have k defined as anything. There's an easier way to do this without needing to initialize empty lists. Just use the copy module and make a copy of your sudokuGrid and replace all the 0 elements with a list of their potential values.
import copy
sudokuGrid = []
sudokuGrid.append([0, 0, 8, 0, 0, 0, 0, 1, 0])
sudokuGrid.append([0, 9, 0, 0, 0, 0, 0, 0, 0])
sudokuGrid.append([3, 4, 0, 5, 9, 0, 0, 0, 7])
sudokuGrid.append([6, 8, 0, 0, 0, 0, 4, 0, 0])
sudokuGrid.append([0, 0, 0, 0, 7, 0, 0, 0, 0])
sudokuGrid.append([0, 0, 4, 8, 0, 0, 1, 0, 0])
sudokuGrid.append([0, 0, 6, 0, 8, 0, 0, 0, 5])
sudokuGrid.append([0, 5, 1, 0, 0, 0, 0, 2, 0])
sudokuGrid.append([0, 0, 0, 0, 2, 0, 0, 9, 0])
availableNumbers = copy.deepcopy(sudokuGrid)
for i in range(0, len(availableNumbers)):
for x in range(0, len(availableNumbers[i])):
if availableNumbers[i][x] == 0:
availableNumbers[i][x] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(availableNumbers)
output:
[[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 8, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 1, [1, 2, 3, 4, 5, 6, 7, 8, 9]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], 9, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6,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, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 2, [1, 2, 3, 4, 5, 6, 7, 8, 9]],
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 2, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]]]
A few small changes will let you build up availablenumbers as you go:
sudokuGrid = []
sudokuGrid.append([0, 0, 8, 0, 0, 0, 0, 1, 0])
sudokuGrid.append([0, 9, 0, 0, 0, 0, 0, 0, 0])
sudokuGrid.append([3, 4, 0, 5, 9, 0, 0, 0, 7])
sudokuGrid.append([6, 8, 0, 0, 0, 0, 4, 0, 0])
sudokuGrid.append([0, 0, 0, 0, 7, 0, 0, 0, 0])
sudokuGrid.append([0, 0, 4, 8, 0, 0, 1, 0, 0])
sudokuGrid.append([0, 0, 6, 0, 8, 0, 0, 0, 5])
sudokuGrid.append([0, 5, 1, 0, 0, 0, 0, 2, 0])
sudokuGrid.append([0, 0, 0, 0, 2, 0, 0, 9, 0])
availableNumbers = []
for i in range (9):
availableNumbers.append([])
for j in range(9):
availableNumbers[i].append([])
if sudokuGrid[i][j] == 0:
availableNumbers[i][j] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
else:
availableNumbers[i][j] = sudokuGrid[i][j]
for a in availableNumbers:
print(a)
By appending to availableNumbers or availableNumbers[i] at the start of each loop we ensure that there is some space in availableNumbers where we can store our variables. That, along with removing your break statement and removing whatever k was will get you the following (line by line) output:
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 8, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3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 2, 3, 4, 5, 6, 7, 8, 9], 6, [1, 2, 3, 4, 5, 6, 7, 8, 9], 8, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 5]
[[1, 2, 3, 4, 5, 6, 7, 8, 9], 5, 1, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 2, [1, 2, 3, 4, 5, 6, 7, 8, 9]]
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 2, [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], 9, [1, 2, 3, 4, 5, 6, 7, 8, 9]]

Making a 10x10 grid from a list of arrays

I'm struggling to list my array as a 10x10 grid, the output I keep getting isn't what I'm looking for. I was hoping someone could help me out.
import numpy as np
x = 1
y = 1
scale = 10
nn = []
for x in range(1,scale+1):
mm = []
for y in range(1,scale+1):
xy = [x,y]
mm.append(xy)
#print(xy)
y=+1
nn.append(mm)
x=+1
nn
grid_array = np.array(nn)
grid=np.meshgrid(grid_array)
But the output I get isn't displaying 10x10
[array([ 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8, 1,
9, 1, 10, 2, 1, 2, 2, 2, 3, 2, 4, 2, 5, 2, 6, 2, 7,
2, 8, 2, 9, 2, 10, 3, 1, 3, 2, 3, 3, 3, 4, 3, 5, 3,
6, 3, 7, 3, 8, 3, 9, 3, 10, 4, 1, 4, 2, 4, 3, 4, 4,
4, 5, 4, 6, 4, 7, 4, 8, 4, 9, 4, 10, 5, 1, 5, 2, 5,
3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8, 5, 9, 5, 10, 6, 1,
6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8, 6, 9, 6,
10, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
7, 9, 7, 10, 8, 1, 8, 2, 8, 3, 8, 4, 8, 5, 8, 6, 8,
7, 8, 8, 8, 9, 8, 10, 9, 1, 9, 2, 9, 3, 9, 4, 9, 5,
9, 6, 9, 7, 9, 8, 9, 9, 9, 10, 10, 1, 10, 2, 10, 3, 10,
4, 10, 5, 10, 6, 10, 7, 10, 8, 10, 9, 10, 10])]
Edited.
This is what I have thus far, thanks for the help guys.
import numpy as np
scale = 10
array = np.empty(shape=(scale, scale, 2)).astype(int)
for x in range(1,scale+1):
for y in range(1,scale+1):
#print([x,y])
array[x-1,y-1] = [x,y]
print(array)
You can use numpy to do that. like this
np.reshape(arr, (-1,10))
See.
Convert a 1D array to a 2D array in numpy
It's pretty far from clear what you want to achieve, but if you simply want to know how to generate a 10x10 numpy array using two for loops, here is what you can do (not he most pythonic way to do it though):
import numpy as np
scale = 10
array = np.empty(shape=(scale, scale))
for x in range(scale):
for y in range(scale):
array[x,y] = 42 # replace with whatever dynamically assigned value you want there
print(array)

Categories

Resources