Vom Data Engineering zum Prompt Engineering

Machen Sie ein Vermögen mit Ihrer kleinen Hand, geben Sie ihr einen Daumen hoch!

alt

Data Engineering macht einen großen Teil des Data-Science-Prozesses aus. In CRISP-DM wird diese Phase des Prozesses „Datenvorbereitung“ genannt. Es umfasst Aufgaben wie Datenaufnahme, Datentransformation und Datenqualitätssicherung. In Kapitel [1] dieses Artikels lösen wir typische Data-Engineering-Aufgaben mit ChatGPT und Python. Dabei erforschen wir den Zusammenhang zwischen Data Engineering und der neuen Disziplin des Prompting Engineering.

Einführung

Im Mai 2022 veröffentlichten Stephen Wolfram und Lex Fridman einen aufschlussreichen Artikel mit dem Titel „Is Programming Dead?“ Sie diskutierten darüber, ob Entwickler künftig noch Hochsprachen verwenden werden. Viele Programmieraufgaben lassen sich laut Wolfram mit großen Sprachmodellen (LLMs) automatisieren. Zum Zeitpunkt des Verfassens dieses Artikels ist ChatGPT das bekannteste Beispiel für ein solches Modell. Seit seiner Einführung Ende 2022 hat es erstaunliche Ergebnisse erzielt. Das Spezifizieren von Aktionen, die von LLM ausgeführt werden sollen, wird als „Hint Engineering“ bezeichnet. Wenn Wolfram Recht hat, wird sich zumindest ein Teil der Softwareentwicklung vom Schreiben von Code zum Schreiben von Hinweisen verlagern.

Wenn es um Datenwissenschaft geht, kann die Datenaufbereitung eine zeitaufwändige und mühsame Aufgabe sein. Warum also nicht versuchen, es mit LLM zu automatisieren? In den folgenden Abschnitten werden wir ChatGPT und Python verwenden, um verschiedene datentechnische Probleme zu lösen. Anstatt den Python-Code selbst zu schreiben, verwenden wir das Hint-Projekt, um ihn zu generieren. Unsere Experimente wurden am 19. Mai 2023 auf Basis der damals neuesten kostenlosen ChatGPT-Version (GPT-3.5) durchgeführt.

Datenrahmen erstellen

Beginnen wir mit einem einfachen Problem und erstellen wir einen Pandas-Datenrahmen aus einem Beispieldatensatz. Tabelle 1 enthält beispielsweise Länderindikatoren der Weltbank.

Bild-20230524153840794
Bild-20230524153840794

Um einen Datenrahmen aus dem obigen Beispiel zu erstellen, haben wir eine neue Konversation mit ChatGPT gestartet und die folgende Eingabeaufforderung gepostet:

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

Als Reaktion darauf generierte das Modell den folgenden Python-Code:

import pandas as pd

data = {
    'Continent': ['Asia''Asia''North America''North America''Europe''Europe'],
    'Country': ['China''China''USA''USA''Germany''Germany'],
    'Year': [202220222022202220222022],
    'Category': ['Economy''Demographics''Economy''Demographics''Economy''Demographics'],
    'Variable': ['GDP''Population''GDP''Population''GDP''Population'],
    'Value': [18.1141225.53333.984]
}

df = pd.DataFrame(data)
print(df)

Wenn wir das Skript ausführen, sehen wir, dass die Ausgabe unseren Spezifikationen entspricht:

alt

尽管我们在实验中使用了示例,但下一节中生成的代码原则上也可以处理更大的数据集。

旋转数据框

对于数据科学,我们在第一个提示中创建的表结构并不是最优的。相反,所谓的“平板”会更方便。在表 1 中,指标“GDP”和“人口”分别用名为“Variable”和“Value”的两列表示。相比之下,平面表格将为每个指标包含单独的列,如表 2 所示。

alt

将表 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

上述消息基于所需数据格式的示例。或者,我们也可以纯口头表达我们的要求。与经典编程一样,在提示工程中有实现目标的替代解决方案。在不久的将来,寻找最佳方式将是一项激动人心的任务。根据我们的提示,ChatGPT对Python代码进行了如下调整:

import pandas as pd

data = {
    'Continent': ['Asia''Asia''North America''North America''Europe''Europe'],
    'Country': ['China''China''USA''USA''Germany''Germany'],
    'Year': [202220222022202220222022],
    'Category': ['Economy''Demographics''Economy''Demographics''Economy''Demographics'],
    'Variable': ['GDP''Population''GDP''Population''GDP''Population'],
    'Value': [18.1141225.53333.984]
}

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)

然而,运行脚本的第二个版本显示输出不是我们所要求的: alt

