LoginSignup
0
0

ChatGPTとPythonで学ぶ plotly と pydataset

Posted at

おはこんばんちわ。今日も引き続きChatGPT先生をお迎えして、「ChatGPTとPythonで学ぶ plotly と pydataset」というテーマで雑談したいと思います。それではChatGPT先生、よろしくお願いします。


assistant:

おはこんばんちわ!引き続き、よろしくお願いします。

先ほどのスレッドではPlotly、Python、そしてデータサイエンスに利用される各種ツールについてお話ししましたね。

今回はそれらのツールを具体的に使用して、plotlyとpydatasetを使って実際にデータをプロットしてみましょう。どのようなデータをプロットしようか、何かお考えはありますか?


user:

それではまず、 pydataset を使ってみましょう。pydataset の colon データセットを plotly で図示するコードを書いてください。


assistant:

まず、pydatasetとplotlyをインストールしていない場合は、以下のコマンドを実行してインストールしてください。

!pip install pydataset
!pip install plotly

次に、pydatasetからデータをロードし、データをプロットするコードを書いてみます。

import plotly.express as px
from pydataset import data

# pydatasetからcolonデータセットをロード
df = data('colon')

# データセットの中身を確認
print(df.head())

# 生存時間と年齢の関係をプロット
fig = px.scatter(df, x='age', y='time', color='status')
fig.show()

上記コードは、pydatasetのcolonデータセットをロードし、最初の数行を表示します。次に、plotly.expressのscatter関数を使って、生存時間('time')と年齢('age')の関係を散布図でプロットします。生存状況('status')によって色分けもしています。最後に、fig.show()でグラフを表示します。

なお、上記のコードはJupyter NotebookやGoogle Colabで実行することを想定しています。また、各自の環境によってはグラフが表示しない場合がありますが、その際は適宜、環境に合わせてコードを調整してみてください。


user:

ありがとうございます。次のような表データと、インタラクティブな散布図が出力されました。

   id  study       rx  sex  age  obstruct  perfor  adhere  nodes  status  \
1   1      1  Lev+5FU    1   43         0       0       0    5.0       1   
2   1      1  Lev+5FU    1   43         0       0       0    5.0       1   
3   2      1  Lev+5FU    1   63         0       0       0    1.0       0   
4   2      1  Lev+5FU    1   63         0       0       0    1.0       0   
5   3      1      Obs    0   71         0       0       1    7.0       1   

   differ  extent  surg  node4  time  etype  
1     2.0       3     0      1  1521      2  
2     2.0       3     0      1   968      1  
3     2.0       3     0      0  3087      2  
4     2.0       3     0      0  3087      1  
5     2.0       2     0      1   963      2  

output_0_2.png


assistant:

そのデータを表示できていますね。

ここには96行と10列のデータが表示されています。サンプルの各データは2行で記録されており、それぞれ生存時間 time とイベントタイプ etype を報告しています。各リスクセットに登録されたときには1行が、その後亡くなったときに2行目が報告されています。

これらのデータは大腸がん患者のある研究から取得され、さまざまな治療法、年齢、性別、その他の変数と生存時間との関係を調査するためのものです。このようなデータセットは、医療研究でよく使用され、特に生存解析や時間関連の分析に利用されます。

これらの情報を元に、さまざまな視点からデータを観察し、必要な情報を見つけ出すことが重要です。Plotlyのような視覚化ツールは、その一助となります。


user:

ついでに、上記データを用いて、散布図以外の方法で可視化して、何か面白い発見をしてみたいです。Pythonコードを提案してください。


assistant:

分析に使用する列をフィルタリングし、それぞれのフィールドがどのような情報を持っているのかを視覚的に理解するために、ヒストグラムや箱ひげ図(Box Plot)を使用すると良いかもしれません。

以下に、年齢(age)、生存時間(time)、リンパ節(nodes)の分布をヒストグラムで表示するコードを示します。

fig = px.histogram(df, x="age", nbins=30, title="Distribution of Age")
fig.show()

fig = px.histogram(df, x="time", nbins=30, title="Distribution of Survival Time")
fig.show()

fig = px.histogram(df, x="nodes", nbins=30, title="Distribution of Lymph Nodes")
fig.show()

output_1_0.png
output_1_1.png
output_1_2.png

次に、治療法(rx)によって、生存時間(time)がどのように分布しているのかを箱ひげ図で表示します。

fig = px.box(df, x="rx", y="time", title="Box Plot of Survival Time by Treatment")
fig.show()

output_2_0.png

以上のような分析から、使用された治療法や年齢層、リンパ節の数などが生存時間にどのように影響を与えるのか、基本的な傾向をつかむことができます。

ただし、これらはあくまで基本的な傾向をつかむためのものであり、統計的な信頼性を確認するためには更に詳細な統計分析が必要です。


user:

ありがとうございます。では、今度は、pydataset の biopsy データセットを plotly で図示するコードを書いてください。


assistant:

まず、pydatasetからbiopsyデータセットをロードし、データをプロットするコードを書いてみましょう。

from pydataset import data

# pydatasetのbiopsyデータセットをロード
df_biopsy = data('biopsy')

# データセットの先頭部分を確認
print(df_biopsy.head())

# Class(良性 or 悪性)とCl.thicknessの関係をプロット
fig_biopsy = px.histogram(df_biopsy, x='Cl.thickness', color='class')
fig_biopsy.show()

上記のコードは、まずpydatasetのbiopsyデータセットをロードし、最初の数行を表示します。次に、plotly.expressのhistogram関数を使って、Cl.thickness(細胞の厚さ)をx軸に、良性(benign)or 悪性(malignant)の分類を色別にして、重ねたヒストグラムを作ります。グラフは、fig_biopsy.show()で表示します。

