Ich versuche, ein Programm zeilenweise aufzuschlüsseln. Y
ist eine Datenmatrix, aber ich kann keine konkreten Daten darüber finden, was .shape[0]
macht genau.
for i in range(Y.shape[0]):
if Y[i] == -1:
Dieses Programm verwendet numpy, scipy, matplotlib.pyplot und cvxopt.
Das shape
-Attribut für numpy-Arrays gibt die Dimensionen des Arrays zurück. Wenn Y
n
Zeilen und m
Spalten hat, dann Y.shape
ist (n,m)
. So Y.shape[0]
ist n
.
In [46]: Y = np.arange(12).reshape(3,4)
In [47]: Y
Out[47]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [48]: Y.shape
Out[48]: (3, 4)
In [49]: Y.shape[0]
Out[49]: 3
shape ist ein Tupel, das die Abmessungen des Arrays angibt.
>>> c = arange(20).reshape(5,4)
>>> c
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19]])
c.shape[0]
5
Gibt die Anzahl der Zeilen an
c.shape[1]
4
Gibt die Anzahl der Spalten an
shape
ist ein Tupel, das die Anzahl der Dimensionen im Array angibt. Also in Ihrem Fall, da der Indexwert von Y.shape[0]
ist 0, Sie arbeiten entlang der ersten Dimension Ihres Arrays.
From http://www.scipy.org/Tentative_NumPy_Tutorial#head-62ef2d3c0a5b4b7d6fdc48e4a60fe48b1ffe5006
An array has a shape given by the number of elements along each axis:
>>> a = floor(10*random.random((3,4)))
>>> a
array([[ 7., 5., 9., 3.],
[ 7., 2., 7., 8.],
[ 6., 8., 3., 2.]])
>>> a.shape
(3, 4)
und http://www.scipy.org/Numpy_Example_List#shape enthält weitere Beispiele.
In Python shape()
wird in pandas verwendet, um die Anzahl der Zeilen/Spalten anzugeben:
Anzahl der Zeilen ist gegeben durch:
train = pd.read_csv('fine_name') //load the data
train.shape[0]
Anzahl der Spalten ist gegeben durch
train.shape[1]
Angenommen, Sie haben in Python die Daten in einem variablen Zug geladen:
train = pandas.read_csv('file_name')
>>> train
train([[ 1., 2., 3.],
[ 5., 1., 2.]],)
Ich möchte überprüfen, wie groß der 'Dateiname' ist. Ich habe die Datei im Zug gespeichert
>>>train.shape
(2,3)
>>>train.shape[0] # will display number of rows
2
>>>train.shape[1] # will display number of columns
3