神经网络——基于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概念 ...
随机推荐
- 从零开始的 dbt 入门教程 (dbt core 开发进阶篇)
引 在上一篇文章中,我们花了专门的篇幅介绍了 dbt 更多实用的命令,那么我们继续按照之前的约定来聊 dbt 中你可能会遇到的疑惑以及有用的概念,如果你是 dbt 初学者,我相信如下知识点一定会对你有 ...
- NVME(学习笔记_杂谈)
NVME 协议中一些概念的理解: 1.Namespace :可以将Namespace 理解为一片Logic Block的区域,一个Controller可以支持多个Namespace,每个Namespa ...
- Maven多模块聚合工程实战
介绍 本文以SpringCloud微服务多模块聚合案例讲解,全程讲解中间涉及的核心知识点并配图加深理解. 更多maven知识点,建议去看<Maven实战>. 创建父工程 新建maven工程 ...
- 【libGDX】使用Mesh绘制立方体
1 前言 本文主要介绍使用 Mesh 绘制立方体,读者如果对 Mesh 不太熟悉,请回顾以下内容: 使用Mesh绘制三角形 使用Mesh绘制矩形 使用Mesh绘制圆形 在绘制立方体的过程中,主 ...
- SUB-LVDS 与LVDS 互联
SUB-LVDS 与 LVDS介绍 电气规范 今天有同学问SUB-LVDS输出是否能接到LVDS输入上,以前没用过SUB-LVDS,一起学习一下. Sub-LVDS is a differential ...
- 关于“非法的前向引用(illegal forward reference)”的探究
1.问题: 有如下代码: public class Test { static { i = 0;// 给变量赋值可以正常编译通过 System.out.print(i);// 编译器会提示" ...
- 使用秘籍|如何实现图数据库 NebulaGraph 的高效建模、快速导入、性能优化
本文整理自 NebulaGraph PD 方扬在「NebulaGraph x KubeBlocks」meetup 上的演讲,主要包括以下内容: NebulaGraph 3.x 发展历程 NebulaG ...
- Nebula 在 Akulaku 智能风控的实践:图模型的训练与部署
本文整理自 Akulaku 反欺诈团队在 nMeetup·深圳场的演讲,B站视频见:https://www.bilibili.com/video/BV1nQ4y1B7Qd 这次主要来介绍下 Nebul ...
- PHP项目&TP框架&SQL&XSS&架构&路由&调试&写法
开发基础-TP框架-入口&调试&路由&写法等 参考手册-TP5开发手册-为了掌握了解框架 首页文件看APP_PATH定义-为了后期分析核心代码 全局搜索:THINK_VERSI ...
- Java对象引用和内存管理的细节
在Java中,当局部变量(比如方法参数)的作用域结束时,这个局部变量的引用确实不再存在,但这并不意味着它引用的对象会被销毁.对象的销毁是由Java的垃圾回收器(Garbage Collector, G ...