|
Sad Panda posted:Next part of my Blackjack program. A lookup table. A short extract of the data would be... You could make each row a namedtuple type with a parameter for each column name
|
# ? Mar 5, 2018 23:31 |
|
|
# ? Jun 5, 2024 07:09 |
|
Sad Panda posted:Next part of my Blackjack program. A lookup table. A short extract of the data would be...
|
# ? Mar 6, 2018 00:00 |
|
baka, thank you. I tried namedtuples but it seemed a bit tedious.Cingulate posted:In pandas, that would be df.loc["Hard 6", 2] Thank you. I created a CSV of the whole table. Python code:
code:
|
# ? Mar 6, 2018 00:38 |
|
Sad Panda posted:baka, thank you. I tried namedtuples but it seemed a bit tedious. That was super simple. Python code:
|
# ? Mar 6, 2018 18:48 |
|
I think that function is overkill. Either that, or it's a bit inefficient to always read in the csv whenever you want to retrieve one single number. You could also write an input check that gracefully fails whenever you request a combination that doesn't exist. In the long run, you probably want to construct a class (to handle state), and that class could store the df, and that function could be a method.
|
# ? Mar 6, 2018 18:54 |
|
Cingulate posted:I think that function is overkill. Either that, or it's a bit inefficient to always read in the csv whenever you want to retrieve one single number. You're right about the reading of the CSV. I've moved that outside of the function so it should just happen at load when the df object is created and then stay loaded in cache right?
|
# ? Mar 6, 2018 19:03 |
|
Sad Panda posted:You're right about the reading of the CSV. I've moved that outside of the function so it should just happen at load when the df object is created and then stay loaded in cache right? Though as I said, you will probably end up using a class or two.
|
# ? Mar 6, 2018 19:06 |
|
I constantly finding myself going what is that path to the thing I need to import and I start guessing is it "Foo" or "Foo.SubFoo" or "Foo.Sub.Bar"? Is there some easy way with Python, I use Pycharm specifically, to not have to Google where to import what I need from?
|
# ? Mar 7, 2018 03:02 |
|
If pycharm is set up correctly it should tab complete imports.
|
# ? Mar 7, 2018 03:38 |
|
You can also press Alt+Enter as you're writing code (referring to an object that you haven't imported yet) and PyCharm will give you a selection of possible imports and automatically add it to the top of the file.
|
# ? Mar 7, 2018 04:26 |
|
SurgicalOntologist posted:You can also press Alt+Enter as you're writing code (referring to an object that you haven't imported yet) and PyCharm will give you a selection of possible imports and automatically add it to the top of the file. Yes, this. It's the best.
|
# ? Mar 7, 2018 07:06 |
|
Thermopyle posted:Yes, this. It really is.
|
# ? Mar 7, 2018 08:11 |
|
Thermopyle posted:Yes, this. I don't know, Ctrl-B to go to wherever the thing your cursor is on is defined gives that a run for its money.
|
# ? Mar 11, 2018 21:42 |
|
Quick question about Pandas or Numpy I have a data file that contains text portions. One portion lists a CSV like structure defining an nxn table. Here is an example: code:
edit: creating a numpy array first then converting to dataframe worked well enough. creatine fucked around with this message at 14:57 on Mar 12, 2018 |
# ? Mar 12, 2018 14:14 |
|
Here is my folder structure:code:
Python code:
|
# ? Mar 14, 2018 19:08 |
|
You'll want to put an __init__.py everywhere so can import your folders as packages which will allow you to do this Python code:
|
# ? Mar 14, 2018 20:38 |
|
Dr Subterfuge posted:You'll want to put an __init__.py everywhere so can import your folders as packages Thanks. Am I correct in thinking that with the following structure and __init__.py placement: code:
Python code:
Python code:
But with the following structure and __init__.py placement: code:
Python code:
|
# ? Mar 14, 2018 21:32 |
|
It's not clear to me that code:
This will work with an __init__.py in /scripts though: code:
Dr Subterfuge fucked around with this message at 22:26 on Mar 14, 2018 |
# ? Mar 14, 2018 22:09 |
|
Jose Cuervo posted:Thanks. You're incorrect in your thinking, so that's why it's not working. In this structure: code:
code:
code:
code:
This part that you wrote is fine: code:
|
# ? Mar 15, 2018 17:28 |
|
__init__.py is basically a flag that tells python the folder that contains it is a package that can be imported. It can optionally contain some code to do some setup work (but it's going to be called every time you import anything in that package, so things should only go there if they really are general for the whole package). Python files can also be imported directly and are known as modules. The lookup behavior for python is determined by an environment variable called PYTHONPATH, which is basically a list of places on your system to look through when python reaches an import statement, plus the current directory (where you launched your script). The search precedence starts with your current directory and works down the list in PYTHONPATH. The search stops as soon as it finds something, so if you have a local module called math, python will import that instead of the default one because your current directory takes precedence over everything else. Any module that is in a folder pointed to in your list of search paths can be imported directly, which means, for example, you if you have main.py in the same folder as foo.py, you can just import foo from main like this: code:
code:
Dr Subterfuge fucked around with this message at 19:32 on Mar 15, 2018 |
# ? Mar 15, 2018 19:26 |
|
Boris Galerkin posted:Helpful explanation. Got it. Dr Subterfuge's suggestion of adding the scripts folder to the path does make what I posted work, and seems much simpler than having to rename the scripts folder.
|
# ? Mar 15, 2018 19:27 |
|
Jose Cuervo posted:Got it. Dr Subterfuge's suggestion of adding the scripts folder to the path does make what I posted work, and seems much simpler than having to rename the scripts folder. Yeah appending to sys.path works too but it’s more of a bandaid solution. I’m not saying it’s wrong to do it or anything but just something to keep in mind. Also you coulda just kept scripts unchanged and do “from scripts import mymodule as ig” as well, didn’t need to rename the directory.
|
# ? Mar 15, 2018 21:26 |
|
Jose Cuervo posted:Got it. Dr Subterfuge's suggestion of adding the scripts folder to the path does make what I posted work, and seems much simpler than having to rename the scripts folder. Be careful. Editing the python path is kinda/sorta of a hacky way to handle the situation. I mean, sometimes you need to do it because you need to preserve an API for legacy reasons or something. You're better off just organizing your poo poo correctly.
|
# ? Mar 16, 2018 03:33 |
|
Yeah I mostly just trying to present a more complete picture of what was going on. The simplest way is making /scripts into a package and... not doing whatever it is you think is making editing sys.path necessary.
|
# ? Mar 16, 2018 04:29 |
|
Question about matrix and vector multiplication: I am working on a small project for work that reads in some scientific data from a flow cytometry experiment. The basic layout of the data is that I have m number of columns which are markers of interest and n number of rows which represent individual cells/events. I have to apply compensation to certain markers based on a given spill over matrix. The general format for the compensation is this: And I was using this to help me understand the math a bit more: So far I can successfully get the inverse of the spillover matrix and select the markers needed for compensation. But, my question is: what would the most efficient way to do the calculations needed? I am hoping there's an easy to use numpy function that can do this so I don't have to iterate through every row and manually do the calculations.
|
# ? Mar 16, 2018 11:42 |
|
creatine posted:Question about matrix and vector multiplication: You can read matrices into 2D numpy arrays (check that it’s in the proper “orientation”). Inverse of a matrix is provided by numpy.linalg.inv(M) and transpose of a matrix is built into the 2D array class with the “.T” property, eg M.T. code:
|
# ? Mar 16, 2018 12:24 |
|
Boris Galerkin posted:You can read matrices into 2D numpy arrays (check that it’s in the proper “orientation”). Sorry I wasn't clear. In second image at the bottom it tells you how to calculate FITCtrue. That's what I'm looking to do for every item in the uncompensated data column. So if i feed in a column of (1000,) and have the compensation vector that is (1,11) I am looking to get a return vector that maintains the (1000,) shape but with new values based on the true equation from above. Right now I'm going to try the dot function in pandas since that's what my datasets are currently read into. Edit: basically I have a column vector of values: [50 60 70] Then I have a row vector of other values [1.005, -0.004, 0] And what I need to do is get the sum of the multiplication of each item in column * row so like: [(50*1.005)+(50*-0.004)+(50*0) Then the same for 60 and 70 and I should get a 3,1 vector as a result creatine fucked around with this message at 00:14 on Mar 17, 2018 |
# ? Mar 16, 2018 12:37 |
|
Python: fly with two words. Rust: Try to manipulate strings and end up with cows borrowed from strange places.
|
# ? Mar 17, 2018 01:28 |
|
I was learning some Comp Sci and that involved learning about linear search, binary search, bubble sort, insertion sort and merge sort. I decided that the best way to make sure that I understand them was to code them. I'm still to write the merge sort, but the other ones work. While the insertion sort is super fast, the bubble sort is certainly not. Could someone point out the glaring mistake in my code? The first is my insertion sort split into two functions. Python code:
Python code:
Sad Panda fucked around with this message at 00:34 on Mar 18, 2018 |
# ? Mar 18, 2018 00:30 |
|
I don't know much about sorting but I do know things tend to run a lot faster when you're not printing a bunch of stuff out.
|
# ? Mar 18, 2018 01:47 |
|
Sad Panda posted:Could someone point out the glaring mistake in my code? This: Sad Panda posted:
|
# ? Mar 18, 2018 02:04 |
Symbolic Butt posted:This: ed, nm, answering the wrong question. PS: you shouldn't pop things from the front of a list. If you must pop from the front, use collections.deque; if the order doesn't matter, just use pop() (without arguments) or iterate through. A couple other bits of python niceties you could use: the enumerate creates an index for the collection you're looping through. EG: Python code:
pre:(0, g) (1, o) (2, o) (3, n) Python code:
Eela6 fucked around with this message at 05:46 on Mar 18, 2018 |
|
# ? Mar 18, 2018 05:03 |
|
Eela6 posted:ed, nm, answering the wrong question. Thanks, I'll look at deque. The explanation of an insertion sort that I saw basically said the way it works is. You get a set of data, [3, 2, 8 , 4, 1, 9, 43] You split it in 2, a sorted list (starts empty) and an unsorted list (starts as the whole of your original list). [] [3, 2, 8 , 4, 1, 9, 43] Iterate through the unsorted list, taking the first value and adding it to the sorted list in the correct position to ensure that it is still sorted. [3] [2, 8 , 4, 1, 9, 43] [2, 3] [8 , 4, 1, 9, 43] [2, 3, 8] [4, 1, 9, 43]
|
# ? Mar 18, 2018 09:30 |
|
Say I have two dataframes: df1 code:
code:
code:
Python code:
Dr Subterfuge fucked around with this message at 10:31 on Mar 18, 2018 |
# ? Mar 18, 2018 09:38 |
|
You mean like this?code:
|
# ? Mar 18, 2018 10:46 |
|
That’s a one liner with pd.merge().
|
# ? Mar 18, 2018 13:55 |
|
Cingulate posted:You mean like this? That feels much better. Somehow I was fixated on directly building the df I wanted instead of relying on col assignment. On the other hand vikingstrike posted:That's a one liner with pd.merge(). Oh hell. So it is. Python code:
|
# ? Mar 18, 2018 17:05 |
|
Sad Panda posted:
Not that it really matters, but the way you're checking if no swaps happened can cause an extra pass to occur. In a list of n items you're doing n-1 comparisons each pass, but you're looking for a chain of n or more comparisons that didn't cause a swap. So with a list like 5,1,2,3,4 the 5 will bubble up on the first pass, and now the list is sorted. But you want 5 swapless comparisons, and so far you have 0, so you do another pass, and now you have a run of 4. Gotta do it another time! Now you have 8 You could change the comparison operator, but really you can do this all a lot simpler - on each pass, set a 'swapped' flag to false. If you do a swap, set it to true. At the end of the pass, if swapped is still false, you're done! You can also do smarter stuff with the looping, based on the idea that the largest item always bubbles to the right so the list gradually gets sorted from right to left (so you don't need to check those end items anymore) but bubble sort isn't very efficient anyway - that's probably why it seems slow, it is! While we're on this, this is the best one of those visualisation videos I've seen https://www.youtube.com/watch?v=sYd_-pAfbBw The hue is where the dot should be in the array, and the closeness to the centre is how out of position it is. As each one gets sorted it flies out to its position on the circle edge. Some wild stuff happens in there baka kaba fucked around with this message at 17:45 on Mar 18, 2018 |
# ? Mar 18, 2018 17:38 |
|
baka kaba posted:While we're on this, this is the best one of those visualisation videos I've seen Holy poo poo Radix is amazing.
|
# ? Mar 18, 2018 19:39 |
|
|
# ? Jun 5, 2024 07:09 |
|
Sad Panda posted:I was learning some Comp Sci and that involved learning about linear search, binary search, bubble sort, insertion sort and merge sort. I decided that the best way to make sure that I understand them was to code them. It's worth pointing out that many of these algorithms operate on *fixed length arrays* not *python lists*. When you're calling pop, insert, append, you're changing the length of the list, as well as changing the list. With these old algorithms, you probably want to stick to arr[0] = ..... Or maybe, build a new list entirely. The code always gets a bit tricky when you're using one data structure in place of two: For example: this operates like an insert sort (find smallest, put at end of new list), but it doesn't re-use the front old list to store the values code:
Insertion sort divides the list into two pieces, the smallest element and all elements larger than it. Bubble sort tries to make the list slightly less unsorted with each pass, moving elements to the right position. Both of these strategies can be adapted to build other sorting algorithms, and sometimes the tactics from the 60's and 70's aren't as valid today You could make different tactics: like keeping the min, max as you scan through the list, and build up a new list from either end. Or perhaps change the strategy: break the list into (smaller than, bigger than) rather than (min, bigger than). Then sort either half. You can start to develop a feel for different ways to sort structures. It might be worth looking at how python does it too, it uses a wonderful trick: Break the list into pre-sorted chunks and merge them. It's a little of both worlds: Scanning through a list to find out of place elements, and breaking the list down into smaller pieces to operate on.
|
# ? Mar 18, 2018 20:11 |