如何在C#中将string [] []转换为double [] []或int [] []

时间:2018-03-24 06:45:32

标签: c# string type-conversion

我有一个二维字符串数组,想要在doubleint转换为classficaton。

我尝试了codification但它出现了以下错误,因此我需要一种替代方法。

  

尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

例如,

string[] columnNames = { "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis" };

string[][] data = {
    new string[] { "Sunny", "Hot", "High", "Weak", "No" },
    new string[] { "Sunny", "Hot", "High", "Strong", "No" },
    new string[] { "Overcast", "Hot", "High", "Weak", "Yes" },
    new string[] { "Rain", "Mild", "High", "Weak", "Yes" },
    new string[] { "Rain", "Cool", "Normal", "Weak", "Yes" },
    new string[] { "Rain", "Cool", "Normal", "Strong", "No" },
    new string[] { "Overcast", "Cool", "Normal", "Strong", "Yes" },
    new string[] { "Sunny", "Mild", "High", "Weak", "No" },
    new string[] { "Sunny", "Cool", "Normal", "Weak", "Yes" },
    new string[] {  "Rain", "Mild", "Normal", "Weak", "Yes" },
    new string[] {  "Sunny", "Mild", "Normal", "Strong", "Yes" },
    new string[] {  "Overcast", "Mild", "High", "Strong", "Yes" },
    new string[] {  "Overcast", "Hot", "Normal", "Weak", "Yes" },
    new string[] {  "Rain", "Mild", "High", "Strong", "No" },
};

// Create a new codification codebook to
// convert strings into discrete symbols
Codification codebook = new Codification(columnNames, data);

// Extract input and output pairs to train
int[][] symbols = codebook.Transform(data);
int[][] inputs = symbols.Get(null, 0, -1); // Gets all rows, from 0 to the last (but not the last)
int[] outputs = symbols.GetColumn(-1);     // Gets only the last column

我也试过了,

 double[][] double_data = data.Select(x => x.Select(str => Convert.ToDouble(str)).ToArray()).ToArray();

并得到了相同的例外情况。如何解决它。

0 个答案:

没有答案