|
Oh cool, I haven't looked at that. Are you talking about the DataFrames structure? I'm not familiar with Python and its packages so I'm using this class also as an opportunity to learn Python.
|
# ? Apr 8, 2016 21:29 |
|
|
# ? May 9, 2024 19:49 |
|
drainpipe posted:Oh cool, I haven't looked at that. Are you talking about the DataFrames structure? I'm not familiar with Python and its packages so I'm using this class also as an opportunity to learn Python. Yep.
|
# ? Apr 8, 2016 21:40 |
|
drainpipe posted:I'm coding up the k-means clustering algorithm for a homework assignment. I'm given a list of data points (each with two coordinates) and I need to put them into k clusters. The way I'm doing it is to define k empty lists and fill them up with indices of my data points according to which cluster they belong to. I just realized that adding an extra column to the data array to indicate cluster assignment might work just as well. full_list = [[jj for jj, cc in enumerate(cluster_indices) if cc == ii] for ii in sorted(list(set(cluster_indices)))] Probably not the smartest way to go about it though. Assuming you have a numpy array where the 1st column is the value and the second column is the cluster index, you'd get the cluster mean for example via arr[arr[:, 1] == cluster_index].mean() or np.where(arr == cluster_index)[0].mean() Pandas is more comfortable though.
|
# ? Apr 8, 2016 21:58 |
|
Pandas is both a bit much and not quite the right fit for this imo. It makes updating the means easier, but it makes finding the cluster assignments harder, so you might as well do it in straight numpy where both things are fairly easy. Storing your cluster ids in the same matrix as your features is probably not ideal, if you keep them separate you can do this: code:
|
# ? Apr 8, 2016 22:30 |
|
I'm probably missing something incredibly stupid, but I've been staring at this and I can't figure it out:code:
edit: if I hardcode rwin to be the right number, it works, but not if I start it at 0 and increment it. long-ass nips Diane fucked around with this message at 03:01 on Apr 9, 2016 |
# ? Apr 9, 2016 02:55 |
|
Cingulate posted:Possibly slightly better: You wrote exactly the same code but with one variable name changed. There is no meaningful difference here
|
# ? Apr 9, 2016 04:39 |
|
Swagger Dagger posted:I'm probably missing something incredibly stupid, but I've been staring at this and I can't figure it out: Difficult to say without having more code. Try printing rwin, size, type(rwin), and type(size) right before the first for loop so that you can be sure that everything is the right value and right type going in. Your circumstance makes it sound like maybe rwin isn't an integer
|
# ? Apr 9, 2016 05:47 |
|
This is also exactly the kind of thing using a debugger will usually solve. A good IDE like PyCharm will have one built in, where you can step through your code line by line as it executes and examine the state of all the variables at any time.
|
# ? Apr 9, 2016 05:53 |
|
sorry, i can't use pycharm because i ssh into work from a tropical beach and the internet is a bit dodgy for anything more graphically intense than vim
|
# ? Apr 9, 2016 05:56 |
|
QuarkJets posted:sorry, i can't use pycharm because i ssh into work from a tropical beach and the internet is a bit dodgy for anything more graphically intense than vim Get pycharm's pro version and use a deployment + remote interpreter.
|
# ? Apr 9, 2016 11:20 |
|
Munkeymon posted:Extension methods don't do what you want? I don't know what you mean - I don't understand how extension methods would be relevant to this, but if you can point me to something I don't know about them that would be great.
|
# ? Apr 9, 2016 11:56 |
|
One of the students in the intro class I'm a TA for did this in his project in order to update the data structure (D and D_new are both dictionaries):Python code:
|
# ? Apr 9, 2016 13:32 |
|
It's parameter unpacking; you can do it whenever calling any function to pass a dict as keyword args. Seems like it's working the same as if you did:Python code:
Python code:
|
# ? Apr 9, 2016 13:55 |
|
QuarkJets posted:You wrote exactly the same code but with one variable name changed. There is no meaningful difference here
|
# ? Apr 9, 2016 14:02 |
|
Cingulate posted:It's a very minor change, but 1. i/I as a variable is sometimes discouraged because it evokes imaginary numbers (don't ask me, I just see this all the time), 2. using _ makes a bit clearer we don't care about it. I don't like this practice of using _ as a name for an "unused" variable. I think it's a bad convention. Firstly _ already means something in the standard Python interactive console, secondly it's opaque and baffling if you haven't seen it before. I prefer to use a name like "_unused" or similar. You only write it in one place after all.
|
# ? Apr 9, 2016 14:44 |
|
Hammerite posted:I don't like this practice of using _ as a name for an "unused" variable. I think it's a bad convention. Firstly _ already means something in the standard Python interactive console, secondly it's opaque and baffling if you haven't seen it before. I prefer to use a name like "_unused" or similar. You only write it in one place after all.
|
# ? Apr 9, 2016 15:14 |
|
Cingulate posted:Hm ... is there a one-character option you like better? Well, no there isn't. There's no one-character name that means "this is an unused variable the author doesn't care about" to me, although I know _ is that to some people. Like I say, given that it's by definition something you'll type only once, there's not much need for terseness.
|
# ? Apr 9, 2016 15:38 |
|
I always thought the idea was to make unused variables disappear into the background, so it makes the code's meaning easier to parse
|
# ? Apr 9, 2016 16:27 |
|
If you're looking for self-contained readability your best bet isPython code:
I'm sure there's an even more concise way of using map() to do this that makes [] a callable so you don't even need the lambda.
|
# ? Apr 9, 2016 16:55 |
|
code:
|
# ? Apr 9, 2016 19:01 |
|
Python code:
|
# ? Apr 9, 2016 19:37 |
|
Asymmetrikon posted:
Python code:
|
# ? Apr 9, 2016 19:50 |
|
Asymmetrikon posted:
You don't get 5 different lists with this, you get 5 references to the same list.
|
# ? Apr 9, 2016 20:09 |
|
Hammerite posted:You don't get 5 different lists with this, you get 5 references to the same list. You're right Python code:
|
# ? Apr 9, 2016 20:16 |
|
OnceIWasAnOstrich posted:
This is the best solution
|
# ? Apr 9, 2016 22:27 |
|
Blinkz0rz posted:This is the best solution code:
|
# ? Apr 9, 2016 22:33 |
|
Man I wish there was something in the documentation for itertools that explained how repeat works wrt object identity.
|
# ? Apr 9, 2016 22:46 |
|
quote:I don't like this practice of using _ as a name for an "unused" variable. I think it's a bad convention. Firstly _ already means something in the standard Python interactive console, secondly it's opaque and baffling if you haven't seen it before. I prefer to use a name like "_unused" or similar. You only write it in one place after all.
|
# ? Apr 10, 2016 00:00 |
|
Nippashish posted:Except it's wrong. Yeah I was just pointing out identical functionality to that class in the standard library. Lets keep this code golf rolling. Don't need the outside list call in py2. Python code:
OnceIWasAnOstrich fucked around with this message at 00:11 on Apr 10, 2016 |
# ? Apr 10, 2016 00:05 |
|
alist = [[l] for l in 'alist'] for sublist in alist: sublist.pop()
|
# ? Apr 10, 2016 00:33 |
|
Python code:
Dominoes fucked around with this message at 00:40 on Apr 10, 2016 |
# ? Apr 10, 2016 00:34 |
|
Cingulate posted:Hm ... is there a one-character option you like better? Yes, the letter i. It's probably the most common single-character "throwaway" variable name for an integer in all of computer science, across almost all languages. In Matlab code it's problematic because i is a predefined variable in Matlab's enormous namespace. That's more Matlab's problem than anything. It's certainly not an issue in Python code.
|
# ? Apr 10, 2016 01:00 |
|
Hello, I hope this is the thread for the stupid newbie questions thread, otherwise, I'm sorry and please point me in the right direction. I'm completely new at Python, having only taken a course in edX. https://courses.edx.org/courses/course-v1:Microsoft+DAT208x+2T2016/info Which is very basic. I'm trying to do some stuff with pandas, some simple DataFrames stuff, importing csv files. I'm using a Python console in Spyder, which came with Anaconda. I have no idea what any of this means, but apparently it was the easiest way for a newbie to get started according to the internet, but that's a different subject. I basically have two lists, and I'm trying to "map" (I think) a column in the first one according to the data in the second one. Python code:
data has five columns, so in order to avoid using .loc I tried masking the column I need to a new list RC=data['Reporter Code'] The thing is, when I try to change ONE data point as in Python code:
Can someone help me? Is the situation clear enough? So far this has been kind of fun. The last programming I did was some Free Pascal several years ago, and I'd forgotten how great it felt to see your code working. I'd love to be able to continue using Python. Any help would be appreciated, thanks in advance. edit: I wasn't being "locked out", the command was just taking a very long while, due to the error popping up every single time it tried to overwrite a value. After dinner I came back to a list full of "JOHN", I am now running with the correct code to see if it works after a while. There's probably a way to check in which loop cycle it is, but I haven't found it so far. There we go, took 7 minutes, but successfully changed all the numbers into the corresponding string. Mr. Nemo fucked around with this message at 02:58 on Apr 10, 2016 |
# ? Apr 10, 2016 01:23 |
|
Nippashish posted:Get pycharm's pro version and use a deployment + remote interpreter. Pydev uses the same codebase for remote debugging and happens to be free, btw.
|
# ? Apr 10, 2016 05:07 |
|
OnceIWasAnOstrich posted:Lets keep this code golf rolling. Python code:
|
# ? Apr 10, 2016 06:38 |
|
BigRedDot posted:I can count the times I've used the standard interactive console _ in the last 20 years on zero fingers. Well since you don't use it, obviously nobody does! You have opened my eyes.
|
# ? Apr 10, 2016 12:01 |
|
Let's not have a fight over something stupid I said.
|
# ? Apr 10, 2016 12:40 |
|
Would partial unpacking be a good language feature? That would resolve this. ie:Python code:
Or per this stack overflow page. Python code:
Dominoes fucked around with this message at 14:37 on Apr 10, 2016 |
# ? Apr 10, 2016 14:21 |
|
Dominoes posted:Would partial unpacking be a good language feature? That would resolve this. ie:
|
# ? Apr 10, 2016 14:31 |
|
|
# ? May 9, 2024 19:49 |
|
Cingulate posted:Only works (comfortably) with continuous slices though. What if you want only the second and second to last of 7 outputs? Do y'all have unused variables besides during unpacking? I think the best way to do this is the last code I posted, but here's another: Python code:
Dominoes fucked around with this message at 14:40 on Apr 10, 2016 |
# ? Apr 10, 2016 14:33 |