AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • Início
  • system&network
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • Início
  • system&network
    • Recentes
    • Highest score
    • tags
  • Ubuntu
    • Recentes
    • Highest score
    • tags
  • Unix
    • Recentes
    • tags
  • DBA
    • Recentes
    • tags
  • Computer
    • Recentes
    • tags
  • Coding
    • Recentes
    • tags
Início / user-159072

user366312's questions

Martin Hope
user366312
Asked: 2024-05-23 18:41:37 +0800 CST

Como posso classificar os arquivos CSV por colunas como vemos nas planilhas?

  • 6

Suponha que eu tenha o seguinte arquivo CSV:

Column1,Column2,Column3
C,3,1
B,2,2
A,3,3
C,3,10
B,2,20
A,2,30
C,3,100
B,1,200
A,1,300

Eu quero resolver isso

  • Primeiro pela Coluna1
  • Segundo pela Coluna2
  • Terceiro pela Coluna3

Portanto o resultado deverá ser o seguinte:

Column1,Column2,Column3
A,1,300
A,2,30
A,3,3
B,1,200
B,2,2
B,2,20
C,3,1
C,3,10
C,3,100

Para classificar CSV por colunas em ordem crescente/decrescente, criei a seguinte classe:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace SikorskiLibMemoryLess
{
    public enum SortTypeEnum
    {
        Ascending,
        Descending
    }

    public static class ListExtensions
    {
        public static string ToCommaSeparatedString(this List<string> list)
        {
            if (list == null || !list.Any())
            {
                return string.Empty;
            }

            return string.Join(",", list);
        }
    }

    public class CSVSorter
    {
        public List<string> Header { get; private set; }
        public List<List<string>> Data { get; private set; }

        public CSVSorter()
        {
            Data = new List<List<string>>();
        }

        public void LoadCSV(string filePath, bool hasHeader = true)
        {
            using (StreamReader sr = new StreamReader(filePath))
            {
                string line;
                bool isFirstLine = true;

                while ((line = sr.ReadLine()) != null)
                {
                    List<string> columns = line.Split(',').Select(col => string.IsNullOrWhiteSpace(col) ? null : col).ToList();

                    if (isFirstLine && hasHeader)
                    {
                        Header = columns;
                        isFirstLine = false;
                    }
                    else
                    {
                        Data.Add(columns);
                        if (isFirstLine)
                        {
                            isFirstLine = false;
                            if (!hasHeader)
                            {
                                Header = Enumerable.Range(1, columns.Count).Select(i => "Column" + i).ToList();
                            }
                        }
                    }
                }
            }
        }

        public void LoadCSV(string fileName, string fileDir, bool hasHeader = true)
        {
            string filePath = Path.Combine(fileDir, fileName);
            LoadCSV(filePath, hasHeader);
        }

        public void LoadCSV(IEnumerable<List<string>> csvData, bool hasHeader = true)
        {
            bool isFirstLine = true;

            foreach (var columns in csvData)
            {
                var processedColumns = columns.Select(col => string.IsNullOrWhiteSpace(col) ? null : col).ToList();
                if (isFirstLine && hasHeader)
                {
                    Header = processedColumns;
                    isFirstLine = false;
                }
                else
                {
                    Data.Add(processedColumns);
                    if (isFirstLine)
                    {
                        isFirstLine = false;
                        if (!hasHeader)
                        {
                            Header = Enumerable.Range(1, processedColumns.Count).Select(i => "Column" + i).ToList();
                        }
                    }
                }
            }
        }

        public void Sort(int[] columns, SortTypeEnum ascendingOrDescending)
        {
            try
            {
                IOrderedEnumerable<List<string>> sortedData = null;

                if (ascendingOrDescending == SortTypeEnum.Ascending)
                {
                    sortedData = Data.OrderBy(row => GetColumnValueSafe(row, columns[0]));
                    for (int i = 1; i < columns.Length; i++)
                    {
                        sortedData = sortedData.ThenBy(row => GetColumnValueSafe(row, columns[i]));
                    }
                }

                Data = sortedData.ToList();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error during sorting: {ex.Message}");
                Console.WriteLine(ex.StackTrace);
            }
        }

        private string GetColumnValueSafe(List<string> row, int columnIndex)
        {
            return columnIndex < row.Count ? row[columnIndex] : null;
        }

        public IEnumerable<List<string>> Get()
        {
            yield return Header;
            foreach (var row in Data)
            {
                yield return row;
            }
        }

        public void SaveCSV(string filePath)
        {
            using (StreamWriter sw = new StreamWriter(filePath))
            {
                // Write the header
                sw.WriteLine(string.Join(",", Header));

                // Write the data rows
                foreach (var row in Data)
                {
                    sw.WriteLine(string.Join(",", row.Select(col => col ?? "")));
                }
            }
        }

        public void SaveCSV(string fileName, string fileDir)
        {
            string filePath = Path.Combine(fileDir, fileName);
            SaveCSV(filePath);
        }
    }

    class Program
    {
        static void Main()
        {
            // Example usage for CSV with potential missing data
            CSVSorter sorter = new CSVSorter();
            sorter.LoadCSV(@"C:\git\rouse_data~~\[PENULTIMATE]\OUTPUT_CSV\input.csv", true); // Adjust path as needed
            sorter.Sort(new int[] { 0,1,2 }, SortTypeEnum.Ascending); // Sort by the second column (e.g., particleVolume) in ascending order
            sorter.SaveCSV(@"C:\git\rouse_data~~\[PENULTIMATE]\OUTPUT_CSV\sorted___input.csv");

            // Display sorted data
            foreach (var row in sorter.Get())
            {
                Console.WriteLine(string.Join(", ", row));
            }
        }
    }
}

