0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[メモ]PCAとt-SNE

Last updated at Posted at 2020-08-02

importする

import scipy as sp
import sklearn.base
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import pandas as pd

dataの読み込み

data = 'table_rdf.csv'
df = pd.read_csv(data,index_col=0).dropna(axis=1)

t-SNEの実行

X_reduced = TSNE(n_components=2, random_state=0).fit_transform(df)

色分けの設定
dataの読み込み

data_2 = 'y_rdf.csv'
df_2 = pd.read_csv(data_2,index_col=0).dropna(axis=1)

列方向(縦方向)にソート

df2_s = df_2.sort_index()
df2_s.head()

mergeする

df_merge = pd.merge(df, df_2, how='left',right_index=True,left_index=True)
df_merge.head()

プリントする

plt.scatter(X_reduced[:, 0], X_reduced[:, 1], c=df_merge['target'])
plt.colorbar()

PCAをする

from sklearn.decomposition import PCA
pca = PCA(n_components=2).fit(df)
values = pca.transform(df)
fig = plt.figure()
plt.scatter(values[:,0], values[:,1], c=df_merge['target'])
plt.xlabel("value1")
plt.ylabel("value2")
plt.colorbar()
plt.show()
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?