ホームページ  >  記事  >  バックエンド開発  >  無料プロジェクト: ChatGPT を使用して Python と Matplotlib でプロットする

無料プロジェクト: ChatGPT を使用して Python と Matplotlib でプロットする

王林
王林オリジナル
2024-08-31 06:02:06855ブラウズ

Free Project: Use ChatGPT to Plot with Python and Matplotlib

Python でプロットする方法を学びたいですか? ChatGPT が役に立ちます。

このプロジェクトでは、ChatGPT に、プロットと視覚化を作成するための Python コードを生成する方法を教えてもらいます。作成したいプロットの簡単な説明を入力するだけで、ChatGPT が一般的な Matplotlib ライブラリを使用して必要なコードを生成します。このツールを使用すると、プログラミング経験のないユーザーでも、データのプロレベルのプロットを作成できます。研究者、データ サイエンティスト、学生のいずれであっても、ChatGPT を使用すると、Python で美しく有益なプロットを簡単に作成できます。 (ChatGPT によって生成され、若干の変更が加えられています)。

JupyterNotebook のプロジェクトは、Cognitive Class で 無料 で利用できます。


1. 学びたい特定の興味分野について質問しましょう。

質問:「Python でプロットする方法を教えてください」
結果:
Python でデータをプロットするにはさまざまな方法があります。以下は、Python の人気のあるデータ視覚化ライブラリである Matplotlib を使用した簡単な例です:

# using the provided by chatGPT to run in the notebook
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
# Create a figure and an axes
fig, ax = plt.subplots()
# Plot the data
ax.plot(x, y)
# Add a title
ax.set_title('My Plot')
# Show the plot
plt.show()

Free Project: Use ChatGPT to Plot with Python and Matplotlib

これにより、x と y のデータの単純な折れ線プロットが作成されます。 Matplotlib でプロットを作成するために使用できるオプションやカスタマイズ オプションが多数あります。たとえば、線の外観の変更、軸ラベルの変更、凡例の追加などを行うことができます。

Matplotlib および Python のその他のプロット ライブラリに関する詳細情報は次のとおりです。
Matplotlib ドキュメント: https://matplotlib.org/stable/index.html
Matplotlib のチュートリアル: https://matplotlib.org/stable/tutorials/index.html
Seaborn、Matplotlib に基づくデータ視覚化ライブラリ: https://seaborn.pydata.org/
Plotly、インタラクティブなプロットを作成するためのライブラリ: https://plotly.com/python/

ご覧のとおり、ChatGPT は導入テキストとコード ブロック (便宜上コピー アイコン付き) を提供し、追加の有用な情報で会話を締めくくります。

2. フォローアップの質問をすることができます

「Python でのプロットの例をもっと見せてください」 と尋ねるだけです。実際、chatGPT は以前の会話を記憶しています。
結果:
Matplotlib ライブラリを使用して Python で作成できるプロットの例をさらにいくつか示します: 散布図

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]

# Create a figure and an axes
fig, ax = plt.subplots()

# Plot the data
ax.scatter(x, y)

# Add a title
ax.set_title('My Scatter Plot')

# Show the plot
plt.show()

Free Project: Use ChatGPT to Plot with Python and Matplotlib

棒グラフ

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]

# Create a figure and an axes
fig, ax = plt.subplots()

# Plot the data
ax.bar(x, y)

# Add a title
ax.set_title('My Bar Plot')

# Show the plot
plt.show()

Free Project: Use ChatGPT to Plot with Python and Matplotlib

円グラフ

import matplotlib.pyplot as plt

# Sample data
labels = ['Apples', 'Bananas', 'Oranges']
sizes = [30, 45, 25]

# Create a figure and an axes
fig, ax = plt.subplots()

# Plot the data
ax.pie(sizes, labels=labels)

# Add a title
ax.set_title('My Pie Chart')

# Show the plot
plt.show()

Free Project: Use ChatGPT to Plot with Python and Matplotlib

これらはほんの数例であり、Matplotlib を使用して Python で作成できる他にも多くの種類のプロットがあることに注意してください。詳細と例は、Matplotlib ドキュメントで見つけることができます: https://matplotlib.org/stable/index.html.

サンプル データセットからプロットしてみましょう

3. いくつかのサンプル データセットを提案する

データをロードする必要がある最初の段階。 「Python 初心者向けのサンプル データセットを教えていただけますか?」
と尋ねることで、ChatGPT にいくつかのサンプル データセットを提案するように依頼できます。 結果:
もちろん!ここでは、Python の初心者に役立つ可能性のあるいくつかのデータセットを示します:

アイリス データセット: これは、がく片、花弁の長さと幅など、さまざまな種類のアイリスの花に関する情報が含まれる小さなデータセットです。機械学習やデータ分析手法を学習するための初心者向けのデータセットとしてよく使用されます。 sklearn.datasets モジュールを使用して、Python で Iris データセットをロードできます。

