再次从我之前的工作继续,我已经设法将列表循环到函数中,但我无法绘制我在列表上运行循环的方式
%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()
我看到的错误是:
(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 [ ]:
我知道列表中的元组被称为X [:,0],X [:,1]但是对于X的形状得到(0,)是什么意思?
答案 0 :(得分:0)
当您指定它是一维数组时,您尝试在X[:, 0]
上的第二维X
处引用。只有在传递到X
子句时,您的循环才会使else
2-dim。如果它没有,它将保持1-dim。从你的打印输出,X是一个空的昏暗阵列。因此,它从未传递到else
子句。
显然,X是一个暗淡而空的。(0,) (array([], dtype=float64), array([], dtype=float64))
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