补全逻辑回归公式

import numpy as np
import matplotlib.pyplot as plt

def logistic(x):
####参考教材内容,补全关键代码,重新运行####
    return 1 / (1 + np.exp(-x))
# 在区间[-10, 10]生成100个点
x = np.linspace(-10, 10, 100)
y = logistic(x)

# 绘制logistic函数曲线
plt.figure(figsize=(5,2))
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('logistic(x)')
#plt.title('logistic Function')
plt.grid(True)
plt.show()

以下3个代码类似,3最好

1
import numpy as np
import matplotlib.pyplot as plt

# 生成随机数据
np.random.seed(0)
X = np.linspace(0, 10, 50)
Y = 2 * X + np.random.normal(0, 1, 50)

# 初始化参数
theta0 = 0
theta1 = 0
learning_rate = 0.01
num_iterations = 100

# 梯度下降训练模型
for i in range(num_iterations):
    # 计算预测值
    Y_pred = theta0 + theta1 * X
    
    # 计算梯度
####参考教材内容,补全关键代码,重新运行####
    gradient0=(1/len(X))*np.sum(Y_pred-Y)
    gradient1 = (1/len(X)) * np.dot((Y_pred - Y), X)
    
    # 更新参数
    theta0 = theta0 - learning_rate * gradient0
####参考教材内容,补全关键代码,重新运行####
    theta1=theta1-learning_rate*gradient1
    regression_lines.append((theta0,theta1))
    
    # 绘制散点图和拟合直线
    plt.figure()
    plt.scatter(X, Y, label='Data')
    plt.plot(X, Y_pred, 'r', label='Regression Line')
    plt.xlabel('X')
    plt.ylabel('Y')
    plt.title(f'Iteration {i+1}')
    plt.legend()
    plt.show()

# 输出最终参数值
print('Final Parameters:')
print('theta0:', theta0)
print('theta1:', theta1)
2
import numpy as np
import matplotlib.pyplot as plt
# 生成随机数据
np.random.seed(0)
X = np.linspace(0, 10, 50)
Y = 2 * X + np.random.normal(0, 1, 50)
# 初始化参数
theta0 = 0
theta1 = 0
learning_rate = 0.01
num_iterations = 100
# 存储每个阶段的拟合直线
regression_lines = []
# 梯度下降训练模型
for i in range(num_iterations):
    # 计算预测值
    Y_pred = theta0 + theta1 * X
    # 计算梯度
    gradient0 = (1/len(X)) * np.sum(Y_pred - Y)
    gradient1 = (1/len(X)) * np.dot((Y_pred - Y), X)
####参考教材内容,补全关键代码,重新运行####
    # 更新参数
####参考教材内容,补全关键代码,重新运行####
    theta0 = theta0 - learning_rate * gradient0
    theta1 = theta1 - learning_rate * gradient1  
    # 将当前阶段的拟合直线添加到列表中
    regression_lines.append((theta0, theta1))
    
# 绘制散点图和多个拟合直线
plt.figure()
plt.scatter(X, Y, label='Data')
for line in regression_lines:
    Y_pred = line[0] + line[1] * X
    plt.plot(X, Y_pred)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Regression Lines in Different Stages')
plt.legend()
plt.show()
# 输出最终参数值
print('Final Parameters:')
print('theta0:', theta0)
print('theta1:', theta1)
3
import numpy as np
import matplotlib.pyplot as plt

# 生成随机数据
np.random.seed(0)
X = np.linspace(0, 10, 50)
Y = 2 * X + np.random.normal(0, 1, 50)

# 初始化参数
theta0 = 0
theta1 = 0
learning_rate = 0.01
num_iterations = 100

# 存储每个阶段的拟合直线和参数
regression_lines = []
parameters = []