なお、各自の環境によってはグラフが表示しない場合がありますが、その際は適宜、環境に合わせてコードを調整してみてください。


user:

ありがとうございます。次のような結果が得られました。

        ID  V1  V2  V3  V4  V5    V6  V7  V8  V9   class
1  1000025   5   1   1   1   2   1.0   3   1   1  benign
2  1002945   5   4   4   5   7  10.0   3   2   1  benign
3  1015425   3   1   1   1   2   2.0   3   1   1  benign
4  1016277   6   8   8   1   3   4.0   3   7   1  benign
5  1017023   4   1   1   3   2   1.0   3   1   1  benign
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[4], line 10
      7 print(df_biopsy.head())
      9 # Class(良性 or 悪性)とCl.thicknessの関係をプロット
---> 10 fig_biopsy = px.histogram(df_biopsy, x='Cl.thickness', color='class')
     11 fig_biopsy.show()

File /usr/local/lib/python3.10/site-packages/plotly/express/_chart_types.py:480, in histogram(data_frame, x, y, color, pattern_shape, facet_row, facet_col, facet_col_wrap, facet_row_spacing, facet_col_spacing, hover_name, hover_data, animation_frame, animation_group, category_orders, labels, color_discrete_sequence, color_discrete_map, pattern_shape_sequence, pattern_shape_map, marginal, opacity, orientation, barmode, barnorm, histnorm, log_x, log_y, range_x, range_y, histfunc, cumulative, nbins, text_auto, title, template, width, height)
    434 def histogram(
    435     data_frame=None,
    436     x=None,
   (...)
    472     height=None,
    473 ) -> go.Figure:
    474     """
    475     In a histogram, rows of `data_frame` are grouped together into a
    476     rectangular mark to visualize the 1D distribution of an aggregate
    477     function `histfunc` (e.g. the count or sum) of the value `y` (or `x` if
    478     `orientation` is `'h'`).
    479     """
--> 480     return make_figure(
    481         args=locals(),
    482         constructor=go.Histogram,
    483         trace_patch=dict(
    484             histnorm=histnorm,
    485             histfunc=histfunc,
    486             cumulative=dict(enabled=cumulative),
    487         ),
    488         layout_patch=dict(barmode=barmode, barnorm=barnorm),
    489     )

File /usr/local/lib/python3.10/site-packages/plotly/express/_core.py:2076, in make_figure(args, constructor, trace_patch, layout_patch)
   2073 layout_patch = layout_patch or {}
   2074 apply_default_cascade(args)
-> 2076 args = build_dataframe(args, constructor)
   2077 if constructor in [go.Treemap, go.Sunburst, go.Icicle] and args["path"] is not None:
   2078     args = process_dataframe_hierarchy(args)

File /usr/local/lib/python3.10/site-packages/plotly/express/_core.py:1485, in build_dataframe(args, constructor)
   1482     args["color"] = None
   1483 # now that things have been prepped, we do the systematic rewriting of `args`
-> 1485 df_output, wide_id_vars = process_args_into_dataframe(
   1486     args, wide_mode, var_name, value_name
   1487 )
   1489 # now that `df_output` exists and `args` contains only references, we complete
   1490 # the special-case and wide-mode handling by further rewriting args and/or mutating
   1491 # df_output
   1493 count_name = _escape_col_name(df_output, "count", [var_name, value_name])

File /usr/local/lib/python3.10/site-packages/plotly/express/_core.py:1210, in process_args_into_dataframe(args, wide_mode, var_name, value_name)
   1208         if argument == "index":
   1209             err_msg += "\n To use the index, pass it in directly as `df.index`."
-> 1210         raise ValueError(err_msg)
   1211 elif length and len(df_input[argument]) != length:
   1212     raise ValueError(
   1213         "All arguments should have the same length. "
   1214         "The length of column argument `df[%s]` is %d, whereas the "
   (...)
   1221         )
   1222     )

