2021-4-26【PTA】【L1-6 吉老师的回归 (15 分)】

该程序读取输入的字符串和剩余题目数量,检查字符串中是否包含特定词汇。若不包含,则在剩余题目数为0时输出字符串,并结束程序。否则减少剩余题目数。最后若未输出字符串,则打印特定标识。

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n,m,cnt=1;//cnt标志
    string str;
    cin>>n>>m;//输入
    getchar();//吸收回车
    while(n--)//
    {
        getline(cin,str);//输入字符串
        if(str.find("qiandao")!=-1||str.find("easy")!=-1);//如果符合则无输出
        else
        {
            if(m==0)//剩余做题数==0
            {
                cout<<str;//输出字符串
                cnt=0;//改标志
                break;//退出循环
            }
            m--;//做题数
        }
    }

    if(cnt)
        cout<<"Wo AK le"<<endl;//如果标志未改变

    return 0;
}
### Logistic回归PTA平台上的习题与解决方案 Logistic回归是一种广泛应用于类问题的统计学习方法,尤其适合二类任务。PTA(Programming Teaching Assistant)平台上确实存在一些与Logistic回归相关的习题,其中6-14-3是常见的题目编号。以下是针对这些题目的析与解决方案[^1]。 #### 6-1 题目解析 题目6-1通常要求实现一个简单的Logistic回归模型,用于解决二类问题。具体任务可能包括以下内容: - 构建Logistic回归模型。 - 使用梯度下降法优化损失函数。 - 对测试数据进行预测并输出结果。 以下是基于Python语言的代码示例,展示了如何实现Logistic回归模型: ```python import numpy as np def sigmoid(z): return 1 / (1 + np.exp(-z)) def compute_cost(X, y, theta): m = len(y) h = sigmoid(np.dot(X, theta)) cost = -(1/m) * np.sum(y * np.log(h) + (1 - y) * np.log(1 - h)) return cost def gradient_descent(X, y, theta, alpha, iterations): m = len(y) cost_history = [] for _ in range(iterations): h = sigmoid(np.dot(X, theta)) gradient = (1/m) * np.dot(X.T, (h - y)) theta -= alpha * gradient cost_history.append(compute_cost(X, y, theta)) return theta, cost_history # 示例数据 X = np.array([[1, 2], [1, 3], [1, 4]]) y = np.array([0, 0, 1]) theta = np.zeros(X.shape[1]) # 超参数 alpha = 0.1 iterations = 1000 # 训练模型 theta_optimized, cost_history = gradient_descent(X, y, theta, alpha, iterations) print("Optimized Theta:", theta_optimized) ``` #### 4-3 题目解析 题目4-3可能涉及更复杂的场景,例如多特征输入或正则化处理。任务通常包括以下步骤: - 加载训练数据集。 - 实现带有正则化的Logistic回归模型。 - 使用交叉验证评估模型性能。 以下是带有L2正则化的Logistic回归实现: ```python def compute_regularized_cost(X, y, theta, lambda_): m = len(y) h = sigmoid(np.dot(X, theta)) regularization = (lambda_ / (2 * m)) * np.sum(theta[1:]**2) cost = -(1/m) * np.sum(y * np.log(h) + (1 - y) * np.log(1 - h)) + regularization return cost def regularized_gradient_descent(X, y, theta, alpha, iterations, lambda_): m = len(y) cost_history = [] for _ in range(iterations): h = sigmoid(np.dot(X, theta)) gradient = (1/m) * np.dot(X.T, (h - y)) gradient[1:] += (lambda_ / m) * theta[1:] theta -= alpha * gradient cost_history.append(compute_regularized_cost(X, y, theta, lambda_)) return theta, cost_history # 示例数据 X = np.array([[1, 2, 3], [1, 3, 4], [1, 4, 5]]) y = np.array([0, 0, 1]) theta = np.zeros(X.shape[1]) # 超参数 alpha = 0.01 iterations = 1000 lambda_ = 0.1 # 训练模型 theta_optimized, cost_history = regularized_gradient_descent(X, y, theta, alpha, iterations, lambda_) print("Regularized Optimized Theta:", theta_optimized) ``` #### 注意事项 - 在实际应用中,需要对数据进行预处理,例如归一化或标准化[^2]。 - 模型的性能可以通过准确率、精确率、召回率等指标进行评估。 - 正则化参数的选择会影响模型的泛化能力,建议通过交叉验证确定最佳值。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Eternity_GQM

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值