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