Hot answers tagged

12

Here is a review of the solution. ^ is xor in Python. It is not for computation of exponentials. When running code outside a method / class, it is a good practice to put the code inside a main guard. See here for more explanation. if __name__ == "__main__": ... When you are providing quick answers on a forum, the guard might not always be ...


7

As mentioned in the comments of another answer, you should use sets for membership tests whenever possible. Combinig this with building up uniques instead of cutting it down, we get this really simple implementation: def duplicates(numbers): uniques = set() result = [] for num in numbers: result.append(num in uniques) uniques.add(...


7

This is not the best algorithm If the goal is to get the best coefficients for a polynomial so it fits the given points, then a polynomial regression algorithm such as numpy.polynomial.polynomial.Polynomial.fit() will give you the best fit much faster, as there is an analytic solution to the polynomial least squares problem. If the goal is to learn about ...


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

Check out unique's documentation again, it offers to give you indices: def duplicates(a): uniques = np.unique(a, True)[1] result = a == a result[uniques] = False return result In benchmarks (reusing riskypenguin's) it's medium for the tiny original case but by far fastest for all larger cases: 100000 executions with 10 random numbers in ...


4

Your current solution does two passes over the input array (the two invocations of np.where) which as stated seems wasteful. One simple way to solve the problem is to use np.argmax which will take linear time: import numpy as np arr = np.array([-1, -2, -3, 4, 5, 6]) np.argmax(arr > 0) # Return (index) 3 At this point, if we can assume (as you seem to ...


4

Minor, but here: ''.join(_ for _ in splitted_doc if _ is not None) _ should be reserved for when you need to bind a object to a name, but don't need the variable. Here is a good resource on the topic. You are in fact using _ though as both the final result and in the check, so I'd give it a proper name. If none of the valid strings are falsey (empty), you ...


4

Remove sorted From the docs: numpy.unique returns the sorted unique elements of an array. You can simply remove the call to sorted: b = np.unique(sorted(a)) # produces the same result as b = np.unique(a) List comprehension In most cases you can and should avoid this pattern of list creation: result = [] for i in a: result.append((b == i) * 1) It can ...


4

As you will be able to see from your provided sample input, your code does not produce the intended result. Here is a minimal example: dates = np.arange(np.datetime64('2018-02-01'), np.datetime64('2018-02-05'), 2) stride = (dates[1] - dates[0]) result = np.arange(np.datetime64(dates[0]), np.datetime64(dates[-1] + stride)) print(dates) # > ['2018-02-01' ...


4

Minor style edits Like you said, the code seems to be perfectly fine and I don't see a very obvious way to make it faster or more efficient, as the consecutive computations you are making in your for loop don't seem to be easy to relate to one another. Of course this is just my input after thinking for some time, other people may have clever suggestions I ...


3

