GVKun编程网logo

Python错误:IndexError:只有整数,切片`:`,省略号`...`,numpy.newaxis`None`和整数或布尔数组都是有效的索引

5

在本文中,我们将给您介绍关于Python错误:IndexError:只有整数,切片`:`,省略号`...`,numpy.newaxis`None`和整数或布尔数组都是有效的索引的详细内容,此外,我们还

在本文中,我们将给您介绍关于Python错误:IndexError:只有整数,切片`:`,省略号`...`,numpy.newaxis`None`和整数或布尔数组都是有效的索引的详细内容,此外,我们还将为您提供关于'(slice(None, None, None), 0)' 是绘制决策函数的无效键、.pip的时候出现Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None))…………、IndexError:只有整数、切片 (`:`)、省略号 (`...`)、numpy.newaxis (`None`) 和整数或布尔数组是有效的索引,我该如何解决?、IndexError:只有整数、切片`:`、省略号`...`、numpy.newaxis`None`和整数或布尔数组是有效的索引错误的知识。

本文目录一览:

Python错误:IndexError:只有整数,切片`:`,省略号`...`,numpy.newaxis`None`和整数或布尔数组都是有效的索引

Python错误:IndexError:只有整数,切片`:`,省略号`...`,numpy.newaxis`None`和整数或布尔数组都是有效的索引

如何解决Python错误:IndexError:只有整数,切片`:`,省略号`...`,numpy.newaxis`None`和整数或布尔数组都是有效的索引

错误-

Python错误:IndexError:仅整数,切片(:),省略号 ({...),numpy.newaxis(None)和整数或布尔数组为 有效索引

注意- 我怀疑np.polyval有问题。

代码-

  1. x = list(range(348))
  2. x_data = np.array(x)
  3. y_data = np.array(new_forces)
  4. p = np.polyfit(x_data,y_data,15)
  5. yp = np.polyval(p,x_data)
  6. fig = plt.figure()
  7. fig,ax = plt.subplots()
  8. ax.plot(yp,color=''green'',linestyle=''solid'')
  9. ax.legend()
  10. fig.savefig("graphsW2/"+str(hgain)+"-"+str(gain)+"-"+str(gaincalc)+"-"+"WForcescumul+P&L-"+p["symbol"]+ylindex+".png")

'(slice(None, None, None), 0)' 是绘制决策函数的无效键

'(slice(None, None, None), 0)' 是绘制决策函数的无效键

如何解决''(slice(None, None, None), 0)'' 是绘制决策函数的无效键

  1. # Plot the training points
  2. ax.scatter(Xtrain.iloc[:,0].values,Xtrain.iloc[:,1].values,c=Ytrain,cmap=cm_bright,edgecolors=''k'')

我正在尝试运行此代码,但一直收到 (slice(None,None,None),0) is an invalid key 错误。关于我做错了什么以及如何解决它的任何想法?

.pip的时候出现Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None))…………

.pip的时候出现Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None))…………

pip的幺蛾子:

有时候在用pip安装python中的包的时候,会出先请求失败的现象,一开始还以为是自己的网不够好,后来才发现。。。。。。

原因:

pip的下载源存在国外和国内之分,一般来说国内的源下载会块很多,国外的源会有速度限制,从而造成请求失败从而不能安装你想要的模块.

解决方法:

几种国内常用的pip源

阿里云 :http://mirrors.aliyun.com/pypi/simple/

中国科技大学 :https://pypi.mirrors.ustc.edu.cn/simple/

豆瓣(douban) :http://pypi.douban.com/simple/

清华大学: https://pypi.tuna.tsinghua.edu.cn/simple/

中国科学技术大学 :http://pypi.mirrors.ustc.edu.cn/simple/

写入文件内容:

1 [global]
2 index-url = https://pypi.tuna.tsinghua.edu.cn/simple
3 [install]
4 trusted-host=mirrors.aliyun.com

 

在windoms中:

直接在user目录中创建一个pip目录,如:C:\Users\xx\pip,新建文件pip.ini
在这里插入图片描述

 

 

在linu中

Linux下,修改 ~/.pip/pip.conf (没有就创建一个文件夹及文件。文件夹要加“.”,表示是隐藏文件夹)
在文件上写入上面配置文件中的内容

 

