import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import datasets
iris = datasets.load_iris()
X = iris.data # 特征矩阵
Y = iris.target # 目标变量
数据标准化
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
划分训练集和测试集
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y,
test_size=0.25,
random_state=0)
X_train.shape,Y_train.shape
((112, 4), (112,))
逻辑回归模型
from sklearn.linear_model import LogisticRegression
model = LogisticRegression(max_iter=200)# max_iter 是迭代次数,默认为100,这里设为200以保证收敛
model.fit(X_train, Y_train)