ValueError: Value of 'x' is not the name of a column in 'data_frame'. Expected one of ['ID', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6', 'V7', 'V8', 'V9', 'class'] but received: Cl.thickness

assistant:

すみません、私のミスでした。'Cl.thickness'カラムはなく、代わりにそれに対応するカラムが'V1'であるようです。修正したコードは以下のようになります。

fig_biopsy = px.histogram(df_biopsy, x='V1', color='class')
fig_biopsy.show()

output_4_0.png

上記のコードは、各細胞のクラスタ厚さ('V1')をX軸とし、それぞれ良性(benign)または悪性(malignant)に基づいて色分けされたヒストグラムを作成します。このヒストグラムは、さまざまなクラスタ厚さの細胞がどの程度良性または悪性であるかを視覚化するのに役立ちます。

このみなさんの環境ではどのような結果が得られましたか?


user:

おお。いいですね。ついでに、plotlyを使って、他の方法で可視化してください。


assistant:

分布だけでなく、各変数間の関連性を調べるために、散布図行列(scatter matrix)も作成します。

以下に、'V1'(Cl.thickness)、'V2'(Cell.size)、'V3'(Cell.shape)の3つの変数についての散布図行列を作成するコードを示します。

fig = px.scatter_matrix(df_biopsy, dimensions=['V1', 'V2', 'V3'], color='class')
fig.show()

output_5_0.png

このコードは、plotly.expressのscatter_matrix関数を使用して、3つの変数('V1', 'V2', 'V3')を含む散布図行列を作成します。色分けは各細胞のクラス('class')に基づいています。

また、出現頻度を見るためのカテゴリ変数に対する棒グラフも有用です。

fig = px.bar(df_biopsy, x='class', title='Frequency of Class')
fig.show()

上記のコードは、plotly.expressのbar関数を使用して、各クラスの頻度を表示します。


user:

fig = px.bar(df_biopsy, x='class', title='Frequency of Class')
fig.show()

output_6_0.png


assistant:

そのコードを実行すると、良性 (benign)および悪性 (malignant) のクラスがそれぞれ何回発生するかを示す棒グラフが表示されます。

この棒グラフは、biopsyデータセットの中で良性細胞と悪性細胞のどちらが多いかを視覚的に把握するのに非常に役立ちます。この情報は、データセットを理解するための重要な一歩となります。

棒グラフが作成できたら、次にどのような分析を進めていくかを考えてみましょう。もし何か具体的な目標や興味があったらお知らせください。特に指定がなければ、個々の特徴が目的変数(ここではクラス)にどのように影響するかをさらに探るための視覚化を提案しましょう。


user:

はい、ぜひ、個々の特徴が目的変数(ここではクラス)にどのように影響するかをさらに探るための視覚化をお願いします。


assistant:

はい、ではそれに沿って進めましょう。

今回は、'V1'(Cl.thickness), 'V2'(Cell.size), 'V3'(Cell.shape)とクラスとの関連性を調べるためにビオリンプロットを作成してみます。ビオリンプロットは、箱ひげ図とカーネル密度推定を組み合わせたもので、データの分布をより詳細に視覚化することができます。

for feature in ['V1', 'V2', 'V3']:
    fig = px.violin(df_biopsy, y=feature, x='class', box=True, title=f'Relationship between {feature} and class')
    fig.show()

output_7_0.png

output_7_1.png

output_7_2.png

上記のコードは、それぞれの特徴量('V1', 'V2', 'V3')に対してビオリンプロットを作成し、各特徴量がクラスにどう影響するかを視覚化しています。box=Trueをセットすることで、ビオリンプロット内に箱ひげ図も表示され、メディアン(中央値)や四分位数も確認できます。

特徴量とクラスの関連性を更に理解するために、これらのプロットを使ってどの特徴量がクラスを良く区別できるかを観察してみてください。


user:

ありがとうございます。大変素晴らしいですね。今度は、pydataset の bladder データセットを plotly で図示するコードを書いてください。


assistant:

pydatasetのbladderデータセットをロードして可視化してみましょう。ここでは、まずデータセットを確認してから、生存時間(stop)の分布をヒストグラムで、さらにリスク(rx)ごとの生存時間のboxplotを作成します。

# pydatasetのbladderデータセットをロード
df_bladder = data('bladder')

# データセットの先頭部分を確認
print(df_bladder.head())

# 生存時間の分布をヒストグラムで表示
fig_bladder_hist = px.histogram(df_bladder, x="stop", nbins=30, title="Distribution of Survival Time")
fig_bladder_hist.show()

# リスクと生存時間の関係をboxplotで表示
fig_bladder_box = px.box(df_bladder, x="rx", y="stop", title="Box plot of Survival Time by Treatment")
fig_bladder_box.show()

このコードは、plotly.expressのhistogram関数とbox関数を使用して各グラフを作成しています。まず、生存時間(stop)の分布を示すヒストグラムを作成し、それを表示します。続いて、リスク(rx)による生存時間の分布を、boxplotで表示します。

なお、各自の環境によってはグラフが表示しない場合がありますが、その際は適宜、環境に合わせてコードを調整してみてください。


user:

   id  rx  number  size  stop  event  enum
1   1   1       1     3     1      0     1
2   1   1       1     3     1      0     2
3   1   1       1     3     1      0     3
4   1   1       1     3     1      0     4
5   2   1       2     1     4      0     1

output_8_1.png

output_8_2.png

素晴らしい結果です。同じデータに対して、plotlyを使った他の可視化手法も試してみてください。


assistant:

もちろんです。他の視覚化手法として、サンプルの生存時間(stop)と年齢(age)の関係を散布図で表示し、リスク(rx)によって色分けしたものを作成してみます。

fig_bladder_scatter = px.scatter(df_bladder, x='stop', y='age', color='rx',
                                 labels={'stop': 'Survival Time', 'age': 'Age'},
                                 title='Scatter plot of Survival Time and Age by Treatment')
fig_bladder_scatter.show()

このコードは、plotly.expressのscatter関数を使用して、各患者の生存時間(stop)と年齢(age)の散布図を作成します。そして、リスク(rx)レベルにより色分けをします。

また、年齢別(age)に生存時間(stop)の平均値を算出し、棒グラフで表示することも試してみましょう。

df_age_mean = df_bladder.groupby('age')['stop'].mean().reset_index()

fig_bladder_bar = px.bar(df_age_mean, x='age', y='stop',
                         labels={'stop': 'Average Survival Time', 'age': 'Age'},
                         title='Bar plot of Average Survival Time by Age')
fig_bladder_bar.show()

このコードは、年齢(age)グループごとの生存時間(stop)の平均値を計算し、その結果を棒グラフで表示します。これにより、年齢が生存時間にどのように影響しているか視覚的に理解することができます。


user:

ありがとうございます。1つ目のコードについて、次のようなエラーが得らーれました。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[10], line 1
----> 1 fig_bladder_scatter = px.scatter(df_bladder, x='stop', y='age', color='rx',
      2                                  labels={'stop': 'Survival Time', 'age': 'Age'},
      3                                  title='Scatter plot of Survival Time and Age by Treatment')
      4 fig_bladder_scatter.show()

File /usr/local/lib/python3.10/site-packages/plotly/express/_chart_types.py:66, in scatter(data_frame, x, y, color, symbol, size, hover_name, hover_data, custom_data, text, facet_row, facet_col, facet_col_wrap, facet_row_spacing, facet_col_spacing, error_x, error_x_minus, error_y, error_y_minus, animation_frame, animation_group, category_orders, labels, orientation, color_discrete_sequence, color_discrete_map, color_continuous_scale, range_color, color_continuous_midpoint, symbol_sequence, symbol_map, opacity, size_max, marginal_x, marginal_y, trendline, trendline_options, trendline_color_override, trendline_scope, log_x, log_y, range_x, range_y, render_mode, title, template, width, height)
     12 def scatter(
     13     data_frame=None,
     14     x=None,
   (...)
     60     height=None,
     61 ) -> go.Figure:
     62     """
     63     In a scatter plot, each row of `data_frame` is represented by a symbol
     64     mark in 2D space.
     65     """
---> 66     return make_figure(args=locals(), constructor=go.Scatter)

File /usr/local/lib/python3.10/site-packages/plotly/express/_core.py:2076, in make_figure(args, constructor, trace_patch, layout_patch)
   2073 layout_patch = layout_patch or {}
   2074 apply_default_cascade(args)
-> 2076 args = build_dataframe(args, constructor)
   2077 if constructor in [go.Treemap, go.Sunburst, go.Icicle] and args["path"] is not None:
   2078     args = process_dataframe_hierarchy(args)

File /usr/local/lib/python3.10/site-packages/plotly/express/_core.py:1485, in build_dataframe(args, constructor)
   1482     args["color"] = None
   1483 # now that things have been prepped, we do the systematic rewriting of `args`
-> 1485 df_output, wide_id_vars = process_args_into_dataframe(
   1486     args, wide_mode, var_name, value_name
   1487 )
   1489 # now that `df_output` exists and `args` contains only references, we complete
   1490 # the special-case and wide-mode handling by further rewriting args and/or mutating
   1491 # df_output
   1493 count_name = _escape_col_name(df_output, "count", [var_name, value_name])

File /usr/local/lib/python3.10/site-packages/plotly/express/_core.py:1210, in process_args_into_dataframe(args, wide_mode, var_name, value_name)
   1208         if argument == "index":
   1209             err_msg += "\n To use the index, pass it in directly as `df.index`."
-> 1210         raise ValueError(err_msg)
   1211 elif length and len(df_input[argument]) != length:
   1212     raise ValueError(
   1213         "All arguments should have the same length. "
   1214         "The length of column argument `df[%s]` is %d, whereas the "
   (...)
   1221         )
   1222     )