# 梯度下降训练模型
for i in range(num_iterations):
    # 计算预测值
    Y_pred = theta0 + theta1 * X
    
    # 计算梯度
    gradient0 = (1/len(X)) * np.sum(Y_pred - Y)
    gradient1 = (1/len(X)) * np.dot((Y_pred - Y), X)
    
    # 更新参数
    theta0 = theta0 - learning_rate * gradient0
    theta1 = theta1 - learning_rate * gradient1
    
    # 将当前阶段的拟合直线和参数添加到列表中
    regression_lines.append((theta0, theta1))
    parameters.append((theta0, theta1))

# 绘制散点图和多个拟合直线
plt.figure()
plt.scatter(X, Y, label='Data')
for i, line in enumerate(regression_lines):
    Y_pred = line[0] + line[1] * X
    if i == len(regression_lines) - 1:
        plt.plot(X, Y_pred, 'r', label='Optimal Line')
    else:
        plt.plot(X, Y_pred, label=f'Line {i+1}')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Regression Lines in Different Stages')
#plt.legend()
plt.show()

# 输出最终参数值
print('Final Parameters:')
print('theta0:', theta0)
print('theta1:', theta1)

以下两个不知

1
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# 定义目标函数
def func(x, y):
    return x**2 + y**2

# 定义目标函数的梯度
def gradient(x, y):
    grad_x = 2*x
    grad_y = 2*y
    return grad_x, grad_y

# 梯度下降函数
def gradient_descent(learning_rate, num_iterations):
    # 初始化起始点和路径轨迹
    x = 4
    y = 4
    path = [(x, y)]

    for _ in range(num_iterations):
        grad_x, grad_y = gradient(x, y)
        x -= learning_rate * grad_x
        y -= learning_rate * grad_y
        path.append((x, y))

    return x, y, path

# 设定学习率和迭代次数
learning_rate = 0.1
num_iterations = 20

# 运行梯度下降算法
x_min, y_min, path = gradient_descent(learning_rate, num_iterations)

# 生成网格数据
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = func(X, Y)

# 绘制三维图形
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='viridis')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('f(X, Y)')

# 绘制路径轨迹
path = np.array(path)
ax.plot(path[:, 0], path[:, 1], func(path[:, 0], path[:, 1]), color='blue', marker='o')
ax.view_init(elev=30, azim=-45)
# 显示图形
plt.show()
2
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# 定义目标函数
def func(x, y):
    return x**2 + y**2

# 定义目标函数的梯度
def gradient(x, y):
    grad_x = 2 * x
    grad_y = 2 * y
    return grad_x, grad_y

# 梯度下降函数
def gradient_descent(learning_rate, num_iterations):
    # 初始化起始点和路径轨迹
    x = 4
    y = 4
    path = [(x, y)]

    for _ in range(num_iterations):
        grad_x, grad_y = gradient(x, y)
        x -= learning_rate * grad_x
        y -= learning_rate * grad_y
        path.append((x, y))

    return x, y, path

# 设定学习率和迭代次数
learning_rate = 0.1
num_iterations = 100

# 运行梯度下降算法
x_min, y_min, path = gradient_descent(learning_rate, num_iterations)

# 绘制曲面图
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = func(X, Y)

ax.plot_surface(X, Y, Z, cmap='viridis')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('f(X, Y)')

# 绘制路径轨迹
path = np.array(path)
ax.plot(path[:, 0], path[:, 1], func(path[:, 0], path[:, 1]), color='red', linewidth=2)

ax.view_init(elev=30, azim=-45)

plt.show()

zp注意

import numpy as np
import matplotlib.pyplot as plt

# 训练集
current = np.array([0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8])
voltage = np.array([1.18, 1.35, 2.02, 2.45, 3.15, 3.52, 4.22])


# 计算最小二乘法的系数(电阻)和截距
mean_current = np.mean(current)
mean_voltage = np.mean(voltage)

