Home > OS >  How to convert Numpy special syntax to Hy
How to convert Numpy special syntax to Hy

Time:08-19

I have this Python code:

import numpy as np
import matplotlib.pyplot as plt

X = np.random.randn(500, 2)
plt.scatter(X[:,0], X[:,1])

But I don't know how to convert X[:,0] and X[:,1], which I think is a special syntax Numpy created, to Hy. I tried these below, but they did not work.

(get X #(: 0))
(get X : 0)
; I want to do `X[:,0]` in Python

CodePudding user response:

As hinted in the comments, NumPy doesn't change Python's syntax; it just has somewhat unusual semantics for indexing. In Python, X[:,0] is shorthand for X[(slice(None), 0)]. You can translate that literally to Hy as (get X #((slice None) 0)). Alternatively, use Hyrule's ncut: (require hyrule [ncut]) (ncut X : 0).

  • Related