Related
I am trying to understand the logic of using array methods within Map functions (such as push, slice, etc). Below, I have a function that is trying to add an element in the middle of a 2D array, but it comes out completely blank. Not sure what I am missing
function array () {
let numbers = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
// Attempted outcome = [[1, 2, "Hello", 3], [4, 5, "Hello", 6], [7, 8, "Hello", 9]]
let newNumbers = numbers.map(function (row) {
return [row.slice(2, 0, "Hello")]
})
Logger.log(newNumbers);
}
In your script, how about the following modification?
From:
let newNumbers = numbers.map(function (row) {
return [row.slice(2, 0, "Hello")]
})
To:
let newNumbers = numbers.map(function (row) {
row.splice(2, 0, "Hello");
return row;
});
or, in this case, the following modification might be able to be used.
let newNumbers = numbers.map(([a, b, ...c]) => [a, b, "Hello", ...c]);
Testing:
let numbers = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
// Attempted outcome = [[1, 2, "Hello", 3], [4, 5, "Hello", 6], [7, 8, "Hello", 9]]
let newNumbers = numbers.map(function (row) {
row.splice(2, 0, "Hello");
return row;
});
console.log(newNumbers);
Reference:
splice()
UPDATE(2):
I got it to work so here is the working fiddle. See below for my solution. If you know of any other solutions, let me know. Thanks! otherwise...feel free to use this example :)
UPDATE(1):
I've continued working on it and here is my updated fiddle. I think I'm overriding my variable/data values each time I loop through. Any help is greatly appreciated.
ORIGINAL POST:
I have a jsfiddle here that shows what I'm trying to do.
I have a series of data in json format that has multiple objects(i.e.,
[
{"name":"name1", "data":[[0,4.3],[1,2.47],[2,0.2],etc.]},
{"name":"name2", "data":[[0,4.3],[1,2.47],[2,0.2],etc.]}
]
)
and am graphing with HighCharts line graphs. Instead, I'd like to define the series data to objects from multiple subtasks all on the same graph. (the variable is better shown in the jsfiddle link)
[
{"subtask":"id1", "":[
{"name":"name1", "data":[[0,4.3],[1,2.47],[2,0.2],etc.]},
{"name":"name2", "data":[[0,3.5],[1,2.12],[2,0.1],etc.]}
]
},
{"subtask":"id2", "":[
{"name":"name1", "data":[[0,4.1],[1,2.23],[2,0.4],etc.]},
{"name":"name2", "data":[[0,3],[1,2.62],[2,0.15],etc.]}
]
}
]
and I'd like the graph to draw a line for each name/data for each subtask (i.e., the graph draw a line for id1.name1.data, id1.name2.data, id2.name1.data, and id2.name2.data)
My Solution:
I got it to work so here is the working fiddle. If you know of any other solutions, let me know. Thanks! otherwise...feel free to use this example :)
// what currently works
var jsonFormatThatWorks = [{
"name": "name1",
"data": [
[0, 100],
[10, 70.02],
[20, 60.7],
[30, 45.3],
[40, 35],
[50, 32],
[60, 14],
[70, 0]
]
},
{
"name": "name2",
"data": [
[0, 100],
[10, 30],
[20, 13],
[30, 8],
[40, 7.5],
[50, 5.2],
[60, 4.54],
[70, 0.3],
[80, 0.01]
]
}
]
// format I want/need to be able to access/graph
var jsonFormatIWant = [{
"p1": "12345",
"taskName": "z-echo",
"taskData": [{
"subTaskId": "z8-echo",
"someTaskStatus": "Rejected",
"someTaskData": [{
"objectId": "name1",
"objectData": [
[0, 100],
[10, 90.80],
[20, 80.37],
[30, 75],
[40, 66],
[50, 33],
[60, 15],
[70, 0]
]
},
{
"objectId": "name2",
"objectData": [
[0, 100],
[10, 23],
[20, 15],
[30, 11],
[40, 8.5],
[50, 6.2],
[60, 3.44],
[70, 0.7],
[80, 0.5]
]
}
]
},
{
"subTaskId": "z9-echo",
"someTaskStatus": "Accepted",
"someTaskData": [{
"objectId": "name1",
"objectData": [
[0, 100],
[10, 70.02],
[20, 60.7],
[30, 45.3],
[40, 35],
[50, 32],
[60, 14],
[70, 0]
]
},
{
"objectId": "name2",
"objectData": [
[0, 100],
[10, 30],
[20, 13],
[30, 8],
[40, 7.5],
[50, 5.2],
[60, 4.54],
[70, 0.3],
[80, 0.01]
]
}
]
}
]
}]
// my chart
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
type: 'line',
zoomType: 'xy',
panning: true,
panKey: 'shift'
},
xAxis: {
title: {
text: 'Some X Label'
},
crosshair: true
},
plotOptions: {
series: {
marker: {
radius: 1
},
allowPointSelect: true
}
},
yAxis: {
labels: {
format: '{value} %'
},
floor: 0,
ceiling: 100,
title: {
text: 'Value (%)'
},
crosshair: true,
gridLineDashStyle: 'ShortDash',
gridLineColor: '#aaaaaa'
},
title: {
text: 'Sample Chart'
},
subtitle: {
text: 'Click and drag to zoom in. Hold down shift key to pan.'
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: seriesOptions
};
var chart = new Highcharts.Chart(options);
});
var seriesOptions = [],
seriesCounter = 0
var data = jsonFormatIWant[0]
var taskData = data.taskData
taskData.forEach(function(element, i) {
element.someTaskData.forEach(function(childElement, j) {
seriesOptions[seriesCounter] = {
//task: element.subTaskId,
name: element.subTaskId + "_" + element.someTaskData[j].objectId,
data: element.someTaskData[j].objectData
}
seriesCounter += 1
//console.log(seriesCounter)
childElement.objectData.forEach(function(grandChildElement, h) {})
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/series-label.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container"></div>
<!-- I'd like to be able to graph each objects data for each subTask
(i.e., z8-echo and z9-echo from the data jsonFormatIWant variable beneath the chart definition)
something like: taskData[0].someTaskData and taskData[1].someTaskData -->
I am trying to use sklearn.preprocessing.OneHotEncoder to binarize my categorical variables before use in some regression methods such as OLS, the Lasso etc.
I have a nested list like so:
l = [[0, 0, 0], [0, 1, 1], [1, 2, 2], [0, 3, 2], [1, 4, 0], [0, 5, 2], [2, 2, 2], [0, 6, 2], [1, 7, 2], [0, 8, 3], [3, 4, 2], [0, 8, 4], [0, 9, 2], [1, 7, 1], [0, 10, 2], [0, 2, 5], [1, 11, 2], [1, 2, 3], [4, 12, 2], [1, 4, 2], [0, 13, 2], [0, 14, 2], [0, 15, 2], [0, 16, 0], [0, 17, 6], [5, 17, 2], [4, 17, 2], [0, 17, 3], [0, 2, 6], [0, 8, 6], [4, 2, 2], [4, 4, 2], [5, 15, 3], [0, 2, 3], [0, 7, 2], [1, 15, 2], [0, 17, 2], [0, 8, 2], [0, 2, 2], [4, 16, 2], [0, 1, 2], [5, 15, 2], [4, 8, 0], [0, 18, 3], [3, 11, 2], [6, 7, 2], [0, 8], [0, 19, 2], [1, 1, 2], [0, 7, 0], [0, 1, 0], [0, 4, 2], [0, 15, 3], [7, 8, 2], [1, 8, 0], [1, 16, 2], [0, 20, 2], [1, 8], [1, 8, 2], [0, 11, 1], [1, 21, 2], [4, 1, 2], [5, 1, 2], [2, 1, 2], [0, 22, 2], [8, 8, 2], [1, 8, 3], [1, 17, 2], [0, 8, 7], [0, 0, 2], [7, 7, 2], [2, 2, 8], [9, 8, 2], [5, 8, 2], [4, 8, 2], [0, 4, 3], [0, 23, 0], [0, 24, 2], [0, 2, 0], [3, 1, 2], [0, 25, 2], [0, 2, 9], [0, 11, 2], [1, 12, 2], [1, 26, 3], [0, 23, 2], [0, 27, 3], [3, 8, 2], [6, 8, 2], [6, 27, 2], [0, 16, 2], [0, 28, 2], [0, 29, 2], [0, 8, 0], [0, 8, 10], [0, 27, 2], [4, 7, 2], [0, 21, 2], [6, 11, 2], [0, 30, 2], [2, 8, 2], [0, 23, 3]]
from sklearn import preprocessing
enc = preprocessing.OneHotEncoder()
enc.fit(l)
However, I am running into the error:
ValueError: setting an array element with a sequence
Here is the most telling callback as far as I can see:
C:\Program Files\Anaconda\lib\site-packages\numpy\core\numeric.pyc in asarray(a, dtype, order)
458
459 """
--> 460 return array(a, dtype, copy=False, order=order)
461
462 def asanyarray(a, dtype=None, order=None):
To try and solve this problem I have attempted to convert my list to a matrix and array in numpy but have had no luck.
In addition, I have made sure that each value in each of the nested lists is an integer. I have also tried converting them to floats, again with no success.
Any help would be wonderful. Thanks.
How to create Two Level JSON, if i have to get data in multiple lists, like below:
Category's List > Item's List
For an example: Sony > LED TV, Laptop, Phones etc..
Earlier i have created single Level JSON,
For an example: LED TV, Laptop, Phones see below:
[
{
"ProductID":"1",
"ProductName":"LED TV"
},
{
"ProductID":"2",
"ProductName":"Laptop"
}
]
So here my question is how my JSON should look like ?
You can use any JSON "data type" as values. So here, you would create an object whose keys are the categories and the values are arrays of products:
{
"Sony": [{
"ProductID": "1",
"ProductName": "LED TV"
}, {
...
}],
"Panasonic": [...]
}
Instead of using an array of products, you could also use an object of object, keyed by produce ID. Optimize the structure for your use case, i.e. structure it in such a way that you can easily access the information you need.
See http://json.org/ for a complete syntax description.
yeah I agree with #FelixKling in one of my app i have used same kind of JSON:
{
"Mixed Platter" : [
{
"title" : "Veggie",
"description" : "Lorem ipsum dolor sit amet, conse adipiscing elit.",
"cost" : "5.25"
},
{
"title" : "Non Veggie",
"description" : "Lorem ipsum dolor sit amet, conse adipiscing elit.",
"cost" : "5.75"
}
],
"Soups" : [
{
"title" : "Mulagatawny Soup",
"description" : "Lorem ipsum dolor sit amet, conse adipiscing elit.",
"cost" : "3.75"
},
{
"title" : "Daal Soup",
"description" : "Lorem ipsum dolor sit amet, conse adipiscing elit.",
"cost" : "3.25"
}
]
}
[
{
"author": "anonymous",
"background": "0xaaaaaa",
"ball": "0xff1111",
"mat": "0xffff00",
"bouncer": "0xff00ff",
"obstacle": "0x00ddff",
"data":
[
[0, 0, 0, 0, 0],
[1, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 0, 0, 1],
[1, 1, 1, 1, 0],
[1, 0, 0, 1, 0],
[1, 0, 0, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 0],
[1, 0, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 0],
[1, 1, 0, 0, 0],
[1, 0, 1, 1, 0],
[1, 1, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 1, 1],
[1, 1, 1, 0, 1],
[0, 0, 0, 0, 1],
[1, 1, 1, 1, 1],
[1, 1, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 1, 1],
[1, 1, 1, 1, 0],
[1, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[0, 0, 0, 0, 1],
[1, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]
]
}
]
I am working on a flot chart and I am unable to covert json string to required flot graph format. Below is what I tried.
My Json
Data: [
{ "Day": 8, "Visits": 145 },
{ "Day": 7, "Visits": 26 }
];
Format I am trying to achieve: [[0, 12], [1, 2], [2, 2], [3, 3], [4, 4]];
$(document).ready(function () {
var data = [{ "Day": 8, "Visits": 145 }, { "Day": 7, "Visits": 26 }];
var dailyHits = [];
$.each(data, function (_index, _item) {
var c = [_item["Day"], _item["Visits"]];
dailyHits.push(c);
});
//var dailyHits1= [[0, 12], [1, 2], [2, 2], [3, 3], [4, 4]];
var chartData = [dailyHits];
console.log(chartData);
$.plot($("#chartHits"), chartData);
});
Any help would be highly appreciated.
Hey i have same problem and i think problem is that you get data in string format, but array should be with Integers