####参考教材内容,补全关键代码,重新运行####
numerator=np.sum((current-mean_current)*(voltage-mean_voltage))
denominator=np.sum((current-mean_current)**2)
coef=nummerator/denominator
intercept=mean_voltage-coef*mean_current
print("截距:", intercept)
print("系数(电阻):", coef)

# 绘制散点图和直线
plt.scatter(current, voltage, color='blue', label='Data Points')
plt.plot(current, intercept + coef * current, color='red', label='Linear Regression')
plt.xlabel('Current (A)')
plt.ylabel('Voltage (V)')
plt.title('Scatter plot with Linear Regression')
plt.legend()
plt.grid(True)
plt.show()
import numpy as np

def softmax(x):
    # 计算指数值
    exp_x = np.exp(x)
    
    # 计算指数值的和
    sum_exp_x = np.sum(exp_x)
    
    # 计算Softmax函数输出
    softmax_x = exp_x / sum_exp_x
    
    return softmax_x

# 输入向量
x = np.array([2, 4, 6])

# 调用softmax函数计算Softmax输出
softmax_output = softmax(x)

# 打印Softmax输出
print(softmax_output)

下面两个类似

sklearn版本
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import numpy as np

# 使用load_diabetes()函数加载糖尿病数据集
diabetes_data = load_diabetes()
X = diabetes_data.data
y = diabetes_data.target


# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 创建线性回归模型
####参考教材内容,补全关键代码,重新运行####
model = LinearRegression()
# 训练模型
####参考教材内容,补全关键代码,重新运行####
model.fit(X_train, y_train)
# 在测试集上进行预测
y_pred = model.predict(X_test)

# 计算均方误差(Mean Squared Error)
mse = mean_squared_error(y_test, y_pred)
print("Mean Squared Error:", mse)
python版本
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
# 生成两个类别的随机样本
np.random.seed(22)
class_0 = np.random.randn(20, 2) + [2, 2]
class_1 = np.random.randn(20, 2) - [2, 2]
# 合并数据,创建标签
X = np.vstack((class_0, class_1))
y = np.hstack((np.zeros(len(class_0)), np.ones(len(class_1))))
# 训练逻辑回归模型
####参考教材内容,补全关键代码,重新运行####
model=LogisticRegression()
model.fit(X,y)
####参考教材内容,补全关键代码,重新运行####
# 绘制散点图
plt.scatter(class_0[:, 0], class_0[:, 1], marker='o', color='red', label='Class 1')
plt.scatter(class_1[:, 0], class_1[:, 1], marker='s', color='blue', label='Class 2')
# 绘制分类曲线
xmin, xmax = plt.xlim()
coef = model.coef_[0]
intercept = model.intercept_
y_boundary = - (coef[0] * np.linspace(xmin, xmax, 100) + intercept) / coef[1]
plt.plot(np.linspace(xmin, xmax, 100), y_boundary, color='green', label='Decision Boundary')


# 添加图例和坐标轴标签
plt.legend()
plt.xlabel('X')
plt.ylabel('Y')
# 显示图形
plt.show()

【实例】成年人收入数据集简要分析。

读数据

import pandas as pd
# 读取数据集
url = 'adult.data'
header = ['age', 'workclass', 'fnlwgt', 'education', 
              'education-num', 'marital-status', 'occupation',
              'relationship', 'race', 'sex', 'capital-gain', 
              'capital-loss', 'hours-per-week', 'native-country', 'income']
data_old = pd.read_csv(url, na_values=' ?')
data_old.columns = header
data_old.head()

####参考教材内容,补全关键代码,重新运行####
missing_count=data_old.isnull().sum()
print(missing_count)

####参考教材内容,补全关键代码,重新运行####
income_counts=data_old['income'].value_counts()
print(income_counts)

import matplotlib.pyplot as plt
income_counts.plot(kind='bar')
plt.xlabel('Income')
plt.ylabel('Count')
plt.title('Income Distribution')
plt.show()

