·
关注 发表在 Towards Data Science · 8 分钟阅读 · 2023 年 5 月 22 日
--
照片由 Ricardo Gomez Angel 提供,来源于 Unsplash
数据工程是数据科学过程中的一个重要部分。在 CRISP-DM 方法中,这个过程阶段被称为“数据准备”。它包括数据摄取、数据转换和数据质量保证等任务。在我们的文章中,我们使用 ChatGPT 和 Python 解决典型的数据工程任务。通过这样做,我们探讨了数据工程与新兴领域提示工程之间的联系。
在 2022 年 5 月,Stephen Wolfram 和 Lex Fridman 进行了题为“编程已死?”的深刻讲座。他们讨论了高层语言是否仍会被开发者使用。根据 Wolfram 的说法,许多编程任务可以用大型语言模型(LLMs)来自动化。在撰写本文时,这种模型的最突出例子是ChatGPT。自 2022 年末推出以来,它产生了惊人的结果。指定 LLM 要执行的操作被称为“提示工程”。如果 Wolfram 是对的,那么软件开发的部分工作将从编写代码转向编写提示。
在数据科学中,数据准备可能是一个耗时且繁琐的任务。那么为什么不尝试用 LLM 自动化呢?在接下来的部分,我们将用 ChatGPT 和 Python 处理不同的数据工程问题。我们没有自己编写 Python 代码,而是使用提示工程来生成代码。我们的实验于 2023 年 5 月 19 日进行,基于当时最新的免费 ChatGPT 版本(GPT-3.5)。
让我们从一个简单的问题开始,并从样本数据集中创建一个 Pandas 数据框。表格 1 包含了由世界银行提供的国家指标。
| Continent | Country | Year | Category | Variable | Value |
|---------------|----------|-------|---------------|-------------|--------|
| Asia | China | 2022 | Economy | GDP | 18.1 |
| Asia | China | 2022 | Demographics | Population | 1412 |
| North America | USA | 2022 | Economy | GDP | 25.5 |
| North America | USA | 2022 | Demographics | Population | 333 |
| Europe | Germany | 2022 | Economy | GDP | 3.9 |
| Europe | Germany | 2022 | Demographics | Population | 84 |
表格 1:包含国家指标的数据集
为了从上述样本创建数据框,我们开始了与 ChatGPT 的新对话,并发布了以下提示:
Create a Pandas table with the following data:
Continent, Country, Year, Category, Variable, Value
Asia, China, 2022, Economy, GDP, 18.1
Asia, China, 2022, Demographics, Population, 1412
North America, USA, 2022, Economy, GDP, 25.5
North America, USA, 2022, Demographics, Population, 333
Europe, Germany, 2022, Economy, GDP, 3.9
Europe, Germany, 2022, Demographics, Population, 84
提示 1:创建数据框
作为回应,模型生成了以下 Python 代码:
import pandas as pd
data = {
'Continent': ['Asia', 'Asia', 'North America', 'North America', 'Europe', 'Europe'],
'Country': ['China', 'China', 'USA', 'USA', 'Germany', 'Germany'],
'Year': [2022, 2022, 2022, 2022, 2022, 2022],
'Category': ['Economy', 'Demographics', 'Economy', 'Demographics', 'Economy', 'Demographics'],
'Variable': ['GDP', 'Population', 'GDP', 'Population', 'GDP', 'Population'],
'Value': [18.1, 1412, 25.5, 333, 3.9, 84]
}
df = pd.DataFrame(data)
print(df)
响应 1:数据框创建代码
运行脚本时,我们看到输出符合我们的规范:
Continent Country Year Category Variable Value
0 Asia China 2022 Economy GDP 18.1
1 Asia China 2022 Demographics Population 1412.0
2 North America USA 2022 Economy GDP 25.5
3 North America USA 2022 Demographics Population 333.0
4 Europe Germany 2022 Economy GDP 3.9
5 Europe Germany 2022 Demographics Population 84.0
即使我们使用了一个样本进行实验,接下来的代码原则上也可以处理更大的数据集。
对于数据科学而言,我们用第一个提示创建的表格结构并不理想。相反,所谓的“平面表”会更方便。在表格 1 中,指标“GDP”和“Population”由名为“Variable”和“Value”的两列表示。相比之下,平面表将包含每个指标的单独列,如表格 2 所示。
| Continent | Country | Year | GDP | Population |
|---------------|----------|-------|-------|-------------|
| Asia | China | 2022 | 18.1 | 1412 |
| North America | USA | 2022 | 25.5 | 333 |
| Europe | Germany | 2022 | 3.9 | 84 |
表格 2:包含国家指标的平面表
将表格 1 的格式转换为表格 2 的格式称为“透视”。让我们尝试用 ChatGPT 解决这个任务。为此,我们在对话中发布了第二个提示:
Transform the table into the following format:
Continent, Country, Year, GDP, Population
Asia, China, 2022, 18.1, 1412
North America, USA, 2022, 25.5, 333
Europe, Germany, 2022, 3.9, 84
提示 2:数据框透视
上述信息基于所需数据格式的示例。我们本可以用纯口头方式表述我们的请求。与经典编程一样,提示工程中有替代解决方案来实现目标。找到最佳方式将是未来令人兴奋的任务。作为我们提示的结果,ChatGPT 调整了 Python 代码如下:
import pandas as pd
data = {
'Continent': ['Asia', 'Asia', 'North America', 'North America', 'Europe', 'Europe'],
'Country': ['China', 'China', 'USA', 'USA', 'Germany', 'Germany'],
'Year': [2022, 2022, 2022, 2022, 2022, 2022],
'Category': ['Economy', 'Demographics', 'Economy', 'Demographics', 'Economy', 'Demographics'],
'Variable': ['GDP', 'Population', 'GDP', 'Population', 'GDP', 'Population'],
'Value': [18.1, 1412, 25.5, 333, 3.9, 84]
}
df = pd.DataFrame(data)
# Pivot the table
pivot_df = df.pivot_table(index=['Continent', 'Country', 'Year'], columns='Category', values='Value').reset_index()
# Rename the columns
pivot_df.columns.name = None
pivot_df.columns = [column if column != 'Variable' else None for column in pivot_df.columns]
print(pivot_df)
响应 2:透视代码
然而,运行脚本的第二个版本显示的输出并不是我们所请求的:
Continent Country Year Demographics Economy
0 Asia China 2022 1412.0 18.1
1 Europe Germany 2022 84.0 3.9
2 North America USA 2022 333.0 25.5
而不是预期的列“GDP”和“人口”,表中包含两列名为“人口统计”和“经济”。为了解决这个问题,我们发布了下一个提示:
The columns are not correct, you should have used
the variables GDP and population
提示 3:修复枢轴代码
对此,模型纠正了错误并生成了 Python 代码的另一版本:
import pandas as pd
data = {
'Continent': ['Asia', 'Asia', 'North America', 'North America', 'Europe', 'Europe'],
'Country': ['China', 'China', 'USA', 'USA', 'Germany', 'Germany'],
'Year': [2022, 2022, 2022, 2022, 2022, 2022],
'Category': ['Economy', 'Demographics', 'Economy', 'Demographics', 'Economy', 'Demographics'],
'Variable': ['GDP', 'Population', 'GDP', 'Population', 'GDP', 'Population'],
'Value': [18.1, 1412, 25.5, 333, 3.9, 84]
}
df = pd.DataFrame(data)
# Pivot the table
pivot_df = df.pivot_table(index=['Continent', 'Country', 'Year'], columns='Variable', values='Value').reset_index()
# Rename the columns
pivot_df.columns.name = None
pivot_df.columns = ['Continent', 'Country', 'Year', 'GDP', 'Population']
print(pivot_df)
响应 3:修正的枢轴代码
运行脚本显示,这次实施了正确的枢轴:
Continent Country Year GDP Population
0 Asia China 2022 18.1 1412.0
1 Europe Germany 2022 3.9 84.0
2 North America USA 2022 25.5 333.0
另一个典型的数据工程任务是用额外信息丰富数据集。为此目的,我们要求 ChatGPT 通过发布以下提示为我们的数据添加ISO 国家代码:
For each country, in the table above, add its ISO code
提示 4:添加国家代码
结果,模型调整了响应并返回了以下代码片段:
import pandas as pd
data = {
'Continent': ['Asia', 'Asia', 'North America', 'North America', 'Europe', 'Europe'],
'Country': ['China', 'China', 'USA', 'USA', 'Germany', 'Germany'],
'Year': [2022, 2022, 2022, 2022, 2022, 2022],
'Category': ['Economy', 'Demographics', 'Economy', 'Demographics', 'Economy', 'Demographics'],
'Variable': ['GDP', 'Population', 'GDP', 'Population', 'GDP', 'Population'],
'Value': [18.1, 1412, 25.5, 333, 3.9, 84]
}
iso_codes = {
'China': 'CN',
'USA': 'US',
'Germany': 'DE'
}
df = pd.DataFrame(data)
# Add ISO code column
df['ISO Code'] = df['Country'].map(iso_codes)
# Pivot the table
pivot_df = df.pivot_table(index=['Continent', 'Country', 'ISO Code', 'Year'], columns='Variable', values='Value').reset_index()
# Rename the columns
pivot_df.columns.name = None
pivot_df.columns = ['Continent', 'Country', 'ISO Code', 'Year', 'GDP', 'Population']
print(pivot_df)
响应 4:带有国家代码的数据框
运行脚本显示,数据框已添加了 ISO 代码的新列。
Continent Country ISO Code Year GDP Population
0 Asia China CN 2022 18.1 1412.0
1 Europe Germany DE 2022 3.9 84.0
2 North America USA US 2022 25.5 333.0
让我们接下来通过将一个国家的 GDP 除以其人口大小来派生一个新列“人均 GDP”。为此,我们发送了这个提示:
Derive a new column "GDP per capita" by dividing the GDP
of a country by its population size
提示 5:派生一个新列
对此,模型修改了代码如下:
import pandas as pd
data = {
'Continent': ['Asia', 'Asia', 'North America', 'North America', 'Europe', 'Europe'],
'Country': ['China', 'China', 'USA', 'USA', 'Germany', 'Germany'],
'Year': [2022, 2022, 2022, 2022, 2022, 2022],
'Category': ['Economy', 'Demographics', 'Economy', 'Demographics', 'Economy', 'Demographics'],
'Variable': ['GDP', 'Population', 'GDP', 'Population', 'GDP', 'Population'],
'Value': [18.1, 1412, 25.5, 333, 3.9, 84]
}
iso_codes = {
'China': 'CN',
'USA': 'US',
'Germany': 'DE'
}
df = pd.DataFrame(data)
# Add ISO code column
df['ISO Code'] = df['Country'].map(iso_codes)
# Pivot the table
pivot_df = df.pivot_table(index=['Continent', 'Country', 'ISO Code', 'Year'], columns='Variable', values='Value').reset_index()
# Calculate GDP per capita
pivot_df['GDP per capita'] = pivot_df['GDP'] / pivot_df['Population']
# Rename the columns
pivot_df.columns.name = None
pivot_df.columns = ['Continent', 'Country', 'ISO Code', 'Year', 'GDP', 'Population', 'GDP per capita']
print(pivot_df)
响应 5:带有新列的数据框
输出显示脚本产生了预期的结果:
Continent Country ISO Code Year GDP Population GDP per capita
0 Asia China CN 2022 18.1 1412.0 0.012819
1 Europe Germany DE 2022 3.9 84.0 0.046429
2 North America USA US 2022 25.5 333.0 0.076577
一旦数据转换完成,实际分析就可以开始了。最后让我们计算国家的 GDP 和人口之间的相关系数。为此,我们发布了最后一个提示:
Use the table above to calculate the correlation coefficient between
the GDP and the population of the countries
提示 6:分析相关性
因此,模型返回了以下代码片段:
import pandas as pd
data = {
'Continent': ['Asia', 'Asia', 'North America', 'North America', 'Europe', 'Europe'],
'Country': ['China', 'China', 'USA', 'USA', 'Germany', 'Germany'],
'Year': [2022, 2022, 2022, 2022, 2022, 2022],
'Category': ['Economy', 'Demographics', 'Economy', 'Demographics', 'Economy', 'Demographics'],
'Variable': ['GDP', 'Population', 'GDP', 'Population', 'GDP', 'Population'],
'Value': [18.1, 1412, 25.5, 333, 3.9, 84]
}
df = pd.DataFrame(data)
# Pivot the table
pivot_df = df.pivot_table(index=['Continent', 'Country', 'Year'], columns='Variable', values='Value').reset_index()
# Calculate correlation coefficient
correlation_coefficient = pivot_df['GDP'].corr(pivot_df['Population'])
print("Correlation Coefficient: {:.3f}".format(correlation_coefficient))
响应 6:分析相关性的代码
运行脚本时,我们发现这两个变量之间存在正相关,这并不令人意外:
Correlation Coefficient: 0.350
诚然,我们的实验基于一个简单的数据集。尽管如此,结果令人瞩目。我们完成了多项数据工程任务,而不用写一行代码。ChatGPT 不仅在大多数情况下能够正确实现我们的提示,即使在模型犯错时,它也能够反思并修正。正如在软件开发中一样,生成的代码必须经过测试。此外,它可能需要重构和优化。在 AI 时代,使用 pylint 仍然是个好主意。总结来说,然而,我们不得不同意沃尔夫勒姆的观点:未来,数据工程的重要部分将从编码转向提示工程。这种新方法不会取代数据工程师,但会让他们变得更高效。
所有图片(除非另有说明)均由作者提供。
克里斯蒂安·科赫是 BWI GmbH 的企业架构师,同时也是纽伦堡理工大学乔治·西蒙·奥姆学院的讲师。
马库斯·施塔迪是 Dehn SE 的高级云数据工程师,多年来一直从事数据工程、数据科学和数据分析工作。
卢卡斯·伯勒 是 TeamBank AG 的数据架构师,专注于设计和实施强大的数据分析架构。