Esta aula não está me dando o resultado desejado.

Column1,Column2,Column3
C,3,1
B,2,2
A,3,3
C,3,10
B,2,20
A,2,30
C,3,100
B,1,200
A,1,300

Existe alguma biblioteca de código aberto para classificar dados CSV por colunas?

c#
  • 1 respostas
  • 29 Views
Martin Hope
user366312
Asked: 2024-01-22 21:43:50 +0800 CST

Como posso retornar A e b desta função de ajuste exponencial?

  • 5
using System;
using System.Collections.Generic;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.Optimization;

namespace NonLinearRegressionExample
{
    public class NonlinearRegressionCurveFitting
    {
        public static (List<double>, List<double>, List<double>) FitCurve(List<double> xData, List<double> yData)
        {
            // example data
            var xDataDense = new DenseVector(xData.ToArray());
            var yDataDense = new DenseVector(yData.ToArray());

            Vector<double> Model(Vector<double> parameters, Vector<double> x)
            {
                var y = CreateVector.Dense<double>(x.Count);
                for (int i = 0; i < x.Count; i++)
                {
                    y[i] = parameters[0] * Math.Exp(parameters[1] * x[i]);
                }
                return y;
            }

            var start = new DenseVector(new double[] { 1.0, 0.1 });
            var objective = ObjectiveFunction.NonlinearModel(Model, xDataDense, yDataDense);
            var solver = new LevenbergMarquardtMinimizer(maximumIterations: 10000);
            var result = solver.FindMinimum(objective, start);

            Vector<double> points = result.MinimizedValues;

            Vector<double> minimizing = result.MinimizingPoint;

            return (xData, new List<double>(points.ToArray()), new List<double>(minimizing.ToArray()));
        }
    }
}

Programa de motorista:

var fittedCurve = NonlinearRegressionCurveFitting.FitCurve(xData, yData);

            List<double> points = fittedCurve.Item2;
            List<double> result = fittedCurve.Item3;

            ///Output the results
            if (result.Count == 2)
            {
                DataPlotter plotter = new DataPlotter();
                //plotter.ZoomXaxis(0, 10);
                plotter.AddCurve("original",
                    new List<double>(xData.ToArray()),
                    new List<double>(yData.ToArray()),
                    IsSymbolVisible: false, color: Color.Red);
                plotter.AddCurve("fitted",
                    new List<double>(xData.ToArray()),
                    new List<double>(points.ToArray()),
                    IsSymbolVisible: false,
                    color: Color.Green);
                plotter.AddPoint("Point", result[0], result[1], Color.BlueViolet);
                plotter.ShowDialog();
            }

