Python Forum
semantics of comma inside array brackets
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
semantics of comma inside array brackets
#1
hi,

I am trying to understand a piece of code, namely
import matplotlib.pyplot as plt
import numpy as np
expdat = np.genfromtxt(fname='expDat.txt', delimiter=',', 
                       dtype=np.int, skip_header=1)[:,1:]
np.set_printoptions(edgeitems=5)
N = 5
plt.figure(figsize=(14,12))
for i in range(N):
    for j in range(N):
        plt.subplot(N, N, i*N+j+1)
        plt.scatter(expdat[:,i], expdat[:,j])    
        if j==0:
            plt.ylabel('$x_{}$'.format(i+1))
        if i==N-1:
            plt.xlabel('$x_{}$'.format(j+1))
plt.suptitle('pairwise scatter plots of the first {} variables (processes)'.format(N))
plt.show()
I want to know what the notation
expdat[:,i]
does.
I tried it on my own by running
import matplotlib.pyplot as plt
N = 2
dat = [[1,2],[3,4]]
for i in range(N):
    for j in range(N):
        plt.subplot(N,N,i*N+j+1)
        plt.scatter(dat[:,i],dat[:,j])
plt.show()
However, it throws
Error:
TypeError: list indices must be integers or slices, not tuple
.
Is this a special operation on an object generated from
np.genfromtxt()
?
Thanks!
Reply
#2
The syntax inside brackets differ for python lists and numpy arrays. You could probably use
dat = np.array([[1,2],[3,4]])
Reply
#3
Thanks for your answer. It seems like
dat[:,i]
prints the i. index of every row, so the i. column of every row.
dat[i:,j]
seems to print the first j elements of row i. But this is kind of conflicting, since
dat[:,i]
prints the i. index of every row
dat[i:,j]
should print one element only, the one at row i column j. Vice versa,
dat[:,i]
should print the first i elements of any row. Can you tell me the general rule? I know this question is pretty basic.

Edit: found a helpful article, thanks :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What does that angle brackets mean? NewPi 5 2,923 Nov-27-2022, 02:09 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020