国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

如何利用 ChatGPT 進行自動數(shù)據(jù)清理和預處理

這篇具有很好參考價值的文章主要介紹了如何利用 ChatGPT 進行自動數(shù)據(jù)清理和預處理。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

推薦:使用 NSDT場景編輯器助你快速搭建可二次編輯的3D應用場景

ChatGPT 已經(jīng)成為一把可用于多種應用的瑞士軍刀,并且有大量的空間將 ChatGPT 集成到數(shù)據(jù)科學工作流程中。

如果您曾經(jīng)在真實數(shù)據(jù)集上訓練過機器學習模型,您就會知道數(shù)據(jù)清理和預處理的步驟對于構(gòu)建可靠的機器學習模型非常重要。在本指南中,我們將了解如何使用 ChatGPT 在示例數(shù)據(jù)集上執(zhí)行這些任務。

我們將使用 UCI 機器學習存儲庫中的銀行營銷數(shù)據(jù)集,并提示 ChatGPT 執(zhí)行以下操作:

  • 獲取并加載數(shù)據(jù)集
  • 檢查缺失值
  • 對分類變量進行編碼

您可以通過登錄您的 OpenAI 帳戶并開始新的 ChatGPT 會話來跟進。如果你愿意,你也可以在谷歌Colab中編碼。

獲取和加載數(shù)據(jù)集

數(shù)據(jù)集有幾個不同版本。我們將使用銀行滿.csv。

提示:今天我們將對真實數(shù)據(jù)集進行數(shù)據(jù)清理和預處理。編寫代碼以執(zhí)行以下操作:1. 從 UCI 機器學習存儲庫中提取銀行營銷數(shù)據(jù)集。使用請求或 urllib 等庫。2. 下載 zip 文件,解壓縮內(nèi)容,并將銀行已滿.csv文件讀取到 pandas 數(shù)據(jù)幀中(稱為數(shù)據(jù))。

在數(shù)據(jù)集中獲取和讀取的代碼如下:

import pandas as pd
import urllib
import zipfile

# URL of the dataset
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/00222/bank.zip"

# Fetch and extract the dataset
urllib.request.urlretrieve(url, "bank.zip")

with zipfile.ZipFile("bank.zip", "r") as zip_ref:
    zip_ref.extractall()

# Load the dataset into a pandas DataFrame
data = pd.read_csv("bank-full.csv", delimiter=";",skipinitialspace=True)

獲取數(shù)據(jù)集的基本信息

讓我們更好地理解數(shù)據(jù)集。

# Display the first few rows of the dataset
print(data.head())

這輸出:

如何利用 ChatGPT 進行自動數(shù)據(jù)清理和預處理,chatgpt,數(shù)據(jù)處理


截斷的輸出 data.head()

提示:使用 pandas 獲取數(shù)據(jù)幀的維度、列的描述性統(tǒng)計信息以及各種列的數(shù)據(jù)類型。

這一步并不需要提示 ChatGPT,因為 pandas 方法非常簡單。

# Get the dimensions of the dataset (rows, columns)
print(data.shape)
Output >>> (45211, 17)

我們有超過 45000 條記錄和 16 個特征(因為 17 個也包括輸出標簽)。

# Get statistical summary of numerical columns
print(data.describe())

如何利用 ChatGPT 進行自動數(shù)據(jù)清理和預處理,chatgpt,數(shù)據(jù)處理


數(shù)據(jù).describe() 的截斷輸出

獲取各個列的數(shù)據(jù)類型的概述也很有幫助。當我們對分類變量進行編碼時,我們將仔細研究它們。

# Check data types of each column
print(data.dtypes)
Output >>>

age           int64
job          object
marital      object
education    object
default      object
balance       int64
housing      object
loan         object
contact      object
day           int64
month        object
duration      int64
campaign      int64
pdays         int64
previous      int64
poutcome     object
y            object
dtype: object

到目前為止,我們已經(jīng)了解了銀行營銷數(shù)據(jù)集。輸出標簽表示客戶是否會訂閱定期存款。該數(shù)據(jù)集包含多個要素,例如年齡、月份、教育程度、婚姻狀況、先前廣告系列的結(jié)果等。

檢查缺失值

提示:我想知道每列中缺失值的數(shù)量。請給我代碼來做這件事。使用熊貓。
# Check for missing values in each column
missing_values = data.isnull().sum()
print(missing_values)

