Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Love makes the world go 'round, with a little help from intrinsic angular momentum.


devel / comp.lang.python / Re: How to replace a cell value with each of its contour cells and yield the corresponding datasets seperately in a list according to a Pandas-way?

SubjectAuthor
o Re: How to replace a cell value with each of its contour cells and yield the corThomas Passin

1
Re: How to replace a cell value with each of its contour cells and yield the corresponding datasets seperately in a list according to a Pandas-way?

<mailman.97.1705857944.15798.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=24917&group=comp.lang.python#24917

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!newsfeed.endofthelinebbs.com!nntp.comgw.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: lis...@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: How to replace a cell value with each of its contour cells and
yield the corresponding datasets seperately in a list according to a
Pandas-way?
Date: Sun, 21 Jan 2024 12:18:31 -0500
Lines: 197
Message-ID: <mailman.97.1705857944.15798.python-list@python.org>
References: <CAGJtH9RvLNeHFQSVk4b-TuYKKLgVbvAnoiQvC37vw5HGm2YjHg@mail.gmail.com>
<a9aa6809-2842-4e7d-9024-fb2549c2eb65@tompassin.net>
<CAGJtH9Svh5Q5B68aB+WK8JaYzV=EY-pfobi=cSoNjrTxBxv5_Q@mail.gmail.com>
<86081560-08ff-42bc-ae6d-dfb03d8b2a7d@tompassin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 1kOOjfSrS9hVnv0i04RK5g4v8M0u91dmpHUXSEQikOKg==
Cancel-Lock: sha1:XdozEeYfy0vcF/FFbckuojjA3/Q= sha256:EMwV8BumCnvxuF/QSqlZjbIS8TjLSANOujuoJMF85+8=
Return-Path: <list1@tompassin.net>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=tompassin.net header.i=@tompassin.net header.b=Znv0uUvG;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'def': 0.04; 'containing':
0.05; 'issue.': 0.05; 'math': 0.05; 'random': 0.05; 'arrays':
0.07; 'queue': 0.07; 'subject:value': 0.07; 'cell.': 0.09;
'dataframe': 0.09; 'filtering': 0.09; 'list.\xc2\xa0': 0.09;
'numpy': 0.09; 'pandas': 0.09; 'skip:z 20': 0.09; 'values.': 0.09;
'subject:list': 0.11; 'import': 0.15; 'url:mailman': 0.15; '(1,':
0.16; '1),': 0.16; '2024': 0.16; '[snip]': 0.16; 'arguments':
0.16; 'be,': 0.16; 'dataframes': 0.16; 'dataset,': 0.16;
'datasets': 0.16; 'datasets,': 0.16; 'itertools': 0.16; 'lambda':
0.16; 'main()': 0.16; 'picking': 0.16; 'received:10.0.0': 0.16;
'received:64.90': 0.16; 'received:64.90.62': 0.16;
'received:64.90.62.162': 0.16; 'received:dreamhost.com': 0.16;
'reformat': 0.16; 'separately': 0.16; 'subject:cell': 0.16;
'subject:each': 0.16; 'subject:way': 0.16; 'tuples': 0.16;
'yield': 0.16; 'wrote:': 0.16; 'python': 0.16; 'larger': 0.17;
'code.': 0.17; 'probably': 0.17; 'libraries': 0.19; 'to:addr
:python-list': 0.20; 'machine': 0.22; 'written': 0.22; 'code':
0.23; 'laptop': 0.23; 'lines': 0.23; 'skip:p 30': 0.23;
'subject:How': 0.23; 'run': 0.23; 'url-ip:188.166.95.178/32':
0.25; 'url-ip:188.166.95/24': 0.25; 'url:listinfo': 0.25; 'url-
ip:188.166/16': 0.25; 'header:User-Agent:1': 0.30; 'seem': 0.31;
'takes': 0.31; 'am,': 0.31; 'approach': 0.31; 'url-ip:188/8':
0.31; 'think': 0.32; "doesn't": 0.32; 'execution': 0.32; 'python-
list': 0.32; 'received:10.0': 0.32; 'received:mailchannels.net':
0.32; 'received:relay.mailchannels.net': 0.32; 'specified': 0.32;
'100': 0.33; 'header:In-Reply-To:1': 0.34; 'complex': 0.35;
'cell': 0.36; 'count': 0.36; 'target': 0.36; 'people': 0.36;
'those': 0.36; 'couple': 0.37; 'lists': 0.37; 'special': 0.37;
'really': 0.37; 'using': 0.37; "it's": 0.37; 'could': 0.38;
'read': 0.38; 'thanks': 0.38; 'changes': 0.39; 'single': 0.39;
'list': 0.39; 'break': 0.39; 'define': 0.40; 'program.': 0.40;
'seconds': 0.40; 'hello,': 0.40; 'want': 0.40; 'should': 0.40;
'four': 0.60; 'initial': 0.61; 'identified': 0.62; 'here': 0.62;
'skip:r 20': 0.64; 'about.': 0.64; 'skip:t 40': 0.64; 'your':
0.64; 'independent': 0.65; 'look': 0.65; 'well': 0.65; 'let':
0.66; 'skip:t 20': 0.66; 'nearly': 0.67; 'skip:t 30': 0.67;
'header:Received:6': 0.67; 'skip:e 20': 0.67; 'choose': 0.67;
'received:64': 0.67; 'skip:i 40': 0.68; 'surrounding': 0.69;
'below': 0.69; 'product': 0.71; '10%': 0.76; 'formatting': 0.76;
'factor.': 0.84; 'janv.': 0.84; 'subject: \n ': 0.84;
'\xc3\xa9crit\xc2\xa0:': 0.84; 'skip:d 30': 0.86; 'thoughts,':
0.91
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1705857513; a=rsa-sha256;
cv=none;
b=mgXHDkVyedkGd+B7+ETGYitjW+HoCusBByB1TznRQO1nCTmeSvzjeUaQ2yyS6Ws8nhPEmy
WnZ2zipyGTItwKTRYrlGc+0OT9LZ8HP6L2g/yMF+/ELrHf1hcH15m8aH2v071eqJZLzo1o
BndJJUl0kRpdNv/CT4TFcInbfAuxZ9v8ZD0jGQ+yIt68rLFFpF4yhK0kgeH5OfJocwEHUq
FuwO+S9JDQqUx6PQUhCq673wWQeJ6aUQxE397SSqad96+JTmTyZQawQpy3sANi02qb4Z3R
KHtSU/xGHlcn8l0ynwMrN95xtPycASa1SjDOXrN2Y1P2EAiy3fO+5mCKpOhiWA==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1705857513;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding:
in-reply-to:in-reply-to:references:references:dkim-signature;
bh=C7CGxHQ43SwEU2xcSTX8WdYG+M33WbvnDwhmGeIZesI=;
b=aw06R0k5DAio5RhXo/q+zC0k9GUQ1njxey0wdJV9wrYi3m4jvV9b7S8LWWwArGkEhGukLa
aTbEIJMBQOOmay7n8Axm55L8V5p8pStiLYoCv4iOqrtn9xJ4YmUwwSuda37rtPHjzQHfh6
rIzy2WoaNrjWjaiDZuCLiyi2VScXOfo98zJ+NNJXzzSAbJ+ej3YJnoq12l0f4KgJpQxIjp
lVVJFABU7gYKXIy/D0ZKQ+e/SdvXUb71gRY9uVUEwhctXBAwH/GmcVJYL9fsq5Fa87ahVc
xh1QL33NVet1S2TOZQPmCjEGJ2P/KY04nQj/H2rIeEC0eTXo75pRa59F/vaO1A==
ARC-Authentication-Results: i=1; rspamd-88587c4b9-gkn4t;
auth=pass smtp.auth=dreamhost smtp.mailfrom=list1@tompassin.net
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
X-MC-Relay: Neutral
X-MailChannels-SenderId: dreamhost|x-authsender|tpassin@tompassin.net
X-MailChannels-Auth-Id: dreamhost
X-Imminent-Robust: 1cb6b5b84955ae22_1705857513806_3328602616
X-MC-Loop-Signature: 1705857513805:2669085195
X-MC-Ingress-Time: 1705857513805
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1705857513;
bh=C7CGxHQ43SwEU2xcSTX8WdYG+M33WbvnDwhmGeIZesI=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=Znv0uUvG1A1+jK6qWR4NUe2Da4BTv4eYO4CsnRxgilRTlm7xK1C+tIBgkRq5Ij0nr
t45pyfQ8050qbCxMIOkysyt2/f43/49YIOOGmZedr0z2kU0p+asoe+2kfck6BmD1V2
4NVNBlbE0uFgU42doamVoDyOHP2eEhsy1dJSTLmhtIgsXITifXvQCNnGnoiZE2PUus
jClM3IdMhvmcThjSo9VPs3wRxFOSeTAUSrYp5kPEcUsYZwfizTKNx2cngUFacFI8MO
xo9sUo/XvtcCvbu/O7YcN1Z7/P7HAQAjz6w5Qe1KwjDP5beaSmpsxGRu13a7/0D7HN
E/mgIQJA62XiQ==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <CAGJtH9Svh5Q5B68aB+WK8JaYzV=EY-pfobi=cSoNjrTxBxv5_Q@mail.gmail.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <86081560-08ff-42bc-ae6d-dfb03d8b2a7d@tompassin.net>
X-Mailman-Original-References: <CAGJtH9RvLNeHFQSVk4b-TuYKKLgVbvAnoiQvC37vw5HGm2YjHg@mail.gmail.com>
<a9aa6809-2842-4e7d-9024-fb2549c2eb65@tompassin.net>
<CAGJtH9Svh5Q5B68aB+WK8JaYzV=EY-pfobi=cSoNjrTxBxv5_Q@mail.gmail.com>
 by: Thomas Passin - Sun, 21 Jan 2024 17:18 UTC

