2018/8/5/31

基于pointnet网络架构的点云数据准备    http://www.pclcn.org/bbs/forum.php?mod=viewthread&tid=2133

Stanford 2D-3D-Semantics Dataset (2D-3D-S)

http://buildingparser.stanford.edu/dataset.html

PointNet: Deep Learning on Point Sets for 3D Classification and Segmentation解析(PointNet模型) 

https://blog.youkuaiyun.com/hit1524468/article/details/80023779

https://data-sci.info/2017/06/27/run-faster-rcnn-tensorflow-detection-api/

https://blog.youkuaiyun.com/tumi678/article/details/80082216

Tensorflow之池化函数汇总    Tensorflow之池化函数汇总

https://blog.youkuaiyun.com/zsean/article/details/77886689




Tensorflow之池化函数汇总 
Tensorflow之池化函数汇总
<think>需求梳理: 1. 遍历文件夹及其子文件夹,找到所有CSV文件。 2. 从文件名中提取年份和月份(例如,文件名如“201802只数.csv”中的“201802”表示2018年2月)。 3. 对于每个CSV文件,读取其内容,并检查日期列(假设日期列的格式为“2018/1/31”等)。 4. 删除日期列中不属于文件名所表示的年月(例如,文件名是201802,则只保留2018年2月的数据,删除2018年1月、3月等的数据)。 5. 将处理后的数据保存为新的CSV文件,保持原有的文件结构和文件名。 注意:由于原始日期格式可能为“2018/1/31”或“2018/01/31”,而文件名中的月份是两位数字(如02),我们需要将日期列解析并提取年月,然后与文件名中的年月比较。 步骤: 1. 遍历目录,获取所有CSV文件路径。 2. 从文件名中提取年月(例如从“201802只数.csv”中提取“201802”,然后转换成年份2018,月份02,注意月份是两位数)。 3. 读取CSV文件,假设日期列的列名(例如列名为“日期”),如果不是,可能需要用户指定或通过其他方式识别。 4. 将日期列转换为统一的日期格式,并提取出年月(格式化为6位数字字符串,如“201802”)。 5. 过滤出日期列中提取的年月与文件名中的年月一致的行。 6. 将过滤后的数据保存回原路径(或者可以选择保存到另一个目录,但这里要求保持同源文件结构和文件名一致,所以直接覆盖或保存到原位置?注意:为了避免操作失误,建议先保存到临时文件或备份,但需求是保持相同结构和文件名,所以我们可以选择覆盖原文件或保存到相同路径。但为了安全,可以先保存到临时文件,再替换原文件,或者用户可以选择输出到另一个镜像目录)。 但是,需求中要求“新文件保持同源文件结构和文件名一致”,所以我们可以将处理后的文件保存到原路径(覆盖原文件)或者保存到另一个与源目录结构相同的目录。这里为了安全,我们假设用户希望保留原始文件,并将处理后的文件输出到另一个目录(比如源目录的父目录下的一个子目录)。但需求没有明确,所以我们可以提供两种方式。 不过,需求并没有明确说明是否覆盖原文件,为了安全起见,我们可以将处理后的文件保存到另一个目录,比如在源目录的同级创建一个新目录(例如“cleaned_data”),并保持相同的子目录结构。 具体步骤: - 输入:源目录路径,输出目录路径(可选,如果不提供则覆盖原文件,但这样有风险,建议默认不覆盖) - 但是根据需求,新文件保持同源文件结构和文件名一致,所以输出目录的结构应该和源目录一致,只是根目录不同。 方案: 1. 用户指定输入目录(input_dir)和输出目录(output_dir)。 2. 遍历input_dir下的所有csv文件,对于每个文件,获取其相对路径(相对于input_dir)。 3. 在output_dir下创建相同的相对路径目录,确保目录存在。 4. 处理文件,然后保存到output_dir下相应的路径。 如果用户希望覆盖原文件,那么可以将output_dir设置为input_dir,但这样会覆盖原文件,所以需要谨慎。 代码实现: - 使用os.walk遍历目录。 - 使用pandas读取CSV文件(如果文件很大,考虑分块读取,但一般情况可以一次性读取)。 - 日期解析:使用pandas的to_datetime,然后提取年月(格式化为“%Y%m”的字符串)。 - 过滤数据:保留日期列的年月字符串与文件名中的年月字符串相同的行。 注意:文件名中的年月提取,我们假设文件名中有连续的6位数字(如201802)表示年月。但文件名可能不止一组6位数字,我们取哪一组?根据需求,文件名是“201802只数”,所以我们可以提取前6位连续数字作为年月标识。但如果文件名中有多组,我们可以使用正则表达式匹配6位连续数字,并取第一个匹配的作为年月。但需求中明确文件名是“201802只数”,所以我们可以认为文件名的前6位就是年月,或者用正则提取6位数字。 步骤: 1. 使用正则表达式从文件名中提取6位连续数字(如201802),如果没有则跳过该文件(或报错)。 2. 将提取的6位数字作为目标年月(target_year_month)。 处理日期列: 1. 读取CSV文件时,指定日期列的列名(这里假设列名为“日期”,但实际可能不同,需要作为参数传入)。 2. 将日期列转换为datetime对象。 3. 从日期列中提取年月(格式化为6位数字字符串,如201802)。 4. 过滤数据:只保留提取的年月等于target_year_month的行。 代码结构: ```python import os import re import pandas as pd from datetime import datetime def process_csv_files(input_dir, output_dir, date_column='日期'): # 遍历input_dir下的所有文件 for root, dirs, files in os.walk(input_dir): for file in files: if file.endswith('.csv'): input_file_path = os.path.join(root, file) # 获取相对路径,以便在output_dir中创建相同结构 rel_path = os.path.relpath(root, input_dir) output_subdir = os.path.join(output_dir, rel_path) os.makedirs(output_subdir, exist_ok=True) output_file_path = os.path.join(output_subdir, file) # 从文件名中提取6位数字年月 match = re.search(r'(\d{6})', file) if not match: print(f"跳过文件 {file},未找到年月信息。") continue target_year_month = match.group(1) # 如'201802' # 读取CSV文件 try: df = pd.read_csv(input_file_path, encoding='utf-8') # 如果编码失败,可以尝试其他编码,这里为了简单,如果utf-8失败,可以尝试gbk except: try: df = pd.read_csv(input_file_path, encoding='gbk') except Exception as e: print(f"读取文件 {input_file_path} 出错: {e}") continue # 检查日期列是否存在 if date_column not in df.columns: print(f"文件 {input_file_path} 中不存在列名为 '{date_column}' 的列。") continue # 将日期列转换为datetime,错误则设为NaT df[date_column] = pd.to_datetime(df[date_column], errors='coerce') # 删除转换失败的行(即NaT的行) df = df.dropna(subset=[date_column]) # 提取年月(格式化为6位字符串,如201802) df['year_month'] = df[date_column].dt.strftime('%Y%m') # 过滤数据:只保留目标年月的数据 filtered_df = df[df['year_month'] == target_year_month] # 删除辅助列 filtered_df = filtered_df.drop(columns=['year_month']) # 保存到输出文件 try: filtered_df.to_csv(output_file_path, index=False, encoding='utf-8') print(f"已处理并保存: {output_file_path}") except Exception as e: print(f"保存文件 {output_file_path} 出错: {e}") if __name__ == '__main__': input_directory = r"E:\source_data" # 替换为你的输入目录 output_directory = r"E:\cleaned_data" # 替换为你的输出目录 process_csv_files(input_directory, output_directory, date_column='日期') ``` 注意: 1. 这里假设日期列名为“日期”,如果实际列名不同,需要修改date_column参数。 2. 日期解析时,使用了`errors='coerce'`,将无法解析的日期设为NaT,然后删除这些行。 3. 如果文件很大,可以考虑使用分块读取,但这里默认一次性读取。 4. 编码问题:先尝试utf-8,再尝试gbk,如果还不行可以继续添加其他编码,或者让用户指定编码。 相关问题: 1. 如果文件名中有多组6位数字,如何准确提取年月部分? 2. 如果日期列有多种格式(如“2018-1-31”、“20180131”),如何统一解析? 3. 如果文件非常大(比如几个GB),如何优化内存使用? 4. 除了CSV,如果还有其他格式(如Excel)需要处理,如何扩展?</think>### 需求梳理 1. **遍历文件**:递归扫描文件夹及子文件夹中的所有CSV文件 2. **文件名解析**:从文件名提取年月(如 `201802只数.csv` → `201802`) 3. **日期过滤**: - 只保留日期列中与文件名年月匹配的数据(如文件名`201802`只保留2018年2月数据) - 日期格式统一处理(支持`2018/1/31`、`2018-02-01`等变体) 4. **输出要求**: - 保持原始目录结构 - 保持原始文件名 - 只输出过滤后的数据 ### Python实现方案 ```python import os import re import pandas as pd from pathlib import Path def filter_csv_by_date(input_dir, output_dir, date_col="日期"): """ 遍历目录处理CSV文件,保留与文件名年月匹配的数据 :param input_dir: 输入目录路径 :param output_dir: 输出目录路径 :param date_col: CSV文件中日期列的列名 """ # 创建输出目录 Path(output_dir).mkdir(parents=True, exist_ok=True) # 遍历所有CSV文件 for root, _, files in os.walk(input_dir): for file in files: if not file.lower().endswith('.csv'): continue input_path = os.path.join(root, file) rel_path = os.path.relpath(root, input_dir) output_path = os.path.join(output_dir, rel_path, file) # 从文件名提取年月 (6位数字) match = re.search(r'(\d{6})', file) if not match: print(f"⚠️ 跳过无法解析年月的文件: {file}") continue file_ym = match.group(1) # 如 '201802' target_year = int(file_ym[:4]) target_month = int(file_ym[4:6]) try: # 读取CSV文件(自动处理编码) df = pd.read_csv(input_path) # 检查日期列是否存在 if date_col not in df.columns: print(f"🚨 文件缺少日期列 '{date_col}': {file}") continue # 统一日期格式并提取年月 df['temp_date'] = pd.to_datetime( df[date_col], errors='coerce', # 无效日期转为NaT format='mixed' # 自动识别多种格式 ) # 过滤非目标年月的数据 filtered = df[ (df['temp_date'].dt.year == target_year) & (df['temp_date'].dt.month == target_month) ].drop(columns=['temp_date']) # 创建输出目录并保存文件 os.makedirs(os.path.dirname(output_path), exist_ok=True) filtered.to_csv(output_path, index=False, encoding='utf-8-sig') print(f"✅ 处理成功: {file} | 保留 {len(filtered)}/{len(df)} 行") except Exception as e: print(f"🔥 处理失败 {file}: {str(e)}") # 使用示例 if __name__ == "__main__": filter_csv_by_date( input_dir="原始数据路径", output_dir="清洗后数据路径", date_col="日期" # 根据实际列名修改 ) ``` ### 关键代码解释 1. **文件名解析**: ```python re.search(r'(\d{6})', file) # 提取连续6位数字作为年月标识 ``` 2. **日期统一处理**: ```python pd.to_datetime(..., format='mixed') # 自动处理多种日期格式 ``` 3. **年月过滤**: ```python (df['temp_date'].dt.year == target_year) & (df['temp_date'].dt.month == target_month) ``` 4. **目录结构保持**: ```python os.path.relpath(root, input_dir) # 保持原始相对路径 os.makedirs(... exist_ok=True) # 递归创建目录 ``` 5. **安全处理**: - `errors='coerce'`:无效日期转为空值(自动过滤) - `try-except`:捕获文件读取/处理异常 - 编码处理:输出使用`utf-8-sig`解决Excel中文乱码 ### 使用注意事项 1. **日期列名称**:如果实际列名不是"日期",修改`date_col`参数 2. **文件名规范**:文件名必须包含6位连续数字(如`202405数据.csv`) 3. **日期格式支持**:自动处理以下格式: - `2018/1/31` - `2018-02-01` - `2018.2.28` - `01Feb2018` 4. **内存管理**:大文件建议添加`chunksize`参数分块读取 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值