# Tag Info

13

I would try to use existing pandas features where possible to keep this code minimal - this aids readability and reduces the possibility of bugs being introduced in complicated loop structures. import pandas from scipy.stats import chi2_contingency def chisq_of_df_cols(df, c1, c2): groupsizes = df.groupby([c1, c2]).size() ctsum = groupsizes.unstack(...

10

Using crosstab, this can be done in a single step: pandas.crosstab(index=test_df['var1'],columns=test_df['var2']) It will give this desired result: var1 | 0 1 -------------------- var2 | -------------------- 0 |0 1 1 |2 0 2 |2 0 You can name the index and colnames and also get the row totals and column totals: ...

8

Python coding conventions There's a few places in the code where you have the following: if y == None and x == None: The Pythonic way to check against None is to use is: if y is None and x is None: if fid is not None: Note that this works because None is always the same object. There's a few other minor formatting issues that aren't considered ...

8

AFAIK it does what you want (but on this site you should generally be sure that your code does what you want beforehand). Beauty, eye of the Beholder, ...; that said, this code can be rewritten in a very concise manner: import pandas as pd import scipy.stats as scs def categories(series): return range(int(series.min()), int(series.max()) + 1) def ...

8

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 your code is the most transferable, re-usable and readable. Second, a comment on the algorithm itself. Your algorithm (and this includes any changes I make to it ...

7

You can simplify your 'verbose' multi-line regexes by using the re.X flag. var_re = re.compile(r''' ^\s* (\S+) \s*=\s* (.+?) \s*$''', re.X) The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping ... 6 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 reuse the name, docstring and signature of the decorated function for the wrapper. The wrapper within the decorator should be aware of both M and sym; this is where things can get tricky ... 6 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 indentation level instead of 8 as recommended per PEP 8; use itertools.product to iterate over all the combinations of parameters in one single loop instead of 3; ... 6 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 driver's license, running python 3.7 on Windows. Note: Because of this, it's quite likely that your performance will differ from mine. Verify these results, it'... 6 You use a docstring to good effect. I'd think it great if you followed the rules closely: """ <whatever sums up this module>. Dominance as defined in Desaulniers/Rakke/Coelho: "A Branch-Price-and-Cut Algorithm for the Inventory-Routing Problem" """ def dominates(self, label1: Label, label2: Label): "&... 5 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 quick factorisation which brings back the code to a reasonable 12 lines (I could not test, obviously, but that seems OK): result_list = [] for name in ["AWA","... 5 Input (copy and paste from the original question): test_df = pandas.DataFrame([[0, 1], [1, 0], [0, 2], [0, 1], [0, 2]], columns=['var1', 'var2']) Desired output (copy and paste from the original question): var1 0 1 --------------------- 0 | 0 1 var2 1 | 2 0 2 | 2 0 One line solution using ... 5 Globals in Python are not necessarily a bad thing, especially for what you're using them for. However, they can be prevented. There are a couple of major flaws I found so I'm glad you came over to get a review. The point you're most worried about will be handled at the end. Style Clean Python code adheres to PEP8, the general style guide for Python. Your ... 5 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 summarizes a whole lot of style advice and best practices generally to be used while coding Python. If you decide to stick with PEP8 or not is up to you, but if you ... 5 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 collection of style best practices you should follow in Python. I think one of the most often cited "rules" is to use snake_case for variable and function names. ... 4 I personally can't answer your first concern. For your second one, two list comprehensions would help. List comprehensions are good when you have a list and a for loop to populate the list. list_ = [] for i in range(10): list_.append(i) To make it a list comprehension is rather easy. list_ = [ i for i in range(10) ] While this is as very ... 4 When trying to speed up Numpy code, it's important to scrutinize every loop. A loop that has to run in the Python interpreter can be hundreds of times slower than a vectorized loop running inside Numpy. Also, when trying to improve the performance of code, there's no substitute for measurement. So let's set up a test case. The matrix csc here has the same ... 4 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 threshold: # m is your dataset in sparse matrix representation above_threshold = m >= v["threshold"] and then you can use the max method to get the maximum entry in each column: cols = ... 4 You seem to know about str.format (since you use it), yet you are still doing string addition: title1 = 'Probability of making loss: ' + '{0:.4g}'.format(P1*100) + '%' title2 = 'Probability of earning more than$20k: ' + '{0:.4g}'.format(P2*100) + '%' Just make it a single string: title1 = 'Probability of making loss: {0:.4g}%'.format(P1*100) title2 = '...

4

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 local variables in a function is faster than a global lookup, so putthing each part in a function can speed up this process already. If you do this, beware that your E_calc functions use global ...

4

There's no docstring for fd_solve. The post contains a description that would make a good start. The docstring has zs[2:, 1:-1, 1:-1] = 0 but this doesn't match the code zs[1:,1:-1,1:-1] = 0. eqn_parse does not seem to parse anything. I think you could pick a better name, for example pde_kernel. eqn_parse might return None in some circumstances. Is this the ...

4

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 rows with their ranks using pandas.DataFrame.rank() function. Convert v to pandas.Seriesand use pandas.Series.rank() function to get ranks. Use pandas.corrwith() function to calculate Spearman ...

4

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 for cosines baseChars is a list of characters, which appears needs to be in exactly the same order as the dictEN, dictFR, dictDE, ... and distCZ dictionary keys. Prior to Python 3.6, the order ...

4

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 descending numbers, choose the transitions (initial zeros, digits 9-0; 10 transitions). Subtract those where initial zeros are followed only by a non-empty string of a ...

3

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 a published method to the letter. Linking to a readily-available description of it. Naming your variables and laying out your code as closely as possible to ...

3

Your function f takes 4 generically named parameters. Obviously it's based on an equation so the parameters can't be more meaningful but it would be best if you renamed the function with something more descriptive and gave it a docstring: def equation(x, a, b, c, d): """Returns the result of a + b*x + c*x**2 + d*x**3 This is also known as an ...

3

"Better" is obviously fairly subjective. After a quick glance I would like to make a few suggestions: If you already imported scipy, you don't need to import scipy.optimize separately. You can either limit the importing by just importing the (sub)modules you need, which is cleaner. Or you can import the entire module and just use what you need. Especially ...

3

Accuracy I don't really see much of an accuracy problem by increasing $\Delta x$. Compare your original: … to the result using my code below, with $\Delta x=0.01$ (and some clarifying visual tweaks): I also tried my code using $\Delta x=0.001$, and the results are visually indistinguishable from the bottom plot, except that the dots in the initial ...

3

scipy.optimize functions take an args parameter, as a way of passing extra arguments to the function. You could use that to pass a number of the parameters to heat_balance. For example, let's assume r, T_a and dx are more 'variable' than R_w; that is, more likely to vary from run to run or test case. I'll also switch the order of T_1 and T_2, since the ...

3

In terms of possible speed optimisations there's not much to work with, but one thing does stand out: def func(t, a, b, c, d, e, f): return a+ b*t+ c*(b*t)**2+ d*(b*t)**3+ e*(b*t)**4+ f*(b*t)**5 (and similarly for the four-element case). It's fairly standard knowledge that polynomials evaluation can be optimised for speed using Horner's ...

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