值错误:无法将输入数组从形状 (857,3) 广播到形状 (857) [英] Value Error: could not broadcast input array from shape (857,3) into shape (857)

查看:51
本文介绍了值错误:无法将输入数组从形状 (857,3) 广播到形状 (857)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图将我从文本文件中解析的数据库绘制成一个numpy数组.该数组有857行.

I've been trying to plot a database that I parsed from a text file into a numpy array. The array has 857 rows.

此错误不断弹出,我不明白这是什么意思.

This error keeps popping up, I dont understand what it means.

import matplotlib
import matplotlib.pyplot as plt
import numpy

def parseFile(file):
    F = open(file)
    n = len(F.readlines())  #no. of lines in file
    numpyMat = numpy.zeros((n,3)) # create numpy matrix
    classLabelVector = []
    F = open(file)          # from the beginning again
    i = 0
    for line in F.readlines():
        line = line.strip()
        listFromLine = line.split()
        numpyMat[i,:] = listFromLine[0:3]   # 3 is the no. of variables/columns
        classLabelVector.append(int(listFromLine[-1]))
    i+=1
    return numpyMat, classLabelVector

dataMatrix = parseFile('kiwibubbles_tran.txt')

fig = plt.figure()

ax = fig.add_subplot(111)
ax.scatter(dataMatrix[:1], dataMatrix[:2])   # Error

plt.show()

错误:ValueError:无法将输入数组从形状(857,3)广播到形状(857)

Error: ValueError: could not broadcast input array from shape (857,3) into shape (857)

推荐答案

你的 dataMatrix 是一个元组,所以你有两个选择:

Your dataMatrix is a tuple, so you have two options:

通过两个不同的变量获取函数的结果:

numpyMat, classLabels = parseFile('kiwibubbles_tran.txt')
fig = plt.figure()

ax = fig.add_subplot(111)
ax.scatter(numpyMat[:,1], numpyMat[:,2])

plt.show()

仅从元组中绘制numpyMat :

dataMatrix = parseFile('kiwibubbles_tran.txt')
fig = plt.figure()

ax = fig.add_subplot(111)
ax.scatter(dataMatrix[0][:,1], dataMatrix[0][:,2])

plt.show()

这篇关于值错误:无法将输入数组从形状 (857,3) 广播到形状 (857)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