O código-fonte acima se ajusta a uma curva exponencial usando regressão não linear.

No entanto, como posso retornar os melhores Avalores bdesta função C# para a fórmula exponencial

y = A * exp(-x * b)

?

.

Esta função está retornando, MinimizingPointo que parece ser o ponto inicial do eixo x.

insira a descrição da imagem aqui

.

c#
  • 1 respostas
  • 70 Views
Martin Hope
user366312
Asked: 2024-01-14 00:58:48 +0800 CST

A função não consegue detectar interseções de duas curvas

  • 6

O código-fonte abaixo deve encontrar uma interseção entre duas curvas.

No entanto, a função não consegue detectar o ponto de intersecção.

Como posso consertar isso?

insira a descrição da imagem aqui

using MathNet.Numerics.Interpolation;
using System.Collections.Generic;
using System.Linq;

public static Vec2 FindIntersectionOfTwoCurves(List<double> xList1, List<double> yList1,
                                               List<double> xList2, List<double> yList2)
{
    if (xList1 == null || yList1 == null || xList2 == null || yList2 == null ||
        xList1.Count != yList1.Count || xList2.Count != yList2.Count)
        return null;

    IInterpolation interpolation1 = Interpolate.Linear(xList1, yList1);
    IInterpolation interpolation2 = Interpolate.Linear(xList2, yList2);

    double lowerBound = Math.Max(xList1.Min(), xList2.Min());
    double upperBound = Math.Min(xList1.Max(), xList2.Max());

    double step = (upperBound - lowerBound) / 1000; // adjust the step size as needed
    for (double x = lowerBound; x <= upperBound; x += step)
    {
        double y1 = interpolation1.Interpolate(x);
        double y2 = interpolation2.Interpolate(x);

        if (Math.Abs(y1 - y2) < 1e-7)
        {
            return new Vec2(x, y1);
        }
    }

    return null;
}

public class Vec2
{
    public double X { get; set; }
    public double Y { get; set; }

    public Vec2(double x, double y)
    {
        X = x;
        Y = y;
    }
}
c#
  • 2 respostas
  • 87 Views
Martin Hope
user366312
Asked: 2023-10-25 08:48:05 +0800 CST

o quadro de dados não é empilhado verticalmente quando salvo

  • 5

dados1.dat

   1 ASN C  7.042   9.118  0.000 1 1 1 1  1  0
   2 LEU H  5.781   5.488  7.470 0 0 0 0  1  0
   3 THR H  5.399   5.166  6.452 0 0 0 0  0  0
   4 GLU H  5.373   4.852  6.069 0 0 0 0  1  0
   5 LEU H  5.423   5.164  6.197 0 0 0 0  2  0
   6 LYS H  5.247   4.943  6.434 0 0 0 0  1  0
   7 ASN C  5.485   8.103  8.264 0 0 0 0  1  0
   8 THR C  6.675   9.152  9.047 0 0 0 0  1  0
   9 PRO C  6.372   8.536 11.954 0 0 0 0  0  0
  10 VAL H  5.669   5.433  6.703 0 0 0 0  0  0
  11 SER H  5.304   4.924  6.407 0 0 0 0  0  0
  12 GLU H  5.461   5.007  6.088 0 0 0 0  1  0
  13 LEU H  5.265   5.057  6.410 0 0 0 0  3  0
  14 ILE H  5.379   5.026  6.206 0 0 0 0  1  0
  15 THR H  5.525   5.154  6.000 0 0 0 0  1  0
  16 LEU H  5.403   5.173  6.102 0 0 0 0  1  0
  17 GLY H  5.588   5.279  6.195 0 0 0 0  1  0
  18 GLU H  5.381   5.238  6.675 0 0 0 0  1  0
  19 ASN H  5.298   5.287  6.668 0 0 0 0  1  0
  20 MSE H  5.704   7.411  4.926 0 0 0 0  1  0