在运用pip安装模会发现速度快了很多。

IndexError:只有整数、切片 (`:`)、省略号 (`...`)、numpy.newaxis (`None`) 和整数或布尔数组是有效的索引,我该如何解决?

IndexError:只有整数、切片 (`:`)、省略号 (`...`)、numpy.newaxis (`None`) 和整数或布尔数组是有效的索引,我该如何解决?

如何解决IndexError:只有整数、切片 (`:`)、省略号 (`...`)、numpy.newaxis (`None`) 和整数或布尔数组是有效的索引,我该如何解决?

  1. import matplotlib as mpl
  2. import matplotlib.pyplot as plt
  3. import matplotlib.pyplot as plt2
  4. import matplotlib.cm as cm
  5. import numpy as np # linear algebra
  6. import pandas as pd
  7. kanser = pd.read_csv(''breast-cancer-wisconsin.csv'') #veri seti okuma
  8. kanser.dropna(inplace=True) #kayıp değerlerin olduğu yerleri kaldırma
  9. kanser.Class = [1 if i == "benign" else 0 for i in kanser.Class] #target için tür döüşümü yapma
  10. X=kanser.drop(''Class'',axis = 1)
  11. y=kanser.Class.values
  12. from sklearn.manifold import TSNE
  13. tsne = TSNE(verbose=1,perplexity=40,n_iter= 4000)
  14. Y = tsne.fit_transform(X)
  15. from sklearn.cluster import KMeans
  16. kmns = KMeans(n_clusters=2,init=''k-means++'',n_init=10,max_iter=300,tol=0.0001,precompute_distances=''auto'',verbose=0,random_state=None,copy_x=True,n_jobs=1,algorithm=''auto'')
  17. kY = kmns.fit_predict(X)
  18. f,(ax1,ax2) = plt.subplots(1,2,sharey=True)
  19. ax1.scatter(Y[:,0],Y[:,1],c=kY,cmap = "jet",edgecolor = "None",alpha=0.35)
  20. ax1.set_title(''k-means clustering plot'')
  21. ax2.scatter(Y[:,c = y[''Class''],alpha=0.35)
  22. ax2.set_title(''Actual clusters'')

我收到此错误。

  1. IndexError Traceback (most recent call last)
  2. <ipython-input-3-fd6a6e8ca8a9> in <module>()
  3. 26 ax1.set_title(''k-means clustering plot'')
  4. 27
  5. ---> 28 ax2.scatter(Y[:,alpha=0.35)
  6. 29 ax2.set_title(''Actual clusters'')
  7. IndexError: only integers,slices (`:`),ellipsis (`...`),numpy.newaxis (`None`) and integer or boolean arrays are valid indices

我能为此做什么?

解决方法

我运行了您的代码并查看了变量。您的对象“y”似乎是一个列表,其 _getitem_ 方法不接受字符串。

  1. from sklearn import datasets
  2. data = datasets.load_breast_cancer()
  3. import matplotlib as mpl
  4. import matplotlib.pyplot as plt
  5. import matplotlib.pyplot as plt2
  6. import matplotlib.cm as cm
  7. import numpy as np # linear algebra
  8. import pandas as pd
  9. # kanser = pd.read_csv(''breast-cancer-wisconsin.csv'') #veri seti okuma
  10. kanser = pd.DataFrame(data.data,columns = data.feature_names)
  11. kanser[''Class''] = data.target_names[data.target]
  12. kanser.dropna(inplace=True) #kayıp değerlerin olduğu yerleri kaldırma
  13. kanser.Class = [1 if i == "benign" else 0 for i in kanser.Class] #target için tür döüşümü yapma
  14. X=kanser.drop(''Class'',axis = 1)
  15. y=kanser.Class.values
  16. from sklearn.manifold import TSNE
  17. tsne = TSNE(verbose=1,perplexity=40,n_iter= 4000)
  18. Y = tsne.fit_transform(X)
  19. from sklearn.cluster import KMeans
  20. kmns = KMeans(n_clusters=2,init=''k-means++'',n_init=10,max_iter=300,tol=0.0001,precompute_distances=''auto'',verbose=0,random_state=None,copy_x=True,n_jobs=1,algorithm=''auto'')
  21. kY = kmns.fit_predict(X)
  22. f,(ax1,ax2) = plt.subplots(1,2,sharey=True)
  23. ax1.scatter(Y[:,0],Y[:,1],c=kY,cmap = "jet",edgecolor = "None",alpha=0.35)
  24. ax1.set_title(''k-means clustering plot'')
  25. xs = Y[:,0]
  26. ys = Y[:,1]
  27. print(f''y = {y}'')
  28. cs = y[''Class'']
  29. ax2.scatter(xs,ys,c = cs,alpha=0.35)
  30. ax2.set_title(''Actual clusters'')

输出:

  1. [t-SNE] Computing 121 nearest neighbors...
  2. [t-SNE] Indexed 569 samples in 0.001s...
  3. [t-SNE] Computed neighbors for 569 samples in 0.018s...
  4. [t-SNE] Computed conditional probabilities for sample 569 / 569
  5. [t-SNE] Mean sigma: 33.679708
  6. [t-SNE] KL divergence after 250 iterations with early exaggeration: 50.078777
  7. [t-SNE] KL divergence after 4000 iterations: 0.221172
  8. y = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  9. 1 0 0 0 0 0 0 0 0 1 0 1 1 1 1 1 0 0 1 0 0 1 1 1 1 0 1 0 0 1 1 1 1 0 1 0 0
  10. 1 0 1 0 0 1 1 1 0 0 1 0 0 0 1 1 1 0 1 1 0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ndexError Traceback (most recent call last)
  26. <ipython-input-5-73736c0bb107> in <module>()
  27. 37 print(f''y = {y}'')
  28. 38
  29. ---> 39 cs = y[''Class'']
  30. 40
  31. 41
  32. IndexError: only integers,slices (`:`),ellipsis (`...`),numpy.newaxis (`None`) and integer or boolean arrays are valid indices

IndexError:只有整数、切片`:`、省略号`...`、numpy.newaxis`None`和整数或布尔数组是有效的索引错误

IndexError:只有整数、切片`:`、省略号`...`、numpy.newaxis`None`和整数或布尔数组是有效的索引错误

如何解决IndexError:只有整数、切片`:`、省略号`...`、numpy.newaxis`None`和整数或布尔数组是有效的索引错误

嗨,我有一个坐标列表来选择图像中的某些感兴趣区域。但是当我将这些坐标传递给图像时,我收到了标题中的错误

代码如下:


coord_list =[''[55:130,190:707]'',''[178:280,30:387]'',''[181:273,424:739]'',424:739]'']

image1 = cv2.imread(r''C:\\\\Users\\\\User\\\\Desktop\\\\images\\\\Sample.jpg'')
image1 = cv2.resize(image,(1000,1000))
image_roi = image1[coord_list[0]]
cv2.imshow(''img'',image_roi)
cv2.waitKey(0)



它必须从列表中迭代,因为我必须对文件夹中的许多图像使用相同的坐标

解决方法

您不能直接对索引为字符串的数组进行切片。你必须先评估它:

image_roi = eval(''image1''+coord_list[0])

编辑:

为了回应您的评论,您可以使用 numpy 来表示。 这样就可以直接使用了,不需要使用eval()

import cv2
import numpy as np

coord_list = [np.s_[55:130,190:707],np.s_[178:280,30:387],np.s_[181:273,424:739],np.s_[182:273,424:738] ]
image1 = cv2.imread(''dog.jpg'')
image1 = cv2.resize(image1,(1000,1000))

image_roi = image1[coord_list[0]]
cv2.imshow(''img'',image_roi)
cv2.waitKey(0)

今天的关于Python错误:IndexError:只有整数,切片`:`,省略号`...`,numpy.newaxis`None`和整数或布尔数组都是有效的索引的分享已经结束,谢谢您的关注,如果想了解更多关于'(slice(None, None, None), 0)' 是绘制决策函数的无效键、.pip的时候出现Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None))…………、IndexError:只有整数、切片 (`:`)、省略号 (`...`)、numpy.newaxis (`None`) 和整数或布尔数组是有效的索引,我该如何解决?、IndexError:只有整数、切片`:`、省略号`...`、numpy.newaxis`None`和整数或布尔数组是有效的索引错误的相关知识,请在本站进行查询。

本文标签: