【问题标题】:IndexError when ploting sklearn manifold TSNE绘制 sklearn 流形 TSNE 时出现 IndexError
【发布时间】:2019-11-08 16:19:09
【问题描述】:

我尝试运行 t-sne,但 python 显示此错误:

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

link 正在提供数据。

代码如下:

import pandas as pd
import numpy as np
import sklearn 
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.manifold import TSNE

#Step 1 - Download the data
dataframe_all = pd.read_csv('https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv')
num_rows = dataframe_all.shape[0]

#Step 2 - Clearn the data
#count the number of missing elements (NaN) in  each column
counter_nan = dataframe_all.isnull().sum()
counter_without_nan = counter_nan[counter_nan==0]
#remove the columns with missing elements
dataframe_all = dataframe_all[counter_without_nan.keys()]
#remove the first 7 columns which contain no descriminative information
dataframe_all = dataframe_all.ix[:,7:]

#Step 3: Create feature vectors
x = dataframe_all.ix[:,:-1].values
standard_scalar = StandardScaler()
x_std = standard_scalar.fit_transform(x)

# t distributed stochastic neighbour embedding (t-SNE) visualization
tsne = TSNE(n_components=2, random_state = 0)
x_test_2d = tsne.fit_transform(x_std)

#scatter plot the sample points among 5 classes
markers=('s','d','o','^','v')
color_map = {0:'red', 1:'blue', 2:'lightgreen', 3:'purple', 4:'cyan'}
plt.figure()
for idx, cl in enumerate(np.unique(x_test_2d)):
    plt.scatter(x=x_test_2d[cl, 0],y =x_test_2d[cl, 1], c=color_map[idx], marker=markers[idx], label=cl)
plt.show()

为了使这项工作有效,我必须进行哪些更改?

【问题讨论】:

标签: python pandas dataframe scatter-plot


【解决方案1】:

错误是由于以下行:

plt.scatter(x_test_2d[cl, 0], x_test_2d[cl, 1], c=color_map[idx], marker=markers[idx])


这里,cl 可以取也可以不取整数值(来自np.unique(x_test_2d)),这会引发错误,例如cl 采用的最后一个值是 99.46295,然后您使用:x_test_2d[cl, 0] 转换为 x_test_2d[99.46295, 0]


定义一个变量y,它持有labels,然后使用:

# variable holding the classes
y = dataframe_all.classe.values
y = np.array([ord(i) for i in y])

#scatter plot the sample points among 5 classes
plt.figure()
plt.scatter(x_test_2d[:, 0], x_test_2d[:, 1], c = y)
plt.show()

完整代码:

import pandas as pd
import numpy as np
import sklearn 
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.manifold import TSNE

#Step 1 - Download the data
dataframe_all = pd.read_csv('https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv')
num_rows = dataframe_all.shape[0]

#Step 2 - Clearn the data
#count the number of missing elements (NaN) in  each column
counter_nan = dataframe_all.isnull().sum()
counter_without_nan = counter_nan[counter_nan==0]
#remove the columns with missing elements
dataframe_all = dataframe_all[counter_without_nan.keys()]
#remove the first 7 columns which contain no descriminative information
dataframe_all = dataframe_all.ix[:,7:]

#Step 3: Create feature vectors
x = dataframe_all.ix[:,:-1].values
standard_scalar = StandardScaler()
x_std = standard_scalar.fit_transform(x)

# t distributed stochastic neighbour embedding (t-SNE) visualization
tsne = TSNE(n_components=2, random_state = 0)
x_test_2d = tsne.fit_transform(x_std)

# variable holding the classes
y = dataframe_all.classe.values # you need this for the colors
y = np.array([ord(i) for i in y]) # convert letters to numbers

#scatter plot the sample points among 5 classes
plt.figure()
plt.scatter(x_test_2d[:, 0], x_test_2d[:, 1], c = y)
plt.show()

【讨论】:

  • 感谢您的帮助!我绝对明白现在的问题是什么。你还能帮我在我的程序中实现你建议的代码吗?不幸的是,我在编码方面相对较新:/
  • 只需用我回答中的这些行替换代码中的最后 5 行。也赞成并接受我的回答有帮助
  • @AiropX 我已经更新了我的答案。考虑接受它。
猜你喜欢
  • 2016-08-20
  • 2013-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-24
  • 2019-01-16
  • 2017-07-21
  • 2020-01-27
相关资源
最近更新 更多