2016-01-30 86 views
1

來自R,我可以通過str()檢查任何對象的內部結構,我對如何在Python中執行相同操作感到困惑。標準是使用dir(my_object),但它沒有列出所有屬性,包括非常重要的屬性。例如:如何列出sklearn.datasets對象的所有屬性?

from sklearn import datasets 
iris = datasets.load_iris() 
dir(iris) 

dir(iris)沒有列出最重要的屬性,如iris.datairis.target

我應該閱讀的文檔,以瞭解這些屬性,或者是有辦法只是從內部看物體而發現?

+0

A [相關](http://stackoverflow.com/questions/27637281/what-are-python-pandas-equivalents-for-r-functions-like-str-summary-and-he)之一。 – akrun

回答

2

數據集被加載到類似字典的對象中,因此您可以查找存儲在dict中的數據,而不是包含標準dict方法的名稱空間中的所有數據。

In [2]: iris = datasets.load_iris() 

In [3]: iris.keys() 
Out[3]: ['target_names', 'data', 'target', 'DESCR', 'feature_names'] 
0

下面是一些屬性:

In [10]: iris.data 
Out[10]: array([[ 5.1, 3.5, 1.4, 0.2], 
       [ 4.9, 3. , 1.4, 0.2], 
       [ 4.7, 3.2, 1.3, 0.2], 
       ... 

In [11]: iris.target 
Out[11]: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
       0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
       1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]) 

In [13]: iris.target_names 
Out[13]: array(['setosa', 'versicolor', 'virginica'], 
     dtype='|S10') 

In [14]: iris.feature_names 
Out[14]: ['sepal length (cm)', 
      'sepal width (cm)', 
      'petal length (cm)', 
      'petal width (cm)'] 

最後一個會給你的數據集的詳細說明與一些概率統計。

In [15]: iris.DESCR 
Out[15]: 'Iris Plants Database\n\nNotes\n-----\nData Set Characteristics:\n :Number of Instances: 150 (50 in each of three 

我截短的iris.datairis.DESCR輸出。 Here are the dataset docs