CCF CSP 202006 01线性分类器 JAVA实现

本文介绍了一种基于Java实现的线性分类器,通过输入坐标和标记值来判断线是否能正确分类点集。该分类器利用点到线的距离公式进行判断,并采用long型变量以避免整数溢出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

202006

01 线性分类器

在这里插入图片描述

import java.util.Scanner;
public class Main {
	public static void main(String[] args)  {
	Scanner inScanner = new Scanner(System.in);
	int N = inScanner.nextInt();
	int M = inScanner.nextInt();
	
	location[] locations = new location[N];
	line[] lines = new line[M];
	
	//读取坐标和标记值
	for (int i = 0; i < locations.length; i++) {
		locations[i] = new location();
		locations[i].x = inScanner.nextInt();
		locations[i].y = inScanner.nextInt();
		locations[i].type = inScanner.next();
	}
	//读取线段的参数
	for (int i = 0; i < lines.length; i++) {
		lines[i] = new line();
		lines[i].theta0 = inScanner.nextInt();
		lines[i].theta1 = inScanner.nextInt();
		lines[i].theta2 = inScanner.nextInt();
	
	}
	
	for (int i = 0; i < lines.length; i++) {
		long result = lines[i].theta1 * locations[0].x + lines[i].theta2 * locations[0].y + lines[i].theta0;//先读取第一个坐标
		String flagString = locations[0].type;//先读取第一个坐标
		boolean flag = true;//标记这条直线是否符合要求
		
		//根据与第一个坐标运算的值 相乘, 符号相同,则是同类,否则是异类
		for (int j = 1; j < locations.length; j++) { // 从第二行开始,遍历每一个坐标
			long temp = lines[i].theta1 * locations[j].x + lines[i].theta2 * locations[j].y + lines[i].theta0;
			if(result * temp > 0 && !locations[j].type.equals(flagString)) // 符号相同,应该是同类的,但标记的类型不同,于是这条直线不符合要求
			{
				flag = false;
				break;
			}
			
			if(result * temp < 0 && locations[j].type.equals(flagString))// 符号相异,不应该是同类的,但标记的类型相同,于是这条直线不符合要求
			{
				flag = false;
				break;
			}
		}
		
		
		
		if (flag) {
			System.out.println("Yes");
		}
		else {
			System.out.println("No");
		}
	
	}
	
	
	
	}
}



class location{
	int x,y;
	String type;
}
class line{
	int theta0,theta1,theta2;
}

收获

由所有点的坐标和给定直线的三个参数均为整数,且绝对值≤10^6;

可能会使得lines[i].theta1 * locations[j].x + lines[i].theta2 * locations[j].y + lines[i].theta0; 超过int的范围 约 2x10^9

故用long存储。

### CCF CSP 考试中的线性分类器实现 尽管在提供的引用中并未提及具体的线性分类器相关内容,但从机器学习的角度来看,可以推测 CCSP 可能会涉及基本的算法设计与实现。以下是关于如何在线性代数和编程框架下实现简单线性分类器的方法。 #### 线性分类器简介 线性分类器是一种基于超平面划分数据集的模型,其目标是最小化错误率或将两类样本分开。常见的线性分类器有感知机 (Perceptron),支持向量机 (SVM) 的线性版本以及逻辑回归 (Logistic Regression)[^4]。 #### 感知机实现 感知机是一个简单的二分类模型,通过不断调整权重来最小化误分类点的数量。其实现如下: ```python import numpy as np class Perceptron: def __init__(self, learning_rate=0.1, max_epochs=100): self.learning_rate = learning_rate self.max_epochs = max_epochs def fit(self, X, y): n_samples, n_features = X.shape self.weights = np.zeros(n_features) self.bias = 0 for _ in range(self.max_epochs): for idx, x_i in enumerate(X): linear_output = np.dot(x_i, self.weights) + self.bias predicted_class = np.where(linear_output >= 0, 1, -1) update = self.learning_rate * (y[idx] - predicted_class) self.weights += update * x_i self.bias += update def predict(self, X): linear_output = np.dot(X, self.weights) + self.bias return np.where(linear_output >= 0, 1, -1) ``` 上述代码实现了感知机的核心功能,其中 `fit` 方法用于训练模型,而 `predict` 则负责预测新数据所属类别[^5]。 #### 支持向量机(SVM)的简化版 对于更复杂的场景,可以通过优化方法求解 SVM 的对偶问题。然而,在竞赛环境中通常不需要完整的库依赖,而是关注核心思想的应用。例如,利用梯度下降法近似解决软间隔 SVM: ```python def svm_loss(w, b, X, y, C): loss = 0 dw = np.zeros_like(w) m = len(y) scores = y * (np.dot(X, w) + b) data_loss = np.maximum(0, 1 - scores) loss += np.sum(data_loss) / m reg_loss = 0.5 * np.linalg.norm(w)**2 total_loss = loss + C * reg_loss dscores = -(scores < 1).astype(float) * y / m dw += np.dot(dscores.T, X) db = np.sum(dscores) return total_loss, dw, db ``` 此函数计算损失并返回梯度更新方向[^6]。 --- ####
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值