from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import Perceptron
import numpy as np
#加载iris数据
iris = datasets.load_iris()
x = iris.data[:,[2,3]]
y = iris.target
print(np.unique(y))
#train_test_split
#分离训练和测试数据,test_size 为分离比例,random_state是打乱顺序在进行分离,
#stratify 为保证测试和训练数据集中的三种类型的数据比例相同
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.3,random_state=1,
stratify=y)
#bincount
print("Labels counts in y: ", np.bincount(y))
print("Labels counts in y_train: ", np.bincount(y_train))
print("Labels counts in y_test: ", np.bincount(y_test))
sc = StandardScaler()
#计算平均值和标准差
sc.fit(x_train)
#利用平均值和标准差进行数据归一化
x_train_std = sc.transform(x_train)
x_test_std = sc.transform(x_test)
#train perceptr
sklearn中的感知机模型perceptron
最新推荐文章于 2023-12-05 10:07:10 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)