【问题标题】:equivalent of numpy.c_ in julia相当于 julia 中的 numpy.c_
【发布时间】:2021-09-14 14:02:01
【问题描述】:

您好,我正在阅读 https://nnfs.io/ 这本书,但使用的是 JuliaLang(更好地了解该语言并更频繁地使用它是一种自我挑战。而不是在 Python 中做同样的事情。)

我遇到过他们自定义编写了一些函数的书的一部分,我需要在 JuliaLang 中重新创建它...

来源:https://cs231n.github.io/neural-networks-case-study/

蟒蛇

N = 100 # number of points per class
D = 2 # dimensionality
K = 3 # number of classes
X = np.zeros((N*K,D)) # data matrix (each row = single example)
y = np.zeros(N*K, dtype='uint8') # class labels
for j in range(K):
  ix = range(N*j,N*(j+1))
  r = np.linspace(0.0,1,N) # radius
  t = np.linspace(j*4,(j+1)*4,N) + np.random.randn(N)*0.2 # theta
  X[ix] = np.c_[r*np.sin(t), r*np.cos(t)]
  y[ix] = j
# lets visualize the data:
plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral)
plt.show()

到目前为止我的 julia 版本....

N = 100 # Number of points per class
D = 2   # Dimensionality
K = 3   # Number of classes

X = zeros((N*K, D))
y = zeros(UInt8, N*K)


# See https://docs.julialang.org/en/v1/base/math/#Base.range

for j in range(0,length=K)
    ix = range(N*(j), length = N+1)
    
    radius = LinRange(0.0, 1, N)
    theta = LinRange(j*4, (j+1)*4, N) + randn(N)*0.2
    X[ix] = ????????

end

请注意 ??????? 区域,因为我现在正试图破译 Julia 是否有这个 numpy 函数的等价物

https://numpy.org/doc/stable/reference/generated/numpy.c_.html

感谢任何帮助.. 或者告诉我是否需要自己写一些东西

【问题讨论】:

  • 您可以尝试编写 for 循环来执行您想要的操作,而不是搜索函数。有时直接的方法是最好的方法。

标签: arrays algorithm matrix julia


【解决方案1】:

numpy.c_ 的等效项似乎是水平串联,您可以使用 hcat 函数或(例如)简单地使用 [a b] 来完成。到目前为止,修复了翻译的其他一些问题,我们最终得到了

N = 100 # Number of points per class
D = 2   # Dimensionality
K = 3   # Number of classes

X = zeros(N*K, D)
y = zeros(UInt8, N*K)

for j in range(0,length=K)
    ix = (N*j+1):(N*(j+1))
        
    radius = LinRange(0.0, 1, N)
    theta = LinRange(j*4, (j+1)*4, N) + randn(N)*0.2
    X[ix,:] .= [radius.*sin.(theta) radius.*cos.(theta)]
    y[ix] .= j
end
# visualize the data:
using Plots
scatter(X[:,1], X[:,2], zcolor=y, framestyle=:box)

【讨论】:

  • 哇.. 我接受了另一个答案.. 但是您不仅在这里为我的第一个问题提供了解决方案.. 而且您还解决了这个问题stackoverflow.com/questions/68228564/… 随时在那里发帖,我会接受那个答案
【解决方案2】:

这是一个特殊对象,可为列连接提供良好的语法。在 Julia 中,这只是内置在语言中,因此您可以这样做:

julia> a=[1,2,3];        
                         
julia> b=[4,5,6];        
                                                
julia> [a b]             
3×2 Matrix{Int64}:       
 1  4                    
 2  5                    
 3  6                                          

对于您的情况,np.c_[r*np.sin(t), r*np.cos(t)] 的 Julian 等效项应该是:

[r .* sin.(t)  r .* cos.(t)]

要了解 Python 的动机,您还可以查看: numpy.r_ is not a function. What is it?

【讨论】:

  • brilliant.. 一旦我解决了我的其他问题,我想我已经准备好了!感谢您的提醒..
  • 从技术上讲,这与np.c_ 的不同之处在于它沿第二个轴连接,而不是最后一个轴。但是,如果您的数组最多是 2D 的,那就没问题了。
  • @BallpointBen 根据文档,np.c_ 仅在第二个轴上起作用.. 你可能在想np.r_ numpy.org/doc/stable/reference/generated/numpy.c_.html "将切片对象转换为沿第二个轴连接。"
  • @Erik “阵列将在升级到至少二维后沿其最后一个轴堆叠...”我认为这意味着最多为 2 维的数组将沿第二维连接(如有必要,使用单维填充),但更高暗淡的数组将沿其最后一个轴连接。但无论哪种方式,我都不完全清楚。
猜你喜欢
  • 2018-06-24
  • 2015-05-17
  • 2019-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多