#print(data_old.describe())
data_old.describe()

数据预处理

#数据预处理
import numpy as np
def preprocess_data(data):
    data = data.dropna()
    #data = data[data['occupation'] != ' ?']
    #data = data[data['native-country'] != ' ?'] 
    cat_cols = ['workclass', 'education', 'marital-status', 
                'occupation', 'relationship', 'race', 'sex', 'native-country']
    cat_df = data[cat_cols]
    cat_1hot = pd.get_dummies(cat_df)
    data_new = pd.concat([cat_1hot, data[['age', 'fnlwgt', 'education-num',
                                          'capital-gain', 'capital-loss', 
                                          'hours-per-week']]], axis=1)
    data_new['income'] = np.where(data['income'] == ' >50K', 1, 0)
    return data_new
data = preprocess_data(data_old)

# 保存预处理后的数据
data.to_csv('preprocessed_data.csv', index=False)

#data = pd.read_csv('adult.csv')
# 重新加载保存的预处理结果文件
data = pd.read_csv('preprocessed_data.csv')

# 查看前几行数据
#print(data.head())
data.head()

# 查看数据集的基本统计信息
#print(data.describe())
data.describe()

####参考教材内容,补全关键代码,重新运行####
income_counts=data['income'].value_counts
print(income_counts)

# 绘制不同教育程度的收入箱线图
data.boxplot(column='education-num', by='income')
plt.xlabel('Income')
plt.ylabel('Education Number')
plt.title('Education Number vs Income')
plt.show()

import pandas as pd
import matplotlib.pyplot as plt

len(header),X.shape

import pandas as pd
# cat_df 是一个包含分类变量的 DataFrame
cat_df = pd.DataFrame(
    {'category': ['A', 'B', 'A', 'C', 'B']})
# 使用 get_dummies() 进行独热编码
encoded_df = pd.get_dummies(cat_df)
# 打印编码后的 DataFrame
print(cat_df,"\n\n",encoded_df)

综合案例:基于逻辑回归的收入级别预测(2分类问题)

python版本

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
class LogisticRegression:
    def __init__(self, learning_rate=0.01, num_iterations=1000):
        self.learning_rate = learning_rate
        self.num_iterations = num_iterations
        self.weights = None
        self.bias = None 
    def sigmoid(self, z):
        z = np.clip(z, -500, 500)  # 将z限制在一个较小的范围内,以避免发生溢出
        z = z.astype(float)
        return 1 / (1 + np.exp(-z)) #可能触发溢出警告
        #return np.expit(z) 
        #from scipy.special import expit 
        #return expit(z)
    def fit(self, X, y):
        num_samples, num_features = X.shape
        self.weights = np.zeros(num_features)
        self.bias = 0
        for i in range(self.num_iterations):
####参考教材内容,补全关键代码,重新运行#### 
            linear_model=np.dot(X,self.weights)+self.bias
            y_pred=self.sigmoid(linear_model)
            dw = (1 / num_samples) * np.dot(X.T, (y_pred - y))
            db = (1 / num_samples) * np.sum(y_pred - y)
            self.weights -= self.learning_rate * dw.astype(float)
            self.bias -= self.learning_rate * db.astype(float) 
            #print( i )
    def predict(self, X):
        linear_model = np.dot(X, self.weights) + self.bias
        y_pred = self.sigmoid(linear_model)
        y_pred_cls = np.where(y_pred > 0.5, 1, 0)
        return y_pred_cls
# def preprocess_data(url):
#     data = pd.read_csv(url, header=None)
    
