generated from LufsX/Experiment-LogReg
This commit is contained in:
+6
-14
@@ -3,8 +3,6 @@ import numpy as np
|
||||
import pandas as pd
|
||||
from sklearn.impute import SimpleImputer
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
from sklearn.utils.class_weight import compute_class_weight
|
||||
from imblearn.over_sampling import SMOTE
|
||||
|
||||
"""
|
||||
实验说明:
|
||||
@@ -31,12 +29,10 @@ name = "陈瑞琪祥"
|
||||
|
||||
|
||||
# 这里写自己的代码实现
|
||||
|
||||
|
||||
def sigmoid(x):
|
||||
return 1 / (1 + np.exp(-x))
|
||||
|
||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization=0.1):
|
||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization=0.01):
|
||||
m, n = X.shape
|
||||
weights = np.zeros(n)
|
||||
for _ in range(iterations):
|
||||
@@ -56,6 +52,10 @@ def preprocess_data(X, y):
|
||||
y[y == 2] = 0
|
||||
y[y == 3] = 0
|
||||
|
||||
# 删除特征3和特征24~28
|
||||
X = np.delete(X, [3], axis=1)
|
||||
X = X[:, :23]
|
||||
|
||||
return X, y
|
||||
|
||||
def load_data(filepath):
|
||||
@@ -96,14 +96,6 @@ def main():
|
||||
X_train = scaler.fit_transform(X_train)
|
||||
X_test = scaler.transform(X_test)
|
||||
|
||||
# 使用SMOTE处理类别不平衡
|
||||
smote = SMOTE(random_state=42)
|
||||
X_train, y_train = smote.fit_resample(X_train, y_train)
|
||||
|
||||
# 计算类别权重
|
||||
class_weights = compute_class_weight('balanced', classes=np.unique(y_train), y=y_train)
|
||||
class_weights = dict(enumerate(class_weights))
|
||||
|
||||
# 训练模型
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000, regularization=0.1)
|
||||
|
||||
@@ -113,6 +105,6 @@ def main():
|
||||
# 计算准确率
|
||||
accuracy = np.mean(y_pred == y_test)
|
||||
print(f"准确率: {accuracy}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user