generated from LufsX/Experiment-LogReg
This commit is contained in:
+8
-21
@@ -32,10 +32,9 @@ from sklearn.preprocessing import StandardScaler
|
|||||||
def sigmoid(intX):
|
def sigmoid(intX):
|
||||||
return 1.0 / (1 + np.exp(-intX))
|
return 1.0 / (1 + np.exp(-intX))
|
||||||
|
|
||||||
|
|
||||||
def train_logreg(X, y, numIter=150):
|
def train_logreg(X, y, numIter=150):
|
||||||
m, n = X.shape
|
m, n = X.shape
|
||||||
weights = np.ones(n)
|
weights = np.zeros(n) # 初始化权重为0而不是1
|
||||||
for j in range(numIter):
|
for j in range(numIter):
|
||||||
dataIndex = list(range(m))
|
dataIndex = list(range(m))
|
||||||
for i in range(m):
|
for i in range(m):
|
||||||
@@ -43,11 +42,10 @@ def train_logreg(X, y, numIter=150):
|
|||||||
randIndex = int(np.random.uniform(0, len(dataIndex)))
|
randIndex = int(np.random.uniform(0, len(dataIndex)))
|
||||||
h = sigmoid(np.dot(X[randIndex], weights))
|
h = sigmoid(np.dot(X[randIndex], weights))
|
||||||
error = y[randIndex] - h
|
error = y[randIndex] - h
|
||||||
weights = weights + np.dot(alpha * error, X[randIndex])
|
weights = weights + alpha * error * X[randIndex]
|
||||||
del(dataIndex[randIndex])
|
del(dataIndex[randIndex])
|
||||||
return weights
|
return weights
|
||||||
|
|
||||||
|
|
||||||
def predict_logreg(X, weights):
|
def predict_logreg(X, weights):
|
||||||
predictions = []
|
predictions = []
|
||||||
for x in X:
|
for x in X:
|
||||||
@@ -55,7 +53,6 @@ def predict_logreg(X, weights):
|
|||||||
predictions.append(1 if prob > 0.5 else 0)
|
predictions.append(1 if prob > 0.5 else 0)
|
||||||
return predictions
|
return predictions
|
||||||
|
|
||||||
|
|
||||||
def load_data(filepath):
|
def load_data(filepath):
|
||||||
data = []
|
data = []
|
||||||
labels = []
|
labels = []
|
||||||
@@ -70,29 +67,19 @@ def load_data(filepath):
|
|||||||
data = np.array(data)
|
data = np.array(data)
|
||||||
labels = np.array(labels)
|
labels = np.array(labels)
|
||||||
target_variable = labels
|
target_variable = labels
|
||||||
target_variable[(target_variable == 2) | (target_variable == 3)] = 0
|
target_variable[np.logical_or(target_variable == 2, target_variable == 3)] = 0
|
||||||
data = np.delete(data, [2] + list(range(23, 28)), axis=1)
|
data = np.delete(data, [2] + list(range(23, 28)), axis=1)
|
||||||
scaler = StandardScaler()
|
scaler = StandardScaler()
|
||||||
data = scaler.fit_transform(data)
|
data = scaler.fit_transform(data)
|
||||||
return data, target_variable
|
return data, target_variable
|
||||||
|
|
||||||
|
|
||||||
def accuracy(X, y, weights):
|
def accuracy(X, y, weights):
|
||||||
predictions = predict_logreg(X, weights)
|
predictions = predict_logreg(X, weights)
|
||||||
return np.mean(predictions == y)
|
return np.mean(predictions == y)
|
||||||
|
|
||||||
|
|
||||||
# 以下代码可用于测试,可自行随意修改
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# 加载训练集
|
X_train, y_train = load_data("./horse-colic.data")
|
||||||
X_train, y_train = load_data("./data/horse-colic.data")
|
X_test, y_test = load_data("./horse-colic.test")
|
||||||
|
final_weights = train_logreg(X_train, y_train)
|
||||||
# 加载测试集
|
final_accuracy = accuracy(X_test, y_test, final_weights)
|
||||||
X_test, y_test = load_data("./data/horse-colic.test")
|
print(f"最终测试集的准确率: {final_accuracy:.4f}")
|
||||||
|
|
||||||
# 训练对率回归模型
|
|
||||||
weights = train_logreg(X_train, y_train)
|
|
||||||
|
|
||||||
# 测试单个数据
|
|
||||||
y_pred = predict_logreg(X_test[11], weights)
|
|
||||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user