ValueError: Value of 'y' is not the name of a column in 'data_frame'. Expected one of ['id', 'rx', 'number', 'size', 'stop', 'event', 'enum'] but received: age

assistant:

申し訳ありません。私のミスで、'bladder' データセットには 'age'というカラム名は含まれていません。そのため、その部分を修正する必要があります。

データ内に存在するサイズ('size')について生存時間('stop')との関係を調べるため、以下のように修正しましょう:

fig_bladder_scatter = px.scatter(df_bladder, x='stop', y='size', color='rx',
                                 labels={'stop': 'Survival Time', 'size': 'Size'},
                                 title='Scatter plot of Survival Time and Size by Treatment')
fig_bladder_scatter.show()

output_11_0.png

このコードは、各患者の生存時間('stop')と腫瘍サイズ('size')の散布図を作成し、リスク('rx')レベルにより色分けをします。この散布図から、サイズが生存時間にどのような影響を及ぼすかを視覚的に把握できます。


user:

ありがとうございます。解決できました。次に、2つ目のコードについて、次のようなエラーが得らーれました。

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[11], line 1
----> 1 df_age_mean = df_bladder.groupby('age')['stop'].mean().reset_index()
      3 fig_bladder_bar = px.bar(df_age_mean, x='age', y='stop',
      4                          labels={'stop': 'Average Survival Time', 'age': 'Age'},
      5                          title='Bar plot of Average Survival Time by Age')
      6 fig_bladder_bar.show()

File /usr/local/lib/python3.10/site-packages/pandas/core/frame.py:8869, in DataFrame.groupby(self, by, axis, level, as_index, sort, group_keys, observed, dropna)
   8866 if level is None and by is None:
   8867     raise TypeError("You have to supply one of 'by' and 'level'")
-> 8869 return DataFrameGroupBy(
   8870     obj=self,
   8871     keys=by,
   8872     axis=axis,
   8873     level=level,
   8874     as_index=as_index,
   8875     sort=sort,
   8876     group_keys=group_keys,
   8877     observed=observed,
   8878     dropna=dropna,
   8879 )

File /usr/local/lib/python3.10/site-packages/pandas/core/groupby/groupby.py:1278, in GroupBy.__init__(self, obj, keys, axis, level, grouper, exclusions, selection, as_index, sort, group_keys, observed, dropna)
   1275 self.dropna = dropna
   1277 if grouper is None:
-> 1278     grouper, exclusions, obj = get_grouper(
   1279         obj,
   1280         keys,
   1281         axis=axis,
   1282         level=level,
   1283         sort=sort,
   1284         observed=False if observed is lib.no_default else observed,
   1285         dropna=self.dropna,
   1286     )
   1288 if observed is lib.no_default:
   1289     if any(ping._passed_categorical for ping in grouper.groupings):

File /usr/local/lib/python3.10/site-packages/pandas/core/groupby/grouper.py:1009, in get_grouper(obj, key, axis, level, sort, observed, validate, dropna)
   1007         in_axis, level, gpr = False, gpr, None
   1008     else:
