使用TSNE进行降维.为什么3D图形不起作用? [英] Using TSNE to dimensionality reduction. Why 3 D graph is not working?

查看:479
本文介绍了使用TSNE进行降维.为什么3D图形不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了Sklearn的Digits数据集,并尝试使用TSNE(t分布随机邻居嵌入)将尺寸从64减少到3:

I have used the Digits dataset from Sklearn and I have tried to reduce the dimension from 64 to 3 using TSNE( t-Distributed Stochastic Neighbor Embedding):

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
#%matplotib inline
from sklearn.manifold import TSNE
from sklearn.datasets import load_digits
from mpl_toolkits.mplot3d import Axes3D


digits = load_digits()
digits_df = pd.DataFrame(digits.data,)
digits_df["target"] = pd.Series(digits.target)

tsne = TSNE(n_components=3)
digits_tsne = tsne.fit_transform(digits_df.iloc[:,:64])
digits_df_tsne = pd.DataFrame(digits_tsne,
                            columns =["Component1","Component2","Component3"])

finalDf = pd.concat([digits_df_tsne, digits_df["target"]], axis = 1)

#Visualizing 3D
figure = plt.figure(figsize=(9,9))
axes = figure.add_subplot(111,projection = "3d")
dots = axes.scatter(xs = finalDf[:,0],ys = finalDf[:,1],zs = finalDf[:,2],
                   c = digits.target, cmap = plt.cm.get_cmap("nipy_spectral_r",10))

finalDf:

Te错误:

TypeError: '(slice(None, None, None), 0)' is an invalid key

怎么了?有人可以帮我吗?

What is wrong? Could someone help me?

推荐答案

您正在尝试对无效的pandas数据帧进行numpy切片,因此请先将数据帧转换为numpy数组.

You're trying numpy slicing on pandas dataframe which is not valid, so first convert the dataframes to numpy arrays.

这是更新的代码:-

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
#%matplotib inline
from sklearn.manifold import TSNE
from sklearn.datasets import load_digits
from mpl_toolkits.mplot3d import Axes3D


digits = load_digits()
digits_df = pd.DataFrame(digits.data,)
digits_df["target"] = pd.Series(digits.target)

tsne = TSNE(n_components=3)
digits_tsne = tsne.fit_transform(digits_df.iloc[:,:64])
digits_df_tsne = pd.DataFrame(digits_tsne,
                            columns =["Component1","Component2","Component3"])

finalDf = pd.concat([digits_df_tsne, digits_df["target"]], axis = 1)

#Visualizing 3D
figure = plt.figure(figsize=(9,9))
axes = figure.add_subplot(111,projection = "3d")
dots = axes.scatter(xs = finalDf.to_numpy()[:,0],ys = finalDf.to_numpy()[:,1],zs = finalDf.to_numpy()[:,2],
                   c = digits.target, cmap = plt.cm.get_cmap("nipy_spectral_r",10))

这篇关于使用TSNE进行降维.为什么3D图形不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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