Issue with ML.NET Fit: Source array was not long enough - c#

The full error is : 'Source array was not long enough. Check the source index, length, and the array's lower bounds. (Parameter 'sourceArray')'
(IDataView trainingDataView, IDataView testDataView) = LoadData(mlContext); // Load
IDataView transformedNewData = dataPrepPipeline.Transform(trainingDataView);
var est = mlContext.Transforms.Categorical.OneHotEncoding("usernameEncoded", "username").Append(mlContext.Transforms.Categorical.OneHotEncoding("tagEncoded", "tag"));
var fitted = est.Fit(transformedNewData);
var data = fitted.Transform(transformedNewData);
var testEst = mlContext.Transforms.Categorical.OneHotEncoding("usernameEncoded", "username").Append(mlContext.Transforms.Categorical.OneHotEncoding("tagEncoded", "tag"));
var testFitted = testEst.Fit(testDataView);
var testData = testFitted.Transform(testDataView);
var options = new FieldAwareFactorizationMachineTrainer.Options
{
FeatureColumnName = "usernameEncoded",
ExtraFeatureColumns = new string[] { "tagEncoded" },
LabelColumnName = "Label",
LambdaLatent = 0.01f,
LambdaLinear = 0.001f,
LatentDimension = 16,
NumberOfIterations = 50,
LearningRate = 0.5f,
Verbose = true
};
FieldAwareFactorizationMachinePredictionTransformer retrainedModel =
mlContext.BinaryClassification.Trainers.FieldAwareFactorizationMachine(options).Fit(data, testData, originalModelParameters); // Error is here

Related

Display Linestring and Trackpoints with Mapsui

my intention is to display a gps track and the corresponding trackpoints with Mapsui (wpf) on a map. I tried the following code. The result is that the blue linestring is displayed (ok), the red track points (ok) but for any reason you see white track points which are very large and I do not want them to appear at the map and I do not know where the white dots are coming from. Any idea what I am doing wrong?
protected ILayer CreateLineStringLayer(String name, List<GeoWaypoint> geoWaypoints)
{
var lineString = new LineString();
List<Feature> featureList = new List<Feature>();
IStyle pointStyle = new SymbolStyle()
{
SymbolScale = 0.30,
Fill = new Brush(Mapsui.Styles.Color.FromString("Red"))
};
foreach (var wp in geoWaypoints)
{
var point = SphericalMercator.FromLonLat(wp.Longitude, wp.Latitude);
lineString.Vertices.Add(point);
var p2 = SphericalMercator.FromLonLat(wp.Longitude, wp.Latitude);
var pointFeature = new Feature();
pointFeature.Geometry = p2;
pointFeature.Styles.Add(pointStyle);
featureList.Add(pointFeature);
}
IStyle linestringStyle = new VectorStyle()
{
Fill = null,
Outline = null,
Line = { Color = Mapsui.Styles.Color.FromString("Blue"), Width = 4 }
};
Feature lineStringFeature = new Feature()
{
Geometry = lineString
};
lineStringFeature.Styles.Add(linestringStyle);
featureList.Add(lineStringFeature);
MemoryProvider memoryProvider = new MemoryProvider(featureList);
return new MemoryLayer
{
DataSource = memoryProvider,
Name = name
};
}
so for everyone who is interest in the answer
return new MemoryLayer
{
DataSource = memoryProvider,
Name = name ,
Style = null
};
You need to set the value for Style to null for the Memorylayer

Styles in Excel using Open XML

