首页  >  文章  >  后端开发  >  免费项目:使用 ChatGPT 通过 Python 和 Matplotlib 进行绘图

免费项目:使用 ChatGPT 通过 Python 和 Matplotlib 进行绘图

王林
王林原创
2024-08-31 06:02:06854浏览

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