generated from LufsX/Experiment-LogReg
This commit is contained in:
+13
-17
@@ -5,26 +5,22 @@ id = "202210423223"
|
||||
name = "李顺潮"
|
||||
|
||||
def load_data(filepath):
|
||||
"""
|
||||
从文件中加载数据,若不自行正确实现,则无法通过评测,不记录成绩。
|
||||
|
||||
Args:
|
||||
- filepath (str): 数据文件路径。
|
||||
|
||||
Returns:
|
||||
- np.array: 特征数据。
|
||||
- np.array: 标签数据。
|
||||
"""
|
||||
# 使用 pandas 加载数据
|
||||
data = pd.read_csv(filepath, header=None)
|
||||
|
||||
# 替换问号为 NaN,并填充或删除 NaN
|
||||
data.replace('?', np.nan, inplace=True)
|
||||
data.dropna(inplace=True) # 删除包含 NaN 的行
|
||||
|
||||
# 假设最后一列是标签,其余是特征
|
||||
X = data.iloc[:, :-1].values.astype(float) # 转换为浮点数
|
||||
y = data.iloc[:, -1].values.astype(float) # 转换为浮点数
|
||||
# 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)
|
||||
|
||||
|
||||
X = data.iloc[:, :-1].values.astype(float)
|
||||
y = data.iloc[:, -1].values.astype(float)
|
||||
return X, y
|
||||
|
||||
def sigmoid(z):
|
||||
|
||||
Reference in New Issue
Block a user