下面是輸出:

Output >>>

age          0
job          0
marital      0
education    0
default      0
balance      0
housing      0
loan         0
contact      0
day          0
month        0
duration     0
campaign     0
pdays        0
previous     0
poutcome     0
y            0
dtype: int64

此版本的銀行營銷數(shù)據(jù)集(包含超過 45000 條記錄)沒有任何缺失值。但實際上,大多數(shù)真實世界的數(shù)據(jù)集都有缺失值。您應該使用合適的插補技術(shù)來處理缺失值。

作為可選的練習,您可以在此處添加一個步驟,提示 ChatGPT 從列的子集中刪除一小部分值,以便您可以練習如何處理缺失值。

編碼分類變量

下一步是對數(shù)據(jù)集中的分類變量進行編碼。我們將首先獲取所有分類列的列表。

提示:提供代碼以獲取此數(shù)據(jù)集中所有分類列的列表。
# Count the number of categorical variables
categorical_columns = data.select_dtypes(include=['object']).columns
print(categorical_columns)
num_categorical_variables = len(categorical_columns)

# Display the count
print("Number of categorical variables:", num_categorical_variables)

分類列列表還包括輸出標簽?y。但是,讓我們先關(guān)注其他分類變量。

Output >>>
Index(['job', 'marital', 'education', 'default', 'housing', 'loan', 'contact',
       'month', 'poutcome', 'y'],
      dtype='object')
Number of categorical variables: 10

仔細查看分類變量的值

接下來,讓我們看看每個分類變量采用的值。

提示:我想了解每個分類變量采用的各種值。給熊貓代碼來做到這一點。
# Get the different values and their counts for each categorical column
categorical_columns = ['job', 'marital', 'education', 'default', 'housing', 'loan', 'contact', 'month', 'poutcome']
for column in categorical_columns:
    value_counts = data[column].value_counts()
    print(f"Column: {column}")
    print(value_counts,"\n")

輸出很難解析:

Output >>>

Column: job
blue-collar      9732
management       9458
technician       7597
admin.           5171
services         4154
retired          2264
self-employed    1579
entrepreneur     1487
unemployed       1303
housemaid        1240
student           938
unknown           288
Name: job, dtype: int64 

...
...
...
Column: poutcome
unknown    36959
failure     4901
other       1840
success     1511
Name: poutcome, dtype: int64 

可視化分類變量的值

讓我們提示 ChatGPT 創(chuàng)建一個可視化,以便更容易理解分類變量采用的值。

提示:我想要一個顯示每個分類變量取值的圖。排除輸出標簽 y。包括所有其他分類變量。

我們開始吧!

import matplotlib.pyplot as plt

# Get the different values and their counts for each categorical column
categorical_columns = ['job', 'marital', 'education', 'default', 'housing', 'loan', 'contact', 'month', 'poutcome']

# Set up the subplot grid
fig, axes = plt.subplots(3, 3, figsize=(10,8))

# Iterate over each categorical column and plot the value counts
for i, column in enumerate(categorical_columns):
    row = i // 3
    col = i % 3
    value_counts = data[column].value_counts()
    ax = axes[row, col]
    value_counts.plot(kind='bar', ax=ax)
    ax.set_xlabel(column)
    ax.set_ylabel('Count')
    ax.set_title(f'{column.title()} Value Counts')

# Adjust spacing between subplots
plt.tight_layout()

# Show the plot
plt.show()

如何利用 ChatGPT 進行自動數(shù)據(jù)清理和預處理,chatgpt,數(shù)據(jù)處理


分類變量的值

獨熱編碼

對于分類列的子集,我們可以使用獨熱編碼。

提示:給 pandas 代碼以對列列表執(zhí)行獨熱編碼。

我們使用從熊貓到獨熱編碼以下列:get_dummies

# Select the categorical columns (excluding education and poutcome)
categorical_columns = ['job', 'marital', 'default', 'housing', 'loan', 'contact']

# Perform one-hot encoding
encoded_data = pd.get_dummies(data, columns=categorical_columns)
提示:我想打印出新添加列的前幾行。編寫代碼以執(zhí)行相同的操作。
# Select the newly added columns
new_columns = encoded_data.columns.difference(data.columns)

# Print the head of the newly added columns
print(encoded_data[new_columns].head())

如何利用 ChatGPT 進行自動數(shù)據(jù)清理和預處理,chatgpt,數(shù)據(jù)處理


