神经网络——基于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概念 ...
随机推荐
- JS Leetcode 263. 丑数 题解分析,来认识有趣的丑数吧
壹 ❀ 引 本题来自LeetCode263. 丑数,难度简单,题目描述如下: 给你一个整数 n ,请你判断 n 是否为 丑数 .如果是,返回 true :否则,返回 false . 丑数 就是只包含质 ...
- 从零开始的微信小程序入门教程(三),有趣且好玩的数据绑定
壹 ❀ 引 我在从零开始的微信小程序入门教程(二),初识WXML与WXSS一文中简单介绍了小程序组件与小程序样式相关概念,在了解这两者之后,其实我们已经可以搭建出简单的静态页面,与书写HTML页面一样 ...
- C语言,函数形参与实参个数不一致问题
最近阅读工程代码的时候,同一个函数,不同场景调用时,输入的实参个数不一样,但是编译却没有问题.查看函数的定义,相关的C文件里并没有给形参指定默认值,这就很奇怪了. 最终,发现在函数相关的头文件 ...
- mdbook安装使用实录
简介 mdbook是一个基于Rust的电子书系统,效果类似于gitbook.本人写图文文章一直都采用的印象笔记,但是写系列教程的情况不方便管理.因此引入mdBook,分享一下基本安装使用过程及遇到的问 ...
- 腾讯云视频转码回调 http code 405
异常信息 405 Method Not Allowed 异常场景 通过腾讯云SDK上传视频,视频转码完成回调我的接口失败,我这边一直没有log.找到腾讯云工作人员,告诉我这边返回405错误和不支持ge ...
- 【LeetCode贪心#05】K 次取反后最大化的数组和(自定义sort、二重贪心)
K次取反后最大化的数组和 力扣题目链接(opens new window) 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这 ...
- 【Azure App Service】当App Service中使用系统标识无法获取Access Token时
问题描述 App Serive上的应用配置了系统标识(System Identity),通过系统标识获取到访问Key Vault资源的Access Token.但这次确遇见了无法获取到正常的Acces ...
- [C++] epoll编写的echo服务端
直接贴代码,代码是运行在Linux上面的,通过 g++ epoll.cpp编译 #include <sys/socket.h> #include <sys/epoll.h> # ...
- 导入Excel文件的时候公式为【#Ref!】应该怎么解决?
前言 在我们使用Excel时,经常会遇到一个问题,就是导入Excel时公式显示为[#Ref!]的情况.这通常是因为公式中引用的单元格已被删除或对应的工作表被删除,导致原公式无法识别对应的参数而显示为[ ...
- Java 封装性的小练习
1 package com.bytezero.test2; 2 3 public class Person 4 { 5 private int age; 6 7 public void setAge( ...