首頁  >  文章  >  後端開發  >  免費專案:使用 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 中的項目可以在認知課程中免費使用。


1. 讓我們詢問一下我們想要學習的特定興趣領域。

詢問:「教我如何用 python 繪圖」
結果:
在 Python 中繪製資料的方法有很多種。這是一個使用 Matplotlib(Python 中流行的資料視覺化函式庫)的簡單範例:

# 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 中載入 Boston Housing 資料集。

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.

要簡潔。冗長而複雜的提示可能會讓模型難以理解,並可能導致反應不太準確或相關。

避免使用模型可能不熟悉的過於技術性的語言或術語。

避免在一個提示中提出多個問題,最好一次問一個問題。

以下是 ChatGPT 的一些好提示範例:
「你能解釋一下監督學習和無監督學習之間的主要區別嗎?」

「神經網路的主要組成部分是什麼以及它們如何運作?」

「請寫一個能讀心術的機器人的小故事」

另一方面,一些不良提示的例子是:

「一切」
「你能告訴我什麼?」
「你知道什麼」

遵循這些指南,您可以協助確保在使用 ChatGPT 時從模型中獲得最準確且相關的回應。

5.2.身為資料科學家請記住

ChatGPT 並不完美;提供的範例來自 sklearn。在現實世界中,您需要使用 Pandas 從您的資源加載自己的資料(下面的範例顯示從本地儲存載入 iris)。

一般來說,作為實踐資料科學家,您會經常遇到資料收集、準備、模型建構和解釋的多面向過程。這些階段在技能和資源方面都有自己獨特的要求。例如,您可以使用 pandas 來載入和操作資料集。請務必記住,ChatGPT 提供的範例旨在作為基礎學習輔助工具,可能不適合處理更複雜或大量的資料集。

5.3.不要驚慌,GPT 不會取代資料科學家(目前為止!)

那麼,讓我們問問 ChatGPT 它是怎麼想的。

ChatGPT 和其他大型語言模型不會取代資料科學家有幾個原因:

資料科學家擁有更廣泛的技能:資料科學家接受過廣泛技能的培訓,包括資料分析、機器學習、程式設計、統計等。 ChatGPT 和其他大型語言模型只能根據給定的提示生成文本,並且不具備資料科學家所擁有的其他技能。

資料科學家可以解釋和解釋他們的工作結果:資料科學家接受過解釋和解釋他們的工作結果的培訓,這對於決策和與利害關係人的溝通至關重要。 ChatGPT 和其他大型語言模型無法解釋或解釋其工作結果。

資料科學家可以處理各種資料:資料科學家接受過處理各種資料(包括結構化和非結構化資料)的培訓,他們可以使用各種工具和技術從資料中提取見解。 ChatGPT 和其他大型語言模型只能根據給定的提示生成文本,並且無法處理其他類型的資料。

資料科學家可以與廣泛的利害關係人合作:資料科學家接受過與廣泛的利害關係人合作的培訓,包括商業領袖、主題專家和技術專家,他們可以以清晰易懂的方式傳達複雜的概念方式。 ChatGPT 和其他大型語言模型無法與利害關係人合作或傳達複雜的概念。

查看這個適合初學者的指導專案以及由 IBM 開發人員和資料科學家開發的其他免費課程,網址為 https://cognitiveclass.ai/

下次再見!

以上是免費專案:使用 ChatGPT 透過 Python 和 Matplotlib 進行繪圖的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn