#๐Ÿ”’ how to graph binary 2d array of pixels?

108 messages ยท Page 1 of 1 (latest)

queen vine
#

array like this (but a lot bigger)```
[[1,0,0],
[1,0,1],
[1,1,1]]

it represents each pixel and 1 being black and 0 being white
how do I graph it?```
sturdy sableBOT
#

@queen vine

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

strong prairie
#

for graphing

queen vine
strong prairie
#

are you using jupter notebooks?

queen vine
strong prairie
#

are you using data science based libraries?

queen vine
#

I can use them sure

#

numpy

#

I am not using scipy but I can

strong prairie
#

alright, you can simply convert your pixels to numpy array

whole viper
#

You'll probably want to use matplotlib.pyplot.imshow.

queen vine
whole viper
#

It's by default normalised.

#

and you can set the vmin and vmax manually

#

and the cmap

queen vine
#

how do I fix this py img = ax.imshow( img, extent=[*big_range, *big_range], aspect="equal", interpolation="gaussian", origin="lower", )

#

I have to run it 5 times for it to work

#
img = network.calculate_outputs(pixels)
    #ax.pcolor(img, cmap='binary')
    #img = ax.pcolormesh(img, cmap='binary')
    custom_cmap = matplotlib.colors.ListedColormap(["#ADD8E6", "white"])

    img = ax.imshow(
        img,
        cmap=custom_cmap,
        extent=[*big_range, *big_range],
        aspect="equal",
        interpolation="gaussian",
        origin="lower",
    )```
whole viper
#

You look like you know more about matplotlib than I do.

queen vine
#

also half of the weights act weird with the graph so I dont think that graphs it correctly

queen vine
#

just combined and copied stuff from stackoverflow

queen vine
#

I cant make this shape with the imshow

whole viper
#
from matplotlib import pyplot as plt

...

plt.imshow(arr, cmap='gray')
plt.show()```This is all I'd end up doing.
#

But yeah, if it's not liking the lists, convert to a numpy array and it should work fine.

queen vine
#
 points = np.column_stack((x, y, colors.astype(int)))
    xs, ys = np.meshgrid(np.linspace(*big_range), np.linspace(*big_range))
    pixels = np.stack((xs.T, ys.T), axis=2)

    from scipy.spatial import ConvexHull
    
    graph = None

    def accuracy():
        nonlocal graph
        if graph:
            graph.remove()

        pointsX = []
        pointsY = []
        colors = []

        total = 0
        for point in points:
            px, py, value = point
            output = network.calculate_output((px, py))
            pointsX.append(px)
            pointsY.append(py)
            colors.append(output == value)
            
            total += np.array(output > 0).astype(int) == value

        img = network.calculate_outputs(pixels)
        
        graph = ax.scatter(pointsX, pointsY, np.array(colors) * 100, c="black")
        
    img = network.calculate_outputs(pixels)
    custom_cmap = matplotlib.colors.ListedColormap(["#ADD8E6", "white"])

    img = ax.imshow(
        img,
        cmap=custom_cmap,
        extent=[*big_range, *big_range],
        aspect="equal",
        interpolation="gaussian",
        origin="lower",
    )
#

dont question the nonlocal

whole viper
#

If you know enough to use it and that it even exists, I'm going to assume you know when and where to use it.

queen vine
#

i just am using it cause I am testing

#

@whole viper maybe it is the way I am getting the pixels? py points = np.column_stack((x, y, colors.astype(int))) xs, ys = np.meshgrid(np.linspace(*big_range), np.linspace(*big_range)) pixels = np.stack((xs.T, ys.T), axis=2)

#

I want pixels to be [(1, 2), (4, 3) ....]

#

and then after pasted through my neural network to be [[1,1,1],[0,0,1],...]

whole viper
#

Oh. That's not what I thought your data looked like.

queen vine
#

is there a way to flatten everything except the inner most (1,2),(1,2)

whole viper
queen vine
#

