r/Python • • Jun 09 '16

Scientific Python Cheat Sheet

https://ipgp.github.io/scientific_python_cheat_sheet/
476 Upvotes

62 comments sorted by

24

u/uhkhu Jun 09 '16

No Pandas?

18

u/Link2mypast Jun 09 '16

That is an excellent point. Looks like you could add it if you are feeling generous:

Github source

7

u/tfhoon Jun 10 '16

Hi,

one of the contributors to https://github.com/IPGP/scientific_python_cheat_sheet here . We don't use pandas much. So we would not know what's essential and should be added to the cheat sheet. It may come with time or as previously said if someone feels generous.

6

u/[deleted] Jun 10 '16

Inclusion of pandas would be beyond awesome! I would have this at my desk. Also, is there a typo in the function call under @myDecorator?

6

u/Link2mypast Jun 10 '16

looks like a typo. you should submit a fix

Github source

11

u/masasin Expert. 3.9. Robotics. Jun 10 '16

Why Python 2?

6

u/tfhoon Jun 10 '16

Good question. I think it is out of habit and has not been thought through.

10

u/[deleted] Jun 10 '16

What dead language is this? print 'func'

5

u/datosh Jun 10 '16

Maybe add the [:] operator as a nice 'pythonic' way to copy a list. Example:

l = [1, 2, 3, 4, 5]
k = l
k[3] = 0
print(k[3] == l[3])   # True
print(k[3], l[3])       # 0 0

l = [1, 2, 3, 4, 5]
k = l[:]
k[3] = 0
print(k[3] == l[3])   # False
print(k[3], l[3])       # 0 4

3

u/flutefreak7 Jun 10 '16

Yeah, and a[::-1] to get the reverse of a numpy array.

1

u/Ogi010 Jun 10 '16

I like the .copy method (only works in Python 3)

k = l.copy()

6

u/hharison Jun 10 '16

This is kind of silly, and there's no reason to prefer one way or the other, but does anyone else do from matplotlib import pyplot as plt rather than import matplotlib.pyplot as plt?

2

u/deaddodo Jun 10 '16

The more "pythonic" way of handling imports is argued to be:

import blah[.blahbity] [as blah2]

but both are perfectly acceptable.

I generally avoid "from...." since it makes the import lines look cluttered (IMO).

1

u/hharison Jun 11 '16

Just curious if you weren't using an alias but wanted to do a nested import without the parent namespace would you do import scipy.stats as stats or from scipy import stats? Just thinking out loud here...

1

u/PeridexisErrant Jun 11 '16

his is kind of silly, and there's no reason to prefer one way or the other, but does anyone else do from matplotlib import pyplot as plt rather than import matplotlib.pyplot as plt?

I prefer the latter; most concise is not always most elegant but there is a strong correlation.

