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 5 years have passed since last update.

Seabornのpairplot関数の結果グラフをGoogle ColaboratoryからGoogle driveにアップロードする方法

Posted at

この記事でできること

表題の通りです。sklearnの関数で取得できるUCI Machine Learning Repositoryのwineデータセットを利用します

手順

  1. ColaboratoryにGoogle Driveのアクセス権限を与える
  2. Google Driveにファイルの出力をする
    手順2までにはpairplot用のDataFrameを定義しておきましょう

詳しく

1. wineデータセットの読み込み

学習に使用することになりそうですし、DataFrameを学習データ(X_train)で作成するのでデータ分割もしています


from sklearn.datasets import load_wine
wine_dataset = load_wine()

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
        wine_dataset['data'], wine_dataset['target'],
        test_size=0.3, random_state=0)

2. DataFrameの作成


wine_df = pd.DataFrame(X_train, columns=wine_dataset.feature_names)
wine_df['species'] = y_train
wine_df.head()

出力結果はこうなります
image.png

3. ColaboratoryにGoogle Driveのアクセス権限を与える


from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

リンクと入力欄が出力されます。そのリンクを踏んでGoogleアカウントでログインして出てきた文字列をコピーして、Google Colaboratoryに戻ってきて入力欄にペーストしてエンター

4. seabornでpairplotを作成し、pngとして保存しておく


import seaborn as sns
sns.pairplot(wine_df, hue='species').savefig('wine_pairplot_hue.png')

このままだとColaboratoryのローカル(Google DriveのColab Notebooksからも見れない)にあるので、Google Driveへのアップロードが必要になります。

5. Google Driveに出力


upload_file_2 = drive.CreateFile()
upload_file_2.SetContentFile("wine_pairplot_hue.png")
upload_file_2.Upload()

6. 結果

image.png

ちゃんと登録できてます。ちなみにワインデータセットくらい特徴量があると画質の影響で文字が見えなくなるので他の対処が必要そうです。(Colaboratory上でもプロットされます。こちらはズーム可能)

image.png

以上です

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?