截斷的輸出 encoded_data['new_colums'].head()

定義自定義映射

對于“education”和“poutcome”(先前結(jié)果)等變量的列,而不是獨熱編碼,最好使用自定義映射在值之間進行一定程度的比較。

此外,當我們使用獨熱編碼來編碼一個分類變量時,該變量采用 k 個不同的值 k 個新列。對于像“月”這樣的分類列,如果不添加太多信息,這將是多余的。

因此,對于這兩種情況,我們將定義一個自定義映射,然后轉(zhuǎn)換值。我們可以提示 ChatGPT 獲取一個通用代碼片段,然后我們可以修改該代碼片段以定義特定列的映射。

提示:我想做一個自定義映射來編碼一些分類變量。首先給我一個通用代碼片段,該代碼片段定義了從分類列中的唯一值到一組不同輸出值的自定義映射。然后,我們應該使用此映射來轉(zhuǎn)換列中的值。
# Define the custom mapping dictionary
custom_mapping = {
    'category1': 'value1',
    'category2': 'value2',
    'category3': 'value3',
    # Add more categories and corresponding values as needed
}

# Specify the categorical column
categorical_column = 'column_name'  # Replace 'column_name' with the actual column name

# Perform the mapping using the custom mapping dictionary
data[categorical_column] = data[categorical_column].map(custom_mapping)

對于“月”列,讓我們像這樣轉(zhuǎn)換月份字符串:

# Define the mapping dictionary for months
month_mapping = {
    'jan': 1,
    'feb': 2,
    'mar': 3,
    'apr': 4,
    'may': 5,
    'jun': 6,
    'jul': 7,
    'aug': 8,
    'sep': 9,
    'oct': 10,
    'nov': 11,
    'dec': 12
}

# Map the values in the month column
encoded_data['month'] = encoded_data['month'].map(month_mapping)

讓我們將“結(jié)果”和“教育”列映射到數(shù)值,如下所示:

# Define the custom mapping for poutcome and education
poutcome_mapping = {
    'unknown': 0,
    'failure': 1,
    'other': 2,
    'success': 3
}

education_mapping = {
    'unknown': 0,
    'primary': 1,
    'secondary': 2,
    'tertiary': 3
}

# Perform ordinal encoding using pandas map
encoded_data['poutcome'] = encoded_data['poutcome'].map(poutcome_mapping)
encoded_data['education'] = encoded_data['education'].map(education_mapping)


# Select the newly added columns
new_columns = ['month','poutcome', 'education']

# Print the head of the newly added columns
print(encoded_data[new_columns].head(10))

對輸出標簽進行編碼

我們還將輸出標簽“是”和“否”分別映射到 1 和 0。

encoded_data['y'] = encoded_data['y'].replace({'no': 0, 'yes': 1})
print(encoded_data['y'])
Output >>>

0        0
1        0
2        0
3        0
4        0
        ..
45206    1
45207    1
45208    1
45209    0
45210    0
Name: y, Length: 45211, dtype: int64

回想一下,我們對“住房”、“默認”和“貸款”列使用了獨熱編碼。由于這些列還采用“是”和“否”值,因此您還可以將“是”和“否”分別映射到 1 和 0。

可視化類標簽的分布

檢查類標簽的分布也很有幫助,這樣我們就可以在需要時在構(gòu)建模型時考慮類不平衡。

提示:我想了解類標簽的分布。請給我代碼以生成合適的圖。

以下是在Seaborn中生成計數(shù)圖的代碼:

import seaborn as sns
import matplotlib.pyplot as plt

# Plot the distribution of output labels
sns.countplot(data=data, x='y')
plt.xlabel('Output Label')
plt.ylabel('Count')
plt.title('Output Label Distribution')
plt.show()

如何利用 ChatGPT 進行自動數(shù)據(jù)清理和預處理,chatgpt,數(shù)據(jù)處理


類標簽的分發(fā)

我們看到存在階級失衡。因此,如果要構(gòu)建模型,則應確保使用適當?shù)闹夭蓸臃椒ń鉀Q類不平衡問題。

通用數(shù)據(jù)清理和預處理管道

盡管數(shù)據(jù)清理和預處理需要開發(fā)人員的努力,但讓我們嘗試獲取一個通用的步驟序列,該步驟序列對于簡單的數(shù)據(jù)集來說相當有效。