I am quite new to C# & got stuck with a problem.
I am trying to add some style to the Excel using OpenXML, though it is not drawing borders, the fills (foreground & background colors) and alignment to the cells. I tried many times with different ways, though I am not able to do so.
The following code is not generating any error. Compiling successfully, however not giving the expected output.
Following is the actual output:
Actual Output
Following is the expected output:
Expected Output
The requirement is that only the top row to have this specific style, how can I achieve it?
Any guidance would be greatly appreciated.
Code snippet:
private void ExportDataSet()
{
WorkbookStylesPart wbsp = workbookPart.AddNewPart<WorkbookStylesPart>();
wbsp.Stylesheet = CreateStylesheet();
wbsp.Stylesheet.Save();
}
private static DocumentFormat.OpenXml.Spreadsheet.Stylesheet CreateStylesheet()
{
DocumentFormat.OpenXml.Spreadsheet.Stylesheet stylesheet1 = new DocumentFormat.OpenXml.Spreadsheet.Stylesheet();
#region Font
DocumentFormat.OpenXml.Spreadsheet.Fonts fonts = new DocumentFormat.OpenXml.Spreadsheet.Fonts() { Count = (DocumentFormat.OpenXml.UInt32Value)2U};
// FontID = 0 (The Default Font)
DocumentFormat.OpenXml.Spreadsheet.Font font0 = new DocumentFormat.OpenXml.Spreadsheet.Font();
DocumentFormat.OpenXml.Spreadsheet.FontSize fontSize0 = new DocumentFormat.OpenXml.Spreadsheet.FontSize() { Val = 11D };
DocumentFormat.OpenXml.Spreadsheet.Color color0 = new DocumentFormat.OpenXml.Spreadsheet.Color() { Theme = (DocumentFormat.OpenXml.UInt32Value)1U };
DocumentFormat.OpenXml.Spreadsheet.FontName fontName0 = new DocumentFormat.OpenXml.Spreadsheet.FontName() { Val = "Calibri" };
font0.Append(fontSize0);
font0.Append(color0);
font0.Append(fontName0);
// FontID = 1
DocumentFormat.OpenXml.Spreadsheet.Font font1 = new DocumentFormat.OpenXml.Spreadsheet.Font();
DocumentFormat.OpenXml.Spreadsheet.FontSize fontSize1 = new DocumentFormat.OpenXml.Spreadsheet.FontSize() { Val = 12D };
DocumentFormat.OpenXml.Spreadsheet.FontName fontName1 = new DocumentFormat.OpenXml.Spreadsheet.FontName() { Val = "Calibri" };
DocumentFormat.OpenXml.Spreadsheet.Color color1 = new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = new DocumentFormat.OpenXml.HexBinaryValue("5B9BD5") };
DocumentFormat.OpenXml.Spreadsheet.Bold bold1 = new DocumentFormat.OpenXml.Spreadsheet.Bold() { Val = true };
// Foreground & Background not working
DocumentFormat.OpenXml.Spreadsheet.ForegroundColor foregroundColor1 = new DocumentFormat.OpenXml.Spreadsheet.ForegroundColor() { Rgb = new DocumentFormat.OpenXml.HexBinaryValue("FF00FF00") };
DocumentFormat.OpenXml.Spreadsheet.BackgroundColor backgroundColor1 = new DocumentFormat.OpenXml.Spreadsheet.BackgroundColor() { Rgb = new DocumentFormat.OpenXml.HexBinaryValue("FF00FF00") };
font1.Append(fontSize1);
font1.Append(fontName1);
font1.Append(color1);
font1.Append(bold1);
font1.Append(foregroundColor1);
font1.Append(backgroundColor1);
fonts.Append(font1);
int fontIndex = fonts.Count() - 1;
#endregion
#region CellBorders
DocumentFormat.OpenXml.Spreadsheet.Borders borders = new DocumentFormat.OpenXml.Spreadsheet.Borders() { Count = (DocumentFormat.OpenXml.UInt32Value)2U };
// Border ID = 0 (The Default Border)
DocumentFormat.OpenXml.Spreadsheet.Border border0 = new DocumentFormat.OpenXml.Spreadsheet.Border();
DocumentFormat.OpenXml.Spreadsheet.LeftBorder leftBorder0 = new DocumentFormat.OpenXml.Spreadsheet.LeftBorder();
DocumentFormat.OpenXml.Spreadsheet.RightBorder rightBorder0 = new DocumentFormat.OpenXml.Spreadsheet.RightBorder();
DocumentFormat.OpenXml.Spreadsheet.TopBorder topBorder0 = new DocumentFormat.OpenXml.Spreadsheet.TopBorder();
DocumentFormat.OpenXml.Spreadsheet.BottomBorder bottomBorder0 = new DocumentFormat.OpenXml.Spreadsheet.BottomBorder();
border0.Append(leftBorder0);
border0.Append(rightBorder0);
border0.Append(topBorder0);
border0.Append(bottomBorder0);
// Border ID = 1
DocumentFormat.OpenXml.Spreadsheet.Border border1 = new DocumentFormat.OpenXml.Spreadsheet.Border();
DocumentFormat.OpenXml.Spreadsheet.LeftBorder leftBorder = new DocumentFormat.OpenXml.Spreadsheet.LeftBorder() { Style = DocumentFormat.OpenXml.Spreadsheet.BorderStyleValues.Thick };
DocumentFormat.OpenXml.Spreadsheet.RightBorder rightBorder = new DocumentFormat.OpenXml.Spreadsheet.RightBorder() { Style = DocumentFormat.OpenXml.Spreadsheet.BorderStyleValues.Thick };
DocumentFormat.OpenXml.Spreadsheet.TopBorder topBorder = new DocumentFormat.OpenXml.Spreadsheet.TopBorder() { Style = DocumentFormat.OpenXml.Spreadsheet.BorderStyleValues.Thick };
DocumentFormat.OpenXml.Spreadsheet.BottomBorder bottomBorder = new DocumentFormat.OpenXml.Spreadsheet.BottomBorder() { Style = DocumentFormat.OpenXml.Spreadsheet.BorderStyleValues.Thick };
border1.Append(leftBorder);
border1.Append(rightBorder);
border1.Append(topBorder);
border1.Append(bottomBorder);
borders.Append(border1);
int borderIndex = borders.Count() - 1;
#endregion
#region Fill
DocumentFormat.OpenXml.Spreadsheet.Fills fills = new DocumentFormat.OpenXml.Spreadsheet.Fills() { Count = (DocumentFormat.OpenXml.UInt32Value)3U };
// Fill ID = 0 (The Default Fill)
DocumentFormat.OpenXml.Spreadsheet.Fill fill0 = new DocumentFormat.OpenXml.Spreadsheet.Fill();
DocumentFormat.OpenXml.Spreadsheet.PatternFill patternFill0 = new DocumentFormat.OpenXml.Spreadsheet.PatternFill() { PatternType = DocumentFormat.OpenXml.Spreadsheet.PatternValues.None }; // required, reserved by Excel
fill0.Append(patternFill0);
// Fill ID = 1 (The default fill of Gray)
DocumentFormat.OpenXml.Spreadsheet.Fill fill1 = new DocumentFormat.OpenXml.Spreadsheet.Fill();
DocumentFormat.OpenXml.Spreadsheet.PatternFill patternFill1 = new DocumentFormat.OpenXml.Spreadsheet.PatternFill() { PatternType = DocumentFormat.OpenXml.Spreadsheet.PatternValues.Gray125 }; // required, reserved by Excel
fill1.Append(patternFill1);
// Fill ID = 2
DocumentFormat.OpenXml.Spreadsheet.Fill fill2 = new DocumentFormat.OpenXml.Spreadsheet.Fill();
DocumentFormat.OpenXml.Spreadsheet.PatternFill patternFill2 = new DocumentFormat.OpenXml.Spreadsheet.PatternFill() { PatternType = DocumentFormat.OpenXml.Spreadsheet.PatternValues.Solid }; // customized Pattern value
DocumentFormat.OpenXml.Spreadsheet.ForegroundColor foregroundColorFill2 = new DocumentFormat.OpenXml.Spreadsheet.ForegroundColor() { Rgb = new DocumentFormat.OpenXml.HexBinaryValue("FF00FF00") };
DocumentFormat.OpenXml.Spreadsheet.BackgroundColor backgroundColorFill2 = new DocumentFormat.OpenXml.Spreadsheet.BackgroundColor() { Rgb = new DocumentFormat.OpenXml.HexBinaryValue("5B9BD500") };
patternFill2.Append(foregroundColorFill2);
patternFill2.Append(backgroundColorFill2);
fill2.Append(patternFill2);
fills.Append(fill2);
int fillIndex = fills.Count() - 1;
#endregion
#region Alignment
DocumentFormat.OpenXml.Spreadsheet.Alignment alignment = new DocumentFormat.OpenXml.Spreadsheet.Alignment()
{
Horizontal = DocumentFormat.OpenXml.Spreadsheet.HorizontalAlignmentValues.Left,
Vertical = DocumentFormat.OpenXml.Spreadsheet.VerticalAlignmentValues.Center
};
#endregion
#region Cell Style Formats & Cell Formats
DocumentFormat.OpenXml.Spreadsheet.CellFormats cellFormats0 = new DocumentFormat.OpenXml.Spreadsheet.CellFormats() { Count = (DocumentFormat.OpenXml.UInt32Value)1U };
// Cell Format ID = 0 (The Default Cell Format)
DocumentFormat.OpenXml.Spreadsheet.CellFormat cellFormat0 = new DocumentFormat.OpenXml.Spreadsheet.CellFormat()
{
NumberFormatId = (DocumentFormat.OpenXml.UInt32Value)0U,
FontId = (DocumentFormat.OpenXml.UInt32Value)0U,
FillId = (DocumentFormat.OpenXml.UInt32Value)0U,
BorderId = (DocumentFormat.OpenXml.UInt32Value)0U,
FormatId = (DocumentFormat.OpenXml.UInt32Value)0U
};
cellFormats0.Append(cellFormat0);
// Cell Format ID = 1
DocumentFormat.OpenXml.Spreadsheet.CellFormat cellFormat1 = new DocumentFormat.OpenXml.Spreadsheet.CellFormat()
{
NumberFormatId = (DocumentFormat.OpenXml.UInt32Value)1U,
FontId = (DocumentFormat.OpenXml.UInt32Value)1U,
FillId = (DocumentFormat.OpenXml.UInt32Value)2U,
BorderId = (DocumentFormat.OpenXml.UInt32Value)1U,
FormatId = (DocumentFormat.OpenXml.UInt32Value)1U,
ApplyBorder = true,
ApplyFont = true,
ApplyFill = true,
};
cellFormats0.Append(cellFormat1);
DocumentFormat.OpenXml.Spreadsheet.CellStyles cellStyles = new DocumentFormat.OpenXml.Spreadsheet.CellStyles() { Count = (DocumentFormat.OpenXml.UInt32Value)1U };
DocumentFormat.OpenXml.Spreadsheet.CellStyle cellStyle = new DocumentFormat.OpenXml.Spreadsheet.CellStyle()
{
Name = "Normal",
FormatId = (DocumentFormat.OpenXml.UInt32Value)0U,
BuiltinId = (DocumentFormat.OpenXml.UInt32Value)1U
};
cellStyles.Append(cellStyle);
#endregion
#region Stylesheet
stylesheet1.Append(fonts);
stylesheet1.Append(fills);
stylesheet1.Append(borders);
stylesheet1.Append(cellFormats0);
stylesheet1.Append(cellStyles);
#endregion
return stylesheet1;
}

