# -*- coding: utf-8 -*-
'''
Created on 2018年1月12日
@author: Jason.F
@summary: Scikit-Learn库感知器学习算法
'''
from sklearn import datasets
import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import Perceptron
from sklearn.metrics import accuracy_score
from matplotlib.colors import ListedColormap
import matplotlib.pyplot as plt
#数据导入
iris=datasets.load_iris()
X=iris.data[:,[2,3]]
y=iris.target
print (np.unique(y))
#训练集和测试集划分
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3,random_state=0)
#标准化
sc=StandardScaler()
sc.fit(X_train)#计算样本的均值和标准差
X_train_std=sc.transform(X_train)
X_test_std=sc.transform(X_test)
#感知器分类
ppn=Perceptron(n_iter=40,eta0=0.1,random_state=0)#迭代次数、学习速率、重排训练集
ppn.fit(X_train_std,y_train)
#模型预测
y_pred=ppn.predict(X_test_std)
print ('Accuracy:
【Python-ML】SKlearn库感知器(perceptron) 使用
最新推荐文章于 2023-12-05 10:07:10 发布