提示:您能否根據(jù)我們到目前為止所做的工作給我一個通用的數(shù)據(jù)清理和預處理管道。獲取數(shù)據(jù)集的基本信息,檢查和處理缺失值,標識分類列,并對分類列進行編碼。只使用熊貓。

這是:

import pandas as pd

# Load the dataset into a DataFrame
data = pd.read_csv('data.csv')

# Step 1: Basic Info
print(data.info())

# Step 2: Missing Values
missing_values = data.isnull().sum()
print(missing_values)

# Step 3: Handle Missing Values
# Identify columns with missing values
columns_with_missing_values = missing_values[missing_values > 0].index

# Impute missing values using fillna() or other suitable method
data[columns_with_missing_values] = data[columns_with_missing_values].fillna(data[columns_with_missing_values].mean())

# Step 4: Get Categorical Columns
categorical_columns = data.select_dtypes(include=['object']).columns

# Step 5: Encode Categorical Columns
# Perform ordinal encoding or one-hot encoding using pandas' get_dummies() function
encoded_data = pd.get_dummies(data, columns=categorical_columns)

# Print the encoded data
print(encoded_data.head())

總結(jié)

可以看出,與 ChatGPT 協(xié)同作用的數(shù)據(jù)科學專業(yè)知識可以幫助使數(shù)據(jù)清理和預處理更簡單、更快捷?,F(xiàn)在,您已經(jīng)準備好了預處理的數(shù)據(jù)集,您可以通過在此銀行營銷數(shù)據(jù)集上構(gòu)建一個簡單的預測模型來進一步實現(xiàn)這一點。

原文鏈接:如何利用 ChatGPT 進行自動數(shù)據(jù)清理和預處理 (mvrlink.com)文章來源地址http://www.zghlxwxcb.cn/news/detail-655836.html

到了這里,關(guān)于如何利用 ChatGPT 進行自動數(shù)據(jù)清理和預處理的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務器費用