该表包含名为“人口统计”和“经济”的两列,而不是预期的“GDP”和“人口”列。为了解决这个问题,我们发布了下一个提示:

The columns are not correct, you should have used 
the variables GDP and population

作为响应,模型纠正了错误并生成了另一个版本的 Python 代码:

import pandas as pd

data = {
    'Continent': ['Asia''Asia''North America''North America''Europe''Europe'],
    'Country': ['China''China''USA''USA''Germany''Germany'],
    'Year': [202220222022202220222022],
    'Category': ['Economy''Demographics''Economy''Demographics''Economy''Demographics'],
    'Variable': ['GDP''Population''GDP''Population''GDP''Population'],
    'Value': [18.1141225.53333.984]
}

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)

运行脚本表明这次旋转已正确实现:

alt

富集数据框

另一个典型的数据工程任务是用附加信息丰富数据集。为此,我们要求 ChatGPT 通过发布以下提示将 ISO 国家代码添加到我们的数据中:

For each country, in the table above, add its ISO code

结果,模型调整了响应并返回了以下代码片段:

import pandas as pd

data = {
    'Continent': ['Asia''Asia''North America''North America''Europe''Europe'],
    'Country': ['China''China''USA''USA''Germany''Germany'],
    'Year': [202220222022202220222022],
    'Category': ['Economy''Demographics''Economy''Demographics''Economy''Demographics'],
    'Variable': ['GDP''Population''GDP''Population''GDP''Population'],
    'Value': [18.1141225.53333.984]
}

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)

运行脚本显示一个带有 ISO 代码的新列已添加到数据框中。

alt

派生列

接下来让我们通过将一个国家的 GDP 除以其人口规模来得出一个新的列“人均 GDP”。为此,我们发送了以下提示:

Derive a new column "GDP per capita" by dividing the GDP 
of a country by its population size

对此,模型修改了代码如下:

import pandas as pd

data = {
    'Continent': ['Asia''Asia''North America''North America''Europe''Europe'],
    'Country': ['China''China''USA''USA''Germany''Germany'],
    'Year': [202220222022202220222022],
    'Category': ['Economy''Demographics''Economy''Demographics''Economy''Demographics'],
    'Variable': ['GDP''Population''GDP''Population''GDP''Population'],
    'Value': [18.1141225.53333.984]
}

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)

输出表明脚本产生了预期的结果:

alt

分析数据

一旦数据转换完成,实际的分析就可以开始了。最后让我们计算一下各国GDP与人口的相关系数。为此,我们贴出最后一个提示:

Use the table above to calculate the correlation coefficient between 
the GDP and the population of the countries

结果,模型返回了以下代码片段:

import pandas as pd

data = {
    'Continent': ['Asia''Asia''North America''North America''Europe''Europe'],
    'Country': ['China''China''USA''USA''Germany''Germany'],
    'Year': [202220222022202220222022],
    'Category': ['Economy''Demographics''Economy''Demographics''Economy''Demographics'],
    'Variable': ['GDP''Population''GDP''Population''GDP''Population'],
    'Value': [18.1141225.53333.984]
}

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

运行脚本时,我们发现两个变量之间存在很强的正相关性,这并不奇怪:

alt

总结

Zugegebenermaßen basieren unsere Experimente auf einem einfachen Datensatz. Die Ergebnisse sind jedoch bemerkenswert. Wir haben mehrere Data-Engineering-Aufgaben durchgeführt, ohne eine einzige Codezeile zu schreiben. Nicht nur, dass ChatGPT unsere Eingabeaufforderungen die meiste Zeit korrekt ausführt. Aber selbst wenn das Modell Fehler macht, ist es in der Lage, diese zu reflektieren und zu korrigieren. Wie bei der Softwareentwicklung muss der resultierende Code getestet werden. Außerdem ist möglicherweise eine Umgestaltung und Optimierung erforderlich. Der Einsatz von Pylint im Zeitalter der KI ist immer noch eine gute Idee. Zusammenfassend müssen wir jedoch Wolfram zustimmen: In Zukunft wird sich ein erheblicher Teil des Data Engineerings vom Coding zum Hint Engineering verlagern. Dieser neue Ansatz wird Dateningenieure nicht ersetzen, sondern ihre Effektivität steigern.

Referenz

[1]

Quelle:„https://towardsdatascience.com/from-data-engineering-to-prompt-engineering-5debd1c636e0“

Dieser Artikel wurde von mdnice multi-platform veröffentlicht

Ich denke du magst

Origin blog.csdn.net/swindler_ice/article/details/131197891
Empfohlen
Rangfolge