dados2.dat

  21 GLY C  5.978   9.254  9.454 0 0 0 0  1  0
  22 LEU C  6.778  10.534 12.640 0 0 1 2  2  0
  23 GLU C  7.187   7.217 10.728 0 0 0 0  2  0
  24 ASN C  5.392   8.296 10.702 0 0 0 0  0  0
  25 LEU C  5.657   6.064  9.609 0 0 0 1  3  0
  26 ALA C  5.446   5.528  7.503 0 0 0 0  2  0
  27 ARG C  5.656   8.071  8.419 0 0 0 0  0  0
  28 MSE C  6.890   9.157  8.728 0 0 0 0  1  0
  29 ARG C  6.330   7.993 11.562 0 0 0 0  0  0
  30 LYS H  5.428   5.207  5.897 0 0 0 0  1  0
  31 GLN H  5.402   5.046  6.349 0 0 0 0  1  0
  32 ASP H  5.426   5.093  6.226 0 0 0 1  1  0
  33 ILE H  5.361   5.004  6.194 0 0 0 0  6  0
  34 ILE H  5.443   5.150  6.190 0 0 0 0  5  0
  35 PHE H  5.403   5.181  6.293 0 0 0 0  1  0
  36 ALA H  5.533   5.357  6.193 0 0 0 0  3  0
  37 ILE H  5.634   5.167  6.025 0 0 0 1  5  0
  38 LEU H  5.402   5.121  6.104 0 0 0 0  3  0
  39 LYS H  5.470   5.092  6.101 0 0 0 0  1  0
  40 GLN H  5.491   5.210  6.054 0 0 0 0  2  0
import os
import pandas as pd
from src.utils.get_root_dir import get_root_directory

def save_dataframe_to_ascii(df, filepath):
    df.to_csv(filepath, sep=',', index=False)

def getDataFrame(dataDirectoryPathString: str) -> pd.DataFrame:
    dataframes = []
    for filename in os.listdir(dataDirectoryPathString):
        if filename.endswith('.dat'):
            filepath = os.path.join(dataDirectoryPathString, filename)
            df = pd.read_csv(filepath, sep='\t')
            dataframes.append(df)
    concatenated_df = pd.concat(dataframes, ignore_index=True)
    return concatenated_df


if __name__ == "__main__":
    dataFrame = getDataFrame(get_root_directory() + "/data/")
    save_dataframe_to_ascii(dataFrame, get_root_directory() + "/save/save.txt")

Saída:

   1 ASN C  7.042   9.118  0.000 1 1 1 1  1  0,  21 GLY C  5.978   9.254  9.454 0 0 0 0  1  0
   2 LEU H  5.781   5.488  7.470 0 0 0 0  1  0,
   3 THR H  5.399   5.166  6.452 0 0 0 0  0  0,
   4 GLU H  5.373   4.852  6.069 0 0 0 0  1  0,
   5 LEU H  5.423   5.164  6.197 0 0 0 0  2  0,
   6 LYS H  5.247   4.943  6.434 0 0 0 0  1  0,
   7 ASN C  5.485   8.103  8.264 0 0 0 0  1  0,
   8 THR C  6.675   9.152  9.047 0 0 0 0  1  0,
   9 PRO C  6.372   8.536 11.954 0 0 0 0  0  0,
  10 VAL H  5.669   5.433  6.703 0 0 0 0  0  0,
  11 SER H  5.304   4.924  6.407 0 0 0 0  0  0,
  12 GLU H  5.461   5.007  6.088 0 0 0 0  1  0,
  13 LEU H  5.265   5.057  6.410 0 0 0 0  3  0,
  14 ILE H  5.379   5.026  6.206 0 0 0 0  1  0,
  15 THR H  5.525   5.154  6.000 0 0 0 0  1  0,
  16 LEU H  5.403   5.173  6.102 0 0 0 0  1  0,
  17 GLY H  5.588   5.279  6.195 0 0 0 0  1  0,
  18 GLU H  5.381   5.238  6.675 0 0 0 0  1  0,
  19 ASN H  5.298   5.287  6.668 0 0 0 0  1  0,
  20 MSE H  5.704   7.411  4.926 0 0 0 0  1  0,
