No399. Evaluate Division

本文介绍了一种解决变量间关系问题的算法,通过构建图结构来求解给定方程组中变量间的比例关系。提供了两种方法:一是深度优先搜索实现;二是利用Floyd算法进行优化。

一、题目描述

Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return -1.0.

Example:
Given a / b = 2.0, b / c = 3.0. 
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? . 
return [6.0, 0.5, -1.0, 1.0, -1.0 ].

二、解题思路

方法一:

首先根据题目给出的信息构建图,构建图的方式是邻接矩阵,采用map做容器,第一个元素(即键key)为除数,第二个元素(即值value)为一个pair,pair中第一个元素为除数,第二个元素为商。主函数中首先对特殊情况给予说明,如果requires中的pair中的两个元素相等,则结果为1.0,如果两个元素中有一个是未出现过的元素,则返回-1.0。下面进入正常情况,对于需要查找的pair,调用search函数,其中包含四个参数,分别为除数(源),被除数(目标),累计商,访问过的点的集合,如果得到的结果不是-1.0(如果结果为-1.0,则没有找到目标,如果结果不是-1.0,则说明找到了目标,并返回了累计商),则插入结果中。search函数首先检查该除数(源)是否被访问过(避免循环访问),如果访问过则直接返回-1.0,如果没有访问过,则继续遍历每个除数对应的每个被除数是否等最初调用search时的目标。如果不相等则继续以该被除数为除数,目标不变,调用search。对于search返回的结果需要进行判定,结果有两种,一种是是没有找到目标的-1.0,一种是找到目标的非-1.0的累计商。

class Solution {
public:
    map<string,vector<pair<string,double>>> graph;
    
    vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
        for(int i=0;i<equations.size();i++){
            graph[equations[i].first].push_back(make_pair(equations[i].second,values[i]));
            graph[equations[i].second].push_back(make_pair(equations[i].first,1.0/values[i]));
        }
        vector<double> result;
        for(pair<string,string> q:queries){
            set<string> visited;
            if(graph.count(q.first)==0||graph.count(q.second)==0) result.push_back(-1.0);
            else if(q.first==q.second) result.push_back(1.0);
            else{
                auto temp = search(q.first,q.second,1.0,visited);
                result.push_back(temp);
            }   
        }
        return result;
    }
    double search(string source,string target,double weight,set<string> visited){
        if(visited.count(source)==0){
            visited.insert(source);
            for(pair<string,double> p:graph[source]){
            if(p.first==target)
                return p.second*weight;
            double rt= search(p.first,target,p.second*weight,visited);
            if(rt!=-1.0) return rt;
            }
        }
        return -1.0;
    }
};

方法二:

floyd算法:

      Floyd算法的基本思想是:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B。所以,我们假设Dis(AB)为节点A到节点B的最短路径的距离,对于每一个节点X,我们检查Dis(AX) + Dis(XB) < Dis(AB)是否成立,如果成立,证明从A到X再到B的路径比A直接到B的路径短,我们便设置Dis(AB) = Dis(AX) + Dis(XB),这样一来,当我们遍历完所有节点X,Dis(AB)中记录的便是A到B的最短路径的距离。

      使用map存储图,构建邻接矩阵,与方法一不同的地方是,用于记录除数对应的被除数和商的数据结构换做了map。对于除数和被除数相等的情况将结果设置为1.0。然后更新所有的商,使其为最大值,同时也求出了一个点到所有其他点的距离,并存储在邻接矩阵中。最后输入需要查找的起点和终点即可。

class Solution {
public:
    map<string, map<string, double>> m;
    vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
        int n = equations.size();
        for (int i = 0; i < n; ++i) {
            m[equations[i].first][equations[i].second] = values[i];
            m[equations[i].second][equations[i].first] = 1.0 / values[i];
        }
        for (auto q: m)
        q.second[q.first] = 1.0;
    
        for (auto k = m.begin(); k != m.end(); ++k)
            for (auto i = m.begin(); i != m.end(); ++i)
                for (auto j = m.begin(); j != m.end(); ++j)
                    i->second[j->first] = max(i->second[j->first], i->second[k->first] * k->second[j->first]);
        vector<double> result;
        for (auto q: queries) {
            auto val = m[q.first][q.second];
            result.push_back(val ? val: -1.0);
        }
        return result;
    }
};