#     header = ['age', 'workclass', 'fnlwgt', 'education', 
#               'education-num', 'marital-status', 'occupation',
#               'relationship', 'race', 'sex', 'capital-gain', 
#               'capital-loss', 'hours-per-week', 'native-country', 'income']
#     data.columns = header
#     data = data[data['workclass'] != ' ?']
#     data = data[data['occupation'] != ' ?']
#     data = data[data['native-country'] != ' ?'] 
#     cat_cols = ['workclass', 'education', 'marital-status', 
#                 'occupation', 'relationship', 'race', 'sex', 'native-country']
#     cat_df = data[cat_cols]
#     cat_1hot = pd.get_dummies(cat_df)
#     data_new = pd.concat([cat_1hot, data[['age', 'fnlwgt', 'education-num',
#                                           'capital-gain', 'capital-loss', 
#                                           'hours-per-week']]], axis=1)
#     data_new['income'] = np.where(data['income'] == ' >50K', 1, 0)
#     return data_new
def accuracy_score(y_true, y_pred):
    accuracy = np.sum(y_true == y_pred) / len(y_true)
    return accuracy
# # 读取数据集
# url = 'data/ch04线性模型/adult.data'
# data = preprocess_data(url)

# # 保存预处理后的数据
# data.to_csv('data/ch04线性模型/preprocessed_data.csv', index=False)

# 重新加载保存的预处理结果文件
data = pd.read_csv('preprocessed_data.csv')


# 划分特征变量和目标变量
X = data.drop(columns=['income']).values
y = data['income'].values
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, 
                                                    random_state=42)
# 创建并训练逻辑回归模型
####参考教材内容,补全关键代码,重新运行####
model=LogisticRegression()
model.fit(X_train,y_train)
# 在测试集上进行预测
y_pred = model.predict(X_test)
# 评估模型性能
accuracy = accuracy_score(y_test, y_pred)
print('模型准确率:', accuracy)

sklearn版本1

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
def preprocess_data(url):
    data = pd.read_csv(url, header=None)
    
    header = ['age', 'workclass', 'fnlwgt', 'education', 
              'education-num', 'marital-status', 'occupation',
              'relationship', 'race', 'sex', 'capital-gain', 
              'capital-loss', 'hours-per-week', 'native-country', 'income']
    data.columns = header
    data = data[data['workclass'] != ' ?']
    data = data[data['occupation'] != ' ?']
    data = data[data['native-country'] != ' ?'] 
    cat_cols = ['workclass', 'education', 'marital-status', 
                'occupation', 'relationship', 'race', 'sex', 'native-country']
    cat_df = data[cat_cols]
    cat_1hot = pd.get_dummies(cat_df)
    data_new = pd.concat([cat_1hot, data[['age', 'fnlwgt', 'education-num',
                                          'capital-gain', 'capital-loss', 
                                          'hours-per-week']]], axis=1)
    data_new['income'] = np.where(data['income'] == ' >50K', 1, 0)
    return data_new

# 1. 数据预处理
#url = 'http://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data'
#url = 'data/ch04线性模型/adult.data'
url = 'adult.data'
data = preprocess_data(url)




# 2. 划分特征变量和目标变量
X = data.drop(columns=['income']).values
y = data['income'].values

# 3. 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, 
                                                    random_state=42)
# 4. 创建并训练逻辑回归模型
####参考教材内容,补全关键代码,重新运行####
model=LogisticRegression()
model.fit(X_train,y_train)
# 5. 在测试集上进行预测
y_pred = model.predict(X_test)
# 6. 评估模型性能
accuracy = accuracy_score(y_test, y_pred)
print('模型准确率:', accuracy)

slearn版本2

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
def preprocess_data(url):
    data = pd.read_csv(url, header=None)
    
    header = ['age', 'workclass', 'fnlwgt', 'education', 
              'education-num', 'marital-status', 'occupation',
              'relationship', 'race', 'sex', 'capital-gain', 
              'capital-loss', 'hours-per-week', 'native-country', 'income']
    data.columns = header
    data = data[data['workclass'] != ' ?']
    data = data[data['occupation'] != ' ?']
    data = data[data['native-country'] != ' ?'] 
    cat_cols = ['workclass', 'education', 'marital-status', 
                'occupation', 'relationship', 'race', 'sex', 'native-country']
    cat_df = data[cat_cols]
    cat_1hot = pd.get_dummies(cat_df)
    data_new = pd.concat([cat_1hot, data[['age', 'fnlwgt', 'education-num',
                                          'capital-gain', 'capital-loss', 
                                          'hours-per-week']]], axis=1)
    data_new['income'] = np.where(data['income'] == ' >50K', 1, 0)
    return data_new