相關(guān)文章

  • 如何基于香橙派AIpro對視頻/圖像數(shù)據(jù)進行預處理

    本文分享自華為云社區(qū)《如何基于香橙派AIpro對視頻/圖像數(shù)據(jù)進行預處理》,作者: 昇騰CANN。 受網(wǎng)絡(luò)結(jié)構(gòu)和訓練方式等因素的影響,絕大多數(shù)神經(jīng)網(wǎng)絡(luò)模型對輸入數(shù)據(jù)都有格式上的限制。在計算機視覺領(lǐng)域,這個限制大多體現(xiàn)在圖像的尺寸、色域、歸一化參數(shù)等。如果源圖

    2024年04月22日
    瀏覽(34)
  • 【Python實戰(zhàn)】數(shù)據(jù)預處理(數(shù)據(jù)清理、集成、變換、歸約)

    【Python實戰(zhàn)】數(shù)據(jù)預處理(數(shù)據(jù)清理、集成、變換、歸約)

    因疫情原因,距上次寫博客已過許久 這次回看以前的書籍,發(fā)現(xiàn)數(shù)據(jù)預處理這塊在業(yè)務中極其重要 業(yè)務中,數(shù)據(jù)的準確率對業(yè)務的影響至關(guān)重要 好的數(shù)據(jù)往往百利而無一害,相對的,不好的數(shù)據(jù)會帶來無法預期的損失 管理好數(shù)據(jù),就能管理好業(yè)務,環(huán)環(huán)相扣,生生不息 所

    2024年02月08日
    瀏覽(24)
  • 掌握無人機遙感數(shù)據(jù)預處理的全鏈條理論與實踐流程、典型農(nóng)林植被性狀的估算理論與實踐方法、利用MATLAB進行編程實踐(腳本與GUI開發(fā))以及期刊論文插圖制作等

    掌握無人機遙感數(shù)據(jù)預處理的全鏈條理論與實踐流程、典型農(nóng)林植被性狀的估算理論與實踐方法、利用MATLAB進行編程實踐(腳本與GUI開發(fā))以及期刊論文插圖制作等

    目錄 專題一 認識主被動無人機遙感數(shù)據(jù) 專題二 預處理無人機遙感數(shù)據(jù) 專題三 定量估算農(nóng)林植被關(guān)鍵性狀 專題四 期刊論文插圖精細制作與Appdesigner應用開發(fā) 近地面無人機植被定量遙感與生理參數(shù)反演 更多推薦 遙感技術(shù)作為一種空間大數(shù)據(jù)手段,能夠從多時、多維、多地等

    2024年02月16日
    瀏覽(29)
  • 如何利用ChatGPT進行論文潤色-ChatGPT潤色文章怎么樣

    如何利用ChatGPT進行論文潤色-ChatGPT潤色文章怎么樣

    ChatGPT可以潤色文章,使用其潤色功能可以為用戶提供更加整潔、清晰、文采動人的文本。但需要注意以下幾點: 需要保持文本的一致性和完整性。當使用ChatGPT進行潤色時,需要注意保持文本的一致性和完整性。不應改變原始文章的意義、論點和邏輯結(jié)構(gòu),尤其是在非常規(guī)文

    2024年02月08日
    瀏覽(22)
  • 如何使用 ChatGPT 進行教學,教師可以利用 ChatGPT 的 5 種方式

    如何使用 ChatGPT 進行教學,教師可以利用 ChatGPT 的 5 種方式

    我們聽說過很多關(guān)于學生如何使用 ChatGPT 撰寫論文和布置家庭作業(yè)的信息。 我們一直在討論圍繞這個問題的擔憂,并爭先恐后地為 ChatGPT 尋找 AI 檢測工具,據(jù)傳 OpenAI 也在致力于此。 但是關(guān)于教師如何將 ChatGPT 用于他們自己的工作的討論并不多。 在從教師的角度對 ChatGPT 進

    2023年04月08日
    瀏覽(22)
  • 利用ChatGPT如何進行批量長文本處理工具GPTBAT

    大家好,我是技術(shù)宅小伙,今天要跟大家分享一下我之前寫的 GPT 長文本處理程序。當時我寫完后就把它放到 Hog 上了,因為最開始是為了自己用,所以后來就忘掉了。最近有同學把它翻出來用,然后經(jīng)常來問我,說不知道這個東西怎么用。其實在我看來這個挺簡單的,但是如

    2023年04月22日
    瀏覽(22)
  • 如何利用ChatGPT自動生成SQL語句

    如何利用ChatGPT自動生成SQL語句

    作為一名開發(fā)者,你可能已經(jīng)使用過自然語言處理(NLP)及其可能徹底改變我們與技術(shù)互動的方式。由OpenAI提供支持的文本到SQL工具是一種強大的方法,可以從自然語言文本中生成SQL語句。在本博客文章中,我們將探討七個創(chuàng)造性和不尋常的示例,展示如何使用ChatGPT生成SQ

    2024年02月01日
    瀏覽(24)
  • 如何利用 Kubernetes 的 HPA 進行自動縮容

    在前面的文章中,我們了解了 Kubernetes 的彈性伸縮機制,以及如何使用 Deployment、StatefulSet、 DaemonSet 等控制器來實現(xiàn)容器的自動擴縮容。其中,Horizontal Pod Autoscaler(HPA)是一種基于 CPU 使用率的自動縮容方案,可以自動調(diào)整 Pod 的數(shù)量,以保證系統(tǒng)的資源利用率和穩(wěn)定性。 一

    2024年02月06日
    瀏覽(16)
  • “利用Python使用API進行數(shù)據(jù)集成和自動化開發(fā)的指南“

    標題:利用Python使用API進行數(shù)據(jù)集成和自動化開發(fā)的指南 摘要:本文將為讀者提供一個詳細而全面的指南,教您如何使用Python編程語言來利用API進行數(shù)據(jù)集成和自動化開發(fā)。我們將介紹API的基本概念,探討Python中常用的API庫和工具,以及演示如何通過編寫Python代碼來調(diào)用和處

    2024年02月13日
    瀏覽(26)
  • RAMMAP(運行內(nèi)存清理工具)自動釋放內(nèi)存,并利用pyqt5制作圖形界面

    RAMMAP(運行內(nèi)存清理工具)自動釋放內(nèi)存,并利用pyqt5制作圖形界面

    上一篇文章用python制作了一個自動清理內(nèi)存的程序,利用cmd端口調(diào)用Rammap,不過只是做了一個托盤圖標,這回用pyqt5做一個簡單的圖形界面,并實現(xiàn)對自動清理模式的一些設(shè)置 首先利用pyqt5工具qt designer生成UI界面,這里命名為F_UI.ui(并利用信號/槽編輯器將滑動條和微調(diào)框綁

    2024年02月11日
    瀏覽(16)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包