generated from LufsX/Experiment-LogReg
This commit is contained in:
+19
-2
@@ -1,6 +1,7 @@
|
||||
# function.py
|
||||
import numpy as np
|
||||
import random
|
||||
import pandas as pd
|
||||
|
||||
"""
|
||||
实验说明:
|
||||
@@ -27,8 +28,8 @@ name = "万子玉"
|
||||
|
||||
|
||||
# 这里写自己的代码实现
|
||||
def sigmoid(z):
|
||||
return 1 / (1 + np.exp(-z))
|
||||
def sigmoid(inx):
|
||||
return 1 / (1 + np.exp(-inx))
|
||||
|
||||
def train_logreg(X, y, learning_rate=0.001, iterations=500, batch_size=16, initial_weights=None):
|
||||
X = np.insert(X, 0, 1, axis=1) # 添加偏置项
|
||||
@@ -140,6 +141,22 @@ def load_data(filepath):
|
||||
y = [row[-1] for row in data]
|
||||
|
||||
return np.array(X), np.array(y)
|
||||
|
||||
data = pd.read_csv(filepath, header=None, delimiter=' ')
|
||||
return data
|
||||
|
||||
# 标准化函数
|
||||
def regularize(xMat):
|
||||
inMat = xMat.copy()
|
||||
inMeans = np.mean(inMat, axis=0)
|
||||
inVar = np.std(inMat, axis=0)
|
||||
inMat = (inMat - inMeans) / inVar
|
||||
return inMat
|
||||
|
||||
def classify(inx, weights):
|
||||
p = sigmoid(np.dot(inx, weights))
|
||||
return 1 if p >= 0.5 else 0
|
||||
|
||||
"""
|
||||
从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。
|
||||
|
||||
|
||||
Reference in New Issue
Block a user