Your code is currently a linear sequence of steps. Even for fairly short programs, that's an inflexible structure that is difficult to experiment with, debug, test, and evolve. The solution is to break your code apart into separate functions, each having a narrow focus. Here's a rough sketch. The basic idea is to place all code (other than imports and ...


3

You should add a requirements.txt containing something like tabulate pandas requests numpy matplotlib The first requirement was hidden and bit me when I attempted to run your code. Introduce error checking to your requests call, and let it handle encoding for you: url = 'https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv' with requests....


3

In addition to SuperStormer's points ... Unused variables _sin and _cos are never used. You don't need to assignments to these variable. Naming conventions In the event you will eventually use them... Leading underscore are used to indicate private members of a class. To avoid collisions with keywords, by convention, a trailing underscore is used (i.e. ...


3

Your regular Python implementation generally looks reasonable, and unless numpy offers a performance boost that you really need, I would not recommend it for this use case: the brevity/clarity tradeoff seems bad. My biggest suggestion is to consider clearer names. A function with signature of take_upto_n(A, n) makes me think the function takes an iterable ...


3

Your code appears to be quadratic in the number of groups. Each call to np.concatenate() allocates enough memory to hold the new array and then copies the data. The first group is copied the first time through the loop. Then the first and second groups on the second time. Then the first to third groups on the third time, etc. To speed this up, keep a list ...


2

A more pythonic method for that last for loop would be to use nested list comprehensions. It's likely faster, as well: [item for sublist in [[x.real, x.imag] for x in answer] for item in sublist] See this question for details about what's happening here, if you're not already familiar with list comprehensions. In other words, instead of these four lines: ...


2

A quick fix to the MemoryError issue is to avoid expanding the sparse similarity matrix into a dense matrix for extracting indices in the get_scores function. Instead, the indices can be extracted by operating directly on the sparse matrix. def get_scores(pairwise_similarity, doc_keys, threshold=0.9): sim_coo = pairwise_similarity.tocoo(copy=False) ...


2

Okay, so there are a lot of problems here. I think the best way to address them is in stages. I want to show you how to go through your own code critically and address problems in it as you write. Before I begin, for the benefit of someone who will review your code, it is helpful to include all the imports you used so that the person doing the review can ...


2

You can replace: d12 = np.array([a+b for a, b in list(itertools.product(p1.T, p2.T))]) with something like: p1 = p1.T p2 = p2.T p3 = p3.T d12 = p1[:,np.newaxis,:] + p2[np.newaxis,:,:] d12 = my_d12.reshape((len(p1)*len(p2),2)) I find it most of the times easier to use the first index of an array for point_index and the second index for the dimensions, hence ...


2

First, use pyplot instead of pylab. Secondly, check out PEP 8, the Python style guide. stdlib imports should be at the very top, then 3rd-party imports. However, math isn't even used anywhere, so you can just remove the import entirely. In addition, your spacing is a bit inconsistent. Assignments should have 1 space on each side and there should be a space ...


2

Using itertools.groupby What you are looking for is itertools.groupby. When there is an odd number of groups then we use try-except block here. from itertools import groupby get_grp_len = lambda grp: len([*grp]) def transform(b): if len(b) == 0: # if not b wouldn't work since your `b` is ndarray return [] it = groupby(b) out = [] ...


2

Since the output of each step depends on the previous step, it is impossible to entirely eliminate the loop unless the formula can be simplified to allow a multi-step computation. However, the code can definitely still be improved. Calling np.concatenate is generally inefficient since it requires memory reallocation and data copying. As long as possible, ...


2

In addition to Reinderien's answer, may I suggest: Yes, Use Memoryviews to speed up access In addition to the code you have, I would also type the a_mat and b_mat matrixes as double[:,::1] following the Typed Memoryviews guide. (the "1" means contiguous and is allows for slightly faster access). You are right in that you can not cdef declare ...


2

You could use np.random.choice() to generate the states, while using a==b and a!=b as masks. def new_states(a,b,n): return [ a*(a==b) + np.random.choice(2,len(a))*(a!=b) for _ in range(n) ] state1 = np.array([0, 1, 0, 1, 1]) state2 = np.array([1, 1, 0, 0, 1]) print(new_states(state1,state2,4))


2

Few suggestions: Input validation: there is no input validation for win_size and padding. If win_size is -3 the exception says ValueError: Stride must satisfy 0 < stride <= -3.. If padding is a string, numpy throws an exception. Type hints: consider adding typing to provide more info to the caller. f-Strings: depending on the version of Python you ...


2

Boy, those are huge numbers. 5000 Nodes results in an adjacency matrix with 25 million entries. But since Deltaik(a,b) == Deltaik(b,a), you don't actually need to consider the whole cartesian product. In fact, your current code doubles the chance for an edge to be created because each pair of nodes (a,b) gets two chances: one for (a,b) and one for (b,a). ...


2

Use of global variable In your function, you use test from the global scope in the comprehension which can lead to nasty side effects. Type hints and formatting Type hints and proper formatting help future readers understand the code easier. Modern code editors and static code analysis tools can also use them. Docstring A function docstring also helps future ...


2

Coordinates as a matrix This: x_co = [np.random.uniform(0, 2) for i in range(n_particles)] y_co = [np.random.uniform(0, 2) for i in range(n_particles)] return x_co, y_co is a little baffling. You call into Numpy to generate uniformly distributed numbers (good) but then put it in a list comprehension (not good) and separate x and y into separate lists (not ...


2

A significant improvement is to use lists and Python's built in append and convert the final list to array, instead of using np.append. I've run a test to demonstrate the performance enhancement: def lorenz(t,u): s=10 r=24 b=8/3 x,y,z=u vx=s*y-s*x vy=r*x-x*z-y vz=x*y-b*z return np.array([vx,vy,vz]) x0=[2,2,2] t, u = rkf( f=...


2

All these lines are really weird: a4 = 9.230769230769231e-01 # 12/13 Unless you have a good reason (which I'd then state in the code as a comment) to do that, just write a4 = 12/13 instead. Gonna be the same anyway: >>> import dis >>> dis.dis('a4 = 12/13') 1 0 LOAD_CONST 0 (0.9230769230769231) 2 ...


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