4 ms·
Recently was trying to convert Python to C#... there are few things that are as absolutely incomprehensible as a Python programmer being clever with numpy. I ga
by lwansbrough 7y ago
Recently was trying to convert Python to C#... there are few things that are as absolutely incomprehensible as a Python programmer being clever with numpy. I gave up on the conversion. Perhaps this is a point for Python’s flexibility, but I’d say it’s just a waste.
Here’s the code. Try to convert this to any C style language.
mins = X.min(axis=0)
maxs = X.max(axis=0)
idxs = np.random.choice(range(self.dim), self.dim-self.exlevel-1, replace=False) # Pick the indices for which the normal vector elements should be set to zero acccording to the extension level.
self.n = np.random.normal(0,1,self.dim) # A random normal vector picked form a uniform n-sphere. Note that in order to pick uniformly from n-sphere, we need to pick a random normal for each component of this vector.
self.n[idxs] = 0
self.p = np.random.uniform(mins,maxs) # Picking a random intercept point for the hyperplane splitting data.
w = (X-self.p).dot(self.n) < 0 # Criteria that determines if a data point should go to the left or right child node.
return Node(X, self.n, self.p, e,\
left=self.make_tree(X[w],e+1,l),\
right=self.make_tree(X[~w],e+1,l),\
node_type = 'inNode' )
- adrianN 7y agoTry converting typical Matlab code to a C-style language and you'll run into similar problems. That's just what you get when your base language isn't all that fast but you have a fast numerics library that allows you to do clever things with arrays and matrices.
- TheDong 7y agoYour challenge isn't possible because the code snippet isn't complete. It doesn't include the definition of 'make_tree', 'Node', or the broader context. That being said, I have no doubt that C++ with Boost would look quite similar to that. The boost libraries have very powerful numeric constructs and can easily handle matrices, uniform random distributions, dot products, and trees.
- lwansbrough 7y agoWell, yeah. My point is the syntax itself is very (too) clever. This syntax is probably possible in C# but you’d be a monster to write it (lots of operator overloads.) The whole implementation is from an implementation of an extended isolation forest. It’s easy enough to find on GitHub if you’re interested.
- pjmlp 7y agoHere is the C# version of that snippet, assuming the existence of such libraries. var mins = X.Min(axis: 0); var maxs = X.Max(axis: 0); var idxs = np.Random.Choice(Enunmeration.Range(this.dim), this.dim-this.exlevel-1, replace: false); // Pick the indices for which the normal vector elements should be set to zero acccording to the extension level. this.n = np.Random.Normal(0,1, this.dim); // A random normal vector picked form a uniform n-sphere. Note that in order to pick uniformly from n-sphere, we need to pick a random normal for each component of this vector. this.n[idxs] = 0; this.p = np.Random.Uniform(mins,maxs); // Picking a random intercept point for the hyperplane splitting data. var w = (X-this.p).Dot(this.n) < 0; // Criteria that determines if a data point should go to the left or right child node. return new Node(X, this.n, this.p, e, left: this.MakeTree(X[w],e+1,l), right: this.MakeTree(X[~w],e+1,l), node_type: "inNode") There is nothing about Python flexibility there other than a possible missing library implementation.