ボストンの住宅データセット: このデータセットには、住宅の中央値、犯罪率、その他の要素を含む、ボストン地域の住宅価格に関する情報が含まれています。回帰手法を学習するための初心者向けのデータセットとしてよく使用されます。 sklearn.datasets モジュールを使用して、ボストン ハウジング データセットを Python でロードできます。

MNIST データセット: これは、60,000 のトレーニング例と 10,000 のテスト例を含む手書き数字のデータセットです。画像分類手法を学習するための初心者向けのデータセットとしてよく使用されます。 keras.datasets モジュールを使用して、Python で MNIST データセットをロードできます。

From these suggested datasets, we select Iris dataset to continue to work with as a sample.

3.1. Load the iris dataset as a sample to work wit

h

We need a proper sample in tabular format, so we asked “load iris data with table looking format”
The result:

To load the Iris dataset in a table-like format in Python, you can use the Pandas library. Here is an example of how you can load the Iris dataset and display it in a table-like format:

import pandas as pd
from sklearn import datasets

# Load the Iris dataset
iris = datasets.load_iris()

# Create a DataFrame with the features and target values
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['target'] = iris.target

# Display the DataFrame
print(df)
#--> or we can call the dataset to displace it
df

Free Project: Use ChatGPT to Plot with Python and Matplotlib

_This will create a Pandas DataFrame with the features (sepal length, sepal width, petal length, and petal width) of the Iris dataset as columns, and the target values (the species of iris) as an additional column.

You can then use the usual Pandas functions to manipulate and analyze the data in the DataFrame. For example, you can use the head() function to display the first few rows of the DataFrame, or use the describe() function to get summary statistics for the numerical columns._

4. Plot the dataset

Conveniently, we can ask follow-up questions; here we asked “plot it”
The result:

To plot the Iris dataset using Matplotlib, you can use the following code:

# --> Trying of the suggestions from ChatGPT
# --> we removed the redundant lines (e.g. loading libraries and importing data) from the suggestion

# Create a figure and an axes
fig, ax = plt.subplots()

# Plot the data, coloring the points by the target values
scatter = ax.scatter(df['sepal length (cm)'], df['sepal width (cm)'], c=df['target'])

# Add a legend
legend = ax.legend(*scatter.legend_elements(), title='Species')

# Add a title
ax.set_title('Iris dataset')

# Show the plot
plt.show()

Free Project: Use ChatGPT to Plot with Python and Matplotlib

4.1. Plot-specific column

We select and ask to plot a specific column in the dataset. For example, asking “show me the distribution of the column ‘sepal length (cm)”
The result:

To show the distribution of a column in a Pandas DataFrame, you can use the hist() function of the DataFrame. For example, to show the distribution of the ‘sepal length (cm)’ column in the Iris dataset, you can use the following code:

# Create a figure and an axes
fig, ax = plt.subplots()

# Plot the distribution of the 'sepal length (cm)' column
df['sepal length (cm)'].hist(ax=ax)

# Add a title
ax.set_title('Distribution of sepal length')

# Show the plot
plt.show()

Free Project: Use ChatGPT to Plot with Python and Matplotlib

As you may notice, the plot does not have labels. Therefore, we can **ask “redo it with proper axis labels”.
The result:
**

# Create a figure and an axes
fig, ax = plt.subplots()

# Plot the distribution of the 'sepal length (cm)' column
df['sepal length (cm)'].hist(ax=ax)

# Add axis labels
ax.set_xlabel('Sepal length (cm)')
ax.set_ylabel('Frequency')

# Add a title
ax.set_title('Distribution of sepal length')

# Show the plot
plt.show()

Free Project: Use ChatGPT to Plot with Python and Matplotlib

4.2. More complex plot

Another useful plot to show the distribution is using boxplot which enables us to visualize standard deviation and identify outliers.

Here we ask for a boxplot of all the columns showing in the single plot by asking “show me boxplot of distributions of all columns in same plot”.
The result:

To show the distribution of all columns in a Pandas DataFrame as boxplots on the same plot, you can use the boxplot() function of the DataFrame.

For example, to show the distributions of all columns in the Iris dataset as boxplots, you can use the following code:

import matplotlib.pyplot as plt
import pandas as pd
from sklearn import datasets

# Load the Iris dataset
iris = datasets.load_iris()

# Create a DataFrame with the features
df = pd.DataFrame(iris.data, columns=iris.feature_names)

# Add a column with the target values
df['target'] = iris.target

# Create a figure and an axes
fig, ax = plt.subplots()

# Plot the boxplots of all columns
df.boxplot(ax=ax)

# Add a title
ax.set_title('Distributions of all columns')

