Numpy is absurd
gist.github.com
gist.github.com
Answer: `a in b` is the same as `(a == b).any()`. This really only makes sense when `a` is a scalar and any behavior for other types of arrays are more "accidental" than designed.
My initial thought was “None of the examples have arguments that are equal to each other, so that should evaluate to false.any()”, but that would mean all should return the same value, so that can’t be true, so I figured out that Numpy overloads/changes a == b to do item-by-item comparison on arrays, and return an array of booleans.
But then, the statement “This really only makes sense when `a` is a scalar” doesn’t make sense to me. That seems to indicate Numpy also overloads a == b as a comparison of each element of a with a single value b and vice versa, but that doesn’t make sense, given that arrays can contain arrays, making it impossible to discriminate between [1,2] as a two-item sequence and [1,2] as a single-item sequence where the item is a two-item sequence.
Reading https://stackoverflow.com/questions/10580676/comparing-two-n... further strengthens my impression that one should avoid using == in Numpy.
I’m only partially joking. But this was one case where they probably thought it was a good idea, turned out to be a bad idea, then couldn’t turn it off without breaking a bunch of code.
For most of my programs safety is of zero concern. I'll trade it for convenience any day. Just let me implicit cast signed to unsigned thank you very much.
They changed the language to change what question `<` asks.
https://stackoverflow.com/questions/10062954/valueerror-the-...
Really there should be separate elementwise operators like `.>` and so on but you can only do that in some languages.
from array import array
it’s right there.> Numpy doesn't distinguish "vector of vector" from "matrix"
numpy.matrix
though to be fair, it’s on its way out.Compare and contrast with languages with built-in N-dimensional array for scientific computing.
>> size(4)
ans =
1 1
which is 1. wrong and 2. evidence that matlab cannot represent numerical objects of less than rank 2.Numpy can:
>> np.shape(4)
()i think it’s fair to call that a (white) lie
Do you have a link to where this is asserted in the documentation? I'm confused as to the basis for this.
it should be clear that i don’t care very much about what matlab has to represent things as. i don’t mean “matlab integers”, i mean the mathematical objects; matlab’s representation of them is poor.
You have to work within the constraints of the tool. Matlab makes it very clear upfront that the semantics of the language mean that naked numbers are in fact matrices, because everything in the language is a matrix. Maybe you feel that's not a good representation, but it's not a lie, and in fact it's very consistent and useful in the context of the language itself.
I don't think so. The array module was included even in Python 1.0.
Or did you mean to use another verb?
commit 778983b48165da25ee11fb97f6855af7c67f4ff2
Author: Guido van Rossum <guido@python.org>
Date: Fri Feb 19 15:55:02 1993 +0000
Added new module "array" (for now optional) defining array objects.Which forces Numpy to be designed in a way to automatically broadcast, guessing what user wants is hard in nested container
import numpy as np
print(np.__version__)
a = np.array([1,2,3])
b = np.array([1])
print(a in b)
print(b in a)
print(np.isin(a, b))
print(np.isin(b, a))
print(np.isin(a.astype(float), b))
print(np.isin(a, b.astype(float))))
1.21.6
True
True
[ True False False]
[ True]
[ True False False]
[ True False False]I'm happy I moved on to Go, which actually adopts this as a core design tenet.
[1]: Even though PEP-20 is largely a meme.
Case in point, historically NumPy has been designed for long-running programs, like hour-long calculations or notebook processing. They've chosen to import "the world" so users don't need explicit subpackage imports, allowing:
>>> import numpy
>>> numpy.polynomial.hermite.hermfromroots([2,2,3,4])
array([ 7.075e+01, -4.625e+01, 1.175e+01, -1.375e+00, 6.250e-02])
This has a high-startup cost which makes NumPy use inappropriate for short-lived programs like shell scripts, even in cases where Python's own startup cost is otherwise appropriate.but, cough, mature and stable scientific computing foundation?
Python does have an array type. It’s existed from at least v2.6 (released 2008). https://docs.python.org/3.10/library/array.html