import numpy as np
import matplotlib.pyplot as plt from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from sklearn import datasets, linear_model
from sklearn.model_selection import train_test_split def load_data():
# 使用 scikit-learn 自带的 iris 数据集
iris=datasets.load_iris()
X_train=iris.data
y_train=iris.target
return train_test_split(X_train, y_train,test_size=0.25,random_state=0,stratify=y_train) #逻辑回归
def test_LogisticRegression(*data):
X_train,X_test,y_train,y_test=data
regr = linear_model.LogisticRegression()
regr.fit(X_train, y_train)
print('Coefficients:%s, intercept %s'%(regr.coef_,regr.intercept_))
print('Score: %.2f' % regr.score(X_test, y_test)) # 加载用于分类的数据集
X_train,X_test,y_train,y_test=load_data()
# 调用 test_LogisticRegression
test_LogisticRegression(X_train,X_test,y_train,y_test) def test_LogisticRegression_multinomial(*data):
'''
测试 LogisticRegression 的预测性能随 multi_class 参数的影响
'''
X_train,X_test,y_train,y_test=data
regr = linear_model.LogisticRegression(multi_class='multinomial',solver='lbfgs')
regr.fit(X_train, y_train)
print('Coefficients:%s, intercept %s'%(regr.coef_,regr.intercept_))
print('Score: %.2f' % regr.score(X_test, y_test)) # 调用 test_LogisticRegression_multinomial
test_LogisticRegression_multinomial(X_train,X_test,y_train,y_test) def test_LogisticRegression_C(*data):
'''
测试 LogisticRegression 的预测性能随 C 参数的影响
'''
X_train,X_test,y_train,y_test=data
Cs=np.logspace(-2,4,num=100)
scores=[]
for C in Cs:
regr = linear_model.LogisticRegression(C=C)
regr.fit(X_train, y_train)
scores.append(regr.score(X_test, y_test))
## 绘图
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.plot(Cs,scores)
ax.set_xlabel(r"C")
ax.set_ylabel(r"score")
ax.set_xscale('log')
ax.set_title("LogisticRegression")
plt.show() # 调用 test_LogisticRegression_C
test_LogisticRegression_C(X_train,X_test,y_train,y_test)

吴裕雄 python 机器学习——逻辑回归的更多相关文章

  1. 吴裕雄 python 机器学习——ElasticNet回归

    import numpy as np import matplotlib.pyplot as plt from matplotlib import cm from mpl_toolkits.mplot ...

  2. 吴裕雄 python 机器学习——Lasso回归

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model from s ...

  3. 吴裕雄 python 机器学习——岭回归

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model from s ...

  4. 吴裕雄 python 机器学习——KNN回归KNeighborsRegressor模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import neighbors, datasets from skle ...

  5. python机器学习-逻辑回归

    1.逻辑函数 假设数据集有n个独立的特征,x1到xn为样本的n个特征.常规的回归算法的目标是拟合出一个多项式函数,使得预测值与真实值的误差最小: 而我们希望这样的f(x)能够具有很好的逻辑判断性质,最 ...

  6. python机器学习——逻辑回归

    我们知道感知器算法对于不能完全线性分割的数据是无能为力的,在这一篇将会介绍另一种非常有效的二分类模型--逻辑回归.在分类任务中,它被广泛使用 逻辑回归是一个分类模型,在实现之前我们先介绍几个概念: 几 ...

  7. 吴裕雄 python 机器学习——回归决策树模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_s ...

  8. 吴裕雄 python 机器学习——集成学习随机森林RandomForestRegressor回归模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets,ensemble from sklear ...

  9. 吴裕雄 python 机器学习——集成学习梯度提升决策树GradientBoostingRegressor回归模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets,ensemble from sklear ...

随机推荐

  1. layui select 选完其他选项, 手工清空选项 又恢复最初的选项?

    启用layui的select  下拉搜索项: lay-search <div class="layui-inline"> <label class="l ...

  2. JSON和JSONP,浅析JSONP解决AJAX跨域问题

    说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决?这两个问题目前都有不同的解决方案,比如数据可以用自定义字符串或者用XML来描述,跨域可以通过服 ...

  3. Ignite(三): Ignite VS Spark

    参考:https://www.itcodemonkey.com/article/9613.html gnite 和 Spark,如果笼统归类,都可以归于内存计算平台,然而两者功能上虽然有交集,并且 I ...

  4. Solr使用访问地址控制索引的,删除、创建

    启动Solr,删除全部索引数据: http://localhost:8080/solr/update/?stream.body=<delete><query>*:*</q ...

  5. 玩转MQTT-阿里云之MQTT使用

    引言 前两年买一款物联网控制板,当时把玩了一阵之后,验证了下串口通讯.MODBUS协议实现.TCP/UDP/DNS/MQTT通讯(基于GPRS)后,就放到角落里吃灰了呵. 最近收到阿里云的优惠推送,说 ...

  6. ZooKeeper是按照CP原则构建的,不适合做Service服务发现

    一.cap 分布式领域中存在CAP理论,且该理论已被证明:任何分布式系统只可同时满足两点,无法三者兼顾. ①C:Consistency,一致性,数据一致更新,所有数据变动都是同步的. ②A:Avail ...

  7. sklearn错误

    1.No module named 'sklearn.cross_validation' sklearn.cross_validation会报错,关键在于新版本的sklearn没有cross_vali ...

  8. Emacs下scheme编程环境的设置

    Scheme编程环境搭建 1.1 安装Chez Scheme git clone https://github.com/cisco/ChezScheme.git cd ChezScheme ./con ...

  9. 在写WebApi判断用户权限时返回数据和接受支付结果 定义返回数据类型

    using ADT.Core.Encrypt; using System; using System.Collections.Generic; using System.Linq; using Sys ...

  10. List集合去重

    本篇包含了两种去重,一种是List集合去重,一种是两个List集合去重合并 List集合去重,一般是两种方式,一种是遍历list集合判断后赋给另一个list集合,一种是用赋给set集合再返回给list ...