## 2017年2月14日 Logistic Regression 原

airxiechao

Logistic Regression assumes that the probability of the dependent varaiable y equaling a positive case can be interpreted as logistic function

We can now defind the cost function as

To minimize cost function, gradient descent method can be used
The process begins with computing the derivative of cost function

Then improved by repeating

``````import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from pylab import scatter, show, legend, xlabel, ylabel

y = df["label"].as_matrix()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5)

# train scikit learn model
clf = LogisticRegression()
clf.fit(X_train,y_train)
print 'scikit score:', clf.score(X_test, y_test)
#scikit score: 0.9

# visualize data
def visualize(X, y):
pos = np.where(y == 1)
neg = np.where(y == 0)
scatter(X[pos, 0], X[pos, 1], marker='o', c='b')
scatter(X[neg, 0], X[neg, 1], marker='x', c='r')
xlabel('Exam 1 score')
ylabel('Exam 2 score')
show()

visualize(X, y)``````

``````class myLogisticRegression:

def sigmoid(self, x):
return 1.0/(1.0+np.exp(-x))

def logistic(self, X, theta):
return self.sigmoid(np.dot(X, theta))

def cost(self, X, y, theta):
m = X.shape[0]
return -1.0 / m * (np.dot(y, np.log(self.logistic(X, theta))) + np.dot((1-y), (1-self.logistic(X, theta))))

def derivative_of_cost(self, X, y, theta):
m = X.shape[0]
return 1.0 / m * np.dot(self.logistic(X, theta)-y, X)

def gradient_descent(self, X, y, theta, alpha):
return theta - alpha * self.derivative_of_cost(X, y, theta)

def fit(self, X, y, alpha, num_iter):
self.theta = np.zeros(X.shape[1])
for i in range(num_iter):
self.theta = self.gradient_descent(X, y, self.theta, alpha)

def predict(self, X):
return self.logistic(X, self.theta)

def score(self, X, y):
return 1.0 - 1.0 * np.count_nonzero(np.round(self.predict(X)) - y) / len(y)

lr = myLogisticRegression()
lr.fit(X_train, y_train, .1, 1000)
print 'score:', lr.score(X_test, y_test)
#score: 0.86``````

### airxiechao

logistic regression识别真假币

robin_Xu_shuai
2017/10/31
0
0
TensorFlow入门——Softmax Regression

Softmax Regression是Logistic回归在多分类上的推广，对于Logistic回归以及Softmax Regression的详细介绍可以参见： 简单易学的机器学习算法——Logistic回归 利用Theano理解深度学习——Log...

2018/04/18
0
0

2017年进九时间 2017进九的日子是12月22日，其实也就是“冬至日”。天文专家介绍说，“九九”是我国北方特别是黄河中下游地区更为适用的一种杂节气。 2017~2018年数九时间表 一九 2017年12月...

anlve
2018/01/27
0
0
Machine Learning－week 3

Question 1 Suppose that you have trained a logistic regression classifier, and it outputs on a new examplex a prediction hθ(x) = 0.7. This means (check all that apply): Our est......

2017/04/10
0
0

2017/11/10
0
0

10
0

（手机横屏看源码更方便） 注：java源码分析部分如无特殊说明均基于 java8 版本。 简介 大家都知道线程是有生命周期，但是彤哥可以认真负责地告诉你网上几乎没有一篇文章讲得是完全正确的。 ...

12
0
jquery--DOM操作基础

5
0
Django的ChoiceField和MultipleChoiceField错误提示，选择一个有效的选项

10
0
Vue核心概念及特性 (一)

Vue (读音 /vjuː/，类似于 view) 是一套用于构建用户界面的渐进式框架。 > 特点: 易用，灵活，高效，渐进式框架。 > 可以随意组合需要用到的模块 vue + components + vue-router + vuex + v...

14
0