一、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的参数介绍及应用的更多相关文章

  1. 【集成学习】lightgbm参数介绍(sklearn)

    #  XGBoost和LightGBM部分参数对比表: lightgbm.sklearn参数介绍(官网)

  2. 数据挖掘入门系列教程(九)之基于sklearn的SVM使用

    目录 介绍 基于SVM对MINIST数据集进行分类 使用SVM SVM分析垃圾邮件 加载数据集 分词 构建词云 构建数据集 进行训练 交叉验证 炼丹术 总结 参考 介绍 在上一篇博客:数据挖掘入门系列 ...

  3. 循环神经网络(RNN, Recurrent Neural Networks)介绍(转载)

    循环神经网络(RNN, Recurrent Neural Networks)介绍    这篇文章很多内容是参考:http://www.wildml.com/2015/09/recurrent-neur ...

  4. 基于sklearn的分类器实战

    已迁移到我新博客,阅读体验更佳基于sklearn的分类器实战 完整代码实现见github:click me 一.实验说明 1.1 任务描述 1.2 数据说明 一共有十个数据集,数据集中的数据属性有全部 ...

  5. 循环神经网络(RNN, Recurrent Neural Networks)介绍

    原文地址: http://blog.csdn.net/heyongluoyao8/article/details/48636251# 循环神经网络(RNN, Recurrent Neural Netw ...

  6. SQLMAP参数介绍

    转自:http://zhan.renren.com/bugpower?gid=3602888498044629629&checked=true SQLMAP参数介绍 sqlmap的使用方式:p ...

  7. Bootstrap Paginator 分页插件参数介绍及使用

    Bootstrap Paginator是一款基于Bootstrap的js分页插件,功能很丰富,个人觉得这款插件已经无可挑剔了.它提供了一系列的参数用来支持用户的定制,提供了公共的方法可随时获得插件状态 ...

  8. Apache中 RewriteRule 规则参数介绍

    Apache中 RewriteRule 规则参数介绍 摘要: Apache模块 mod_rewrite 提供了一个基于正则表达式分析器的重写引擎来实时重写URL请求.它支持每个完整规则可以拥有不限数量 ...

  9. Python 基于python操纵zookeeper介绍

    基于python操纵zookeeper介绍 by:授客  QQ:1033553122 测试环境 Win7 64位 Python 3.3.4 kazoo-2.6.1-py2.py3-none-any.w ...

  10. Python3学习之路~8.1 socket概念及参数介绍

    一 socket介绍 TCP/IP 基于TCP/IP协议栈的网络编程是最基本的网络编程方式,主要是使用各种编程语言,利用操作系统提供的套接字网络编程接口,直接开发各种网络应用程序. socket概念 ...

随机推荐

  1. Unix\Linux 执行 shell 报错:“$'\r': 未找到命令” 的解决办法

    原因 原因是因为 shell 脚本是在 Windows 编写导致的换行问题,具体原因是 Windows 的换行符号为 CRLF(\r\n),而 Unix\Linux 为 LF(\n),Macintos ...

  2. 【分布式】load balance 02-consistent hash algorithm 一致性哈希算法原理详解

    负载均衡系列专题 01-负载均衡基础知识 02-一致性 hash 原理 03-一致性哈希算法 java 实现 04-负载均衡算法 java 实现 概念 一致哈希是一种特殊的哈希算法. 在使用一致哈希算 ...

  3. db2伪表sysibm.sysdummy1

    $ db2 describe table sysibm.sysdummy1 Column                         Type      Type name             ...

  4. 《系列二》-- 8、单例bean的创建

    目录 1 源码入口概述 2 getSingleton(beanName, ObjectFactory) 的行为 总结 阅读之前要注意的东西:本文就是主打流水账式的源码阅读,主导的是一个参考,主要内容需 ...

  5. 一键部署Home Assistant ubuntu 20.4.3 树莓派3b+脚本

      树莓派3b+安装好 Ubuntu Server 20.04.3 LTS 32bit 后即可适用此脚本,其他版本树莓派/系统可能需要微调脚本*为方便一些未知/已知错误排查 脚本存在冗余部分,足够了解 ...

  6. 【Android逆向】破解看雪9月算法破解第三题

    这题的目标是算法还原,并写出注册机 1. 9月份算法第一题.apk 安装到手机 2. 随意输入账号密码,提示错误 3. apk拖入到jadx中 public native boolean regist ...

  7. django中使用redis管道

    管道(事务),要是都成功则成功,失败一个全部失败 原理:将数据操作放在内存中,只有成功后,才会一次性全部放入redis 记住,redis中的管道可以开启事务处理,但是并没有回滚这一说法!跟mysql中 ...

  8. 在本地运行 LLMs 的 6 种方法

    商业人工智能和大型语言模型 (LLM) 有一个很大的缺点:隐私.在处理敏感数据或专有数据时,我们无法从这些工具中获益.因此,我们需要了解如何在本地运行私人 LLM.开源模型提供了一种解决方案,但它们也 ...

  9. 维基百科Wikipedia镜像网站列表(全部已被封)

    序号 网址 备注 1 https://chi.jinzhao.wiki/wiki (中文版) https://en.jinzhao.wiki/wiki/Main_Page (英文版)  支持多种语言. ...

  10. 【Azure 应用服务】VS2019发布应用到正在运行的App Service时失败问题的解决

    问题描述 在VS 2019中配置号App Service的Publish Profile后,发布应用出现错误.根据VS 2019中的输出消息可知有文件正在运行中,无法被替换,所以发布失败. 问题解决 ...