-> 1009         raise KeyError(gpr)
   1010 elif isinstance(gpr, Grouper) and gpr.key is not None:
   1011     # Add key to exclusions
   1012     exclusions.add(gpr.key)

KeyError: 'age'

assistant:

申し訳ありませんが、このエラーも同様に私の誤りです。指摘して頂き、ありがとうございます。'bladder'データセットに'age'というカラムは存在しないため、この部分を修正が必要です。

ここでは、'bladder'データセットの特徴量である rx (リスク)ごとに stop (生存時間) の平均を求めて棒グラフを作成してみましょう。

修正したコードは以下の通りです:

df_rx_mean = df_bladder.groupby('rx')['stop'].mean().reset_index()

fig_bladder_bar = px.bar(df_rx_mean, x='rx', y='stop',
                         labels={'stop': 'Average Survival Time', 'rx': 'Risk'},
                         title='Bar plot of Average Survival Time by Risk Level')
fig_bladder_bar.show()

output_12_0.png

このコードは、リスクレベル('rx')グループごとの生存時間('stop')の平均値を計算し、その結果を棒グラフで表示します。これにより、リスクレベルが生存時間にどのような影響を与えているかを視覚的に理解することができます。


user:

ありがとうございます。解決しました。では、今度は、pydataset の cancer データセットを plotly で図示するコードを書いてください。


assistant:

pydatasetのcancerデータセットをロードして可視化してみましょう。まず、データセットを確認し、次に場所(state)による発症数(number)を棒グラフで表示します。

# pydatasetのcancerデータセットをロード
df_cancer = data('cancer')

# データセットの先頭部分を確認
print(df_cancer.head())

# 場所(state)による発症数(number)を棒グラフで表示
fig_cancer = px.bar(df_cancer, x='state', y='number', title='Number of cancer cases by state')
fig_cancer.show()

このコードは、plotly.expressのbar関数を使用して各州における発症数を棒グラフで表示します。

設定の方法や表示に問題が生じた場合は、適宜提供されたコードを調整してご確認ください。


user:

ありがとうございます。設定の方法や表示に問題が生じました。

   inst  time  status  age  sex  ph.ecog  ph.karno  pat.karno  meal.cal  \
1   3.0   306       2   74    1      1.0      90.0      100.0    1175.0   
2   3.0   455       2   68    1      0.0      90.0       90.0    1225.0   
3   3.0  1010       1   56    1      0.0      90.0       90.0       NaN   
4   5.0   210       2   57    1      1.0      90.0       60.0    1150.0   
5   1.0   883       2   60    1      0.0     100.0       90.0       NaN   

   wt.loss  
1      NaN  
2     15.0  
3     15.0  
4     11.0  
5      0.0  
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[14], line 8
      5 print(df_cancer.head())
      7 # 場所(state)による発症数(number)を棒グラフで表示
----> 8 fig_cancer = px.bar(df_cancer, x='state', y='number', title='Number of cancer cases by state')
      9 fig_cancer.show()

File /usr/local/lib/python3.10/site-packages/plotly/express/_chart_types.py:373, in bar(data_frame, x, y, color, pattern_shape, facet_row, facet_col, facet_col_wrap, facet_row_spacing, facet_col_spacing, hover_name, hover_data, custom_data, text, base, error_x, error_x_minus, error_y, error_y_minus, animation_frame, animation_group, category_orders, labels, color_discrete_sequence, color_discrete_map, color_continuous_scale, pattern_shape_sequence, pattern_shape_map, range_color, color_continuous_midpoint, opacity, orientation, barmode, log_x, log_y, range_x, range_y, text_auto, title, template, width, height)
    325 def bar(
    326     data_frame=None,
    327     x=None,
   (...)
    367     height=None,
    368 ) -> go.Figure:
    369     """
    370     In a bar plot, each row of `data_frame` is represented as a rectangular
    371     mark.
    372     """
--> 373     return make_figure(
    374         args=locals(),
    375         constructor=go.Bar,
    376         trace_patch=dict(textposition="auto"),
    377         layout_patch=dict(barmode=barmode),
    378     )

File /usr/local/lib/python3.10/site-packages/plotly/express/_core.py:2076, in make_figure(args, constructor, trace_patch, layout_patch)
   2073 layout_patch = layout_patch or {}
   2074 apply_default_cascade(args)
-> 2076 args = build_dataframe(args, constructor)
   2077 if constructor in [go.Treemap, go.Sunburst, go.Icicle] and args["path"] is not None:
   2078     args = process_dataframe_hierarchy(args)

File /usr/local/lib/python3.10/site-packages/plotly/express/_core.py:1485, in build_dataframe(args, constructor)
   1482     args["color"] = None
   1483 # now that things have been prepped, we do the systematic rewriting of `args`
-> 1485 df_output, wide_id_vars = process_args_into_dataframe(
   1486     args, wide_mode, var_name, value_name
   1487 )
   1489 # now that `df_output` exists and `args` contains only references, we complete
   1490 # the special-case and wide-mode handling by further rewriting args and/or mutating
   1491 # df_output
   1493 count_name = _escape_col_name(df_output, "count", [var_name, value_name])

File /usr/local/lib/python3.10/site-packages/plotly/express/_core.py:1210, in process_args_into_dataframe(args, wide_mode, var_name, value_name)
   1208         if argument == "index":
   1209             err_msg += "\n To use the index, pass it in directly as `df.index`."
-> 1210         raise ValueError(err_msg)
   1211 elif length and len(df_input[argument]) != length:
   1212     raise ValueError(
   1213         "All arguments should have the same length. "
   1214         "The length of column argument `df[%s]` is %d, whereas the "
   (...)
   1221         )
   1222     )

