sklearn学习笔记之神经网络

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37195257/article/details/79901643
# -*- coding: utf-8 -*-
import sklearn
from sklearn.neural_network import MLPClassifier

import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn import datasets
import pandas as pd
import numpy


def getData_1():

    iris = datasets.load_iris()
    X = iris.data   #样本特征矩阵,150*4矩阵,每行一个样本,每个样本维度是4
    y = iris.target #样本类别矩阵,150维行向量,每个元素代表一个样本的类别


    df1=pd.DataFrame(X, columns =['SepalLengthCm','SepalWidthCm','PetalLengthCm','PetalWidthCm'])
    df1['target']=y

    return df1

df=getData_1()


X_train, X_test, y_train, y_test = train_test_split(df.iloc[:,0:3],df['target'], test_size=0.3, random_state=42)
print X_train, X_test, y_train, y_test

model = MLPClassifier(activation='relu', solver='adam', alpha=0.0001,max_iter=10000)  # 神经网络
"""参数
---
    n_neighbors: 使用邻居的数目
    n_jobs:并行任务数
"""
model.fit(X_train,y_train)
predict=model.predict(X_test)
print predict
print y_test.values

print '神经网络分类:{:.3f}'.format(model.score(X_test, y_test))

结果:
当没有设定 max_iter=10000,默认迭代次数为200,会出现
ConvergenceWarning: Stochastic Optimizer: Maximum iterations (200) 
reached and the optimization hasn't converged yet. % self.max_iter,
 ConvergenceWarning)

放开迭代次数后,最终结果


Name: target, dtype: int32

[1 0 2 1 1 0 1 1 1 1 1 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0 0 0 1 0 0 2 1
 0 0 0 2 1 1 0 0]
[1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0 0 0 1 0 0 2 1
 0 0 0 2 1 1 0 0]
神经网络分类准确度 :0.956

阅读更多
换一批

没有更多推荐了,返回首页