import os import numpy as np import torch import torch.nn as nn from torch.utils.data import Dataset, DataLoader import tifffile from sklearn.metrics import accuracy_score, classification_report class GeologicalDataset(Dataset): def __init__(self, positive_paths, negative_paths, transform=None): self.file_paths = [] self.labels = [] # 加载正样本 for path in positive_paths: for file in os.listdir(path): if file.endswith('.tif'): self.file_paths.append(os.path.join(path, file)) self.labels.append(1) # 加载负样本 for path in negative_paths: for file in os.listdir(path): if file.endswith('.tif'): self.file_paths.append(os.path.join(path, file)) self.labels.append(0) self.transform = transform def __len__(self): return len(self.file_paths) def __getitem__(self, idx): img = tifffile.imread(self.file_paths[idx]) label = self.labels[idx] # 验证数据维度 if img.shape[0] !=13: raise ValueError(f"Expected 13 channels, but got {img.shape[0]} channels in file {self.file_paths[idx]}") # 数据预处理 img = np.clip(img, -1e6, 1e6) for i in range(img.shape[0]): channel = img[i] mean = np.mean(channel) std = np.std(channel) if std > 0: img[i] = (channel - mean) / std img = torch.from_numpy(img).float() return img, label class SimplifiedLeNet5(nn.Module): def __init__(self, input_channels=13, input_size=32): super(SimplifiedLeNet5, self).__init__() self.conv1 = nn.Conv2d(input_channels, 26, 5, padding=0) self.relu1 = nn.ReLU() self.maxpool1 = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(26, 52, 3, padding=1) self.relu2 = nn.ReLU() self.maxpool2 = nn.MaxPool2d(2, 2) # 动态计算fc1的输入尺寸 with torch.no_grad(): x = torch.randn(1, input_channels, input_size, input_size) x = self.conv1(x) x = self.relu1(x) x = self.maxpool1(x) x = self.conv2(x) x = self.relu2(x) x = self.maxpool2(x) self.fc_input_size = x.view(1, -1).size(1) self.flatten = nn.Flatten() self.fc1 = nn.Linear(self.fc_input_size, 120) self.relu3 = nn.ReLU() self.dropout = nn.Dropout(0.5) self.fc2 = nn.Linear(120, 84) self.relu4 = nn.ReLU() self.output = nn.Linear(84, 2) self._initialize_weights() def _initialize_weights(self): for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.xavier_uniform_(m.weight) if m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.xavier_uniform_(m.weight) nn.init.constant_(m.bias, 0) def forward(self, x): x = self.conv1(x) x = self.relu1(x) x = self.maxpool1(x) x = self.conv2(x) x = self.relu2(x) x = self.maxpool2(x) x = self.flatten(x) x = self.fc1(x) x = self.relu3(x) x = self.dropout(x) x = self.fc2(x) x = self.relu4(x) x = self.output(x) return x def train_and_evaluate(): # 设置随机种子 torch.manual_seed(42) np.random.seed(42) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print(f"Using device: {device}") # 数据路径配置 train_positive = [r'D:\201app\arcgis出图资料\地质特征量化\样本_芮芮\分割数据集\正样本\训练集'] train_negative = [r"D:\201app\arcgis出图资料\地质特征量化\样本_芮芮\分割数据集\负样本\训练集"] test_positive = [r"D:\201app\arcgis出图资料\地质特征量化\样本_芮芮\分割数据集\正样本\测试集"] test_negative = [r"D:\201app\arcgis出图资料\地质特征量化\样本_芮芮\分割数据集\负样本\测试集"] # 创建数据集和数据加载器 train_dataset = GeologicalDataset(train_positive, train_negative) test_dataset = GeologicalDataset(test_positive, test_negative) # # 验证输入尺寸 # sample_img, _ = train_dataset[0] # print(f"Input image size: {sample_img.shape[1:]}") # 输出应该是 [24, 24] # # # 动态获取输入尺寸并初始化模型 # input_size = sample_img.shape[1] # 假设高度和宽度相同 # model = SimplifiedLeNet5(input_size=input_size).to(device) train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True, num_workers=4) test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False, num_workers=4) # 初始化模型 model = SimplifiedLeNet5().to(device) criterion = nn.CrossEntropyLoss() optimizer = torch.optim.AdamW(model.parameters(), lr=1e-4, weight_decay=1e-4) # 学习率调度器 scheduler = torch.optim.lr_scheduler.OneCycleLR( optimizer, max_lr=1e-3, steps_per_epoch=len(train_loader), epochs=20, pct_start=0.3 ) # 训练参数 num_epochs = 20 best_acc = 0.0 save_dir = r"D:\201app\arcgis出图资料\地质特征量化\模型" os.makedirs(save_dir, exist_ok=True) # 训练循环 for epoch in range(num_epochs): model.train() running_loss = 0.0 all_preds = [] all_labels = [] for inputs, labels in train_loader: inputs, labels = inputs.to(device), labels.to(device) optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, labels.long()) loss.backward() optimizer.step() scheduler.step() running_loss += loss.item() preds = torch.argmax(outputs, dim=1) all_preds.extend(preds.cpu().numpy()) all_labels.extend(labels.cpu().numpy()) # 计算训练指标 train_acc = accuracy_score(all_labels, all_preds) print( f"Epoch [{epoch + 1}/{num_epochs}] | Loss: {running_loss / len(train_loader):.4f} | Train Acc: {train_acc:.4f}") # 每个epoch后进行验证 model.eval() test_preds = [] test_labels = [] with torch.no_grad(): for inputs, labels in test_loader: inputs = inputs.to(device) outputs = model(inputs) preds = torch.argmax(outputs, dim=1) test_preds.extend(preds.cpu().numpy()) test_labels.extend(labels.cpu().numpy()) # 计算测试指标 test_acc = accuracy_score(test_labels, test_preds) report = classification_report(test_labels, test_preds, target_names=['负样本', '正样本']) print(f"Validation Results:") print(report) # 保存最佳模型 if test_acc > best_acc: best_acc = test_acc torch.save(model.state_dict(), os.path.join(save_dir, "best_CNN_model.pth")) # 最终测试评估 print("\nFinal Test Evaluation:") final_acc, detailed_report, neg_acc, pos_acc = predict_model(model, test_loader, device) print(f"Overall Accuracy: {final_acc:.4f}") print(f"Negative Sample Recall: {neg_acc:.4f}") print(f"Positive Sample Recall: {pos_acc:.4f}") print("Detailed Classification Report:") print(detailed_report) def predict_model(model, test_loader, device): model.eval() all_preds = [] all_labels = [] with torch.no_grad(): for inputs, labels in test_loader: inputs = inputs.to(device) outputs = model(inputs) preds = torch.argmax(outputs, dim=1).cpu().numpy() all_preds.extend(preds) all_labels.extend(labels.numpy()) accuracy = accuracy_score(all_labels, all_preds) report = classification_report(all_labels, all_preds, target_names=['负样本', '正样本'], output_dict=True) return accuracy, report, report['负样本']['recall'], report['正样本']['recall'] if __name__ == "__main__": train_and_evaluate() 我的代码的训练过程是这样,有什么问题,帮我分析一下D:\201app\PythonProject_new\.venv\Scripts\python.exe D:\201app\PythonProject_new\地质特征量化\CNN_SimplifiedLeNet5.py Using device: cpu Epoch [1/20] | Loss: 0.3016 | Train Acc: 0.9232 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 Epoch [2/20] | Loss: 0.2439 | Train Acc: 0.9449 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 Epoch [3/20] | Loss: 0.2056 | Train Acc: 0.9449 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 Epoch [4/20] | Loss: 0.1867 | Train Acc: 0.9449 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 Epoch [5/20] | Loss: 0.1820 | Train Acc: 0.9488 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 Epoch [6/20] | Loss: 0.1317 | Train Acc: 0.9528 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 Epoch [7/20] | Loss: 0.1064 | Train Acc: 0.9587 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 Epoch [8/20] | Loss: 0.0677 | Train Acc: 0.9783 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 Epoch [9/20] | Loss: 0.0520 | Train Acc: 0.9803 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 Epoch [10/20] | Loss: 0.0329 | Train Acc: 0.9941 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 Epoch [11/20] | Loss: 0.0190 | Train Acc: 0.9921 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 Epoch [12/20] | Loss: 0.0074 | Train Acc: 1.0000 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 Epoch [13/20] | Loss: 0.0047 | Train Acc: 1.0000 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 Epoch [14/20] | Loss: 0.0023 | Train Acc: 1.0000 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 Epoch [15/20] | Loss: 0.0022 | Train Acc: 1.0000 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 Epoch [16/20] | Loss: 0.0034 | Train Acc: 1.0000 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 Epoch [17/20] | Loss: 0.0021 | Train Acc: 1.0000 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 Epoch [18/20] | Loss: 0.0023 | Train Acc: 1.0000 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 Epoch [19/20] | Loss: 0.0028 | Train Acc: 1.0000 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Epoch [20/20] | Loss: 0.0019 | Train Acc: 1.0000 D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Validation Results: precision recall f1-score support 负样本 0.00 0.00 0.00 7 正样本 0.94 1.00 0.97 120 accuracy 0.94 127 macro avg 0.47 0.50 0.49 127 weighted avg 0.89 0.94 0.92 127 Final Test Evaluation: D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) D:\201app\PythonProject_new\.venv\Lib\site-packages\sklearn\metrics\_classification.py:1565: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, f"{metric.capitalize()} is", len(result)) Overall Accuracy: 0.9449 Negative Sample Recall: 0.0000 Positive Sample Recall: 1.0000 Detailed Classification Report: {'负样本': {'precision': 0.0, 'recall': 0.0, 'f1-score': 0.0, 'support': 7.0}, '正样本': {'precision': 0.9448818897637795, 'recall': 1.0, 'f1-score': 0.97165991902834, 'support': 120.0}, 'accuracy': 0.9448818897637795, 'macro avg': {'precision': 0.47244094488188976, 'recall': 0.5, 'f1-score': 0.48582995951417, 'support': 127.0}, 'weighted avg': {'precision': 0.8928017856035712, 'recall': 0.9448818897637795, 'f1-score': 0.9181038604992189, 'support': 127.0}} Process finished with exit code 0
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值