Blank chart when using candlesticks

When trying to plot candlestick chart using Oxyplot library, it is empty, despite the fact that I assigned model to the plot view.
var plotModel1 = new PlotModel { Title = "Large Data Set (wide window)" };
var timeSpanAxis1 = new DateTimeAxis { Position = AxisPosition.Bottom };
plotModel1.Axes.Add(timeSpanAxis1);
var linearAxis1 = new LinearAxis { Position = AxisPosition.Left };
plotModel1.Axes.Add(linearAxis1);
var n = 10000;
var items = HighLowItemGenerator.MRProcess(n).ToArray();
var series = new CandleStickSeries
{
Color = OxyColors.Black,
IncreasingColor = OxyColors.DarkGreen,
DecreasingColor = OxyColors.Red,
DataFieldX = "Time",
DataFieldHigh = "H",
DataFieldLow = "L",
DataFieldOpen = "O",
DataFieldClose = "C",
TrackerFormatString =
"High: {2:0.00}\nLow: {3:0.00}\nOpen: {4:0.00}\nClose: {5:0.00}",
ItemsSource = items
};
timeSpanAxis1.Minimum = items[n - 200].X;
timeSpanAxis1.Maximum = items[n - 130].X;
linearAxis1.Minimum = items.Skip(n - 200).Take(70).Select(x => x.Low).Min();
linearAxis1.Maximum = items.Skip(n - 200).Take(70).Select(x => x.High).Max();
plotModel1.Series.Add(series);
timeSpanAxis1.AxisChanged += (sender, e) => AdjustYExtent(series, timeSpanAxis1, linearAxis1);
var controller = new PlotController();
controller.UnbindAll();
controller.BindMouseDown(OxyMouseButton.Left, PlotCommands.PanAt);
plotView1.Model = plotModel1;
Strange thing is that I've just copied few things from the Oxyplot series example. I've also created minimal project with the problem described.
The objects generated by the HighLowItemGenerator have different names of properties than defined in the CandleStickSeries definition. Check the items objects in the debugger to see it. Maybe the sample is a bit out of date. The solution is to change the series definition to use the correct properties this is how it should look like:
var series = new CandleStickSeries
{
Color = OxyColors.Black,
IncreasingColor = OxyColors.DarkGreen,
DecreasingColor = OxyColors.Red,
DataFieldX = "X",
DataFieldHigh = "High",
DataFieldLow = "Low",
DataFieldOpen = "Open",
DataFieldClose = "Close",
TrackerFormatString =
"High: {2:0.00}\nLow: {3:0.00}\nOpen: {4:0.00}\nClose: {5:0.00}",
ItemsSource = items
};