# 1. 数据预处理
#url = 'http://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data'
#url = 'data/ch04/adult.data'
url = 'adult.data'
data = preprocess_data(url)
# 2. 划分特征变量和目标变量
X = data.drop(columns=['income']).values
y = data['income'].values

# 3. 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, 
                                                    random_state=42)
# 4. 创建并训练逻辑回归模型
####参考教材内容,补全关键代码,重新运行####
model=LogisticRegression()
model.fit(X_train,y_train)
# 5. 在测试集上进行预测
y_pred = model.predict(X_test)
# 6. 评估模型性能
accuracy = accuracy_score(y_test, y_pred)
print('模型准确率:', accuracy)

【实例】 提前将MNIST数据集下载到本地,以避免网络故障

import numpy as np
from sklearn.datasets import fetch_openml

# 下载MNIST数据集
mnist = fetch_openml('mnist_784')

# 将特征数据和标签数据分开
X = mnist.data
y = mnist.target

# 将特征数据和标签数据保存到本地
np.save('mnist_X.npy', X)
np.save('mnist_y.npy', y)

【实例】MNIST数据集简要分析。

import numpy as np
# 重新加载特征数据和标签数据,设置allow_pickle为True
#X = np.load('data/ch04线性模型/mnist_X.npy', allow_pickle=True)
#y = np.load('data/ch04线性模型/mnist_y.npy', allow_pickle=True)
X = np.load('/data/bigfiles/mnist_X.npy', allow_pickle=True)
y = np.load('/data/bigfiles/mnist_y.npy', allow_pickle=True)

print("数据集大小:", X.shape)
print("标签数量:", len(np.unique(y)))
X[0,:],y[0]

import matplotlib.pyplot as plt
plt.figure(figsize=(8, 3))
for i in range(10):
    plt.subplot(2, 5, i+1)
    plt.imshow(X[i].reshape(28, 28), cmap='gray')
    plt.axis('off')
    plt.title(str(y[i]))  # 添加y值作为子标题
plt.show()

综合案例:Softmax回归的手写字符分类 (多分类问题)

import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report

# 1. 加载MNIST数据集
# 方法1,在线下载:大约500M,网络可能不稳定
#from sklearn.datasets import fetch_openml
#mnist = fetch_openml('mnist_784')
#X = mnist.data
#y = mnist.target.astype(np.int)

#方法2,本地加载:请提前下载数据集,并确保路径正确。
# 重新加载特征数据和标签数据,设置allow_pickle为True
#X = np.load('data\\ch04\\mnist_X.npy', allow_pickle=True)
#y = np.load('data\\ch04\\mnist_y.npy', allow_pickle=True)

X = np.load('/data/bigfiles/mnist_X.npy', allow_pickle=True)
y = np.load('/data/bigfiles/mnist_y.npy', allow_pickle=True)

# 2. 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 3. 创建并训练Softmax回归模型
####参考教材内容,补全关键代码,重新运行####
model=LogisticRegression(multi_class='multinomial',solver='lbfgs',max_iter=1000)
model.fit(X_train,y_train)
# 4. 在测试集上进行预测
####参考教材内容,补全关键代码,重新运行####
y_pred=model.predict(X_test)
# 5. 评估模型性能
accuracy = accuracy_score(y_test, y_pred)
print('模型准确率:', accuracy)

report = classification_report(y_test, y_pred)
print('分类指标报告:\n', report)