cause pixels ... [[-2.85714286e-02 -2.00000000e-01] [-2.85714286e-02 -1.71428571e-01] [-2.85714286e-02 -1.42857143e-01] [-2.85714286e-02 -1.14285714e-01] [-2.85714286e-02 -8.57142857e-02] [-2.85714286e-02 -5.71428571e-02] [-2.85714286e-02 -2.85714286e-02] [-2.85714286e-02 -2.77555756e-17] [-2.85714286e-02 2.85714286e-02] [-2.85714286e-02 5.71428571e-02] [-2.85714286e-02 8.57142857e-02] [-2.85714286e-02 1.14285714e-01] [-2.85714286e-02], [[-2.85714286e-02 -2.00000000e-01] [-2.85714286e-02 -1.71428571e-01] [-2.85714286e-02 -1.42857143e-01] [-2.85714286e-02 -1.14285714e-01] [-2.85714286e-02 -8.57142857e-02] [-2.85714286e-02 -5.71428571e-02] [-2.85714286e-02 -2.85714286e-02] [-2.85714286e-02 -2.77555756e-17] [-2.85714286e-02 2.85714286e-02] [-2.85714286e-02 5.71428571e-02] [-2.85714286e-02 8.57142857e-02] [-2.85714286e-02 1.14285714e-01] [-2.85714286e-02

queen vine
whole viper
#

I'm lost, sorry.

#

You started simple.

#

Now it is not.

queen vine
# whole viper I'm lost, sorry.

if I have a range from 0->1 (through linspace) how do I get all diferent pairs like (0.01, 0.01), (0.02, 0.01), (0.03, 0.01), ... (0.01, 0.02),,,

whole viper
#

What's the population?

queen vine
#

1 person

#

jk wdym

whole viper
#

!d numpy.linspace

sturdy sableBOT
#

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, *, device=None)```
Return evenly spaced numbers over a specified interval.

Returns *num* evenly spaced samples, calculated over the interval [*start*, *stop*].

The endpoint of the interval can optionally be excluded.

Changed in version 1.16.0: Non-scalar *start* and *stop* are now supported.

Changed in version 1.20.0: Values are rounded towards `-inf` instead of `0` when an integer `dtype` is specified. The old behavior can still be obtained with `np.linspace(start, stop, num).astype(int)`
whole viper
#

num

queen vine
#

anything I want

#

how slow I want it to be

whole viper
#

Give me an idea.

queen vine
#

100

#

start 0 , stop 1

whole viper
#

Is the order of the pairs significant?

queen vine
#

no

whole viper
#

Worst case scenario, how large is num?

queen vine
#

150

whole viper
#

Because you could use itertools.combinations.

queen vine
#

if it is too slow I would lower num

queen vine
whole viper
#

Eh, I think it'd just be num ** 2 to calculate.

#

That's not terrible.

queen vine
#

how do I use it

queen vine
#

my code didnt realize I only needed to do it once

whole viper
#

!d itertools.combinations

sturdy sableBOT
#

itertools.combinations(iterable, r)```
Return *r* length subsequences of elements from the input *iterable*.

The combination tuples are emitted in lexicographic ordering according to the order of the input *iterable*. So, if the input *iterable* is sorted, the output tuples will be produced in sorted order.

Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeated values in each combination.

Roughly equivalent to:
queen vine
#

itertools.combinations(np.linspace(0,1,100), 2)?

#

that prob doesnt work

whole viper
#

!e py import itertools data = [1, 2, 3] result = [*itertools.combinations(data, 2)] print(result)

sturdy sableBOT
#

@whole viper :white_check_mark: Your 3.12 eval job has completed with return code 0.

[(1, 2), (1, 3), (2, 3)]
whole viper
#

Hang on..

queen vine
#

ok I willtry that

#

while you hang on

whole viper
#

product

#

!d itertools.product

sturdy sableBOT
#

itertools.product(*iterables, repeat=1)```
Cartesian product of input iterables.

Roughly equivalent to nested for-loops in a generator expression. For example, `product(A, B)` returns the same as `((x,y) for x in A for y in B)`.

The nested loops cycle like an odometer with the rightmost element advancing on every iteration. This pattern creates a lexicographic ordering so that if the inputโ€™s iterables are sorted, the product tuples are emitted in sorted order.

To compute the product of an iterable with itself, specify the number of repetitions with the optional *repeat* keyword argument. For example, `product(A, repeat=4)` means the same as `product(A, A, A, A)`.
whole viper
#

I've used this before, but I'm having a stupid brain moment.

#

Oh, right.

#

!e py import itertools data = [1, 2, 3] result = [*itertools.product(data, repeat=2)] print(result)

queen vine
#

so itertools.product(np.linespace?

sturdy sableBOT
#

@whole viper :white_check_mark: Your 3.12 eval job has completed with return code 0.

[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
queen vine
#

ohh

#

that looks more right

whole viper
#

There we go. It needed it as a keyword argument, because *iterables.

#
for a in data:
    for b in data:
        (a, b)```Basically.
queen vine
#

oh darn it File "c:\Users\jacob\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\axes_axes.py", line 5665, in imshow
im.set_data(X)
File "c:\Users\jacob\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\image.py", line 710, in set_data
raise TypeError("Invalid shape {} for image data"
TypeError: Invalid shape (10000,) for image data

#

thats why I had ugly meshgrid

#

but then the input to my neural network is broken

#

okay so I have a bunch of pixels [(1,2),(2,2),(3,2)...] and I have whether to color the pixel white or black [0,1,0,...] @whole viper

#

how do I graph that now

#

wait what if every 100 index it becomes a new array so that the black and white are 2d
[[0,1,0,0,0,0,0,... (100 times)],
[0,1,0,0,0,0,0,... (100 times)],

#

then maybe imshow can graph that

#

but that breaks the order....

#

nvm the pixels do need order

#

!d numpy.meshgrid

sturdy sableBOT
#

numpy.meshgrid(*xi, copy=True, sparse=False, indexing='xy')```
Return a tuple of coordinate matrices from coordinate vectors.

Make N-D coordinate arrays for vectorized evaluations of N-D scalar/vector fields over N-D grids, given one-dimensional coordinate arrays x1, x2,โ€ฆ, xn.

Changed in version 1.9: 1-D and 0-D cases are allowed.
queen vine
whole viper
#

I don't understand how to help you at the present time. I don't understand the problem as it is being presented.

queen vine
#

let me test some stuff

sturdy sableBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.