17 篇文章 6 订阅

# 逻辑回归——详解

https://towardsdatascience.com/logistic-regression-detailed-overview-46c4da4303bc

• 预测一个邮件是否是垃圾邮件
• 判断肿瘤是否是恶性的

。。。上面这段在说啥啊歪果仁的思维搞不懂，难道是我翻译水平问题？

Consider a scenario where we need to classify whether an email is spam or not. If we use linear regression for this problem, there is a need for setting up a threshold based on which classification can be done. Say if the actual class is malignant, predicted continuous value 0.4 and the threshold value is 0.5, the data point will be classified as not malignant which can lead to serious consequence in real time.

From this example, it can be inferred that linear regression is not suitable for classification problem. Linear regression is unbounded, and this brings logistic regression into picture. Their value strictly ranges from 0 to 1.

## 简单逻辑回归

### 模型

Output = 0 or 1

hΘ(x) = sigmoid (Z)

Sigmoid 函数：

### 假设分析

X = [x0 x1] = [1 IP-Address]

## Python 实现

def weightInitialization(n_features):
w = np.zeros((1,n_features))
b = 0
return w,b
def sigmoid_activation(result):
final_result = 1/(1+np.exp(-result))
return final_result

def model_optimize(w, b, X, Y):
m = X.shape[0]

#Prediction
final_result = sigmoid_activation(np.dot(w,X.T)+b)
Y_T = Y.T
cost = (-1/m)*(np.sum((Y_T*np.log(final_result)) + ((1-Y_T)*(np.log(1-final_result)))))
#

dw = (1/m)*(np.dot(X.T, (final_result-Y.T).T))
db = (1/m)*(np.sum(final_result-Y.T))

grads = {"dw": dw, "db": db}

def model_predict(w, b, X, Y, learning_rate, no_iterations):
costs = []
for i in range(no_iterations):
#
#
#weight update
w = w - (learning_rate * (dw.T))
b = b - (learning_rate * db)
#

if (i % 100 == 0):
costs.append(cost)
#print("Cost after %i iteration is %f" %(i, cost))

#final parameters
coeff = {"w": w, "b": b}
gradient = {"dw": dw, "db": db}

def predict(final_pred, m):
y_pred = np.zeros((1,m))
for i in range(final_pred.shape[1]):
if final_pred[0][i] > 0.5:
y_pred[0][i] = 1
return y_pred


## 成本与迭代次数的关系

https://github.com/SSaishruthi/LogisticRegression_Vectorized_Implementation/blob/master/Logistic_Regression.ipynb

• 11
点赞
• 0
评论
• 9
收藏
• 一键三连
• 扫一扫，分享海报

06-21

10-29 23万+
03-25 778
11-10 22万+
07-16 29万+
11-13 8万+
03-02 1603
10-29 6万+
10-27 5566
07-30 12万+
09-24 1239
11-30 10万+