generated from LufsX/Experiment-LogReg
This commit is contained in:
+9
-9
@@ -55,19 +55,19 @@ def predict_logreg(inX, weights):
|
|||||||
# 从文件中加载数据
|
# 从文件中加载数据
|
||||||
def load_data(filepath):
|
def load_data(filepath):
|
||||||
data = pd.read_csv(filepath, header=None, na_values='?', delim_whitespace=True)
|
data = pd.read_csv(filepath, header=None, na_values='?', delim_whitespace=True)
|
||||||
for col in data.columns:
|
|
||||||
if pd.api.types.is_numeric_dtype(data[col]):
|
|
||||||
data[col].fillna(data[col].median(), inplace=True)
|
|
||||||
else: # 处理非数值列,例如用众数填充或其他策略
|
|
||||||
data[col].fillna(data[col].mode()[0], inplace=True)
|
|
||||||
|
|
||||||
X = data.iloc[:, :-1].to_numpy()
|
# 使用SimpleImputer进行中位数填充
|
||||||
y = data.iloc[:, -1].to_numpy()
|
imputer = SimpleImputer(strategy='median')
|
||||||
|
data_imputed = imputer.fit_transform(data)
|
||||||
|
data = pd.DataFrame(data_imputed)
|
||||||
|
|
||||||
|
X = data.iloc[:, :-1].to_numpy() # 特征
|
||||||
|
y = data.iloc[:, -1].to_numpy() # 标签
|
||||||
|
# 使用MinMaxScaler进行特征缩放
|
||||||
scaler = MinMaxScaler()
|
scaler = MinMaxScaler()
|
||||||
X = scaler.fit_transform(X) # 缩放特征
|
X = scaler.fit_transform(X)
|
||||||
|
|
||||||
return X, y
|
return X, y
|
||||||
|
|
||||||
# 准确率计算函数
|
# 准确率计算函数
|
||||||
def calculate_accuracy(X, y, weights):
|
def calculate_accuracy(X, y, weights):
|
||||||
predictions = np.array([predict_logreg(x, weights) for x in X])
|
predictions = np.array([predict_logreg(x, weights) for x in X])
|
||||||
|
|||||||
Reference in New Issue
Block a user