Could not find column 'PredictedLabel' - c#

I'm using ML.Net and trying to cluster some data, and the exception I'm getting is in the title: 'Could not find column 'PredictedLabel'. But I do have this column in my prediction class!
public class ClusterPrediction
{
public uint Label { get; set; }
// Predicted label from the trainer.
//[ColumnName("PredictedLabel")] <-- doesn't make a differnce if I have this or not
public uint PredictedLabel { get; set; }
public float[] Score { get; set; } = new float[0];
public float[] Features { get; set; } = new float[0];
}
Here is how I setup my pipeline:
var clusterSchema = SchemaDefinition.Create(typeof(InputCombinedClusterData));
for (int i = 0; i < 5; i++)
{
var col = clusterSchema[i];
var itemType = ((VectorDataViewType)col.ColumnType).ItemType;
col.ColumnType = new VectorDataViewType(itemType, InputCombinedClusterData.LOOKBACK);
}
var trainingData = mlContext.Data.LoadFromEnumerable(data.TrainingData);
var options = new KMeansTrainer.Options
{
// will tune this later
NumberOfClusters = 15,
OptimizationTolerance = 1e-9f,
MaximumNumberOfIterations = 10000,
};
var fundamentalCols = new List<string> { tons of columns };
string featuresColumnName = "Features";
var pipeline = mlContext.Transforms.Categorical.OneHotEncoding("Sector")
.Append(mlContext.Transforms.Categorical.OneHotEncoding("Industry"));
foreach (var col in fundamentalCols)
{
pipeline.Append(mlContext.Transforms.NormalizeMinMax(col));
}
pipeline.Append(mlContext.Transforms.Concatenate(featuresColumnName, "Open", "High", "Low", "Close", "Volume", "Sector", "Industry", {lots more columns}));
pipeline.Append(mlContext.Clustering.Trainers.KMeans(options));
// Train the model.
var model = pipeline.Fit(trainingData);
// test the model
var testData = mlContext.Data.LoadFromEnumerable(data.TestingData);
// Run the model on test data set
var transformedTestData = model.Transform(testData);
// Goes BOOM here
var predictions = mlContext.Data.CreateEnumerable<ClusterPrediction>(
transformedTestData, reuseRowObject: false).ToList();
I do have an alternate clustering trainer that DOES work, but it has far fewer columns. I'm not seeing the difference why the one above with more columns would blow up on not finding a PredictedLabel column when both trainers are using the same prediction class? Here is the one that does work for reference:
var clusterSchema = SchemaDefinition.Create(typeof(InputPriceClusterData));
for (int i = 0; i < 5; i++)
{
var col = clusterSchema[i];
var itemType = ((VectorDataViewType)col.ColumnType).ItemType;
col.ColumnType = new VectorDataViewType(itemType, InputPriceClusterData.LOOKBACK);
}
var trainingData = mlContext.Data.LoadFromEnumerable(data.TrainingData);
var options = new KMeansTrainer.Options
{
NumberOfClusters = 15,
OptimizationTolerance = 1e-9f,
MaximumNumberOfIterations = 10000
};
// Define the trainer.
string featuresColumnName = "Features";
var pipeline = mlContext.Transforms
.Concatenate(featuresColumnName, "Open", "High", "Low", "Close", "Volume")
.Append(mlContext.Clustering.Trainers.KMeans(options));
// Train the model.
var model = pipeline.Fit(trainingData);
// test the model
var testData = mlContext.Data.LoadFromEnumerable(data.TestingData);
// Run the model on test data set.
var transformedTestData = model.Transform(testData);
// Convert IDataView object to a list.
var predictions = mlContext.Data.CreateEnumerable<ClusterPrediction>(
transformedTestData, reuseRowObject: false).ToList();
For the initial broken trainer, what am I missing? Ordering of the appends?

Might be a bit of a facepalm moment, but the .Append method on a pipeline doesn't do an in place append like someIList.Sort() would. I also found that I can batch up all the column pairs for normalization too. Doing it all in 1 shot resolved the issue:
string featuresColumnName = "Features";
var colPairs = new List<InputOutputColumnPair>();
foreach (var col in fundamentalCols)
{
colPairs.Add(new InputOutputColumnPair(col, col));
}
var pipeline = mlContext.Transforms.Categorical.OneHotEncoding("Sector")
.Append(mlContext.Transforms.Categorical.OneHotEncoding("Industry"))
.Append(mlContext.Transforms.NormalizeMinMax(colPairs.ToArray()))
.Append(mlContext.Transforms.Concatenate(featuresColumnName, "Open", "High", "Low", "Close", "Volume", "Sector", "Industry", {boatload of columns}))
.Append(mlContext.Clustering.Trainers.KMeans(options));

