[英]Python too many indices for array shape (0,)
again continue from my previous work, I have manage to loop through the list into function but I am not able to plot the way I run the loop on a list 再次从我以前的工作继续,我已经设法循环列表功能,但我无法绘制我在列表上运行循环的方式
%matplotlib inline
from pylab import *
import numpy as np
with open('/home/../Downloads/combinationplot.txt','rb') as file:
a = file.readlines()
df = pd.read_csv('20130831_000000.csv')
df = df.fillna(0)
def createtuple(cola,colb):
names = df[str(cola)]
names1 = df[str(colb)]
X = []
y = []
for i in range(len(names)):
if type(names[i])==str or type(names1[i])==str :
pass
else:
X.append([names[i],names1[i]])
y.append(i+1)
X = np.array(X)
y = np.array(y)
return (X,y)
def plotgraph():
plt.figure(figsize=(10, 6))
plt.scatter(X[:,0], X[:,1],c=y.astype(np.float),alpha=.5)
#plt.scatter(X[:,0], X[:,1],c=y.astype(np.float),alpha=.5) names[i] <=float(0) or names1[i]<=float(0) or
plt.show()
for i in range(len(a)):
b = a[i].split("'")
(X,y) = createtuple(b[1],b[3])
print (X,y)
print np.shape(X)
plt.figure(figsize=(10, 6))
plt.scatter(X[:,0], X[:,1],c=y.astype(np.float),alpha=.5)
#plt.scatter(X[:,0], X[:,1],c=y.astype(np.float),alpha=.5) names[i] <=float(0) or names1[i]<=float(0) or
plt.show()
The error I am seeing is this: 我看到的错误是这样的:
(0,)
(array([], dtype=float64), array([], dtype=float64))
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-109-1875f2993e46> in <module>()
39 plt.figure(figsize=(10, 6))
40
---> 41 plt.scatter(X[:,0], X[:,1],c=y.astype(np.float),alpha=.5)
42 #plt.scatter(X[:,0], X[:,1],c=y.astype(np.float),alpha=.5) names[i] <=float(0) or names1[i]<=float(0) or
43 plt.show()
IndexError: too many indices for array
<matplotlib.figure.Figure at 0x7f46edb9b9d0>
In [ ]:
I know the tuple in the list is being called X[:,0], X[:,1] but what does it mean of getting (0,) for shape of X? 我知道列表中的元组被称为X [:,0],X [:,1]但是对于X的形状得到(0,)是什么意思?
You're trying to reference at second dimension X[:, 0]
on X
when you specified it is a one dimensional array. 你试图在第二个维度引用
X[:, 0]
上X
时指定它是一个一个维数组。 Your loop will make X
2-dim only if it passes into the else
clause. 只有当它传入
else
子句时,你的循环才会使X
2-dim。 If it doesn't, it will remain 1-dim. 如果没有,它将保持1-dim。 From your print out, X is an empty one dim array.
从你的打印输出,X是一个空的昏暗阵列。 Therefore, It never passed into the
else
clause. 因此,它从未传递到
else
子句中。
(0,) (array([], dtype=float64), array([], dtype=float64))
Clearly, X is one-dim and empty. 很明显,X是一个暗淡而空洞的。
X = [] # <---one dimension
for i in range(len(names)):
if type(names[i])==str or type(names1[i])==str :
pass
else:
# if loop never gets here, X will remain one dimensional
X.append([names[i],names1[i]]) # <---two dimensions
y.append(i+1)
X = np.array(X) # <---still one dimension if not passed through else clause
y = np.array(y)
return (X,y)
def plotgraph():
plt.figure(figsize=(10, 6))
plt.scatter(X[:,0], X[:,1],c=y.astype(np.float),alpha=.5)
# ^ ^
# | |
# Asking for two
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.