博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
05机器学习实战之Logistic 回归scikit-learn实现
阅读量:6954 次
发布时间:2019-06-27

本文共 2657 字,大约阅读时间需要 8 分钟。

似然函数

 原理:极大似然估计是建立在极大似然原理的基础上的一个统计方法,是概率论在统计学中的应用。极大似然估计提供了一种给定观察数据来评估模型参数的方法,即:“模型已定,参数未知”。通过若干次试验,观察其结果,利用试验结果得到某个参数值能够使样本出现的概率为最大,则称为极大似然估计。

    由于样本集中的样本都是独立同分布,可以只考虑一类样本集D,来估计参数向量θ。记已知的样本集为:

 似然函数(linkehood function):联合概率密度函数称为相对于的θ的似然函数。

对于似然函数的定义有些不正确,只看求导过程的推导

 

 

In [7]:
import numpy as npimport matplotlib.pyplot as pltnp.random.seed(666)X = np.random.normal(0, 1, size=(200, 2))y = np.array(X[:, 0] ** 2 + X[:, 1] < 1.5, dtype='int')for _ in range(20):    y[np.random.randint(200)] = 1  # 生成噪音数据plt.scatter(X[y == 0, 0], X[y == 0, 1])plt.scatter(X[y == 1, 0], X[y == 1, 1])plt.show()
 
In [25]:
from sklearn.model_selection import train_test_splitX_train, X_test, y_train, y_test = train_test_split(X, y, random_state=666)
In [26]:
from sklearn.linear_model import LogisticRegressionfrom sklearn.preprocessing import PolynomialFeaturesfrom sklearn.pipeline import Pipelinelog_reg = LogisticRegression(solver='lbfgs')log_reg.fit(X_train, y_train)
Out[26]:
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,          intercept_scaling=1, max_iter=100, multi_class='warn',          n_jobs=None, penalty='l2', random_state=None, solver='lbfgs',          tol=0.0001, verbose=0, warm_start=False)
In [27]:
log_reg.score(X_train, y_train)
Out[27]:
0.7933333333333333
In [28]:
log_reg.score(X_test, y_test)
Out[28]:
0.86
In [34]:
def plot_decision_boundary(model, axis):    x0, x1 = np.meshgrid(        np.linspace(axis[0], axis[1], int((axis[1] - axis[0]) * 100)),        np.linspace(axis[2], axis[3], int((axis[3] - axis[2]) * 100))    )    X_new = np.c_[x0.ravel(), x1.ravel()]    y_predict = model.predict(X_new)    zz = y_predict.reshape(x0.shape)    from matplotlib.colors import ListedColormap    custom_cmap = ListedColormap(['#EF9A9A', '#FFF59D', '#90CAF9'])    plt.contourf(x0, x1, zz, cmap=custom_cmap)
In [35]:
plot_decision_boundary(log_reg,axis=[-4,4,-4,4])plt.scatter(X[y == 0, 0], X[y == 0, 1])plt.scatter(X[y == 1, 0], X[y == 1, 1])plt.show()
 
 

多项式特征应用于逻辑回归

In [38]:
from sklearn.preprocessing import StandardScalerdef PolynomialLogisticRegression(degree):    return Pipeline([        ('Poly', PolynomialFeatures(degree=degree)),        ('std_scaler', StandardScaler()),        ('Logistic', LogisticRegression(solver='lbfgs'))    ])log_reg2 = PolynomialLogisticRegression(2)log_reg2.fit(X_train, y_train)log_reg2.score(X_train, y_train)
Out[38]:
0.9066666666666666
In [39]:
log_reg2.score(X_test, y_test)
Out[39]:
0.94
In [40]:
plot_decision_boundary(log_reg2, axis=[-4, 4, -4, 4])plt.scatter(X[y == 0, 0], X[y == 0, 1])plt.scatter(X[y == 1, 0], X[y == 1, 1])plt.show()
 

转载于:https://www.cnblogs.com/xinmomoyan/p/10506929.html

你可能感兴趣的文章
[转] Windows Server 2012 Beta (Hyper-V 3.0) VM Replica與Live Migration Winout Shared Storage
查看>>
枚举 enum
查看>>
JavaScript 学习笔记
查看>>
C# 文件读写系列三
查看>>
让Android的输入框与文本框带滚动条ScrollView
查看>>
基于jQuery或Zepto实现实时监控用户浏览信息
查看>>
【高德地图API】如何打造十月妈咪品牌地图?
查看>>
laravel Ajax请求 X-CSRF验证问题
查看>>
deb包的安装及dpkg命令小结
查看>>
网站程序 Bin目录下 dll无法删除,删除并编译后自动重新引用的解决方法
查看>>
git配置
查看>>
centos7下mongoDB安装和配置
查看>>
强化学习——从最简单的开始入手
查看>>
KVC 与 KVO 理解
查看>>
Python 学习第一周
查看>>
poj1691 Painting A Board
查看>>
maven ...../.m2/settings.xml
查看>>
第六周作业
查看>>
jquery连续滚动
查看>>
洛谷——P1744 采购特价商品
查看>>