Related

How can I store multiple dynamic products in stripe checkout webforms (ASP.NET C#) I tried a lot but static it's worked but dynamic not?

here is the code:-
static I used its worked fine.. how can I store product dynamically in using asp.net c#
LineItems = new List<SessionLineItemOptions>
{
for (int i = 0; i < dtOrder.Rows.Count; i++){
new SessionLineItemOptions
{
Name=dtOrder.Rows[i]["ProductName"].toString(),
Currency="cad",
Amount =Convert.toInt64(dtOrder>Rows[i]["Price"])*100,
Quantity = 1,
},
}
},
Extending the snippet shown in the API reference here, we can replace Price = 'price_123' with PriceData (API ref) like so:
var options = new SessionCreateOptions
{
SuccessUrl = "https://example.com/success",
CancelUrl = "https://example.com/cancel",
PaymentMethodTypes = new List<string>
{
"card",
},
LineItems = new List<SessionLineItemOptions>
{
new SessionLineItemOptions
{
PriceData = new SessionLineItemPriceDataOptions
{
Currency = "usd",
UnitAmount = 50000,
ProductData = new SessionLineItemPriceDataProductDataOptions
{
Name = "some product name",
}
},
Quantity = 2,
},
},
Mode = "payment",
};
var service = new SessionService();
service.Create(options);
You can find all the type definitions in the source code.
I integrated Mulitple Account Payment of stripe and fixed the issue like this
and its working for me now , you can also use simple checkout method like this fetching dynamically product from db
List lineItemsOptions = new List();
string cmdText = "select * from tableorder where sessionid='"+ sessionid + "'";
DataSet ds = dbm.getDs(cmdText);
foreach (DataRow row in ds.Tables[0].Rows)
{
var currentLineItem = new SessionLineItemOptions
{
Name = row["ProductName"].ToString(),
Amount = 100,//Convert.ToInt32( row["variationPrice"].ToString()),
Currency = "usd",
Quantity = Convert.ToInt32(row["Quantity"].ToString()),
};
lineItemsOptions.Add(currentLineItem);
}
StripeConfiguration.ApiKey = ".......................................";
var options = new SessionCreateOptions();
options = new SessionCreateOptions
{
PaymentMethodTypes = new List<string>
{
"card",
},
LineItems = lineItemsOptions,
PaymentIntentData = new SessionPaymentIntentDataOptions
{
ApplicationFeeAmount = 1,
},
Mode = "payment",
SuccessUrl = "https://www.......com/success.aspx",
CancelUrl = "https://example.com/cancel",
};
var requestOptions = new RequestOptions
{
StripeAccount = ".............",
};
var service = new SessionService();
Session session = service.Create(options, requestOptions);

Google Sheets API .NET v4 Creating Drop-down list for a grid cell

I'm trying to create a drop-down list on a grid. I was following the advice on this page. However, I'm not able to enter any string values for UserEnteredValue. The underlined error I'm getting:
Cannot implicitly convert type 'Google.Apis.Sheets.v4.Data.ConditionValue' to 'System.Collections.Generic.IList'.An explicit conversion exists(are you missing a cast?)
public Request createDataValidationRequest(int sheetID, int startRow, int endRow, int startColumn, int endColumn)
{
var updateCellsRequest = new Request() {
SetDataValidation = new SetDataValidationRequest()
{
Range = new GridRange()
{
SheetId = sheetID,
StartRowIndex = startRow,
StartColumnIndex = startColumn,
EndRowIndex = endRow,
EndColumnIndex = endColumn
},
Rule = new DataValidationRule()
{
Condition = new BooleanCondition()
{
Type = "ONE_OF_LIST",
Values = new ConditionValue()
{
UserEnteredValue = "awer"
}
},
InputMessage = "Select an Option",
ShowCustomUi = true,
Strict = true
}
}
};
return updateCellsRequest;
}
I was able to get the following, for my situation to work.
SpreadsheetsResource.GetRequest request = _sheetsService.Spreadsheets.Get(newTimeSheet.Id);
Spreadsheet spreadsheet = request.Execute();
SheetProperties timeSheetProperties = new SheetProperties();
for (int z = 0; z < spreadsheet.Sheets.Count; z++)
{
SheetProperties sheetProperties = spreadsheet.Sheets[z].Properties;
if (sheetProperties.Title == "3c TIME")
{
timeSheetProperties = sheetProperties;
break;
}
}
var updateCellsRequest = new Request()
{
SetDataValidation = new SetDataValidationRequest()
{
Range = new GridRange()
{
SheetId = timeSheetProperties.SheetId,
StartRowIndex = 2,
StartColumnIndex = 0,
EndColumnIndex = 1
},
Rule = new DataValidationRule()
{
Condition = new BooleanCondition()
{
//Type = "ONE_OF_RANGE",
Type = "ONE_OF_LIST",
Values = new List<ConditionValue>()
{
new ConditionValue()
{
UserEnteredValue = "YES",
},
new ConditionValue()
{
UserEnteredValue = "NO",
},
new ConditionValue()
{
UserEnteredValue = "MAYBE",
}
}
},
InputMessage = "Select an Option",
ShowCustomUi = true,
Strict = true
}
}
};
var requestBody = new Google.Apis.Sheets.v4.Data.BatchUpdateSpreadsheetRequest();
var requests = new List<Request>();
requests.Add(updateCellsRequest);
requestBody.Requests = requests;
var batchRequest = _sheetsService.Spreadsheets.BatchUpdate(requestBody, newTimeSheet.Id);
batchRequest.Execute();

foreach and index in .ToDictionary C#

I am web-scraping some data and trying to write the scraped data to a json file using C# newtonsoft.Json
I get stuck when writing a foreach in my .ToDictionary function as well as not being able to ++ an index into my .ToDictionary function.
My class:
public class JsonParametersData
{
public bool required { get; set; }
public bool list { get; set; }
public List<string> options { get; set; }
}
My arrays
var jsonData = new List<Dictionary<string, Dictionary<string, JsonParametersData>>>();
var moduleParameters = new List<string>();
var parameterOptionsArray = new List<List<string>>();
var parameterOptions = new List<string>();
var requiredArray = new List<bool>();
var listArray = new List<bool>();
string moduleName = item.Attributes["href"].Value.Replace("_module.html", "");
The code which is commented shows what I am trying to do.
int index = 0;
jsonData.Add(new Dictionary<string, Dictionary<string, JsonParametersData>>()
{
{
moduleName,
moduleParameters
.ToDictionary(n => n,
n => new JsonParametersData
{
required = requiredArray[index],
list = listArray[index],
options = new List<string>() { "option1", "option2" },
/*
foreach (var parameteroption in parameterOptionsArray[index])
{
options.Add(parameteroption);
}
index++;
*/
})
}
});
string json = JsonConvert.SerializeObject(jsonData.ToArray());
//write string to file
System.IO.File.WriteAllText(#"path", json);
Your parameterOptionsArray is not an Array, but a List of lists.
The thing is that parameterOptionsArray[index] is a List, not a string. So you should use AddRange() instead of Add().
parameterOptionsArray.Foreach(parameteroption => options.AddRange(parameteroption));
As I´ve written in the comments you can make only assignments in an object-initializer. Thus the following is allowed:
var a = new { MyMember = anInstance }
whilst this is not:
var a = new { MyMember = anInstance, anInstance.DoSomething() };
That´s one of those cases where you should not use Linq at all, as it leads to more confusion than it helps. Instead use a good old-styled loop:
int index = 0;
var innerDict = new Dictionary<string, JsonParametersData>();
foreach(var name in moduleParameters)
{
innerDict[name] = new JsonParametersData
{
required = requiredArray[index],
list = listArray[index],
options = new List<string>() { "option1", "option2" },
}
innerDict[name].Options.AddRange(parameterOptionsArray[index]);
index++;
}
var dict = new Dictionary<string, Dictionary<string, JsonParametersData>>();
dict[moduleName] = innerDict;
jsonData.Add(dict);
string json = JsonConvert.SerializeObject(jsonData.ToArray());
You appear to have a jagged array in parameterOptionsArray. You can make use of SelectMany here. Perhaps following sample can help:
string[][] parameterOptionsArray = new string[2][];
parameterOptionsArray[0] = new string[2];
parameterOptionsArray[0][0] = "1";
parameterOptionsArray[0][1] = "2";
parameterOptionsArray[1] = new string[2];
parameterOptionsArray[1][0] = "3";
parameterOptionsArray[1][1] = "4";
var testing = new {options = parameterOptionsArray.SelectMany(x => x).ToList()};
testing.options.ForEach(x => Console.WriteLine(x));

C# (Xamarin iOS) How to loop son data on Custom UITableViewCell?

Any Help will be appreaciated :) Thank you in advance
I tried to loop other object inside of the function and its working but on this, it can't loop. Help. this is rush, and I'm not that familiar with creating iOS app.
public override void ViewDidLoad()
{
base.ViewDidLoad();
using (var web = new WebClient())
{
var url = "http://www.creativeinterlace.com/smitten/maintenance/api/feeds/get-miss-location/101";
json = web.DownloadString(url);
}
json = json.Replace("{\"location\":", "").Replace("}]}", "}]");
var ls = JArray.Parse(json);
if (ls.Count != 0)
{
foreach (var x in ls)
{
var name = x.SelectToken("location");
name1 = Convert.ToString(name);
var loc = x.SelectToken("address");
loc1 = Convert.ToString(loc);
var time = x.SelectToken("time_ago");
time1 = Convert.ToString(time);
locations = new List<Locations>
{
new Locations
{
shopname = name1,
address= loc1,
time = time1
},
};
}
nmtable.Source = new LocationSource(locations);
nmtable.RowHeight = 60;
nmtable.ReloadData();
}
}
You initialize the locations every time in the loop,so the list updates with only the newest object. You should initialize the list outside of the loop , and add object every time.
locations = new List<Locations>();
if (ls.Count != 0)
{
foreach (var x in ls)
{
var name = x.SelectToken("location");
name1 = Convert.ToString(name);
var loc = x.SelectToken("address");
loc1 = Convert.ToString(loc);
var time = x.SelectToken("time_ago");
time1 = Convert.ToString(time);
locations.Add(new Locations{ shopname = name1,address= loc1,time = time1});
};
}

Passing multiple line items with GP webservice

Below is the code I'm working with to pass multiple line items to create sales order through GP Web service. I can pass single Line Item without any problem , but when I pass multiple Items it is only taking the last one. The array has around 5 Item ID and I'm passing fixed Quantity as 15, Need to make both dynamic. But for the testing purpose I'm trying like this. I know the problem with the creation/initialization of some web service objects. As novice to the entire set of things I couldn't find the exact problem.
C# Code
CompanyKey companyKey;
Context context;
SalesOrder salesOrder;
SalesDocumentTypeKey salesOrderType;
CustomerKey customerKey;
BatchKey batchKey;
// SalesOrderLine salesOrderLine;
ItemKey orderedItem;
Quantity orderedAmount;
Policy salesOrderCreatePolicy;
DynamicsGPClient wsDynamicsGP = new DynamicsGPClient();
wsDynamicsGP.ClientCredentials.Windows.ClientCredential.UserName = "Admin";
wsDynamicsGP.ClientCredentials.Windows.ClientCredential.Password = "pass";
wsDynamicsGP.ClientCredentials.Windows.ClientCredential.Domain = "Gp";
System.ServiceModel.WSHttpBinding binding;
binding = new System.ServiceModel.WSHttpBinding(System.ServiceModel.SecurityMode.None);
context = new Context();
companyKey = new CompanyKey();
companyKey.Id = (1);
context.OrganizationKey = (OrganizationKey)companyKey;
salesOrder = new SalesOrder();
salesOrderType = new SalesDocumentTypeKey();
salesOrderType.Type = SalesDocumentType.Order;
salesOrder.DocumentTypeKey = salesOrderType;
customerKey = new CustomerKey();
customerKey.Id = "121001";
salesOrder.CustomerKey = customerKey;
batchKey = new BatchKey();
batchKey.Id = "RMS";
salesOrder.BatchKey = batchKey;
// SalesOrderLine[] orders = new SalesOrderLine[6];
SalesOrderLine[] lines = { };
for (int i = 1; i < 5; i++)
{
SalesOrderLine salesOrderLine = new SalesOrderLine();
orderedItem = new ItemKey();
orderedItem.Id = Arr[i].ToString();
salesOrderLine.ItemKey = orderedItem;
orderedAmount = new Quantity();
orderedAmount.Value = 15;
salesOrderLine.Quantity = orderedAmount;
lines = new SalesOrderLine[] { salesOrderLine };
MessageBox.Show(lines.Count().ToString());
}
salesOrder.Lines = lines;
//salesOrder.Lines = orders;
salesOrderCreatePolicy = wsDynamicsGP.GetPolicyByOperation("CreateSalesOrder", context);
wsDynamicsGP.CreateSalesOrder(salesOrder, context, salesOrderCreatePolicy);
if (wsDynamicsGP.State != CommunicationState.Faulted)
{
wsDynamicsGP.Close();
}
MessageBox.Show("Success");
lines = new SalesOrderLine[] { salesOrderLine }; will recreate the lines array object each time meaning you loose any previously added objects. Effectively only the final object in the loop is actually added.
Try using a List<T> like so:
SalesOrderLine[] lines = { }; Becomes List<SalesOrderLine> lines = new List<SalesOrderLine>();
lines = new SalesOrderLine[] { salesOrderLine }; Becomes: lines.Add(salesOrderLine);
If its important you end up with an array as the input:
salesOrder.Lines = lines; Becomes: salesOrder.Lines = lines.ToArray();

Categories

Resources