4 ms·
Because Python has NumPy, which implements vectorized math on arrays and matrices. Machine learning algorithms are implemented naturally and efficiently with t
by chubot 5y ago
Because Python has NumPy, which implements vectorized math on arrays and matrices. Machine learning algorithms are implemented naturally and efficiently with those primitives. PyTorch, TensorFlow, and I think every other machine learning framework in Python all use NumPy.
JavaScript, Ruby, and Perl either don't have this abstraction at all, or they have much weaker versions of it, and many fewer scientific libraries.
NumPy started in the early 2000's and continues to this day. It takes decades to build up this infrastructure! This recent interview with NumPy creator Travis Oliphant is great:
https://www.youtube.com/watch?v=gFEE3w7F0ww https://www.youtube.com/watch?v=gFEE3w7F0ww
He talks about how there were competing abstractions like "Numeric" and another library, and his goal with NumPy was to unify them. And how there are still some open design issues / regrets.
There were multiple people in the nascent Python community who were tired of MATLAB, not just because it's proprietary, but because it's a weak and inefficient language for anything other than its scientific use cases. You won't have a good time trying to write a web app wrapper in MATLAB, for example.
The much more recent Julia language is also inspired positively and negatively by MATLAB, and is very suitable for machine learning, though it doesn't have the decades of libraries that Python has.
-----
The NumPy extension was in turn enabled by operator overloading in Python (which is actually a very C++ influenced mechanism). JavaScript doesn't have operator overloading; I'm pretty sure Perl doesn't, but not sure about Ruby. Lua and Tcl do not have it. (Lua does have a machine learning framework though -- http://torch.ch/ http://torch.ch/ -- but I think PyTorch is more popular now.)
So if Guido didn't design Python with operator overloading, then NumPy would not have grown out of it.
Also relevant is Guy Steele's famous talk Growing a Language (late 90's or early 2000's I think). He advocates for operator overloading in Java so end users can evolve language with their domain expertise! Well Java never got it, and Python ended up having the capabilities to grow linear algebra.
Guido has even said he doesn't really use or even "get" NumPy! So it turns out that an extensible design does have the benefits that Steele suggested (although it's a very difficult language design problem.) There have been several enhancements to Python driven by the NumPy community, like slicing syntax and semantics and the @ matrix multiplication operator. And I think many parts of the C API like buffers.
-----
Another interesting thing from Oliphant's interview is that he really liked that Python has complex numbers. (I don't think any of JavaScript, Ruby, Perl, or Lua have them in the core, which is important.) That piqued his interest and kicked off a few decades of hacking on Python.
He was an electrical engineering Ph.D. student and professor, and complex numbers are ubiquitous in that domain. Example:
$ python3 -c 'print(3j * 2 + 1)'
(1+6j)
This is another simple type built on Python's extensible core, and it's short.
$ Python-3.9.4$ wc -l Objects/complexobject.c
1125 Objects/complexobject.c
I recommend writing a Python extension in C if you want to see how it works. See Modules/xx*.c in the Python source code for some templates / examples. IMO the Python source code is a lot more approachable than Perl, Ruby, or any JS engine I've looked at.
- chubot 5y agoI should also add a cultural / social reason why Python is used in scientific computing and machine learning much more than JS/Ruby/Perl: Python was the only one of those languages (partially) funded by government research agencies. Guido was a research programmer in the Netherlands at CWI, and then he moved to the US when he was hired by CNRI, a research agency headed by Bob Kahn (loosely connected with DARPA as far as I remember). If you look at the backgrounds of Brendan Eich, Matz, and Larry Wall (creators of JS, Ruby, and Perl), they are quite different. None of them really worked in a research setting, and they certainly didn't develop their language in a research setting. https://en.wikipedia.org/wiki/History_of_Python https://en.wikipedia.org/wiki/History_of_Python 3 hour oral history with Guido: https://www.youtube.com/watch?v=Pzkdci2HDpU&t=12s https://www.youtube.com/watch?v=Pzkdci2HDpU&t=12s Lex Fridman interview with Guido: https://www.youtube.com/watch?v=ghwaIiE3Nd8 https://www.youtube.com/watch?v=ghwaIiE3Nd8 Lua was developed in a research setting, funded partially by Brazilian oil companies as far as I remember, but I don't think it ever had a "scientific computing" focus. It was picked up more in games and apps due to the C embeddability and features like coroutines. The ML framework Torch was built on LuaJIT because it has math nearly as fast as C. But I think the language Lua is less suited toward linear algebra, again due to the lack of operator overloading. Not to mention that Lua doesn't even have separate ints and floats! This is also an issue with using JavaScript for scientific computing.
- kragen 5y agoPerl was originally written at JPL, which is the epitome of government-funded research, and for the first many years of its life most of its numerous contributors were at one or another government-funded research institution, because people who weren't didn't have internet access. Lua does support operator overloading: $ luajit LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/ JIT: ON SSE2 SSE3 SSE4.1 AMD fold cse dce fwd dse narrow loop abc sink fuse > x = setmetatable({}, {__add=function() return 37 end}) > print(x+5) 37 Not sure if that was true 20 years ago.
- chubot 5y agoHm interesting, it's hard to find references to Wall working at JPL, but here's a very non-authoritative one: https://old.reddit.com/r/perl/comments/5lj9ms/did_larry_wall_work_at_nasa/ https://old.reddit.com/r/perl/comments/5lj9ms/did_larry_wall... Wikipedia doesn't mention it: https://en.wikipedia.org/wiki/Larry_Wall https://en.wikipedia.org/wiki/Larry_Wall https://en.wikipedia.org/wiki/Perl#Early_versions https://en.wikipedia.org/wiki/Perl#Early_versions That does sound right, since I vaguely recall an interview with Wall talking about JPL. ----- I think there's still a difference because Python was literally funded as a research project by CNRI, a government research institution. It wasn't created there, and it was funded by different entities afterward, but I think that's the period when contributors with a scientific background like Travis Oliphant, Jim Huginin, and David Beazley started working on Python's libraries and infrastructure. At best it seems like Wall worked at JPL for a short time and started Perl there. It also matters what kind of research it was. Perl is aimed much more at text processing and not linear algebra, while Python is more general purpose in this respect. Also, if my memory is right, by early 2000's JPL had jobs in Python, and python.org said JPL was a user. I could be wrong but I don't think Perl ever caught on as much as Python did at JPL. ----- Yes good point about Lua's metatable mechanism.