Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

equivalent of numpy.c_ in julia

Hi I am going through the book https://nnfs.io/ but using JuliaLang (it's a self-challenge to get to know the language better and use it more often.. rather than doing the same old same in Python..)

I have come across a part of the book in which they have custom wrote some function and I need to recreate it in JuliaLang...

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

python

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()

my julia version so far....

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

notice the ??????? area because I am now trying to decipher if Julia has an equivalent for this numpy function

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

Any help is appreciated.. or just tell me if I need to write something myself

like image 610
Erik Avatar asked Feb 02 '26 03:02

Erik


1 Answers

This is a special object to provide nice syntax for column concatanation. In Julia this is just built into the language hence you can do:

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

For your case the Julian equivalent of np.c_[r*np.sin(t), r*np.cos(t)] should be:

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

To understand Python's motivation you can also have a look at : numpy.r_ is not a function. What is it?

like image 128
Przemyslaw Szufel Avatar answered Feb 03 '26 18:02

Przemyslaw Szufel