为什么我的数组未初始化?脾气暴躁-错误

时间:2019-04-11 17:54:16

标签: python numpy artificial-intelligence data-science

我正在尝试对另一个数组进行更改后对其进行初始化。

在适用于默认pydataset的python上使用Numpy库函数

import numpy as np
from pydataset import data
iris_data=data('iris')
iris_arr=iris_data.values 

sp_l = iris_arr[:,0] #sepal.length
sp_w = iris_arr[:,1] #sepal.width

sp_l = np.array(sp_l)
sp_w = np.array(sp_w) 

if(sp_l.any() <= 5 and sp_w.any() <= 3):
   sp_le = np.asarray(sp_l)
   sp_we = np.asarray(sp_w) 

NameError:未定义名称'sp_le'

我希望sp_le被初始化

2 个答案:

答案 0 :(得分:0)

我认为唯一的问题是条件表达式。您正在使用的数据可能无法通过该条件。因此,当您使用下面的sp_le时,它不会被初始化。如果您可以给出sp_l和sp_w的值并检查它是否良好,还可以参考hpaulj的内容,如果您想知道sp_l是否具有小于5的元素,则最好使用(sp_l <= 5)。任何()

答案 1 :(得分:0)

我可以通过以下方式从iris加载sklearn数据集:

In [317]: from sklearn.datasets import load_iris  
In [321]: arr = load_iris().data                                                
In [322]: arr.shape                                                             
Out[322]: (150, 4)

结果是2d数组;前5行是:

In [323]: arr[:5,:]                                                             
Out[323]: 
array([[5.1, 3.5, 1.4, 0.2],
       [4.9, 3. , 1.4, 0.2],
       [4.7, 3.2, 1.3, 0.2],
       [4.6, 3.1, 1.5, 0.2],
       [5. , 3.6, 1.4, 0.2]])

第一和第二列是:

In [324]: sp_l = arr[:,0]                                                       
In [325]: sp_w = arr[:,1]                                                       
In [326]: sp_l.shape                                                            
Out[326]: (150,)

sp_l.any()只是测试是否有任何值不为0。我认为您不需要这样做。

sp_l<=5测试sp_l的值是否小于或等于5

In [327]: (sp_l<=5).any()                                                       
Out[327]: True                # at least some are
In [328]: (sp_l<=5).sum()                                                       
Out[328]: 32                  # there are 32 true values in that test
In [329]: (sp_w<=3).sum()                                                       
Out[329]: 83                  # and 83 sp_w values are small enough.

目前尚不清楚您想要什么,但是一种可能是您希望sp_l为5或以下且sp_w为3或以下的行。

In [330]: (sp_l<=5)&(sp_w<=3)           # the () and & are important                                        
Out[330]: 
array([False,  True, False, False, False, False, False, False,  True,
       False, ... False])
In [331]: ((sp_l<=5)&(sp_w<=3)).sum()                                           
Out[331]: 12

我们用where获得这些行的索引:

In [332]: idx =  np.where(((sp_l<=5)&(sp_w<=3)))                                
In [333]: idx                                                                   
Out[333]: (array([  1,   8,  12,  13,  25,  38,  41,  45,  57,  60,  93, 106]),)

和实际行:

In [334]: arr[idx[0]]                                                           
Out[334]: 
array([[4.9, 3. , 1.4, 0.2],
       [4.4, 2.9, 1.4, 0.2],
       [4.8, 3. , 1.4, 0.1],
       [4.3, 3. , 1.1, 0.1],
       [5. , 3. , 1.6, 0.2],
       [4.4, 3. , 1.3, 0.2],
       [4.5, 2.3, 1.3, 0.3],
       [4.8, 3. , 1.4, 0.3],
       [4.9, 2.4, 3.3, 1. ],
       [5. , 2. , 3.5, 1. ],
       [5. , 2.3, 3.3, 1. ],
       [4.9, 2.5, 4.5, 1.7]])