How to display percentages with decimals in an Epplus Pie chart?

In a EPPLUS Pie chart, the percentages are rounded automatically by default, how can I display them with 2 decimals programatically (10.75 instead of 11) ?
I only provide the values (integers) and the percentages are calculated automatically by the component.
Dont really see an option in EPPlus to set it - would have to be on the Serie.DataLabel object.
Looks like it has to be done via XML. Here is an example of how to do it (might have to be tweaked for different chart types):
[TestMethod]
public void PieChartDataLabelPercent()
{
//http://stackoverflow.com/questions/42393711/how-to-display-percentages-with-decimals-in-an-epplus-pie-chart
var file = new FileInfo(#"c:\temp\PieChartDataLabelPercent.xlsx");
if (file.Exists)
file.Delete();
var pck = new ExcelPackage(file);
var workbook = pck.Workbook;
var worksheet = workbook.Worksheets.Add("newsheet");
var rand = new Random();
var data = new List<KeyValuePair<string, int>>();
for (var i = 0; i < 10; i++)
data.Add(new KeyValuePair<string, int>($"Group {i}", rand.Next(10, 100)));
//Fill the table
var startCell = worksheet.Cells[1, 1];
startCell.Offset(0, 0).Value = "Group Name";
startCell.Offset(0, 1).Value = "Group Value";
startCell.Offset(1, 0).LoadFromCollection(data);
//Add the chart to the sheet
var pieChart = worksheet.Drawings.AddChart("Chart1", eChartType.Pie);
pieChart.SetPosition(data.Count + 1, 0, 0, 0);
pieChart.SetSize(500, 400);
pieChart.Title.Text = "Test Chart";
//Set the data range
var series = pieChart.Series.Add(worksheet.Cells[2, 2, data.Count + 1, 2], worksheet.Cells[2, 1, data.Count + 1, 1]);
var pieSeries = (ExcelPieChartSerie)series;
pieSeries.Explosion = 5;
//Format the labels
pieSeries.DataLabel.ShowValue = true;
pieSeries.DataLabel.ShowPercent = true;
pieSeries.DataLabel.ShowLeaderLines = true;
pieSeries.DataLabel.Separator = "; ";
pieSeries.DataLabel.Position = eLabelPosition.BestFit;
var xdoc = pieChart.ChartXml;
var nsuri = xdoc.DocumentElement.NamespaceURI;
var nsm = new XmlNamespaceManager(xdoc.NameTable);
nsm.AddNamespace("c", nsuri);
//Added the number format node via XML
var numFmtNode = xdoc.CreateElement("c:numFmt", nsuri);
var formatCodeAtt = xdoc.CreateAttribute("formatCode", nsuri);
formatCodeAtt.Value = "0.00%";
numFmtNode.Attributes.Append(formatCodeAtt);
var sourceLinkedAtt = xdoc.CreateAttribute("sourceLinked", nsuri);
sourceLinkedAtt.Value = "0";
numFmtNode.Attributes.Append(sourceLinkedAtt);
var dLblsNode = xdoc.SelectSingleNode("c:chartSpace/c:chart/c:plotArea/c:pieChart/c:ser/c:dLbls", nsm);
dLblsNode.AppendChild(numFmtNode);
//Format the legend
pieChart.Legend.Add();
pieChart.Legend.Position = eLegendPosition.Right;
pck.Save();
}
Which gives this in the output:

How to use string values for Y-Axis in xlLine ChartType

I need to generate some line charts using excel.interop and I would like to set text for the Y-Axis values.
Dictionary<string, int> data = new Dictionary<string, int>();
data.Add("abc", 1);
data.Add("def", 2);
data.Add("ghi", 3);
data.Add("jkl", 4);
data.Add("mno", 5);
var application = new excel.Application();
var workbook = application.Workbooks.Open(docName);
var worksheet = workbook.Worksheets[1] as excel.Worksheet;
var charts = worksheet.ChartObjects() as excel.ChartObjects;
var chartObject = charts.Add(60, 10, 300, 300);
var chart = chartObject.Chart;
chart.ChartType = excel.XlChartType.xlLine;
chart.Location(excel.XlChartLocation.xlLocationAsObject, worksheetName);
var seriesCollection = (excel.SeriesCollection)chart.SeriesCollection();
var series = seriesCollection.NewSeries();
series.Values = data.Keys.ToArray();
series.XValues = data.Values.ToArray();
series.Name = "test";
workbook.Save();
But this is my result (series.Values contains only 0'values).
Perhaps you want to use text for the X-Axis?
//series.Values = data.Keys.ToArray();
//series.XValues = data.Values.ToArray();
series.XValues = data.Keys.ToArray();
series.Values = data.Values.ToArray();
Or perhaps you could use a bar chart:

Categories

Resources