There actually is a (potential, IIRC...) difference in behaviour when you're monkeypatching something. Basically from x import y detaches y from the parent namespace and changes don't propagate back, while changing x.y after import x.y will change the instance used in other modules. This should only come up when you have circular patched imports, I think (I'm not proud of that code).

1

u/hharison Jun 11 '16

Good point. Come to think of it, I think the reason I do it with from is similar structure to other nested imports. If I wasn't using an alias I would do from matplotlib import pyplot rather than import matplotlib.pyplot as pyplot. So it makes sense to do it the same way with an alias. If I want to switch between using an alias and not using an alias, I don't also switch the structure of my import statement.

I just realized this is probably my motivation. I don't think it's a good reason, readability is more important, so I'll probably switch to the traditional way.

1

u/TheBlackCat13 Jun 11 '16

I find it harder to mentally parse your version. It is longer and has an additional unneeded statement that makes it more complex.

0

u/[deleted] Jun 10 '16

[deleted]

1

u/XtremeGoose f'I only use Py {sys.version[:3]}' Jul 28 '16

You know ipython has a %matplotlib inline now? %pylab is depreciated.

1

u/[deleted] Jul 28 '16

Yeah, but all my muscle memory is for pylab and not matplotlib or plt.

3

u/Architect-Jeff Jun 09 '16

Wow, thank you for taking the time to post this.

3

u/neuroneuroInf Jun 09 '16

This is great, thank you!

3

u/azur08 Jun 10 '16

This is VERY awesome. I find myself forgetting fundamentals pretty frequently

2

u/qu4ku Jun 10 '16 edited Jun 10 '16

generators:

x = [for i in firstn(10)]

should be:

x = [i for i in firstn(10)]

2

u/Misterandrist Jun 10 '16 edited Jun 10 '16

Plus as written it demonstrates a list comprehension and not a generator (well... kind of) there's a difference between them that I think could be expressed more clearly

Edit: spelling

4

u/[deleted] Jun 10 '16

s/compression/comprehension

1

u/Misterandrist Jun 10 '16

Uf da. autocorrect strikes again.

2

u/tfhoon Jun 10 '16

thanks for pointing that. it's been edited and made clearer.

2

u/datosh Jun 10 '16

In the dict examples thrid line: shouldnt it be

c = [value for key, value in **a**.items()] 

since a is the dict and b is only a single item from the list assigned in the previous line?

1

u/tfhoon Jun 10 '16 edited Jun 10 '16

yep, you're right.

2

u/tfhoon Jun 15 '16

a new Pandas section is opened on the cheat sheet, we need help to fill it and extract the core things to remember about Pandas. Don't hesitate to contribute on github.

1

u/ggchappell Jun 10 '16

What does the red highlighting mean? For example, in the "rounding" subsection, "round" is in red, but "ceil" and "floor" are not. Is this supposed to communicate something meaningful, or is it just a mistake?

2

u/jkw552403 Jun 10 '16

Those are in red ('any', 'all', 'round', etc.) used the same naming as some python built-in functions and the syntax highlighter make them red.

1

u/ggchappell Jun 10 '16

Ah, that makes sense. Thanks.

1

u/SUBWAYJAROD Jun 10 '16

I make it a goal to write up a much more simple version of one of these for each language I learn. This is amazing. Can you do more?

1

u/[deleted] Jun 10 '16

Nicely done! I remember that I made a "matrix" cheatsheet a few years ago (which is visually not so nicely done :P), you are welcome to take & add content from there if useful

1

u/Ogi010 Jun 10 '16

btw looked at yours, to create a column vector in python, numpy you can use the np.array.ravel() function, instead of reshape(1, 3) as in your example.

1

u/TheBlackCat13 Jun 11 '16

ravel doesn't create a column vector, it creates a true (1D) vector. Those are not the same thing for arrays.

1

u/Ogi010 Jun 11 '16

Good point!

-32

u/Homersteiner Jun 09 '16 edited Jun 10 '16

iPython, ha ha, nope.

EDIT: If you use iPython you are fucking retarded.

15

u/Link2mypast Jun 09 '16

Ipython is extremely helpful in scientific endeavors. When you want to pull apart a data set and explore it for instance.

-33

u/Homersteiner Jun 09 '16

Ipython is extremely helpful in scientific endeavors.

If you are not scientifically trained.

10

u/azur08 Jun 10 '16

What does that even mean?

1

u/[deleted] Jun 11 '16

Ignore Homersteiner; they are, at best, not helpful and, at worst, just trolling.

-31

u/Homersteiner Jun 10 '16

Science -> Training -> Your partner has none.

12

u/swingking8 Jun 10 '16

I'm scientifically trained and I love IPython. What's your background? The overwhelming majority of educated people I know don't feel the need to talk down other people's education

7

u/rxcdb Jun 10 '16

Don't bother. He is a troll.

I am a PhD student and IPython Notebooks (or Jupyter) made my life so much easier. Inline matplotlib and so much more!

3

u/lcq92 Jun 10 '16

I use and love Ipython notebooks. But what's that 'inline matplotlib'? never heard of it

4

u/swingking8 Jun 10 '16

I use and love Ipython notebooks. But what's that 'inline matplotlib'? never heard of it

It's a way to get embedded graphs in your notebook

3

u/rxcdb Jun 10 '16

Execute %matplotlib inline before you plot anything. The plots will then appear in the notebook instead of a separate window. Very convenient.

-13

u/Homersteiner Jun 10 '16

I have a phd and 13 years of experience. So suck it.

8

u/ZedOud Jun 10 '16

In what?

(Business studies amiright. No? No takers? K.)

Are you implying your Ph.D. and the experience were usefully exclusive but not in unrelated fields?

"I have Ph.D. in Group Dynamics with 13 years experience of talking down to others and getting nothing done because I ignore the realities of my field of expertise."

Your training for scientific computing, was it the type I went through in an academic environment where they extolled the virtues of specialized computation at the cost of reproducibility? The biggest sin of a scientific endeavor is to fail to document your endeavor. The tool you disparaged is a shining example of one such tool meant for widely communicating and (more importantly) easily reproducing experiments.

For my own speculation: your under appreciation of such a useful tool might speak to insulation at a rational or interpersonal level in your academic, career, or other "training" and "experience" related environments. Insulation hinders science. Fix it.

3

u/planx_constant Jun 10 '16

If you look at that guy's comment history, he's either a deliberate troll or a huge asshole. I wouldn't waste much time on him either way.

5

u/sportif11 Jun 10 '16

Your post history is gross.

-9

u/Homersteiner Jun 10 '16

This is not The Facebook you dipshit.

5

u/sportif11 Jun 10 '16

What does that even mean?

-9

u/Homersteiner Jun 10 '16

That means you are either retarded or just stupid. Take your stupid shit back to The Facebook FFS.

2

u/sportif11 Jun 10 '16

You are lonely.

-4

u/Homersteiner Jun 10 '16

Because i dont have fake friends on The Facebook? Nah, i have actual friends and have a rich professional/personal life. Thanks for your concern though.

6

u/sportif11 Jun 10 '16

Projecting so hard.

1

u/Homersteiner Jun 10 '16

What is important to you? Money? Girlfriend/Wife? Lifestyle? Neighborhood? Work/Research? Leisure? Travel?

3

u/sportif11 Jun 10 '16

Let me guess: you have it all.

→ More replies (0)