Skip to main content
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
  • 65.3k
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
  • 41.1k
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
  • 34.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.5k
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

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,559
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,278
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,278
5 votes

Srivastava multivariate Fox H function in MATLAB

There are some obvious inefficiencies in the code that I can point out. The profiler shows that most time is spent doing symbolic math. You use symbolic math to compute the product of a series of ...
Cris Luengo's user avatar
  • 6,237
4 votes

ChiSquare test Code in Python

Pandas and Scipy have seen several non-backward-compatible changes since this question was posted. I will claim that even in 2016, the code should have been written to better use the Pandas API at the ...
Reinderien's user avatar
  • 65.3k
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
  • 41.1k
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.7k
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

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
  • 34k
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

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.3k
4 votes
Accepted

Python Implementation of Canopy Structural Dynamic Model using scipy.optimize.least_squares

Convex optimiser This class of problem is very sensitive to initial conditions if you aren't careful. It has significant degrees of freedom and multiple exponential terms. Your provided estimate is ...
Reinderien's user avatar
  • 65.3k
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
  • 65.3k
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
  • 34.9k
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

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

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,501
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.2k
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

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
  • 41.1k
3 votes

Double pendulum real time plot

There's a lot to like about this program: It's relatively light on the requirements The animation is smooth, seemingly accurate, and updates in real-time with the parameter controls It's pretty ...
Reinderien's user avatar
  • 65.3k
3 votes

Numerical Double Integration using numba and scipy

Prior to vectorisation, the simple version of this can look like ...
Reinderien's user avatar
  • 65.3k

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