ValueError: Value of 'x' is not the name of a column in 'data_frame'. Expected one of ['inst', 'time', 'status', 'age', 'sex', 'ph.ecog', 'ph.karno', 'pat.karno', 'meal.cal', 'wt.loss'] but received: state

assistant:

私の誤りで、申し訳ありません。 'state''number' というカラムは cancer データセットに存在しません。代わりに、'sex'(性別)によってグルーピングし、そのグループ内での'status'(状態)の平均を計算しましょう。以下に修正したコードを提示します。

# 'sex'による'status'の平均を計算
df_sex_status_mean = df_cancer.groupby('sex')['status'].mean().reset_index()

# 性別によるステータス平均を棒グラフで表示
fig_cancer_bar = px.bar(df_sex_status_mean, x='sex', y='status', 
                        labels={'sex': 'Sex', 'status': 'Average Status'},
                        title='Bar plot of Average Status by Sex')
fig_cancer_bar.show()

output_14_0.png

この修正版のコードは、各性別 (sex) グループ内の状態 (status) の平均を計算し、それを棒グラフで表示します。これにより、性別が病状にどのような影響を与えているかの一端を視覚的に把握することができます。


user:

ありがとうございます。解決できました。同じデータについて、plotlyを用いた他の可視化をいくつか試みてください。


assistant:

もちろんです、'cancer'データセットに対する他の視覚化手法をいくつか提供いたします。

  1. 年齢('age')のヒストグラムを作成する:
fig_cancer_hist = px.histogram(df_cancer, x='age', nbins=30, 
                               labels={'age': 'Age'},
                               title='Histogram of Age')
fig_cancer_hist.show()

output_15_0.png

このコードは年齢のヒストグラムを作成します。これにより、患者の年齢分布を視覚的に把握できます。

  1. 年齢('age')と Karnofsky スコア('ph.karno')の散布図を作成する:
fig_cancer_scatter = px.scatter(df_cancer, x='age', y='ph.karno', color='sex', 
                                labels={'age': 'Age', 'ph.karno': 'Karnofsky Score'},
                                title='Scatter plot of Age and Karnofsky Score by Sex')
fig_cancer_scatter.show()

output_16_0.png

この散布図は、年齢とKarnofskyスコア(身体能力を示す指標)の関係を表します。色は性別('sex')で区分しています。

  1. Karnofsky スコア('ph.karno')の箱ひげ図を作成する:
fig_cancer_box = px.box(df_cancer, y='ph.karno', color='sex', 
                        labels={'ph.karno': 'Karnofsky Score'},
                        title='Boxplot of Karnofsky Score by Sex')
fig_cancer_box.show()

output_17_0.png

この箱ひげ図は、性別別('sex')のKarnofskyスコア('ph.karno')の分布を示しています。各性別の中央値、四分位範囲、外れ値を把握することができます。

以上のサンプルで、不明な部分やエラーが生じた場合は、お知らせください。


user:

ありがとうございます。では、今度は、pydataset の ovarian データセットを plotly で図示するコードを書いてください。


assistant:

pydatasetのovarianデータセットをロードして可視化してみましょう。まず、データを確認し、それから年齢(age)と生存時間(futime)の散布図を作ります。

# pydatasetのovarianデータセットをロードします。
df_ovarian = data('ovarian')

# データセットの先頭部分を確認します。
print(df_ovarian.head())

# 年齢(age)と生存時間(futime)の散布図を作成します。
fig_ovarian_scatter = px.scatter(df_ovarian, x='age', y='futime', color='rx',
                                 labels={'age': 'Age', 'futime': 'Survival Time'},
                                 title='Scatter plot of Age and Survival Time')
fig_ovarian_scatter.show()

このコードでは、plotly.expressscatter関数を使って年齢(age)と生存時間(futime)の散布図を作成しています。色分けはリスク(rx)レベルに基づいています。これによって年齢と生存時間の関係を、さらにリスクレベルで分けて視覚的に把握することが可能です。

なお、設定方法や表示に問題が生じた場合には、ぜひお知らせください。

   futime  fustat      age  resid.ds  rx  ecog.ps
1      59       1  72.3315         2   1        1
2     115       1  74.4932         2   1        1
3     156       1  66.4658         2   1        2
4     421       0  53.3644         2   2        1
5     431       1  50.3397         2   1        1

output_18_1.png


user:

ありがとうございます。同じデータについて、plotlyを用いた他の可視化をいくつか試みてください。


assistant:

もちろんです、'ovarian'データセットに対する他の視覚化手法をいくつか提示いたします。

  1. ph.ecog(身体活動指数)のヒストグラムを作成しましょう。
fig_ovarian_hist = px.histogram(df_ovarian, x='ph.ecog', nbins=30, 
                               title='Histogram of ECOG Score')
fig_ovarian_hist.show()

このコードでは'ph.ecog'のヒストグラムを作成します。これを用いることで、患者のECOGスコアの分布を視覚的に把握できます。

  1. wt.loss(体重減少)と生存時間(futime)の散布図を作成しましょう。
fig_ovarian_scatter = px.scatter(df_ovarian, x='wt.loss', y='futime', color='rx',
                               title='Scatter plot of Weight Loss and Survival Time')
fig_ovarian_scatter.show()

この散布図では、体重減少と生存時間の関係を表します。色はリスクレベル('rx')に基づいて区分します。

  1. 生存時間(futime)の箱ひげ図を作成しましょう。
fig_ovarian_box = px.box(df_ovarian, y='futime', color='rx', 
                       title='Boxplot of Survival Time by Risk Level')
fig_ovarian_box.show()

output_21_0.png

