0

코드 예 (http://scikit-learn.org/stable/auto_examples/linear_model/plot_iris_logistic.html)를 참조하며,이 라인 iris.data[:, :2]으로 특히 혼란 스럽습니다. 왜냐하면 iris.data는 150 (행) * 4 (열) 차원이므로 치수를 선택했다고 생각합니다. 모든 행 및 처음 두 열. 나는 시간이 걸리지 만 그러한 구문 정의 공식 문서를 찾을 수 없기 때문에 나의 이해가 정확한지를 확인하기 위해 여기에 묻는다.numpy.ndarray 구문 확인을위한 이해

또 다른 질문은 다음 코드를 사용하여 # 행과 열 수를 얻는 것입니다. 내 코드는 파이썬 고유의 스타일이고 numpy가 관련 값을 얻는 데 더 좋은 스타일인지 확실하지 않습니다.

print len(iris.data) # for number of rows 
print len(iris.data[0]) # for number of columns 

미니콘 인터프리터와 함께 Python 2.7 사용.

print(__doc__) 


# Code source: Gaël Varoquaux 
# Modified for documentation by Jaques Grobler 
# License: BSD 3 clause 

import numpy as np 
import matplotlib.pyplot as plt 
from sklearn import linear_model, datasets 

# import some data to play with 
iris = datasets.load_iris() 
X = iris.data[:, :2] # we only take the first two features. 
Y = iris.target 

h = .02 # step size in the mesh 

logreg = linear_model.LogisticRegression(C=1e5) 

# we create an instance of Neighbours Classifier and fit the data. 
logreg.fit(X, Y) 

# Plot the decision boundary. For that, we will assign a color to each 
# point in the mesh [x_min, m_max]x[y_min, y_max]. 
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5 
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5 
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) 
Z = logreg.predict(np.c_[xx.ravel(), yy.ravel()]) 

# Put the result into a color plot 
Z = Z.reshape(xx.shape) 
plt.figure(1, figsize=(4, 3)) 
plt.pcolormesh(xx, yy, Z, cmap=plt.cm.Paired) 

# Plot also the training points 
plt.scatter(X[:, 0], X[:, 1], c=Y, edgecolors='k', cmap=plt.cm.Paired) 
plt.xlabel('Sepal length') 
plt.ylabel('Sepal width') 

plt.xlim(xx.min(), xx.max()) 
plt.ylim(yy.min(), yy.max()) 
plt.xticks(()) 
plt.yticks(()) 

plt.show() 

안부, 린

+2

자네 말이 맞아 : 당신은 여기에 몇 가지 문서를 찾을 수 있습니다. 첫 번째 구문은 처음 두 열/피쳐를 선택합니다. 치수를 쿼리하는 또 다른 방법은'iris.data.shape'를 보는 것입니다. 길이가있는 n 차원 튜플을 반환합니다. –

+0

고맙습니다 @PankajDaga, 치수를 참조하는 공식 문서가 있다면 아시나요,'numpy'의'ndarray'에 대한 하위 배열 (제 예제)을 선택 하시겠습니까? –

+1

여기에서 볼 수 있습니다. http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html –

답변

1

당신은 맞다. 첫 번째 구문은 처음 두 열/피쳐를 선택합니다. 치수를 쿼리하는 또 다른 방법은 iris.data.shape입니다. 길이가있는 n 차원 튜플을 반환합니다. http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html

import numpy as np 
x = np.random.rand(100, 200) 
# Select the first 2 columns 
y = x[:, :2] 
# Get the row length 
print (y.shape[0]) 
# Get the column length 
print (y.shape[1]) 
# Number of dimensions 
print (len(y.shape)) 
+0

감사합니다. Pankaj, 답변을 답글로 표시하십시오. . –