31 votes

Python decibel meter-accurate?

Indent your code with a PEP8-compliant IDE or linter; it's a perfect mess right now. Move your global code into functions and maybe classes. There are two good use cases for classes here - one for a ...
Reinderien's user avatar
10 votes

Chi Square Independence Test for Two Pandas DF columns

Using pandas.crosstab, this can be done in a single step: pandas.crosstab(index=test_df['var1'],columns=test_df['var2']) It ...
Thameem Ansari's user avatar
9 votes
Accepted

Farthest point algorithm in Python

First, a style comment. On the internet, especially in programming, and in particular on this website, English is the lingua franca. So you should avoid mixing other languages and English. This way ...
Graipher's user avatar
  • 40.9k
8 votes
Accepted

Fourier Series of a given function

I like it. This code hews closely to the underlying math, using lovely identifiers, and is very clear. For {a,b}_k, here's the only part I'm not super happy with: ...
J_H's user avatar
  • 24.9k
7 votes
Accepted

Factorio analysis: data munging

You can simplify your 'verbose' multi-line regexes by using the re.X flag. ...
Peilonrayz's user avatar
  • 43.3k
6 votes
Accepted

Epidemic simulation

78 characters of indentation at its deepest: this code is unreadable. We can't easily match the core of the code with the definition of the parameters. To improve that, you can: use 4 space per ...
301_Moved_Permanently's user avatar
6 votes

20+ functions for generating different waveforms

I do believe that the decorator way is the proper one. There are some tradeoff using it but you can easily overcome your main concern using functools.wraps: it will ...
301_Moved_Permanently's user avatar
6 votes

Simulation of spring-loaded inverted pendulum

Inspired by @Alex's answer, I did some profiling and simple timing of your code, and some changes that seemed "obvious" to me. My environment for this is a 32-bit laptop that is old enough to have a ...
aghast's user avatar
  • 12.4k
6 votes

compare between two labels /objects -dominance rules

You use a docstring to good effect. I'd think it great if you followed the rules closely: ...
greybeard's user avatar
  • 6,416
5 votes
Accepted

Counts instances for each category for many Matlab files

You felt it right, there's a lot of unnecessary copy/paste in the first part. Your 5 data structures are similar, and you create a lot of variables to perform the same processing 5 times. I did a ...
Jean-François Fabre's user avatar
5 votes
Accepted

Monte Carlo errors estimation routine

Before starting with feedback on the actual code, I would like to share some thoughts about the style. Chose a style, be consistent Python comes with an official style guide called PEP8 which ...
AlexV's user avatar
  • 7,250
5 votes
Accepted

Simulation of spring-loaded inverted pendulum

Style Your code looks quite good in general, but there is always something to nitpick on, isn't it? Python comes with an official Style Guide, often just called PEP8. It has quite an extensive ...
AlexV's user avatar
  • 7,250
4 votes
Accepted

Conditional removal of columns in sparse matrix

Assuming that the threshold is positive, then you can use the >= operator to construct a sparse Boolean array indicating which points are above or equal to the ...
Gareth Rees's user avatar
  • 49.6k
4 votes
Accepted

Probability of event using Central Limit Theorem + plotting results

You seem to know about str.format (since you use it), yet you are still doing string addition: ...
Graipher's user avatar
  • 40.9k
4 votes
Accepted

2d linear Partial Differential Equation Solver using finite differences

There's no docstring for fd_solve. The post contains a description that would make a good start. The docstring has ...
Gareth Rees's user avatar
  • 49.6k
4 votes
Accepted

Calculate electric field of a charged annulus

I think the main time consuming factor here is calculating the dbquad, that said there are some smaller improvements you can make split into functions lookup of ...
Maarten Fabré's user avatar
4 votes
Accepted

Spearman correlations between Numpy array and every Pandas DataFrame row

Since Spearman correlation is the Pearson correlation coefficient of the ranked version of the variables, it is possible to do the following: Replace values in df ...
Roman Prilepskiy's user avatar
4 votes
Accepted

Simple natural language classifier

Welcome to Code Review! This is an interesting program; thanks for sharing! To help you maintain it ... baseDict appears to be unused, and can be removed. Ditto ...
AJNeufeld's user avatar
  • 33.5k
4 votes

Counting Bouncy Numbers: Project Euler #113

Project Euler problems generally can be computed with a calculator or manually. So, take a different approach: For ascending numbers, choose the transitions (digits 0-9; 9 transitions). For ...
Deduplicator's user avatar
  • 19.1k
4 votes
Accepted

Local solver built into a global one

As a start: Do not write experimental_data_list all in one line; give each row its own line and put it into an np.array. Combine ...
Reinderien's user avatar
4 votes
Accepted

Matrix Factorisation class packaging methods for factorisation of explicit & implicit data matrices using Gradient Descent, SGD and ALS

imports I don't understand why you omitted these lines. I added them back. They alter the meaning of the submitted source code, and make it reviewable. ...
J_H's user avatar
  • 24.9k
3 votes

Multidimensional matrix exponentiation

Dig through the source code for expm, which leads ultimately to a rather extensive bit of code in scipy.sparse.linalg.matfuncs.py...
hpaulj's user avatar
  • 1,491
3 votes
Accepted

Polynomial curve-fitting over a large 3D data set

In terms of possible speed optimisations there's not much to work with, but one thing does stand out: ...
Peter Taylor's user avatar
  • 24.1k
3 votes

Raster processing for climate change model

First, fix the bug! You're using sys.exc_info()[-1].tb_lineno but you're not importing the sys module anywhere, so do that ...
Grajdeanu Alex's user avatar
3 votes
Accepted

Compute Gini Coefficient

Stick closely to the sources It's helpful when coding math in cases like this to base your approach on established methods and language. It might seem a bit extreme, but this can include: Following ...
Ryan Mills's user avatar
  • 1,010
3 votes
Accepted

Implement metaclass for Maximumlikelihood estimator

Instead of constructing the object and then modifying its methods and attributes, you can directly add them in the __new__ method of the metaclass: ...
301_Moved_Permanently's user avatar
3 votes
Accepted

Fast distance calculation for Starcraft2 bot

You should probably have another look at the scipy.spatial module. It provides (hopefully) faster methods for most of these checks using a k-d-tree. ...
Graipher's user avatar
  • 40.9k
3 votes

Applying Minimum Image Convention in Python

I think you could move the where function out of the loop: ...
daveg's user avatar
  • 141
3 votes

Matrix Factorisation class packaging methods for factorisation of explicit & implicit data matrices using Gradient Descent, SGD and ALS

It is great that you used docstrings and comments. Here are some minor suggestions, purely for coding style. There are many long lines of code, and many can be shortened by moving the end-of-line ...
toolic's user avatar
  • 3,141
2 votes

Python script to store nd array into images

Instead of: ensure_directory_exist(image_path) use the built-in os.makedirs: ...
Gareth Rees's user avatar
  • 49.6k

Only top scored, non community-wiki answers of a minimum length are eligible