On 1/21/2024 11:54 AM, marc nicole wrote:
> Thanks for the reply,
>
> I think using a Pandas (or a Numpy) approach would optimize the
> execution of the program.
>
> Target cells could be up to 10% the size of the dataset, a good example
> to start with would have from 10 to 100 values.

Thanks for the reformatted code. It's much easier to read and think about.

For say 100 points, it doesn't seem that "optimization" would be much of
an issue. On my laptop machine and Python 3.12, your example takes
around 5 seconds to run and print(). OTOH if you think you will go to
much larger datasets, certainly execution time could become a factor.

I would think that NumPy arrays and/or matrices would have good potential.

Is this some kind of a cellular automaton, or an image filtering process?

> Let me know your thoughts, here's a reproducible example which I formatted:
>
>
>
> from numpy import random
> import pandas as pd
> import numpy as np
> import operator
> import math
> from collections import deque
> from queue import *
> from queue import Queue
> from itertools import product
>
>
> def select_target_values(dataframe, number_of_target_values):
>     target_cells = []
>     for _ in range(number_of_target_values):
>         row_x = random.randint(0, len(dataframe.columns) - 1)
>         col_y = random.randint(0, len(dataframe) - 1)
>         target_cells.append((row_x, col_y))
>     return target_cells
>
>
> def select_contours(target_cells):
>     contour_coordinates = [(0, 1), (1, 0), (0, -1), (-1, 0)]
>     contour_cells = []
>     for target_cell in target_cells:
>         # random contour count for each cell
>         contour_cells_count = random.randint(1, 4)
>         try:
>             contour_cells.append(
>                 [
>                     tuple(
>                         map(
>                             lambda i, j: i + j,
>                             (target_cell[0], target_cell[1]),
>                             contour_coordinates[iteration_],
>                         )
>                     )
>                     for iteration_ in range(contour_cells_count)
>                 ]
>             )
>         except IndexError:
>             continue
>     return contour_cells
>
>
> def create_zipf_distribution():
>     zipf_dist = random.zipf(2, size=(50, 5)).reshape((50, 5))
>
>     zipf_distribution_dataset = pd.DataFrame(zipf_dist).round(3)
>
>     return zipf_distribution_dataset
>
>
> def apply_contours(target_cells, contour_cells):
>     target_cells_with_contour = []
>     # create one single list of cells
>     for idx, target_cell in enumerate(target_cells):
>         target_cell_with_contour = [target_cell]
>         target_cell_with_contour.extend(contour_cells[idx])
>         target_cells_with_contour.append(target_cell_with_contour)
>     return target_cells_with_contour
>
>
> def create_possible_datasets(dataframe, target_cells_with_contour):
>     all_datasets_final = []
>     dataframe_original = dataframe.copy()
>
>     list_tuples_idx_cells_all_datasets = list(
>         filter(
>             lambda x: x,
>             [list(tuples) for tuples in
> list(product(*target_cells_with_contour))],
>         )
>     )
>     target_original_cells_coordinates = list(
>         map(
>             lambda x: x[0],
>             [
>                 target_and_contour_cell
>                 for target_and_contour_cell in target_cells_with_contour
>             ],
>         )
>     )
>     for dataset_index_values in list_tuples_idx_cells_all_datasets:
>         all_datasets = []
>         for idx_cell in range(len(dataset_index_values)):
>             dataframe_cpy = dataframe.copy()
>             dataframe_cpy.iat[
>                 target_original_cells_coordinates[idx_cell][1],
>                 target_original_cells_coordinates[idx_cell][0],
>             ] = dataframe_original.iloc[
>                 dataset_index_values[idx_cell][1],
> dataset_index_values[idx_cell][0]
>             ]
>             all_datasets.append(dataframe_cpy)
>         all_datasets_final.append(all_datasets)
>     return all_datasets_final
>
>
> def main():
>     zipf_dataset = create_zipf_distribution()
>
>     target_cells = select_target_values(zipf_dataset, 5)
>     print(target_cells)
>     contour_cells = select_contours(target_cells)
>     print(contour_cells)
>     target_cells_with_contour = apply_contours(target_cells, contour_cells)
>     datasets = create_possible_datasets(zipf_dataset,
> target_cells_with_contour)
>     print(datasets)
>
>
> main()
>
> Le dim. 21 janv. 2024 à 16:33, Thomas Passin via Python-list
> <python-list@python.org <mailto:python-list@python.org>> a écrit :
>
> On 1/21/2024 7:37 AM, marc nicole via Python-list wrote:
> > Hello,
> >
> > I have an initial dataframe with a random list of target cells
> (each cell
> > being identified with a couple (x,y)).
> > I want to yield four different dataframes each containing the
> value of one
> > of the contour (surrounding) cells of each specified target cell.
> >
> > the surrounding cells to consider for a specific target cell are
> : (x-1,y),
> > (x,y-1),(x+1,y);(x,y+1), specifically I randomly choose 1 to 4
> cells from
> > these and consider for replacement to the target cell.
> >
> > I want to do that through a pandas-specific approach without
> having to
> > define the contour cells separately and then apply the changes on the
> > dataframe
>
> 1. Why do you want a Pandas-specific approach?  Many people would
> rather
> keep code independent of special libraries if possible;
>
> 2. How big can these collections of target cells be, roughly speaking?
> The size could make a big difference in picking a design;
>
> 3. You really should work on formatting code for this list.  Your code
> below is very complex and would take a lot of work to reformat to the
> point where it is readable, especially with the nearly impenetrable
> arguments in some places.  Probably all that is needed is to replace
> all
> tabs by (say) three spaces, and to make sure you intentionally break
> lines well before they might get word-wrapped.  Here is one example I
> have reformatted (I hope I got this right):
>
> list_tuples_idx_cells_all_datasets = list(filter(
>     lambda x: utils_tuple_list_not_contain_nan(x),
>     [list(tuples) for tuples in list(
>           itertools.product(*target_cells_with_contour))
>     ]))
>
> 4. As an aside, it doesn't look like you need to convert all those
> sequences and iterators to lists all over the place;
>
>
> > (but rather using an all in one approach):
> > for now I have written this example which I think is not Pandas
> specific:
> [snip]
>
> --
> https://mail.python.org/mailman/listinfo/python-list
> <https://mail.python.org/mailman/listinfo/python-list>
>

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor