神经网络——基于sklearn的参数介绍及应用
一、MLPClassifier&MLPRegressor参数和方法
参数说明(分类和回归参数一致):
hidden_layer_sizes :例如hidden_layer_sizes=(50, 50),表示有两层隐藏层,第一层隐藏层有50个神经元,第二层也有50个神经元。
activation :激活函数,{‘identity’, ‘logistic’, ‘tanh’, ‘relu’}, 默认relu
identity:f(x) = x
logistic:其实就是sigmod,f(x) = 1 / (1 + exp(-x)).
tanh:f(x) = tanh(x).
relu:f(x) = max(0, x)
solver: 权重优化器,{‘lbfgs’, ‘sgd’, ‘adam’}, 默认adam
lbfgs:quasi-Newton方法的优化器
sgd:随机梯度下降
adam: Kingma, Diederik, and Jimmy Ba提出的机遇随机梯度的优化器
注意:默认solver ‘adam’在相对较大的数据集上效果比较好(几千个样本或者更多),对小数据集来说,lbfgs收敛更快效果也更好。
alpha :float,可选的,默认0.0001,正则化项参数
batch_size : int , 可选的,默认’auto’,随机优化的minibatches的大小batch_size=min(200,n_samples),如果solver是’lbfgs’,分类器将不使用minibatch
learning_rate :学习率,用于权重更新,只有当solver为’sgd’时使用,{‘constant’,’invscaling’, ‘adaptive’},默认constant
‘constant’: 有’learning_rate_init’给定的恒定学习率
‘incscaling’:随着时间t使用’power_t’的逆标度指数不断降低学习率learning_rate_ ,effective_learning_rate = learning_rate_init / pow(t, power_t)
‘adaptive’:只要训练损耗在下降,就保持学习率为’learning_rate_init’不变,当连续两次不能降低训练损耗或验证分数停止升高至少tol时,将当前学习率除以5.
power_t: double, 可选, default 0.5,只有solver=’sgd’时使用,是逆扩展学习率的指数.当learning_rate=’invscaling’,用来更新有效学习率。
max_iter: int,可选,默认200,最大迭代次数。
random_state:int 或RandomState,可选,默认None,随机数生成器的状态或种子。
shuffle: bool,可选,默认True,只有当solver=’sgd’或者‘adam’时使用,判断是否在每次迭代时对样本进行清洗。
tol:float, 可选,默认1e-4,优化的容忍度
learning_rate_int:double,可选,默认0.001,初始学习率,控制更新权重的补偿,只有当solver=’sgd’ 或’adam’时使用。
属性说明:
coefs_包含w的矩阵,可以通过迭代获得每一层神经网络的权重矩阵
classes_:每个输出的类标签
loss_:损失函数计算出来的当前损失值
coefs_:列表中的第i个元素表示i层的权重矩阵
intercepts_:列表中第i个元素代表i+1层的偏差向量
n_iter_ :迭代次数
n_layers_:层数
n_outputs_:输出的个数
out_activation_:输出激活函数的名称
二、使用MLPClassifier进行分类
import numpy as np
import pandas as pd
from sklearn.neural_network import MLPClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn import metrics data=load_iris()
feature=data.data
target=data.target
print(np.unique(target)) xtrain,xtest,ytrain,ytest=train_test_split(feature,target,train_size=0.7,random_state=421) nn=MLPClassifier(hidden_layer_sizes=(3,5),activation="tanh",shuffle=False,solver="lbfgs",alpha=0.001)
model=nn.fit(xtrain,ytrain)
pre=model.predict(xtest) print(pre)
print(ytest)
print(model.coefs_)
print(model.n_layers_)
print(model.n_outputs_)
print(model.predict_proba(xtest))
print(model.score(xtest,ytest))
print(model.classes_)
print(model.loss_)
print(model.activation)
print(model.intercepts_)
print(model.n_iter_)
print(metrics.confusion_matrix(ytest,pre)) print("分类报告:", metrics.classification_report(ytest,pre))
print("W权重:",model.coefs_[:1])
print("损失值:",model.loss_) index=0
for w in model.coefs_:
index += 1
print('第{}层网络层:'.format(index))
print('权重矩阵:', w.shape)
print('系数矩阵:', w)
三、使用MLPRegressor进行回归
import numpy as np
import pandas as pd
from sklearn.neural_network import MLPRegressor
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split data=load_boston()
feature=data.data
target=data.target xtrain,xtest,ytrain,ytest=train_test_split(feature,target,train_size=0.7,random_state=421) nn=MLPRegressor(hidden_layer_sizes=(100,100),activation="identity",shuffle=False,solver="lbfgs",alpha=0.001)
model=nn.fit(xtrain,ytrain)
pre=model.predict(xtest) print(pre)
print(ytest)
print(model.coefs_)
print(model.n_layers_)
print(model.n_outputs_)
print(model.score(xtest,ytest)) index=0
for w in model.coefs_:
index += 1
print('第{}层网络层:'.format(index))
print('仅重矩阵:', w.shape)
print('系数矩阵:', w) plt.plot(range(152),pre,color='red')
plt.plot(range(152),ytest,color='blue') plt.show()
神经网络——基于sklearn的参数介绍及应用的更多相关文章
- 【集成学习】lightgbm参数介绍(sklearn)
# XGBoost和LightGBM部分参数对比表: lightgbm.sklearn参数介绍(官网)
- 数据挖掘入门系列教程(九)之基于sklearn的SVM使用
目录 介绍 基于SVM对MINIST数据集进行分类 使用SVM SVM分析垃圾邮件 加载数据集 分词 构建词云 构建数据集 进行训练 交叉验证 炼丹术 总结 参考 介绍 在上一篇博客:数据挖掘入门系列 ...
- 循环神经网络(RNN, Recurrent Neural Networks)介绍(转载)
循环神经网络(RNN, Recurrent Neural Networks)介绍 这篇文章很多内容是参考:http://www.wildml.com/2015/09/recurrent-neur ...
- 基于sklearn的分类器实战
已迁移到我新博客,阅读体验更佳基于sklearn的分类器实战 完整代码实现见github:click me 一.实验说明 1.1 任务描述 1.2 数据说明 一共有十个数据集,数据集中的数据属性有全部 ...
- 循环神经网络(RNN, Recurrent Neural Networks)介绍
原文地址: http://blog.csdn.net/heyongluoyao8/article/details/48636251# 循环神经网络(RNN, Recurrent Neural Netw ...
- SQLMAP参数介绍
转自:http://zhan.renren.com/bugpower?gid=3602888498044629629&checked=true SQLMAP参数介绍 sqlmap的使用方式:p ...
- Bootstrap Paginator 分页插件参数介绍及使用
Bootstrap Paginator是一款基于Bootstrap的js分页插件,功能很丰富,个人觉得这款插件已经无可挑剔了.它提供了一系列的参数用来支持用户的定制,提供了公共的方法可随时获得插件状态 ...
- Apache中 RewriteRule 规则参数介绍
Apache中 RewriteRule 规则参数介绍 摘要: Apache模块 mod_rewrite 提供了一个基于正则表达式分析器的重写引擎来实时重写URL请求.它支持每个完整规则可以拥有不限数量 ...
- Python 基于python操纵zookeeper介绍
基于python操纵zookeeper介绍 by:授客 QQ:1033553122 测试环境 Win7 64位 Python 3.3.4 kazoo-2.6.1-py2.py3-none-any.w ...
- Python3学习之路~8.1 socket概念及参数介绍
一 socket介绍 TCP/IP 基于TCP/IP协议栈的网络编程是最基本的网络编程方式,主要是使用各种编程语言,利用操作系统提供的套接字网络编程接口,直接开发各种网络应用程序. socket概念 ...
随机推荐
- 【Unity3D】地面网格特效
1 前言 本文实现了地面网格特效,包含以下两种模式: 实时模式:网格线宽度和间距随相机的高度实时变化: 分段模式:将相机高度分段,网格线宽度和间距在每段中对应一个值. 本文完整资源见→Unit ...
- 【Unity3D】刚体组件Rigidbody
1 前言 刚体(Rigidbody)是运动学(Kinematic)中的一个概念,指在运动中和受力作用后,形状和大小不变,而且内部各点的相对位置不变的物体.在 Unity3D 中,刚体组件赋予了游戏 ...
- Spring Cloud Openfeign微服务接口调用与Hystrix集成实战
关于openfeign 可以认为OpenFeign是Feign的增强版,不同的是OpenFeign支持Spring MVC注解.OpenFeign和Feign底层都内置了Ribbon负载均衡组件,在导 ...
- ORACLE FORALL介绍
ORACLE 10G OFFICIAL DOCUMNET ---------------------------------------------------------------------- ...
- 关于动态抽样(Dynamic Sampling)
关于动态抽样(Dynamic Sampling) 原文:http://www.oracle.com/technetwork/issue-archive/2009/09-jan/o19asktom-08 ...
- Java Socket编程系列(四)开发支持多客户端访问的Server
例子来自Java官方教程,稍作调整. 上一篇介绍了单客户端访问的Server实现,这一篇实现的是多个客户端请求服务端,根据服务端提示进行一系列操作. 协议类(和系列三一样没变): package co ...
- SpringBoot中Redis的基础使用
基础使用 首先引入依赖 <!-- redis依赖--> <dependency> <groupId>org.springframework.boot</gro ...
- VS2019 添加三方文件夹遇到的坑
在开发新项目时需要用到一些三方 API,这些三方 API 没有生成 lib,所以我们在 VS 编译器中添加这些三方文件夹的头文件路径后 会出现 ERROR LNK2019 的错误提示,这些提示通常都是 ...
- win32 - Direct3D 11的demo创建
我们可以使用D3D为游戏,科学和桌面应用程序创建3-D图形. 非官方demo实例: https://github.com/Ray1024/D3D11Tutorial 当然,我们第一步要开始认识里面的基 ...
- OpenCV开发笔记(六十八):红胖子8分钟带你使用特征点Flann最邻近差值匹配识别(图文并茂+浅显易懂+程序源码)
若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...