如何对数据集虹膜python使用cov函数

时间:2019-04-04 06:49:52

标签: python python-3.x numpy covariance iris-dataset

我想从虹膜数据集https://www.kaggle.com/jchen2186/machine-learning-with-iris-dataset/data

获得协方差

我正在使用numpy,并且功能-> np.cov(iris)

with open("Iris.csv") as iris:
    reader = csv.reader(iris)
    data = []
    next(reader)
    for row in reader:
        data.append(row)

for i in data:
    i.pop(0)
    i.pop(4)

iris = np.array(data)
np.cov(iris)

我收到此错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-bfb836354075> in <module>
----> 1 np.cov(iris)

D:\Anaconda\lib\site-packages\numpy\lib\function_base.py in cov(m, y, rowvar, bias, ddof, fweights, aweights)
   2300             w *= aweights
   2301 
-> 2302     avg, w_sum = average(X, axis=1, weights=w, returned=True)
   2303     w_sum = w_sum[0]
   2304 

D:\Anaconda\lib\site-packages\numpy\lib\function_base.py in average(a, axis, weights, returned)
    354 
    355     if weights is None:
--> 356         avg = a.mean(axis)
    357         scl = avg.dtype.type(a.size/avg.size)
    358     else:

D:\Anaconda\lib\site-packages\numpy\core\_methods.py in _mean(a, axis, dtype, out, keepdims)
     73             is_float16_result = True
     74 
---> 75     ret = umr_sum(arr, axis, dtype, out, keepdims)
     76     if isinstance(ret, mu.ndarray):
     77         ret = um.true_divide(

TypeError: cannot perform reduce with flexible type

我不明白这是什么意思。

1 个答案:

答案 0 :(得分:0)

因此,如果您想修改代码,可以尝试使用带有launch-buildserver的{​​{1}}函数。然后选择所需的适当列。

但是,这里有一些命令可以简化此任务。他们使用[{"aptNumber": "", "city": "", "key": "6bba4c82-18e2-3c10-a798-028f42d97857", "nickName": "My name ", "numberofbathrooms": "2", "numberofbedrooms": "1", "state": "", "streetName": "Fskslal", "streetNumber": "Fjsl", "typeof": "Apartment", "userid": "sb7Hj93D08XiaRguQ4cbSjz4vXC3"}, { "aptNumber": "", "city": "", "key": "714590f6-e8da-2d29-20c2-e5010a3a9df4", "nickName": "BATMAN", "numberofbathrooms": "2", "numberofbedrooms": "0", "state": "", "streetName": "", "streetNumber": "", "typeof": "Apartment", "userid": "sb7Hj93D08XiaRguQ4cbSjz4vXC3"}, { "aptNumber": "", "city": "", "key": "bd2ad46a-f307-4f8e-1fc8-ba828b6651bc", "nickName": "NOT BATMAN", "numberofbathrooms": "0", "numberofbedrooms": "0", "state": "", "streetName": "", "streetNumber": "", "typeof": "Apartment", "userid": "sb7Hj93D08XiaRguQ4cbSjz4vXC3"} ] Iris.csv加载虹膜数据集,获得X和y并获得协方差矩阵:

pandas.read_csv

希望这有所帮助。