# Show the plot
plt.show()

Free Project: Use ChatGPT to Plot with Python and Matplotlib

5. Using chatGPT for your own project

ChatGPT is an excellent tool for working on your project. It is important to frame the question in a way that addresses your problem. Keep in mind that dividing your project into smaller blocks and addressing them individually is the key to addressing the project.

To ask a programming question from ChatGPT, you can simply provide a clear and concise description of your problem, along with any relevant code or error messages. You should also specify the programming language and any libraries or frameworks that you are using.

5.1. How to properly formulate a question (aka. prompt) for ChatGPT

So, let's ask ChatGPT what it thinks.

When formulating a prompt for ChatGPT, it’s important to keep a few things in mind in order to get the most out of the model:
Be specific and clear about what you want the model to do. For example, instead of asking “What do you know about this topic?”, ask “Can you provide a brief overview of the history of the topic?”

Provide context if necessary. If you’re asking a question about a specific event or person, give some background information so that the model can understand the context in which you’re asking the question.

簡潔にしてください。長くて複雑なプロンプトはモデルが理解するのが難しく、応答の精度や関連性が低下する可能性があります。

モデルが馴染みのない過度に専門的な言葉や専門用語の使用は避けてください。

1 つのプロンプトで複数の質問をすることは避けてください。一度に 1 つずつ質問することをお勧めします。

ChatGPT の適切なプロンプトの例をいくつか示します。
「教師あり学習と教師なし学習の主な違いを説明できますか?」

「ニューラル ネットワークの主なコンポーネントは何ですか?また、それらはどのように機能しますか?」

「心を読むロボットについての短編小説を書いてください。」

一方、不適切なプロンプトの例は次のとおりです。

「すべて」
「何を教えてもらえますか?」
「あなたが知っていること」

これらのガイドラインに従うことで、ChatGPT の使用時にモデルから最も正確で関連性の高い応答を確実に取得できるようになります。

5.2.データサイエンティストとして留意すべきこと

ChatGPT は完璧ではありません。提供されている例は sklearn からのものです。現実の世界では、Pandas を使用してリソースから独自のデータをロードする必要があります (以下の例は、ローカル ストレージから iris をロードすることを示しています)。

一般に、現役のデータ サイエンティストとして、データの収集、準備、モデルの構築、解釈という多面的なプロセスに頻繁に遭遇することになります。これらの各ステージには、スキルとリソースに関して独自の要件があります。たとえば、パンダを利用してデータセットを読み込み、操作することができます。 ChatGPT が提供するサンプルは、基礎的な学習補助として機能することを目的としており、より複雑または大量のデータセットの処理には適していない可能性があることに留意することが重要です。

5.3.パニックにならないでチャットしましょう GPT はデータ サイエンティストに代わるものではありません (まだ!)

それでは、ChatGPT に意見を聞いてみましょう。

ChatGPT やその他の大規模言語モデルがデータ サイエンティストに取って代わることができない理由はいくつかあります。

データ サイエンティストは、より幅広いスキル セットを備えています。データ サイエンティストは、データ分析、機械学習、プログラミング、統計などを含む幅広いスキルのトレーニングを受けています。 ChatGPT やその他の大規模な言語モデルは、指定されたプロンプトに基づいてテキストを生成することしかできず、データ サイエンティストが持つその他のスキルは備えていません。

データ サイエンティストは自分の作業の結果を解釈して説明できる: データ サイエンティストは、自分の作業の結果を解釈して説明できるように訓練を受けています。これは、意思決定や利害関係者とのコミュニケーションにとって重要です。 ChatGPT やその他の大規模な言語モデルは、その作業結果を解釈したり説明したりすることができません。

データ サイエンティストは幅広いデータを扱うことができます: データ サイエンティストは、構造化データと非構造化データを含む幅広いデータを扱うように訓練されており、さまざまなツールやテクニックを使用してデータから洞察を抽出できます。 ChatGPT およびその他の大規模な言語モデルは、指定されたプロンプトに基づいてテキストを生成することしかできず、他のタイプのデータを操作することはできません。

データ サイエンティストは幅広い利害関係者と連携できる: データ サイエンティストは、ビジネス リーダー、対象分野の専門家、技術専門家などの幅広い利害関係者と連携できるように訓練を受けており、複雑な概念を明確でわかりやすい言葉で伝えることができます。方法。 ChatGPT やその他の大規模な言語モデルは、利害関係者と連携したり、複雑な概念を伝達したりすることができません。

https://cognitiveclass.ai/

で、IBM 開発者とデータ サイエンティストによって開発されたこの初心者向けのガイド付きプロジェクトとその他の無料コースをチェックしてください。

次回まで!

以上が無料プロジェクト: ChatGPT を使用して Python と Matplotlib でプロットするの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。