この箱ひげ図では、リスクレベル('rx')に基づいた生存時間('futime')の分布を示しています。各リスクレベルの中央値、四分位範囲、外れ値を視覚的に把握できます。

以上のサンプルで不明点やエラーがあればお知らせください。


user:

3つ目のコードだけエラーなく表示できました。1つ目のコードは次のエラーになりました。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[20], line 1
----> 1 fig_ovarian_hist = px.histogram(df_ovarian, x='ph.ecog', nbins=30, 
      2                                title='Histogram of ECOG Score')
      3 fig_ovarian_hist.show()

File /usr/local/lib/python3.10/site-packages/plotly/express/_chart_types.py:480, in histogram(data_frame, x, y, color, pattern_shape, facet_row, facet_col, facet_col_wrap, facet_row_spacing, facet_col_spacing, hover_name, hover_data, animation_frame, animation_group, category_orders, labels, color_discrete_sequence, color_discrete_map, pattern_shape_sequence, pattern_shape_map, marginal, opacity, orientation, barmode, barnorm, histnorm, log_x, log_y, range_x, range_y, histfunc, cumulative, nbins, text_auto, title, template, width, height)
    434 def histogram(
    435     data_frame=None,
    436     x=None,
   (...)
    472     height=None,
    473 ) -> go.Figure:
    474     """
    475     In a histogram, rows of `data_frame` are grouped together into a
    476     rectangular mark to visualize the 1D distribution of an aggregate
    477     function `histfunc` (e.g. the count or sum) of the value `y` (or `x` if
    478     `orientation` is `'h'`).
    479     """
--> 480     return make_figure(
    481         args=locals(),
    482         constructor=go.Histogram,
    483         trace_patch=dict(
    484             histnorm=histnorm,
    485             histfunc=histfunc,
    486             cumulative=dict(enabled=cumulative),
    487         ),
    488         layout_patch=dict(barmode=barmode, barnorm=barnorm),
    489     )

File /usr/local/lib/python3.10/site-packages/plotly/express/_core.py:2076, in make_figure(args, constructor, trace_patch, layout_patch)
   2073 layout_patch = layout_patch or {}
   2074 apply_default_cascade(args)
-> 2076 args = build_dataframe(args, constructor)
   2077 if constructor in [go.Treemap, go.Sunburst, go.Icicle] and args["path"] is not None:
   2078     args = process_dataframe_hierarchy(args)

File /usr/local/lib/python3.10/site-packages/plotly/express/_core.py:1485, in build_dataframe(args, constructor)
   1482     args["color"] = None
   1483 # now that things have been prepped, we do the systematic rewriting of `args`
-> 1485 df_output, wide_id_vars = process_args_into_dataframe(
   1486     args, wide_mode, var_name, value_name
   1487 )
   1489 # now that `df_output` exists and `args` contains only references, we complete
   1490 # the special-case and wide-mode handling by further rewriting args and/or mutating
   1491 # df_output
   1493 count_name = _escape_col_name(df_output, "count", [var_name, value_name])

File /usr/local/lib/python3.10/site-packages/plotly/express/_core.py:1210, in process_args_into_dataframe(args, wide_mode, var_name, value_name)
   1208         if argument == "index":
   1209             err_msg += "\n To use the index, pass it in directly as `df.index`."
-> 1210         raise ValueError(err_msg)
   1211 elif length and len(df_input[argument]) != length:
   1212     raise ValueError(
   1213         "All arguments should have the same length. "
   1214         "The length of column argument `df[%s]` is %d, whereas the "
   (...)
   1221         )
   1222     )

ValueError: Value of 'x' is not the name of a column in 'data_frame'. Expected one of ['futime', 'fustat', 'age', 'resid.ds', 'rx', 'ecog.ps'] but received: ph.ecog

assistant:

申し訳ございません、私のミスでした。'ph.ecog' というカラムが存在しないため、'ecog.ps' を使用して直します。

以下に修正されたコードを示します:

  1. ecog.ps(身体活動指数)のヒストグラムを作成します:

    fig_ovarian_hist = px.histogram(df_ovarian, x='ecog.ps', nbins=30, 
                                    title='Histogram of ECOG Score')
    fig_ovarian_hist.show()
    

    output_22_0.png

  2. resid.ds(病気の残存範囲)と生存時間(futime)の散布図を作成します。

    fig_ovarian_scatter = px.scatter(df_ovarian, x='resid.ds', y='futime', color='rx',
                                    title='Scatter plot of Residual Disease and Survival Time')
    fig_ovarian_scatter.show()
    

    output_23_0.png

なお、設定方法や表示に問題が生じた場合には、ぜひお知らせください。


user:

ありがとうございます。解決できました。2つ目のコードは次のようなエラーでした。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[21], line 1
----> 1 fig_ovarian_scatter = px.scatter(df_ovarian, x='wt.loss', y='futime', color='rx',
      2                                title='Scatter plot of Weight Loss and Survival Time')
      3 fig_ovarian_scatter.show()

File /usr/local/lib/python3.10/site-packages/plotly/express/_chart_types.py:66, in scatter(data_frame, x, y, color, symbol, size, hover_name, hover_data, custom_data, text, facet_row, facet_col, facet_col_wrap, facet_row_spacing, facet_col_spacing, error_x, error_x_minus, error_y, error_y_minus, animation_frame, animation_group, category_orders, labels, orientation, color_discrete_sequence, color_discrete_map, color_continuous_scale, range_color, color_continuous_midpoint, symbol_sequence, symbol_map, opacity, size_max, marginal_x, marginal_y, trendline, trendline_options, trendline_color_override, trendline_scope, log_x, log_y, range_x, range_y, render_mode, title, template, width, height)
     12 def scatter(
     13     data_frame=None,
     14     x=None,
   (...)
     60     height=None,
     61 ) -> go.Figure:
     62     """
     63     In a scatter plot, each row of `data_frame` is represented by a symbol
     64     mark in 2D space.
     65     """
