gpt4 book ai didi

python - 使用 python 解析 CSV 文件(稍后制作决策树)

转载 作者:太空狗 更新时间:2023-10-30 01:47:22 24 4
gpt4 key购买 nike

<分区>

首先,全面披露:这是一项单项作业,所以我不想收到代码。 :).我更在寻找方法;我是 python 的新手,读过一本书但还没有编写任何代码。

整个任务是导入 CSV 文件的内容,根据 CSV 文件的内容创建决策树(使用 ID3 algorithm ),然后解析第二个 CSV 文件以针对树运行。有一个很大的(可以理解的)偏好让它能够处理不同的 CSV 文件(我问我们是否允许对列名进行硬编码,主要是为了消除它的可能性,答案是否定的)。

CSV 文件采用相当标准的格式;标题行标有#,然后显示列名,之后的每一行都是一系列简单的值。示例:

# Column1, Column2, Column3, Column4
Value01, Value02, Value03, Value04
Value11, Value12, Value13, Value14

目前,我正在尝试完成第一部分:解析 CSV。要为决策树做出决策,字典结构似乎是最合乎逻辑的;所以我想按照这些思路做一些事情:

Read in each line, character by character
If the character is not a comma or a space
Append character to temporary string
If the character is a comma
Append the temporary string to a list
Empty string
Once a line has been read
Create a dictionary using the header row as the key (somehow!)
Append that dictionary to a list

但是,如果我那样做,我不确定如何在键和值之间建立映射。我还想知道是否有某种方法可以对列表中的每个字典执行操作,因为我需要做一些事情来达到“每个人都返回列 Column1 和 Column4 的值,所以我可以数谁有什么!” - 我假设存在某种机制,但我不认为我知道该怎么做。

字典是最好的方法吗?使用其他数据结构会更好吗?如果是,是什么?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com