質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

1003閲覧

matplotlibによる画像のプロット時のエラーpython

kusegasugoi0221

総合スコア11

Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/12/12 02:23

編集2020/12/13 14:51

各特徴量をX_embeddedscにラベルを1~8でlabelに格納しています
それをmatplotlibで表示しようとしたところ以下のようなエラーが発生しました。
#エラー

Traceback (most recent call last): File "C:\Users\tSNE_CNN.py", line 248, in <module> main() File "C:\Users\tSNE_CNN.py", line 209, in main plt.scatter(X_embeddedsc[label==index,0], X_embeddedsc[label==index,1], X_embeddedsc[label==index,2],X_embeddedsc[label==index,3], IndexError: index 2 is out of bounds for axis 1 with size 2

こちらはどういったエラーなのでしょうか。以下にそれらを8種類分類用にして作ったコードを掲載します。
よろしくお願いします。

#コード

# -*- coding: utf-8 -* import glob import cv2 import numpy as np import time from tqdm import tqdm from skimage import feature,exposure from keras.models import model_from_json from keras.utils.vis_utils import plot_model from keras.models import Model import pandas as pd from matplotlib import pylab as plt from sklearn.manifold import TSNE from sklearn.preprocessing import StandardScaler from sklearn.preprocessing import MinMaxScaler # 特徴量抽出 def create_images_array(load_img_paths,model,layer_name): imgs=[] middle_layer_model = Model(inputs=model.input, outputs=model.get_layer(layer_name).output) for load_img_path in tqdm(load_img_paths): img = cv2.imread(load_img_path) #型を合わせる。255は正規化のため。 target = np.reshape(img, (1, img.shape[0], img.shape[1], img.shape[2])).astype('float') / 255.0 middle_output = middle_layer_model.predict(target) imgs.append(middle_output) #imgsは384枚の特徴量配列(384,1,7,7,2048)をもつ。それを(384,100352)に変更する imgs = np.reshape(imgs, [np.shape(imgs)[0], -1]) print('\nimgs.shape') print(imgs.shape) return imgs def tSNE(X,seikika): print('正規化start') #データの正規化 #X_pre_emb = seikika.fit_transform(X) #tSNEの適用 print('t-SNE適用start') tsne = TSNE(n_components=2, random_state=0) X_embedded = tsne.fit_transform(X) #tSNEで次元削減したデータの正規化 X_2d= seikika.fit_transform(X_embedded) print(X_2d.shape) #print(X_2d) return X_2d def main(): t1 = time.time() json_string = open('./preprocess_images_keras_2class/case3/model.json').read() model = model_from_json(json_string) model.load_weights('./preprocess_images_keras_2class/case3/weight.hdf5') layer_name = 'conv5_block3_out' # 学習用の画像ファイルの格納先 LOAD_TRAIN_IMG1S_PATH = './preprocess_images_keras/case3/train/T1-FE/*' LOAD_TRAIN_IMG2S_PATH = './preprocess_images_keras/case3/train/T2-FE/*' LOAD_TRAIN_IMG3S_PATH = './preprocess_images_keras/case3/train/T3-FE/*' LOAD_TRAIN_IMG4S_PATH = './preprocess_images_keras/case3/train/T4-FE/*' LOAD_TRAIN_IMG5S_PATH = './preprocess_images_keras/case3/train/T5-FE/*' LOAD_TRAIN_IMG6S_PATH = './preprocess_images_keras/case3/train/T6-FE/*' LOAD_TRAIN_IMG7S_PATH = './preprocess_images_keras/case3/train/T7-FE/*' LOAD_TRAIN_IMG8S_PATH = './preprocess_images_keras/case3/train/T8-FE/*' LOAD_TRAIN_IMG9S_PATH = './preprocess_images_keras/case3/train/T1-W/*' LOAD_TRAIN_IMG10S_PATH = './preprocess_images_keras/case3/train/T2-W/*' LOAD_TRAIN_IMG11S_PATH = './preprocess_images_keras/case3/train/T3-W/*' LOAD_TRAIN_IMG12S_PATH = './preprocess_images_keras/case3/train/T4-W/*' LOAD_TRAIN_IMG13S_PATH = './preprocess_images_keras/case3/train/T5-W/*' LOAD_TRAIN_IMG14S_PATH = './preprocess_images_keras/case3/train/T6-W/*' LOAD_TRAIN_IMG15S_PATH = './preprocess_images_keras/case3/train/T7-W/*' LOAD_TRAIN_IMG16S_PATH = './preprocess_images_keras/case3/train/T8-W/*' # 学習用の画像ファイルのパスを取得 load_img1_paths = glob.glob(LOAD_TRAIN_IMG1S_PATH) load_img2_paths = glob.glob(LOAD_TRAIN_IMG2S_PATH) load_img3_paths = glob.glob(LOAD_TRAIN_IMG3S_PATH) load_img4_paths = glob.glob(LOAD_TRAIN_IMG4S_PATH) load_img5_paths = glob.glob(LOAD_TRAIN_IMG5S_PATH) load_img6_paths = glob.glob(LOAD_TRAIN_IMG6S_PATH) load_img7_paths = glob.glob(LOAD_TRAIN_IMG7S_PATH) load_img8_paths = glob.glob(LOAD_TRAIN_IMG8S_PATH) load_img9_paths = glob.glob(LOAD_TRAIN_IMG9S_PATH) load_img10_paths = glob.glob(LOAD_TRAIN_IMG10S_PATH) load_img11_paths = glob.glob(LOAD_TRAIN_IMG11S_PATH) load_img12_paths = glob.glob(LOAD_TRAIN_IMG12S_PATH) load_img13_paths = glob.glob(LOAD_TRAIN_IMG13S_PATH) load_img14_paths = glob.glob(LOAD_TRAIN_IMG14S_PATH) load_img15_paths = glob.glob(LOAD_TRAIN_IMG15S_PATH) load_img16_paths = glob.glob(LOAD_TRAIN_IMG16S_PATH) # 学習用の画像ファイルをロードし特徴量抽出 imgs1 = create_images_array(load_img1_paths,model,layer_name) imgs2 = create_images_array(load_img2_paths,model,layer_name) imgs3 = create_images_array(load_img3_paths,model,layer_name) imgs4 = create_images_array(load_img4_paths,model,layer_name) imgs5 = create_images_array(load_img5_paths,model,layer_name) imgs6 = create_images_array(load_img6_paths,model,layer_name) imgs7 = create_images_array(load_img7_paths,model,layer_name) imgs8 = create_images_array(load_img8_paths,model,layer_name) imgs9 = create_images_array(load_img9_paths,model,layer_name) imgs10 = create_images_array(load_img10_paths,model,layer_name) imgs11 = create_images_array(load_img11_paths,model,layer_name) imgs12 = create_images_array(load_img12_paths,model,layer_name) imgs13 = create_images_array(load_img13_paths,model,layer_name) imgs14 = create_images_array(load_img14_paths,model,layer_name) imgs15 = create_images_array(load_img15_paths,model,layer_name) imgs16 = create_images_array(load_img16_paths,model,layer_name) X = np.r_[imgs1, imgs2, imgs3, imgs4, imgs5, imgs6, imgs7, imgs8, imgs9, imgs10, imgs11, imgs12, imgs13, imgs14, imgs15, imgs16] print('X') print(X) # 正解ラベルを生成imgs.番号でラベルを決める labels1 = np.full(len(load_img1_paths), 0, np.int32) labels2 = np.full(len(load_img2_paths), 1, np.int32) labels3 = np.full(len(load_img3_paths), 2, np.int32) labels4 = np.full(len(load_img4_paths), 3, np.int32) labels5 = np.full(len(load_img5_paths), 4, np.int32) labels6 = np.full(len(load_img6_paths), 5, np.int32) labels7 = np.full(len(load_img7_paths), 6, np.int32) labels8 = np.full(len(load_img8_paths), 7, np.int32) labels9 = np.full(len(load_img9_paths), 0, np.int32) labels10 = np.full(len(load_img10_paths), 1, np.int32) labels11 = np.full(len(load_img11_paths), 2, np.int32) labels12 = np.full(len(load_img12_paths), 3, np.int32) labels13 = np.full(len(load_img13_paths), 4, np.int32) labels14 = np.full(len(load_img14_paths), 5, np.int32) labels15 = np.full(len(load_img15_paths), 6, np.int32) labels16 = np.full(len(load_img16_paths), 7, np.int32) label = np.r_[labels1, labels2, labels3, labels4, labels5, labels6, labels7, labels8, labels9, labels10, labels11, labels12, labels13, labels14, labels15, labels16] print('label') print(label.shape) #正規化 seikika = StandardScaler() #seikika = MinMaxScaler() X_embeddedsc = tSNE(X,seikika) print(X_embeddedsc.shape) # グラフを横長にする from matplotlib.pylab import rcParams rcParams['figure.figsize'] = 15, 6 import matplotlib as mpl mpl.rcParams['font.family'] = ['serif'] #8種類分類のプロット print("-----------------------------------------------------------") # 可視化の用意 #listは1番目は0から読み取る。matlabとの大きな違い color_list1 = ["blue","green", "red","cyan","magenta","yellow","black","white"] color_list2 = ["blue","green", "red","cyan","magenta","yellow","black","black"] marker_list = ["o","o","o","o","o","o","o","o"] label_list = ["T1", "T2","T3", "T4","T5", "T6","T7","T8"] plt.figure(figsize=(7,7)) plt.title("feautures") #plt.tick_params(labelsize=16) plt.xlabel("t-SNE comp-1") plt.ylabel("t-SNE comp-2") for index in range(len(np.unique(label))): plt.scatter(X_embeddedsc[label==index,0], X_embeddedsc[label==index,1], X_embeddedsc[label==index,2],X_embeddedsc[label==index,3], X_embeddedsc[label==index,4],X_embeddedsc[label==index,5],X_embeddedsc[label==index,6],X_embeddedsc[label==index,7], edgecolor=color_list2[index],color=color_list1[index], marker=marker_list[index], label=label_list[index]) plt.legend(loc="upper right") plt.show() print("-----------------------------------------------------------\n\n") t2 = time.time() elapsed_time = (t2-t1)/3600 print(f"経過時間:{elapsed_time}") if __name__ == '__main__': main()

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問