Home > database >  Create automatically property and assign value from column
Create automatically property and assign value from column

Time:01-21

I`m building a model from this tutorial: https://docs.microsoft.com/en-us/dotnet/machine-learning/tutorials/iris-clustering

I`m wondering if there is a better solution for this part of code, which would not require writing [LoadColumn(n)] every time as well as creating new properties.

public class IrisData
{
    [LoadColumn(0)]
    public float SepalLength;

    [LoadColumn(1)]
    public float SepalWidth;

    [LoadColumn(2)]
    public float PetalLength;

    [LoadColumn(3)]
    public float PetalWidth;
}

I was thinking to replace the code above with something like:

IDictionary<int, float> irisData = new Dictionary<int, float>();
    for (int i = 0; i < 4; i  )
    {
        irisData.Add(i, [LoadColumn(i)])
    }

But it gives me a bunch of errors. What is the best way to do it?


It seems easier that I thought. I ended up with this solution:

[LoadColumn(0, 3)]
[VectorType(4)]
public float[] iris_data { get; set; }

CodePudding user response:

Assuming, the number of properties is not 4, but 400, it would be too hard type [LoadColumn(n) public float name_n; by hand

From this comment on another answer, don't forget; you're a software developer - you can code up a solution to the problem i.e. write some code that writes C# like some steing concat and fill down on excel or SQL that outputs 400 rows that are c# text :)

Or you can use an already invented wheel. Sublime Text's a very powerful text editor with features that will handle this

If I had 400 public properties in a class and I wanted to put 400 attributes on, with an incrementing number in each I would

  • ensure I had sublime with the test pastry plug-in installed
  • paste my class in with 400 props
  • double click the top public key on the prop
  • hold down Ctrl-D on the keyboard (because I like taking a break) and watch sublime multiselect ervery one of 400 public words, giving me 400 cursors
  • type [LoadColumn( on the keyboard, watch sublime type it 400 times for me
  • call up the command palette window and find "text pastry 1 to X", also known as \i, run it. 1 to 400 appears in the document
  • finish typing )] <ENTER> public to close the attribute and restore the public that was overwritten initially

Visual Studio can do the same if you have Multiple Carets Booster installed

  • again select one public
  • hold down SHIFTALT> til they're all selected
  • type LoadColumn(1 so all the numbers are 1 then press SHIFTLEFT_CURSOR to select all the 1s
  • press SHIFTALTS to replace them with an incrementing sequence starting from 1
  • finish )] <ENTER> public

You can also achieve the same in VSCode (it has multiple carets and text pastry)

CodePudding user response:

I am not sure what's wrong with LoadColumn? It's declarative purpose is very different from the code that you show as imperative alternative. By putting LoadColumn(0) you create LoadColumnAttribute that maps a field to column in your data file.

What are you trying to accomplish with the imperative code? You could use something like new LoadColumnAttribute(i) - but again the result is the attribute that mathches field to column; not the value in the column

Maybe explain what you want to do

  •  Tags:  
  • Related