---> 66     return make_figure(args=locals(), constructor=go.Scatter)

File /usr/local/lib/python3.10/site-packages/plotly/express/_core.py:2076, in make_figure(args, constructor, trace_patch, layout_patch)
   2073 layout_patch = layout_patch or {}
   2074 apply_default_cascade(args)
-> 2076 args = build_dataframe(args, constructor)
   2077 if constructor in [go.Treemap, go.Sunburst, go.Icicle] and args["path"] is not None:
   2078     args = process_dataframe_hierarchy(args)

File /usr/local/lib/python3.10/site-packages/plotly/express/_core.py:1485, in build_dataframe(args, constructor)
   1482     args["color"] = None
   1483 # now that things have been prepped, we do the systematic rewriting of `args`
-> 1485 df_output, wide_id_vars = process_args_into_dataframe(
   1486     args, wide_mode, var_name, value_name
   1487 )
   1489 # now that `df_output` exists and `args` contains only references, we complete
   1490 # the special-case and wide-mode handling by further rewriting args and/or mutating
   1491 # df_output
   1493 count_name = _escape_col_name(df_output, "count", [var_name, value_name])

File /usr/local/lib/python3.10/site-packages/plotly/express/_core.py:1210, in process_args_into_dataframe(args, wide_mode, var_name, value_name)
   1208         if argument == "index":
   1209             err_msg += "\n To use the index, pass it in directly as `df.index`."
-> 1210         raise ValueError(err_msg)
   1211 elif length and len(df_input[argument]) != length:
   1212     raise ValueError(
   1213         "All arguments should have the same length. "
   1214         "The length of column argument `df[%s]` is %d, whereas the "
   (...)
   1221         )
   1222     )

ValueError: Value of 'x' is not the name of a column in 'data_frame'. Expected one of ['futime', 'fustat', 'age', 'resid.ds', 'rx', 'ecog.ps'] but received: wt.loss

assistant:

申し訳ありません、再びカラム名のエラーが発生していますね。'wt.loss'というカラムが存在しないためです。この代わりに'age'(年齢)と生存時間('futime')の関連性を散布図にしてみましょう。

修正版のコードは以下の通りです:

fig_ovarian_scatter = px.scatter(df_ovarian, x='age', y='futime', color='rx',
                                    title='Scatter plot of Age and Survival Time')
fig_ovarian_scatter.show()

output_24_0.png

こちらのコードでは年齢('age')と生存時間('futime')の散布図を作成し、色分けはリスクレベル('rx')によって行っています。

お手数をおかけして申し訳ありませんが、何か問題が生じましたらお知らせください。


user:

ありがとうございます。解決できました。最後に、pydataset の veteran データセットを plotly で図示するコードを書いてください。


assistant:

pydatasetのveteranデータセットをロードして可視化してみましょう。まず、データを確認し、それからセル型(celltype)と生存時間(time)の箱ひげ図を作ります。

# pydatasetのveteranデータセットをロードします。
df_veteran = data('veteran')

# データセットの先頭部分を確認します。
print(df_veteran.head())

# セル型(celltype)と生存時間(time)の箱ひげ図を作成します。
fig_veteran_box = px.box(df_veteran, x='celltype', y='time',  
                         labels={'celltype': 'Cell Type', 'time': 'Survival Time'},
                         title='Boxplot of Survival Time by Cell Type')
fig_veteran_box.show()

このコードでは、plotly.expressbox関数を使ってセル型(celltype)と生存時間(time)の箱ひげ図を作成しています。これによって各セル型における生存時間の分布を視覚的に把握することが可能です。

なお、設定方法や表示に問題が生じた場合には、ぜひお知らせください。


user:

いいすね。素晴らしいす。

   trt  celltype  time  status  karno  diagtime  age  prior
1    1  squamous    72       1     60         7   69      0
2    1  squamous   411       1     70         5   64     10
3    1  squamous   228       1     60         3   38      0
4    1  squamous   126       1     60         9   63     10
5    1  squamous   118       1     70        11   65     10

output_25_1.png

同じデータについて、plotlyを用いた他の可視化をいくつか試みてください。


assistant:

まったく問題ありません。「veteran」データセットに対するいくつかの視覚化手法を以下に示します。

  1. 各病状(celltype)の年齢(age)分布のヒストグラムを作成します。
fig_veteran_hist = px.histogram(df_veteran, x='age', color='celltype', nbins=30, 
                                title='Histogram of Age by Cell Type')
fig_veteran_hist.show()

output_26_0.png

  1. 年齢(age)と生存時間(time)の散布図を作成します。
fig_veteran_scatter = px.scatter(df_veteran, x='age', y='time', color='celltype',
                                 title='Scatter plot of Age and Survival Time')
fig_veteran_scatter.show()

output_27_0.png

  1. セルタイプ(celltype)別のカルノフスキー・パフォーマンススコア(karno)の箱ひげ図を作成します。
fig_veteran_box = px.box(df_veteran, x='celltype', y='karno', 
                        title='Boxplot of Karnofsky Performance Score by Cell Type')
fig_veteran_box.show()

output_28_0.png

以上のサンプルで不明点やエラーがあればお知らせください。


user:

問題ありません。ありがとうございました!


assistant:

どういたしまして! 何か他に質問がありましたら、いつでもお気軽にどうぞ。お手伝いできることがあれば幸いです。 データ分析の旅はこれからも続きますね、頑張ってください!


0
0
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
0