# Logistic regression intuition and conditional probabilities使用sc-learn训练logistic regression 模型使用正则化（r

logit函数输入参与p属于（0,1），函数值为整个实数域，可以在特征值与逻辑比率之间建立线性关系

### 似然函数

y=1或者y=0

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.001, 0.999, 100)
plt.plot(x, -np.log(x),'b', label='y=1')
plt.plot(x, -np.log(1-x), 'r--', label='y=0')
plt.ylim(0, 5)
plt.xlim(0, 1)
plt.xlabel('$\phi(x)$')
plt.ylabel('$J(w)$')
plt.legend()
plt.show()

# 使用sc-learn训练logistic regression 模型

from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.cross_validation import train_test_split
import numpy as np
import matplotlib
from sklearn import datasets
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
from matplotlib.colors import ListedColormap
# print iris
X = iris.data[:, [2, 3]]
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

sc = StandardScaler()
sc.fit(X_train)
X_train_std = sc.transform(X_train)
# 测试集做同样的标准化，就是对测试集做相同的平移伸缩操作
X_test_std = sc.transform(X_test)
'''sc.scale_标准差, sc.mean_平均值, sc.var_方差'''

lr = LogisticRegression(C=1000.0, random_state=0)
lr.fit(X_train_std, y_train)

# 预测
y_pred = lr.predict(X_test_std)

print('Misclassified samples: %d' % (y_test != y_pred).sum())
print('Accuracy: %.2f' % accuracy_score(y_test, y_pred))

x1_min, x1_max = X_train_std[:, 0].min() - 1, X_train_std[:, 0].max() + 1
x2_min, x2_max = X_train_std[:, 1].min() - 1, X_train_std[:, 1].max() + 1

resolution = 0.01
# xx1 X轴，每一个横都是x的分布，所以每一列元素一样，xx2 y轴 每一列y分布，所以每一横元素一样
xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),np.arange(x2_min, x2_max, resolution))

# .ravel() 函数是将多维数组降位一维，注意是原数组的视图，转置之后成为两列元素
z = lr.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
'''
contourf画登高线函数要求 *X* and *Y* must both be 2-D with the same shape as *Z*, or they
must both be 1-D such that len(X) is the number of columns in
*Z* and len(Y) is the number of rows in *Z*.
'''
#　ｚ形状要做调整
z = z.reshape(xx1.shape)

# 填充等高线的颜色, 8是等高线分为几部分
markers = ('s', 'x', 'o', '^', 'v')
colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
cmap = ListedColormap(colors[:len(np.unique(y))])

for i, value in enumerate(np.unique(y)):
temp = X_train_std[np.where(y_train==value)]
plt.scatter(x=temp[:,0],y=temp[:,1], marker=markers[value],s=69, c=colors[value], label=value)

plt.scatter(x=X_test_std[:, 0],y=X_test_std[:,1], marker= 'o',s=69, c='none', edgecolors='r', label='test test')

plt.xlim(xx1.min(), xx1.max())
plt.ylim(xx2.min(), xx2.max())
plt.xlabel('petal length [standardized]')
plt.ylabel('petal width [standardized]')
plt.contourf(xx1, xx2, z, len(np.unique(y)), alpha = 0.4, cmap = cmap)
plt.legend(loc='upper left')
plt.show()

# 使用正则化（regularization）处理过拟合（overftting ）

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

xmajorLocator = MultipleLocator(2)
xminorLocator = MultipleLocator(1)
ymajorLocator = MultipleLocator(0.5)

N = 7
x = np.linspace(-N, N, 100)
z = 1/(1+np.e**(-x))
fig = plt.figure(1)
axes = plt.subplot(111)

plt.ylim(-0.1, 1.1)
plt.axvline(0.0, color='k')
plt.axhline(0, ls='dotted',color='r')
plt.axhline(0.5, ls='dotted',color='r')
plt.axhline(1, ls='dotted',color='r')
#plt.axhspan(0.0,1.0, facecolor='1.0', alpha=1.0, ls='dotted')
axes.xaxis.set_major_locator(xmajorLocator)
axes.xaxis.set_minor_locator(xminorLocator)
axes.yaxis.set_major_locator(ymajorLocator)
plt.xlabel("z")
plt.ylabel("$\phi (z)$")
plt.plot(x, z)
plt.show()

95 篇文章31 人订阅

0 条评论

## 相关文章

46470

11610

15920

60860

29260

### 【学术】谷歌AI课程附带的机器学习术语整理（超详细！）

AiTechYun 编辑：xiaoshan 为了帮助大家更好的了解机器学习，谷歌在上周推出了一系列免费的AI课程，同时还附带了一个详细地机器学习术语库。 本术语...

35870

12920

32230

35260

11120