generated from LufsX/Experiment-LogReg
This commit is contained in:
+24
-6
@@ -5,23 +5,41 @@ id = "202210423223"
|
||||
name = "李顺潮"
|
||||
|
||||
def load_data(filepath):
|
||||
data = pd.read_table(filepath, header=None, sep=' ')
|
||||
"""
|
||||
从文件中加载数据,处理缺失值和数据类型。
|
||||
|
||||
Args:
|
||||
filepath (str): 数据文件路径。
|
||||
|
||||
Returns:
|
||||
tuple: 特征数据 (np.array) 和标签数据 (np.array)。 返回None如果加载失败。
|
||||
"""
|
||||
try:
|
||||
# 使用正则表达式作为分隔符,更灵活地处理多个空格
|
||||
data = pd.read_csv(filepath, header=None, delim_whitespace=True)
|
||||
|
||||
# 替换问号为 NaN
|
||||
data.replace('?', np.nan, inplace=True)
|
||||
|
||||
# Identify numeric columns
|
||||
# 填充数值型缺失值,这里使用中位数填充
|
||||
numeric_cols = data.select_dtypes(include=np.number).columns
|
||||
|
||||
# Fill numeric missing values with median
|
||||
for col in numeric_cols:
|
||||
data[col].fillna(data[col].median(), inplace=True)
|
||||
|
||||
# Drop rows with non-numeric values in any column (a more strict approach)
|
||||
data.dropna(subset=numeric_cols, inplace=True)
|
||||
# 删除包含非数值型缺失值的列 (如果存在)
|
||||
data.dropna(inplace=True)
|
||||
|
||||
|
||||
# 假设最后一列是标签,其余是特征
|
||||
X = data.iloc[:, :-1].values.astype(float)
|
||||
y = data.iloc[:, -1].values.astype(float)
|
||||
return X, y
|
||||
except pd.errors.ParserError as e:
|
||||
print(f"数据加载错误: {e}")
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"发生未知错误: {e}")
|
||||
return None
|
||||
|
||||
def sigmoid(z):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user