generated from Python-2026Spring/assignment-05-final-project-template
init: 提交电信流失预测代码
This commit is contained in:
parent
b77ff15e43
commit
b46c65ac73
0
requirements.txt
Normal file
0
requirements.txt
Normal file
0
src/__init__.py
Normal file
0
src/__init__.py
Normal file
54
src/data.py
Normal file
54
src/data.py
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
"""电信客户流失数据集加载与清洗(最终可运行版)"""
|
||||||
|
import polars as pl
|
||||||
|
import os # 用于检查文件是否存在
|
||||||
|
|
||||||
|
def load_telco_data():
|
||||||
|
"""加载并清洗电信流失数据集,绝对路径+完整容错"""
|
||||||
|
# ========== 1. 正确的绝对路径(二选一即可) ==========
|
||||||
|
# 方式1:双反斜杠(推荐)
|
||||||
|
data_path = "C:\\Users\\s1313\\Desktop\\telco_churn_analysis\\data\\WA_Fn-UseC_-Telco-Customer-Churn.csv"
|
||||||
|
# 方式2:原始字符串(注释掉方式1,解开下面注释也可以)
|
||||||
|
# data_path = r"C:\Users\s1313\Desktop\telco_churn_analysis\data\WA_Fn-UseC_-Telco-Customer-Churn.csv"
|
||||||
|
|
||||||
|
# ========== 2. 检查文件是否存在(关键) ==========
|
||||||
|
if not os.path.exists(data_path):
|
||||||
|
print(f"\n❌ 错误:文件不存在!")
|
||||||
|
print(f"👉 请检查路径是否正确:{data_path}")
|
||||||
|
print(f"👉 确认文件是否在这个位置,且文件名没有写错")
|
||||||
|
return None # 避免程序崩溃
|
||||||
|
|
||||||
|
# ========== 3. 读取并清洗数据 ==========
|
||||||
|
try:
|
||||||
|
df = pl.read_csv(data_path)
|
||||||
|
|
||||||
|
# 安全清洗TotalCharges字段
|
||||||
|
if df["TotalCharges"].dtype == pl.Utf8:
|
||||||
|
df = df.with_columns(
|
||||||
|
pl.col("TotalCharges")
|
||||||
|
.str.replace(" ", "0")
|
||||||
|
.cast(pl.Float64, strict=False)
|
||||||
|
.fill_null(0.0)
|
||||||
|
.alias("TotalCharges")
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
df = df.with_columns(
|
||||||
|
pl.col("TotalCharges")
|
||||||
|
.fill_null(0.0)
|
||||||
|
.alias("TotalCharges")
|
||||||
|
)
|
||||||
|
|
||||||
|
# ========== 4. 输出成功结果 ==========
|
||||||
|
print("\n✅ 数据集加载并清洗完成!")
|
||||||
|
print(f"📊 数据规模:{df.shape[0]}行 × {df.shape[1]}列")
|
||||||
|
print(f"📈 TotalCharges字段类型:{df['TotalCharges'].dtype}")
|
||||||
|
print("🔍 前2行预览:")
|
||||||
|
print(df.head(2))
|
||||||
|
return df
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\n❌ 数据处理出错:{type(e).__name__} → {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
# 测试入口
|
||||||
|
if __name__ == "__main__":
|
||||||
|
load_telco_data()
|
||||||
0
src/features.py
Normal file
0
src/features.py
Normal file
0
src/infer.py
Normal file
0
src/infer.py
Normal file
0
src/train.py
Normal file
0
src/train.py
Normal file
Loading…
Reference in New Issue
Block a user