【问题标题】:Why is my array not initialising? Numpy - error为什么我的数组没有初始化? Numpy - 错误
【发布时间】:2019-04-11 17:54:15
【问题描述】:

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

在 python 上使用 Numpy 库函数处理默认的 pydataset

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 被初始化

【问题讨论】:

  • 你在这段代码后面使用sp_lelower?
  • iris_data 背后的数据是什么?
  • 后面的数据是一串数字
  • 是的,但是给我们你认为sp_l.any() &lt;= 5 and sp_w.any() &lt;= 3这个表达式应该评估为真的样本
  • 条件不是true时不会初始化。你确定是true

标签: python numpy artificial-intelligence data-science


【解决方案1】:

我认为唯一的问题是条件表达式。您使用的数据可能无法通过条件。所以当你使用下面的sp_le时,它并没有被初始化。如果您可以给出 sp_l 和 sp_w 的值并检查它是否良好。并且正如 hpaulj 发布的那样,如果您想判断 sp_l 是否有小于 5 的元素,最好使用 (sp_l

【讨论】:

    【解决方案2】:

    我可以从sklearn 加载iris 数据集:

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

    结果是二维数组;前 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&lt;=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]])
    

    【讨论】:

      猜你喜欢
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      • 2012-01-07
      • 2015-02-20
      • 2012-07-28
      相关资源
      最近更新 更多