,  22 LEU C  6.778  10.534 12.640 0 0 1 2  2  0
,  23 GLU C  7.187   7.217 10.728 0 0 0 0  2  0
,  24 ASN C  5.392   8.296 10.702 0 0 0 0  0  0
,  25 LEU C  5.657   6.064  9.609 0 0 0 1  3  0
,  26 ALA C  5.446   5.528  7.503 0 0 0 0  2  0
,  27 ARG C  5.656   8.071  8.419 0 0 0 0  0  0
,  28 MSE C  6.890   9.157  8.728 0 0 0 0  1  0
,  29 ARG C  6.330   7.993 11.562 0 0 0 0  0  0
,  30 LYS H  5.428   5.207  5.897 0 0 0 0  1  0
,  31 GLN H  5.402   5.046  6.349 0 0 0 0  1  0
,  32 ASP H  5.426   5.093  6.226 0 0 0 1  1  0
,  33 ILE H  5.361   5.004  6.194 0 0 0 0  6  0
,  34 ILE H  5.443   5.150  6.190 0 0 0 0  5  0
,  35 PHE H  5.403   5.181  6.293 0 0 0 0  1  0
,  36 ALA H  5.533   5.357  6.193 0 0 0 0  3  0
,  37 ILE H  5.634   5.167  6.025 0 0 0 1  5  0
,  38 LEU H  5.402   5.121  6.104 0 0 0 0  3  0
,  39 LYS H  5.470   5.092  6.101 0 0 0 0  1  0
,  40 GLN H  5.491   5.210  6.054 0 0 0 0  2  0

As linhas deveriam ter sido empilhadas verticalmente.

Por que a saída está quebrada?

Como posso consertar isso?

python
  • 1 respostas
  • 39 Views
Martin Hope
user366312
Asked: 2023-09-17 20:04:05 +0800 CST

Por que trunc() retorna 0?

  • 7
  • O teste de unidade falha mesmo que os valores sejam iguais

Eu estava preso no problema acima.

Ao tentar resolver isso, escrevi a seguinte listagem.

Espero que mantenha dois dígitos após a vírgula.

#include "simulation.hpp"
#include <cstdio>

typedef double real;

double truncate_(double f)
{
    return std::trunc(f * 100.0);
}

int main()
{
    //Simulation sim;
    //sim.run();
    LennardJones lj(Constants::EPSILON, Constants::SIGMA, Constants::KB);

    Vec3 diatance(4.0, 4.0, 4.0);

    real attractive = lj.getPotentialAttractive(diatance);
    real repulsive = lj.getPotentialRepulsive(diatance);

    std::cout << attractive << std::endl;
    std::cout << repulsive << std::endl;

    real attractive2 = truncate_(attractive);
    real repulsive2 = truncate_(repulsive);

    std::cout << attractive2 << std::endl;
    std::cout << repulsive2 << std::endl;

    getchar(); // Wait for a keypress
}

Saída:

-4.84292e-06
6.82474e-08
-0
0
c++
  • 1 respostas
  • 65 Views
Martin Hope
user366312
Asked: 2023-08-19 19:26:31 +0800 CST

Por que eles estão preenchendo apenas uma memória compartilhada?

  • 4
  • Um tratamento otimizado de acessos strided usando leituras aglutinadas da memória global

O link acima diz que:

__global__ void coalescedMultiply(float *a, float *c, int M)
{
  __shared__ float aTile[TILE_DIM][TILE_DIM],
                   transposedTile[TILE_DIM][TILE_DIM];
  int row = blockIdx.y * blockDim.y + threadIdx.y;
  int col = blockIdx.x * blockDim.x + threadIdx.x;
  float sum = 0.0f;
  aTile[threadIdx.y][threadIdx.x] = > a[row*TILE_DIM+threadIdx.x];
  transposedTile[threadIdx.x][threadIdx.y] =
      a[(blockIdx.x*blockDim.x + threadIdx.y)*TILE_DIM +
      threadIdx.x];  
  __syncthreads();
  for (int i = 0; i < TILE_DIM; i++) {
      sum += aTile[threadIdx.y][i]* transposedTile[i][threadIdx.x];
  }
  c[row*M+col] = sum;
}

(...)
Esses conflitos multibanco são muito caros. A solução simples é preencher a matriz de memória compartilhada para que ela tenha uma coluna extra, como na linha de código a seguir.

__shared__ float transposedTile[TILE_DIM][TILE_DIM+1];

Minha pergunta é,

Por que eles estão preenchendo apenas a memória compartilhada do lado direito ( transposedTile), não ambos ( transposedTilee aTile)?

cuda
  • 1 respostas
  • 28 Views

Sidebar

Stats

  • Perguntas 205573
  • respostas 270741
  • best respostas 135370
  • utilizador 68524
  • Highest score
  • respostas
  • Marko Smith

    Reformatar números, inserindo separadores em posições fixas

    • 6 respostas
  • Marko Smith

    Por que os conceitos do C++20 causam erros de restrição cíclica, enquanto o SFINAE antigo não?

    • 2 respostas
  • Marko Smith

    Problema com extensão desinstalada automaticamente do VScode (tema Material)

    • 2 respostas
  • Marko Smith

    Vue 3: Erro na criação "Identificador esperado, mas encontrado 'import'" [duplicado]

    • 1 respostas
  • Marko Smith

    Qual é o propósito de `enum class` com um tipo subjacente especificado, mas sem enumeradores?

    • 1 respostas
  • Marko Smith

    Como faço para corrigir um erro MODULE_NOT_FOUND para um módulo que não importei manualmente?

    • 6 respostas
  • Marko Smith

    `(expression, lvalue) = rvalue` é uma atribuição válida em C ou C++? Por que alguns compiladores aceitam/rejeitam isso?

    • 3 respostas
  • Marko Smith

    Um programa vazio que não faz nada em C++ precisa de um heap de 204 KB, mas não em C

    • 1 respostas
  • Marko Smith

    PowerBI atualmente quebrado com BigQuery: problema de driver Simba com atualização do Windows

    • 2 respostas
  • Marko Smith

    AdMob: MobileAds.initialize() - "java.lang.Integer não pode ser convertido em java.lang.String" para alguns dispositivos

    • 1 respostas
  • Martin Hope
    Fantastic Mr Fox Somente o tipo copiável não é aceito na implementação std::vector do MSVC 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant Encontre o próximo dia da semana usando o cronógrafo 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor O inicializador de membro do construtor pode incluir a inicialização de outro membro? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský Por que os conceitos do C++20 causam erros de restrição cíclica, enquanto o SFINAE antigo não? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul O C++20 mudou para permitir a conversão de `type(&)[N]` de matriz de limites conhecidos para `type(&)[]` de matriz de limites desconhecidos? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann Como/por que {2,3,10} e {x,3,10} com x=2 são ordenados de forma diferente? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller O ponto e vírgula agora é opcional em condicionais bash com [[ .. ]] na versão 5.2? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench Por que um traço duplo (--) faz com que esta cláusula MariaDB seja avaliada como verdadeira? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng Por que `dict(id=1, **{'id': 2})` às vezes gera `KeyError: 'id'` em vez de um TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob: MobileAds.initialize() - "java.lang.Integer não pode ser convertido em java.lang.String" para alguns dispositivos 2024-03-20 03:12:31 +0800 CST

Hot tag

python javascript c++ c# java typescript sql reactjs html

Explore

  • Início
  • Perguntas
    • Recentes
    • Highest score
  • tag
  • help

Footer

AskOverflow.Dev

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve