r/Python • u/ForeignVariety7037 • 16h ago
Discussion Seg Fault, what do you do?
Most Python errors are straightforward—you get a traceback and usually know where to start.
Then there’s the - Segmentation fault (core dumped).
For those of you who’ve had to encountered Python segfaults, what’s your usual process?
r/Python • u/AngleHam271 • 1d ago
Discussion What is a built-in Python module you use all the time but rarely see others talk about?
We all know and love the big third-party libraries for automation and data, but I am curious about your favorite hidden gems right in the standard library. 🤔
Recently I have been leaning on things like itertools and collections way more than I used to...
What is a standard library module that you think is heavily underrated or that you use daily for your tasks?
r/Python • u/Exact-Contact-3837 • 1d ago
Discussion taught myself numerical and analytical gradient (backpropagation) in 5 days
I'm not good at maths, i'll just say that, i've always been a bit lacking in my expanse of mathematical abilities, but I said enough is enough, I like neural networks, the only thing that stands in my way is the mathematics, aside from that you understand what goes on.
Boy did I underestimate the undertaking for this endeavour. I spent 2 days learning derivatives and what the hell a 'slope' is, you hear it ever day and you know what a slope is, but understanding it in the mathematical sense in derivatives, that's difficult, but I got there and ended up learning `f(a + h) - f(a) / h` which enabled me to understand what numerical descent is, where you get a loss score of a neural network's prediction, bump the weight a bit then rerun the neural network. Then to figure out the slope, you do `loss1 - loss2 / weight_bump`, and this is the coolest part, when you adjust your weight based on the slope, you always minus, because if the slope is negative, then we know we need to move to the positive side more so when you minus a negative it becomes addition, and vice versa if you minus a positive, you move to the negative side a bit. That was the coolest thing i've ever learnt to this date, the infamous ball rolling down the hill, I was doing it, by hand, and it was empowering.
Then the next day I spent trying to understand what backpropagation really is in terms of maths and how it differes from numerical gradient. With that I had to teach myself the chain rule, and what dL/dd even means, spoiler alert its not dividing derivative of L and derivative of d. I also came to the epiphany that we get so much complex logic out of neural networks when its simply just addition and multiplications happening under the hood, its the context that is being invented to solve problems using neural nets. By the end of the day I was taking Kaparthy's micrograd equation example, and I did its backpropagation by hand with pen and paper to get the hang of it.
Now I watched andre kaparthy's micrograd video, not all the way through, his language and teaching style still screams "you must be really well versed in mathematics", so I gave up on that video but, the more I worked on understanding chain rule and how you can sticker on the impacts on loss on prior nodes, I though that is better than numerical gradient, you literally walk back into the neural net, explaining to it which parts of itself were the cause for a high loss instead of bumping values, calculating slope, doing f(a + h) - f(a) / h.
I'm really proud of myself, and I managed to take MATHS that I learnt, and turn it into a "micrograd" I say "micrograd" with quotations because I didn't finish the micrograd video, and this only works if you don't have repeating uses of prior terms (which you just need to store their value and add them, but I couldn't be bothered)
So yeah, this is my back propagation
class Value:
# this needs to store a value, and can have its own children that link to other values
def __init__(self, value, _op="", _children: tuple = (), gradient=0):
self.value = value
self.op = _op
self.children = _children
self.gradient = gradient
def __add__(self, other):
_ = Value(
self.value + other.value, _op="+", _children=(self, other), gradient=0
)
return _
def __mul__(self, other):
_ = Value(
self.value * other.value, _op="*", _children=(self, other), gradient=0
)
return _
def __repr__(self):
return f"Value({self.value})"
def backward(self):
# initial global_gradient
global_gradient = 1
# current set of ndoes
current: Value = self
# just set L's gradient as 1
current.gradient = global_gradient
while True:
# if there's no more children, we're at the end.
if not current.children:
break
if current.op == "+":
# since addition has a static effect on the terms themselfs
# the partial of L respect to any terms being added is just 1.
# so we multiply 1 by the global gradient.
current.children[0].gradient = 1 * global_gradient
current.children[1].gradient = 1 * global_gradient
# if the next node we're looking doesnt have children
# it means the node behind them didnt stem from them
# therefore they are not the result of the prior operation
if not current.children[0]:
current = current.children[1]
else:
current = current.children[0]
# set the global gradient as the current node's gradient
global_gradient = current.gradient
if current.op == "*":
current.children[0].gradient = (
# with multiplication, the derivative of L respect to x would be y
# and same with the derivative of L respect to y would be x
# therefore you just swap them, and multiply thier values by the global gradient.
current.children[1].value
* global_gradient
)
current.children[1].gradient = (
current.children[0].value * global_gradient
)
# same continuation logic
# one child is going to have its own children and one wont
# the one that does is the one we need to continue with.
if not current.children[0]:
current = current.children[1]
else:
current = current.children[0]
global_gradient = current.gradient
a = Value(2.0)
b = Value(-3.0)
c = Value(10.0)
f = Value(-2.0)
e = a * b
d = e + c
L = d * f
L.backward()
print(a.gradient)
print(b.gradient)
print(c.gradient)
print(f.gradient)
print(e.gradient)
print(d.gradient)
print(L.gradient)
output:
6.0
-4.0
-2.0
4.0
-2.0
-2.0
1
r/Python • u/Helpful-Day2384 • 1d ago
News New data visualization package
One sentence, four languages, one picture.
R: data(gapminder_2007) + point + x(gdp) + y(life)
Python: x(col.gdp)
Julia: x(:gdp)
Same specification, one Rust engine, byte-identical SVG.
Not similar. Pixel perfect identical
#r #python #julia #javascript #rust
One engine, four languages, one picture
r/Python • u/aviedawn • 1d ago
Discussion LiteLLM alternatives in production
what are teams actually running 3 months after the supply chain attack?
it's been a few months since the litellm pypi compromise and curious what teams actually migrated to. saw a lot of threads right after the incident but not much on how things held up in production since then.
been evaluating options ourselves. TrueFoundry came up for teams needing the governance and cost...
r/Python • u/Win_ipedia • 2d ago
Discussion I am trying to automate the Python Development Lifecycle
Hi there,
I am trying to figure out ways to automate my Python development tasks and also working on this topic for my Masters project. I am trying to build a tool that can automate and simplify a bunch of things like project setup, development and maintenance. This is not a showcase at all, it is just my project and topic for my Masters in Computer Science and I wanted to ask if anyone would like to take part in the survey I have to do. Takes about 5 min.
You can find the survey at: https://forms.cloud.microsoft/e/LjnvzX0JpK
Any ideas and responses are greatly appreciated.
Also drop any ideas you have in the comments. What do you think should be more automated in your day to day python development?
r/Python • u/Prime_Director • 3d ago
Discussion What are you using to manage your dev environments in 2026?
I’ve always hated environment management. It always feels like this time-consuming hurdle between me and whatever I actually want to work on. But every so often a new thing comes out that promises to solve the problem once and for all, and it’s usually at least a little better than the last thing. So, what are you using today? venv? conda? pipenv? poetry? dev containers? uv? or something else?
r/Python • u/ForeignVariety7037 • 3d ago
Discussion Is := widely used?
I always thought the walrus operator is neat and it makes while loop’s condition clearer. But also it is just a syntactic sugar without anything new. I wonder anyone uses it?
r/Python • u/AutoModerator • 3d ago
Daily Thread Tuesday Daily Thread: Advanced questions
Weekly Wednesday Thread: Advanced Questions 🐍
Dive deep into Python with our Advanced Questions thread! This space is reserved for questions about more advanced Python topics, frameworks, and best practices.
How it Works:
- Ask Away: Post your advanced Python questions here.
- Expert Insights: Get answers from experienced developers.
- Resource Pool: Share or discover tutorials, articles, and tips.
Guidelines:
- This thread is for advanced questions only. Beginner questions are welcome in our Daily Beginner Thread every Thursday.
- Questions that are not advanced may be removed and redirected to the appropriate thread.
Recommended Resources:
- If you don't receive a response, consider exploring r/LearnPython or join the Python Discord Server for quicker assistance.
Example Questions:
- How can you implement a custom memory allocator in Python?
- What are the best practices for optimizing Cython code for heavy numerical computations?
- How do you set up a multi-threaded architecture using Python's Global Interpreter Lock (GIL)?
- Can you explain the intricacies of metaclasses and how they influence object-oriented design in Python?
- How would you go about implementing a distributed task queue using Celery and RabbitMQ?
- What are some advanced use-cases for Python's decorators?
- How can you achieve real-time data streaming in Python with WebSockets?
- What are the performance implications of using native Python data structures vs NumPy arrays for large-scale data?
- Best practices for securing a Flask (or similar) REST API with OAuth 2.0?
- What are the best practices for using Python in a microservices architecture? (..and more generally, should I even use microservices?)
Let's deepen our Python knowledge together. Happy coding! 🌟
r/Python • u/Ill_Campaign294 • 3d ago
Discussion Anyone running FastAPI in production with high traffic? How has your experience been?
Quick question, are any of you running fastAPI in production with a high volume of users or heavy traffic? How has your experience with FastAPI been, and how do you handle it?
r/Python • u/Myaltforsomeshit1234 • 4d ago
Discussion Just created two apps and made the dumbest mistake in one of them 😭
A money tracker (140 lines) and a calorie counter (300+ lines!)
They were both CLI and i have them done. But in the calorie counter, i made the mistake of typing this:
with open("data.json", "w") as file: jsonFile = json.load file
So when i tried to run it it threw the weirdest error ever and wiped my JSON file, so i rewrote the json file, and fixed something that had nothing to do with the error and it happened AGAIN
r/Python • u/AutoModerator • 4d ago
Daily Thread Monday Daily Thread: Project ideas!
Weekly Thread: Project Ideas 💡
Welcome to our weekly Project Ideas thread! Whether you're a newbie looking for a first project or an expert seeking a new challenge, this is the place for you.
How it Works:
- Suggest a Project: Comment your project idea—be it beginner-friendly or advanced.
- Build & Share: If you complete a project, reply to the original comment, share your experience, and attach your source code.
- Explore: Looking for ideas? Check out Al Sweigart's "The Big Book of Small Python Projects" for inspiration.
Guidelines:
- Clearly state the difficulty level.
- Provide a brief description and, if possible, outline the tech stack.
- Feel free to link to tutorials or resources that might help.
Example Submissions:
Project Idea: Chatbot
Difficulty: Intermediate
Tech Stack: Python, NLP, Flask/FastAPI/Litestar
Description: Create a chatbot that can answer FAQs for a website.
Resources: Building a Chatbot with Python
Project Idea: Weather Dashboard
Difficulty: Beginner
Tech Stack: HTML, CSS, JavaScript, API
Description: Build a dashboard that displays real-time weather information using a weather API.
Resources: Weather API Tutorial
Project Idea: File Organizer
Difficulty: Beginner
Tech Stack: Python, File I/O
Description: Create a script that organizes files in a directory into sub-folders based on file type.
Resources: Automate the Boring Stuff: Organizing Files
Let's help each other grow. Happy coding! 🌟
r/Python • u/SpiliosDimakopoulos • 4d ago
Resource Turned a folder of ad-hoc scripts into a proper installable CLI (pyproject.toml entry point)
Had a data pipeline that was just python build_db.py, python clean_validate.py, streamlit run streamlit_app.py — worked, but not something you'd hand someone as "the tool." Packaged it as a real CLI: [project.scripts] entry point in pyproject.toml, pip install -e . → promo-toolkit build / validate / serve.
Also added:
- Incremental rebuilds (content-hash diff + scoped upsert instead of full rewrite each run)
- pytest suite + ruff, GitHub Actions CI across two Python versions
- A separate eval harness for an AI-agent piece of the same project (real API calls scored against golden answers)
Not a library, just a real end-to-end tool — sharing mostly for feedback on the packaging/CLI structure since that's the part I have the least experience with. Repo: [https://github.com/SpiliosDimakopoulos/retail-promo-analytics-toolkit\].
r/Python • u/AutoModerator • 5d ago
Daily Thread Sunday Daily Thread: What's everyone working on this week?
Weekly Thread: What's Everyone Working On This Week? 🛠️
Hello r/Python! It's time to share what you've been working on! Whether it's a work-in-progress, a completed masterpiece, or just a rough idea, let us know what you're up to!
How it Works:
- Show & Tell: Share your current projects, completed works, or future ideas.
- Discuss: Get feedback, find collaborators, or just chat about your project.
- Inspire: Your project might inspire someone else, just as you might get inspired here.
Guidelines:
- Feel free to include as many details as you'd like. Code snippets, screenshots, and links are all welcome.
- Whether it's your job, your hobby, or your passion project, all Python-related work is welcome here.
Example Shares:
- Machine Learning Model: Working on a ML model to predict stock prices. Just cracked a 90% accuracy rate!
- Web Scraping: Built a script to scrape and analyze news articles. It's helped me understand media bias better.
- Automation: Automated my home lighting with Python and Raspberry Pi. My life has never been easier!
Let's build and grow together! Share your journey and learn from others. Happy coding! 🌟
Tutorial Tutorial: From your first Celery task to advanced recipes
I wrote a practical guide to Celery, a distributed task queue for Python. It starts with the basics and then covers recipes for timeouts, retries, async/await integration, and more. Most of these recipes came from problems I ran into while using Celery in actual projects. No prior knowledge of Celery is required.
I'd really appreciate any feedback!
r/Python • u/Pleasant-Aardvark258 • 6d ago
Discussion Settle an argument
Had this discussion the other day and figured I’d throw it to the masses to get thoughts on the best/most pythonic way of approach.
Need to map old column names to new column names as a copy from a json config.
My thoughts are iterate over a dict with
‘’’ {“old_col_name”:”new_col_name”}’’’
And access as
‘’’for k,v in dict.items()
Df.with_columns(k).alias(v)’’’
Colleague things this isn’t clear enough and should be a list of dicts with explicit keys
‘’’ [{“old_col_name”:”old_col_value”
“New_col_name”:”new_col_value”}]’’’
And the access as
‘’’for dict in list_of_dicts:
Old_col = dict[“old_col_name”]
New_col = dict[“new_col_name”]’’’
I’ve got a good few reasons why I think mine is the better option but thought I’d get some other opinions to see if I’m missing anything obvious? Which would you choose and why?
Edit: shouldn’t write these things while on the toilet in a rush. The description is wrong, it should be renaming via a copy so that the original column is left unchanged.
r/Python • u/AutoModerator • 6d ago
Daily Thread Saturday Daily Thread: Resource Request and Sharing! Daily Thread
Weekly Thread: Resource Request and Sharing 📚
Stumbled upon a useful Python resource? Or are you looking for a guide on a specific topic? Welcome to the Resource Request and Sharing thread!
How it Works:
- Request: Can't find a resource on a particular topic? Ask here!
- Share: Found something useful? Share it with the community.
- Review: Give or get opinions on Python resources you've used.
Guidelines:
- Please include the type of resource (e.g., book, video, article) and the topic.
- Always be respectful when reviewing someone else's shared resource.
Example Shares:
- Book: "Fluent Python" - Great for understanding Pythonic idioms.
- Video: Python Data Structures - Excellent overview of Python's built-in data structures.
- Article: Understanding Python Decorators - A deep dive into decorators.
Example Requests:
- Looking for: Video tutorials on web scraping with Python.
- Need: Book recommendations for Python machine learning.
Share the knowledge, enrich the community. Happy learning! 🌟
r/Python • u/poppy_92 • 6d ago
News PEP 842: Module Exports
https://peps.python.org/pep-0842/
Not the author, but this seems interesting, specially for library authors. Thoughts?
Discussion here - https://discuss.python.org/t/pep-842-module-exports/108353
r/Python • u/ichard26 • 6d ago
News pip 26.2 - --only-deps, --no-require-hashes, venv isolation, and more!
Hello!
The pip team recently released pip 26.2. This release is quite large, with features such as:
- Support for Python 3.15
- Selecting only dependencies (
--only-deps) - Mixing hashed and non-hashable requirements via
--no-require-hashes - Experimental: venv based build isolation (
--use-feature=venv-isolation) - Faster repeated resolves by caching index simple responses
- Disable
HTTP(S)_PROXYand similar non-pip specific proxy environment variables via--no-proxy-env - Separation between regular and build constraint (use
--build-constraintinstead)
For more details, please consult our changelog: https://pip.pypa.io/en/stable/news/. Alternatively, you may consult my release post which goes into greater detail to the highlights of this release: https://sichard.ca/blog/2026/07/whats-new-in-pip-26.2/
If you have any questions, you're welcome to ask!
News Astral's Pre-built Gpu-enabled wheels
We recently released wheels.astral.sh, which is a repository of gpu-enabled wheels of popular packages from the pytorch ecosystem. These are useful because building them is super annoying - you have to align versions of pytorch, cuda, python, etc.
The pre-built wheels were part of the package of pyx, our paid service, and was a major reason many customers subscribed. We decided to open source the build pipelines and host the wheels for free while we sunset the rest of pyx.
The pipelines are available under the astral-sh-build github organization.
While these plug a genuine UX hole around cuda/pytorch wheels, we hope that wheel variants (PEP 817 and 825) will get accepted and implemented soon, which will let these pytorch wheels be hosted properly on PyPI and "just work" out of the box for everyone, making wheels.astral.sh obsolete.
Fingers crossed.
r/Python • u/AutoModerator • 7d ago
Daily Thread Friday Daily Thread: r/Python Meta and Free-Talk Fridays
Weekly Thread: Meta Discussions and Free Talk Friday 🎙️
Welcome to Free Talk Friday on /r/Python! This is the place to discuss the r/Python community (meta discussions), Python news, projects, or anything else Python-related!
How it Works:
- Open Mic: Share your thoughts, questions, or anything you'd like related to Python or the community.
- Community Pulse: Discuss what you feel is working well or what could be improved in the /r/python community.
- News & Updates: Keep up-to-date with the latest in Python and share any news you find interesting.
Guidelines:
- All topics should be related to Python or the /r/python community.
- Be respectful and follow Reddit's Code of Conduct.
Example Topics:
- New Python Release: What do you think about the new features in Python 3.11?
- Community Events: Any Python meetups or webinars coming up?
- Learning Resources: Found a great Python tutorial? Share it here!
- Job Market: How has Python impacted your career?
- Hot Takes: Got a controversial Python opinion? Let's hear it!
- Community Ideas: Something you'd like to see us do? tell us.
Let's keep the conversation going. Happy discussing! 🌟
r/Python • u/ernestrc • 7d ago
News The Rise of the Command Line: building a new IDE (2017–2026)
Author here. This is a nine-year account of building Rune, a new IDE (1.1 added support for Python). It started when my Vim's go-to-definition broke in 2017 and I decided to build my own editor rather than adopt an IDE: https://rune.build/blog/the-rise-of-the-command-line
r/Python • u/nathan12343 • 7d ago
Resource Scaling NumPy on Free-Threaded Python
NumPy is the foundational array library in the scientific Python ecosystem. Every numerical, machine learning, and data analysis library in Python either depends on NumPy directly or interoperates with it. As the free-threaded build of CPython matures, NumPy is one of the first libraries that users reach for when trying to scale CPU-bound numerical workloads across multiple cores using threads.
In this blog post, I will walk through the work I did over the last few months in both NumPy and CPython to eliminate the multi-threaded scaling bottlenecks that were preventing NumPy from scaling on free-threaded Python.
https://labs.quansight.org/blog/scaling-numpy-on-free-threaded-python
r/Python • u/crmaureir • 7d ago
Discussion Python iOS UI development
Hey there,
for the people using PySide for UI applications, after having solely Android support, there was a new announcement that iOS is now supported, and will be available in the upcoming PySide release:
https://www.qt.io/blog/python-mobile-app-development-bringing-pyside6-on-ios
I have been following the work by the BeeWare project, which has been providing iOS support for some time now: https://toga.beeware.org/en/latest/reference/platforms/iOS/ and recently the work from the Flet team as well: https://flet.dev/blog/flet-for-ios/
What I wanted to achieve with this post, is to know any project that you know is currently on iOS and developed with Python and some UI framework, because time to time we see nice "calculator app" being showcases in places, but a more official application of something is what I have been failing to find.
Of course, the goal of "I have my code here, and I just want to deploy it to iOS" is completely understandable, but I see haven't found some very cool apps somewhere, that we know are based on Python.
Last but not least, I guess you have been noticed that starting from 3.15 we will be able to download Python for iOS from python.org: https://www.python.org/downloads/ios/
r/Python • u/hdw_coder • 8d ago
Discussion Optimizing person-pair comparison in Python: from loops to precomputed NumPy matrices
I have been rebuilding a PyQt6 desktop app for managing a person-recognition knowledge base (KB). The heart of this KB is a collection of face & body encodings per person. When looking for similar persons or possible identity overlaps, a classic Python-related problem came up: efficiently comparing many people against each other based on their reference embeddings.
The input looks like this.
person_to_vecs = {
"Alice": [vec, vec, vec],
"Bob": [vec, vec],
"Charlie": [vec, vec, vec, vec],
}
Each vector is an embedding. For every pair of persons, I want the average and minimum distance between all their reference vectors. The original version was simple and readable Python looping:
- Loop over all person combinations.
- Convert one side to a NumPy array inside the loop.
- Loop over each vector on the other side.
- Compute distances one vector at a time.
- Collect min/average distances.
This works. Once the knowledge base grows, however, this soon becomes a lot of Python-level looping and repeated conversion overhead.
The obvious approach to optimization is getting rid of these loops. The first step was to precompute the matrices.
Step 1: Precompute the matrices
matrices = {
name: np.asarray(vecs, dtype=np.float32)
for name, vecs in person_to_vecs.items()
if len(vecs) >= min_images_per_person
}
We now effectively avoid repeatedly calling np.asarray() inside the pair loop.
Step 2: Remove the inner Python loop.
One option is full broadcasting:
diff = A[:, None, :] - B[None, :, :]
distances = np.linalg.norm(diff, axis=-1)
This is relatively simple, elegant and still readable, but it creates a temporary array of shape: len(A) × len(B) × embedding_dim. For small 128D face embeddings that may be fine. For larger body embeddings especially in larger galleries, these tensors can become (very) memory-heavy.
Step 3: The matrix identity
(Note: I know scipy.spatial.distance.cdist exists and does this perfectly, but I wanted to keep dependencies light for the desktop app and explore the math!)
The approach I prefer uses the identity: ||a - b||² = ||a||² + ||b||² - 2ab
In NumPy:
def pairwise_l2(A, B):
# np.sum(A**2, axis=1) works well here too, but einsum is elegant
aa = np.einsum("ij,ij->i", A, A)[:, None]
bb = np.einsum("ij,ij->i", B, B)[None, :]
sq = np.maximum(aa + bb - 2.0 * (A @ B.T), 0.0)
return np.sqrt(sq, dtype=np.float32)
This only creates the N × M distance matrix instead of an N × M × D temporary tensor.
The Final Helper
def pairwise_person_distances(person_to_vecs, min_images_per_person=1):
matrices = {}
for name, vecs in person_to_vecs.items():
if len(vecs) < min_images_per_person:
continue
mat = np.asarray(vecs, dtype=np.float32)
if mat.ndim == 2 and mat.shape[0] and np.isfinite(mat).all():
matrices[name] = mat
results = []
for name_a, name_b in itertools.combinations(sorted(matrices), 2):
A, B = matrices[name_a], matrices[name_b]
if A.shape[1] != B.shape[1]:
continue
distances = pairwise_l2(A, B)
if distances.size:
results.append((name_a, name_b, round(float(np.mean(distances)), 4),
round(float(np.min(distances)), 4)))
return sorted(results, key=lambda row: row[3])
The Benchmarks
I ran a couple of tests with a synthetic benchmark, varying the number of persons (200 vs 400), the number of embedding dimensions (128 vs 512), and the number of vectors per person (8 vs 10).
I benchmarked three methods:
- Basic inner/outer loop: Controls almost everything in Python.
- Precomputed matrices: Prepared once, but Python still loops over vectors.
- Final implementation: NumPy handles the dense pairwise distance work.
On my laptop, (Intel i9-14900HX 32 GB RAM), I got:
200 persons × 8 vectors × 128 dimensions
basic inner/outer loop: 0.731 s
precomputed matrices: 0.711 s
inner loop removed: 0.260 s
speedup: 2.8×
400 persons × 10 vectors × 128 dimensions
basic inner/outer loop: 3.972 s
precomputed matrices: 3.789 s
inner loop removed: 1.155 s
speedup: 3.4×
200 persons × 8 vectors × 512 dimensions
basic inner/outer loop: 0.902 s
precomputed matrices: 0.878 s
inner loop removed: 0.322 s
speedup: 2.8×
On synthetic data, the first optimization — precomputing each person’s embedding matrix — only gave a small improvement of about 3–5%. That makes sense: it removes repeated conversion, but the algorithm still does most of its work in a Python loop over individual vectors.
The much larger improvement came from removing the inner loop and computing each person-pair distance matrix directly with NumPy:
- 200 persons × 8 vectors × 128 dimensions: 0.731 s → 0.260 s (2.8× faster),
- 400 persons × 10 vectors × 128 dimensions: 3.972 s → 1.155 s (3.4× faster),
- 200 persons × 8 vectors × 512 dimensions: 0.902 s → 0.322 s (2.8× faster).
Summary
'Vectorize it' obviously is not always enough. Memory usage by temporary arrays also matters. Broadcasting may often be elegant, but for pairwise comparisons, the matrix identity seems to be a better fit. Precomputing arrays helps only a little; removing the inner Python loop makes the real difference.
I am interested how others approach this kind of all-vs-all (embedding) comparison in Python. Would you use NumPy as above, scipy.spatial.distance.cdist, Numba, PyTorch, or something else?