Could not feed my convolution1d with csv data - machine-learning

I need a help for my following problem.
I'm trying to feed my csv data to my first layer which is convolution1d but it shows
Input 0 is incompatible with layer conv1d_Conv1D1: expected ndim=3, found ndim=2
Here is my code
//move the tfjs_binding.node file in build-tmp-napi-v7/Release folder to build-tmp-napi-v7 folder will solve the problem.
const dfd = require("danfojs-node");
const tf = require("#tensorflow/tfjs-node");
var petData;
const TIME_STEPS = (24 * 60) / 60;
console.log("start");
var model = tf.sequential();
model.add(
tf.layers.conv1d({
filters: 3,
kernelSize: 3,
inputShape:[1]
})
);
// model.add(tf.layers.dropout({ rate: 0.2 }));
// model.add(
// tf.layers.conv1d({
// filters: 16,
// kernelSize: 7,
// padding: "same",
// strides: 2,
// activation: "relu",
// })
// );
// model.add(
// tf.layers.conv1d({
// filters: 16,
// kernelSize: 7,
// padding: "same",
// strides: 2,
// activation: "relu",
// })
// );
// model.add(tf.layers.dropout({ rate: 0.2 }));
// model.add(
// tf.layers.conv1d({
// filters: 32,
// kernelSize: 7,
// padding: "same",
// strides: 2,
// activation: "relu",
// })
// );
// model.add(
// tf.layers.conv1d({
// filters: 1,
// kernelSize: 7,
// padding: "same",
// })
// );
model.compile({
optimizer: tf.train.adam((learningRate = 0.001)),
loss: tf.losses.meanSquaredError,
});
model.summary();
console.log("model created.");
dfd
.read_csv("./petTempData.csv", (chunk = 10000))
.then((df) => {
let encoder = new dfd.LabelEncoder();
let cols = ["Date", "Time"];
cols.forEach((col) => {
encoder.fit(df[col]);
enc_val = encoder.transform(df[col]);
df.addColumn({ column: col, value: enc_val });
});
petData = df.iloc({ columns: [`1`] });
yData = df["Temperature"];
// let scaler = new dfd.MinMaxScaler();
// scaler.fit(petData);
// petData = scaler.transform(petData);
// petData = petData.tensor.expandDims(-1);
// const data = petData.tensor.reshape([24, 2, 1]);
console.log(petData.shape);
model.fit(petData.tensor, yData.tensor, {
epochs: 10,
batchSize: 4,
// validationSplit: 0.01,
callbacks: tf.callbacks.earlyStopping({
monitor: "loss",
patience: "5",
mode: "min",
}),
});
})
.catch((err) => {
console.log(err);
});
And here is my csv raw file
Date,Time,Temperature
31-12-2020,01:30,36.6
31-12-2020,02:30,36.7
31-12-2020,03:30,36.6
31-12-2020,04:30,36.5
31-12-2020,05:30,36.8
31-12-2020,06:30,36.6
31-12-2020,07:30,36.6
31-12-2020,08:30,36.5
31-12-2020,09:30,36.6
31-12-2020,10:30,36.7
31-12-2020,11:30,36.6
31-12-2020,12:30,36.7
31-12-2020,13:30,36.7
31-12-2020,14:30,36.8
31-12-2020,15:30,36.9
31-12-2020,16:30,36.6
31-12-2020,17:30,36.7
31-12-2020,18:30,36.8
31-12-2020,19:30,36.7
31-12-2020,20:30,36.6
31-12-2020,21:30,36.6
31-12-2020,22:30,36.5
31-12-2020,23:30,36.5
,,
I've tried to reshape my input, and expandDims but none of them work.
Any solution is much appreciated!

The conv1d layer expects an inputShape of dim 2, therefore, the inputShape needs to be [a, b](with a, b positive integers).
model = tf.sequential();
model.add(
tf.layers.conv1d({
filters: 3,
kernelSize: 1,
inputShape:[1, 3]
})
);
model.predict(tf.ones([1, 1, 3])).print()

Related

Extract all percentages present within a text imported by IMPORTXML (Google Sheets)

As these percentages are within a function that creates the bar chart, the only way I was able to extract this data was by importing the entire function with this formula:
=IMPORTXML(
"https://int.soccerway.com/teams/spain/real-madrid-club-de-futbol/2016/statistics/",
"//div[#id='charts']//script[#type='text/javascript']")
The result is the following value:
(function(){ function drawScoringMinutesChart() { var hSize = 180, vSize =
180; var opts = { width: hSize, height: vSize, axisTitlesPosition: 'none',
legend: 'none', chartArea: { left: 0, top: 0, width: hSize, height: vSize
}, backgroundColor: '#f4f4f4', colors: ['#f85f00'], hAxis: { textPosition:
'none', viewWindowMode: 'explicit', viewWindow: { min: 0, max: 8 },
baselineColor: '#999999' }, vAxis: { textPosition: 'in' },
enableInteractivity: true }; var data_a = new
google.visualization.DataTable(); data_a.addColumn('string', 'Minute');
data_a.addColumn('number', 'Goals'); data_a.addRows(6); data_a.setValue(0,
0, '20.0%'); data_a.setValue(0, 1, 6); data_a.setValue(1, 0, '13.3%');
data_a.setValue(1, 1, 4); data_a.setValue(2, 0, '3.3%'); data_a.setValue(2,
1, 1); data_a.setValue(3, 0, '23.3%'); data_a.setValue(3, 1, 7);
data_a.setValue(4, 0, '13.3%'); data_a.setValue(4, 1, 4);
data_a.setValue(5, 0, '26.7%'); data_a.setValue(5, 1, 8); var chart_a = new
google.visualization.BarChart(document.getElementById('scoring_minutes_chart_a'));
chart_a.draw(data_a, opts); } $scru.execute(drawScoringMinutesChart,
['google:visualization']); })();
I would like to know if there is any formula that would be able to filter this text and deliver only the percentages, like this:
20.0%
13.3%
3.3%
23.3%
13.3%
26.7%
So that I can, in the end, put it like this in the spreadsheet:
0-15' 20.0%
15-30' 13.3%
30-45' 3.3%
45-60' 23.3%
60-75' 13.3%
75-90' 26.7%
try:
=QUERY(FLATTEN(SPLIT(REGEXREPLACE(IMPORTXML(A1,
"//div[#id='charts']//script[#type='text/javascript']"),
"[^(\d+.\d+%)]", " "), " ")),
"where Col1 contains '%'", 0)

Holt winters Alpha

I am trying to get the HoltWinters Alpha function added.
I have a Table called Sales1 and the code should refer to this table.
Is there anyone who can correct or amend my code below ,so i get the Holtwinters Alpha instead of the Chronbachs Alpha?
Holt winters calc (need this probably amended)
library(forecast)
library(Metrics
)
read_file(sales1)
x <- sales
x = c(Sales1)
mSES = HoltWinters(x, alpha = 0.5, beta = FALSE, gamma = FALSE)
mHW = HoltWinters(x, alpha = 0.5, beta = FALSE, gamma = FALSE)
mSES$SSE
mHW$SSE
HoltWinters(x, alpha = NULL, beta = NULL, gamma = NULL,
seasonal = c("additive", "multiplicative"),
start.periods = 2, l.start = NULL, b.start = NULL,
s.start = NULL,
optim.start = c(alpha = 0.3, beta = 0.1, gamma = 0.1),
optim.control = list())
chronbachs alpha calc
read_file(sales1)
library(tidyverse)
library(psy)
Number of rows before to take into account
rolling = 2
sales1 <- sales::sales( ~date, ~sales,)
#Lag
sales1 = sales1 %>% mutate(lagsales = lag(sales))
#Rolling Chronbachs Alpha.:( I need the Holtwinter Alpha here )
sales1$alpha = c( rep(NA, rolling),
map_dbl((rolling + 1):nrow(sales1), function(x){
cronbach(sales1 %>% select(sales, lagsales) %>% slice((x-rolling):x))$alpha
})
)
sales1
tibbet from Sales1 table:
df <- tibble::tribble(
~seq, ~date, ~sales,
1, "3/01/2017", 40,
2, "4/01/2017", 2,
3, "5/01/2017", 2,
4, "6/01/2017", 2,
5, "7/01/2017", 30,
6, "8/01/2017", 2,
7, "1/02/2017", 9,
8, "2/02/2017", 5,
9, "3/02/2017", 65,
10, "4/02/2017", 3,
11, "5/02/2017", 65

Show Series and colorAxis both in Legend

Is it possible to have both colorAxis and series in the legend? http://jsfiddle.net/6k17dojn/ i see i can only show one at a time when I toggle this setting
colorAxis: {
showInLegend: true,
}
Currently to show a basic legend with colorAxis, you need to add some code to Highcharts core. This plugin below allows you to add colorAxis to a legend if showInLegend property is set to false:
(function(H) {
H.addEvent(H.Legend, 'afterGetAllItems', function(e) {
var colorAxisItems = [],
colorAxis = this.chart.colorAxis[0],
i;
if (colorAxis && colorAxis.options) {
if (colorAxis.options.dataClasses) {
colorAxisItems = colorAxis.getDataClassLegendSymbols();
} else {
colorAxisItems.push(colorAxis);
}
}
i = colorAxisItems.length;
while (i--) {
e.allItems.unshift(colorAxisItems[i]);
}
});
}(Highcharts))
Live demo: http://jsfiddle.net/BlackLabel/hs1zeruy/
API Reference: https://api.highcharts.com/highcharts/colorAxis.showInLegend
Docs: https://www.highcharts.com/docs/extending-highcharts
It's possible, but not with the data you currently work with. A heatmap's data is a set of coordinates, but here, your two series overlap.
Your raw data is :
[
[0,0,0.2, 0.4],
[0,1,0.1, 0.5],
[0,2,0.4, 0.9],
[0,3,0.7, 0.1],
[0,4,0.3, 0.6]
]
From there, you're mapping two series: 2018, and 2019 via the seriesMapping: [{x: 0, y: 1, value: 2}, {x: 0, y: 1, value: 3}] option.
You thus end up with the following two series:
2018 2019 2019 should be
[ [ [
[0, 0, 0.2], [0, 0, 0.4], [1, 0, 0.4],
[0, 1, 0.1], [0, 1, 0.5], [1, 1, 0.5],
[0, 2, 0.4], [0, 2, 0.9], [1, 2, 0.9],
[0, 3, 0.7], [0, 3, 0.1], [1, 3, 0.1],
[0, 4, 0.3] [0, 4, 0.6] [1, 4, 0.6]
] ] ]
Notice that in both cases, the coordinates are the same, but for 2019, the x value should be 1. Since you have 0 as x coordinate for both series, they overlap.
To fix you issue, you need to change your data (or pre-process it, whatever is easier). For example:
var data = '[[0,0,0.2, 0.4],[0,1,0.1, 0.5],[0,2,0.4, 0.9],[0,3,0.7, 0.1],[0,4,0.3, 0.6]]';
var rows = JSON.parse(data);
rows = $.map(rows, function(arr){
return [[
arr[0], arr[1], arr[2], // 2018
arr[0] + 1, arr[1], arr[3], // 2019
]];
});
// and the seriesMapping changes to
data: {
rows: rows,
firstRowAsNames: false,
seriesMapping: [{x: 0, y: 1, value: 2}, {x: 3, y: 4, value: 5}]
},
You can see it in action here: http://jsfiddle.net/Metoule/qgd2ca6p/6/

How to set tooltip background as point color for split chart in Highcharts?

I am going to set tooltip background as its point color when tooltip.split is set in Highcharts.
// Create the chart
Highcharts.chart('container', {
chart: {
type: 'spline',
},
title: {
text: 'Mountain house indoor temperatures'
},
subtitle: {
text: 'Split tooltips in Highcharts makes it easier to read overlapping line series'
},
tooltip: {
split: true,
},
xAxis: {
crosshair: {
enabled: true
}
},
yAxis: {
title: {
text: 'Temperatur'
}
},
plotOptions: {
series: {
lineWidth: 1.5,
marker: {
radius: 2
},
events: {
afterAnimate() {
console.log("secondthis", this);
this.tooltipOptions.backgroundColor = this.color
}
}
}
},
data: {
columns: [
["Time", 1451616120000, 1451865660000, 1451952060000, 1452038400000, 1452124800000, 1452211200000, 1452297600000, 1452384000000, 1452470400000, 1452556800000, 1452643200000, 1452729600000, 1452816000000, 1452902400000, 1452988800000, 1453075200000, 1453161600000, 1453248000000, 1453334400000, 1453420800000, 1453507200000, 1453593600000, 1453680000000, 1453766400000, 1453852800000, 1453939200000, 1454025600000],
["Kitchen", 5, 4, 5, 9, 6, 15, 19, 14, 6, 5, 6, 6, 15, 18, 15, 6, 6, 6, 6, 6, 6, 6, 16, 10, 6, 6, 6],
["Living room", 9, 10, 16, 13, 6, 20, 24, 16, 7, 7, 6, 6, 20, 23, 18, 9, 7, 6, 6, 7, 6, 21, 20, 16, 6, 6, 6],
["Hall", 7, 7, 13, 12, 5, 17, 22, 14, 4, 5, 5, 6, 18, 21, 17, 9, 5, 6, 5, 6, 6, 18, 20, 14, 5, 5, 5],
["Bathroom", 7, 7, 13, 12, 5, 17, 22, 14, 4, 5, 5, 6, 18, 21, 17, 9, 5, 6, 5, 6, 6, 18, 20, 14, 5, 5, 5],
["Bedroom 1", 6, 19, 19, 10, 5, 15, 21, 14, 6, 6, 5, 5, 17, 21, 16, 6, 5, 5, 5, 5, 5, 17, 18, 13, 5, 5, 5],
["Bedroom 2", 7, 19, 19, 9, 5, 11, 19, 15, 6, 5, 6, 6, 16, 19, 17, 8, 9, 6, 5, 6, 5, 17, 19, 14, 6, 6, 6],
["Shed", 6, 6, 5, 5, 6, 6, 6, 5, 5, 6, 6, 5, 6, 6, 6, 6, 6, 6, null, null, 6, 6, 6, 6, 6, 6, 6]
]
}
});
#container {
max-width: 800px;
height: 400px;
margin: 1em auto;
}
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/data.js"></script>
<div id="container"></div>
I tried to set on events but its not working.
How can I do that?
tooltip: {
split: true,
},
You can achieve it by wrapping Highcharts.Tooltip.prototype.renderSplit method and adding this lines of code there:
pointColor = H.charts[0].options.colors[pick(
point.colorIndex,
series.colorIndex,
'none'
)];
pointColor = pointColor ? pointColor : '#efefef';
pointColor = H.Color(pointColor);
pointColor.rgba[3] = 0.8; // opacity
And then use pointColor to set fill property for particular tooltip element:
if (!chart.styledMode) {
attribs.fill = pointColor.get('rgba');
attribs.stroke = (
options.borderColor ||
point.color ||
series.color ||
'#333333'
);
attribs['stroke-width'] = options.borderWidth;
}
Whole wrapper code:
(function(H) {
H.Tooltip.prototype.renderSplit = function(labels, points) {
var tooltip = this,
pick = H.pick,
boxes = [],
chart = this.chart,
ren = chart.renderer,
rightAligned = true,
options = this.options,
headerHeight = 0,
headerTop,
tooltipLabel = this.getLabel(),
distributionBoxTop = chart.plotTop;
// Graceful degradation for legacy formatters
if (H.isString(labels)) {
labels = [false, labels];
}
// Create the individual labels for header and points, ignore footer
labels.slice(0, points.length + 1).forEach(function(str, i) {
if (str !== false && str !== '') {
var point = points[i - 1] || {
// Item 0 is the header. Instead of this, we could also
// use the crosshair label
isHeader: true,
plotX: points[0].plotX,
plotY: chart.plotHeight
},
owner = point.series || tooltip,
tt = owner.tt,
series = point.series || {},
colorClass = 'highcharts-color-' + pick(
point.colorIndex,
series.colorIndex,
'none'
),
target,
x,
bBox,
boxWidth,
pointColor,
attribs;
// Store the tooltip referance on the series
if (!tt) {
attribs = {
padding: options.padding,
r: options.borderRadius
};
// CUSTOM COLORS //
pointColor = H.charts[0].options.colors[pick(
point.colorIndex,
series.colorIndex,
'none'
)];
pointColor = pointColor ? pointColor : '#efefef';
pointColor = H.Color(pointColor);
pointColor.rgba[3] = 0.8;
// CUSTOM COLORS //
if (!chart.styledMode) {
attribs.fill = pointColor.get('rgba');
attribs.stroke = (
options.borderColor ||
point.color ||
series.color ||
'#333333'
);
attribs['stroke-width'] = options.borderWidth;
}
owner.tt = tt = ren
.label(
null,
null,
null,
(
point.isHeader ? options.headerShape :
options.shape
) || 'callout',
null,
null,
options.useHTML
)
.addClass('highcharts-tooltip-box ' + colorClass)
.attr(attribs)
.add(tooltipLabel);
}
tt.isActive = true;
tt.attr({
text: str
});
if (!chart.styledMode) {
tt.css(options.style)
.shadow(options.shadow);
}
// Get X position now, so we can move all to the other side in
// case of overflow
bBox = tt.getBBox();
boxWidth = bBox.width + tt.strokeWidth();
if (point.isHeader) {
headerHeight = bBox.height;
if (chart.xAxis[0].opposite) {
headerTop = true;
distributionBoxTop -= headerHeight;
}
x = Math.max(
0, // No left overflow
Math.min(
point.plotX + chart.plotLeft - boxWidth / 2,
// No right overflow (#5794)
chart.chartWidth +
(
// Scrollable plot area
chart.scrollablePixels ?
chart.scrollablePixels - chart.marginRight :
0
) -
boxWidth
)
);
} else {
x = point.plotX + chart.plotLeft -
pick(options.distance, 16) - boxWidth;
}
// If overflow left, we don't use this x in the next loop
if (x < 0) {
rightAligned = false;
}
// Prepare for distribution
target = (point.series && point.series.yAxis &&
point.series.yAxis.pos) + (point.plotY || 0);
target -= distributionBoxTop;
if (point.isHeader) {
target = headerTop ?
-headerHeight :
chart.plotHeight + headerHeight;
}
boxes.push({
target: target,
rank: point.isHeader ? 1 : 0,
size: owner.tt.getBBox().height + 1,
point: point,
x: x,
tt: tt
});
}
});
// Clean previous run (for missing points)
this.cleanSplit();
if (options.positioner) {
boxes.forEach(function(box) {
var boxPosition = options.positioner.call(
tooltip,
box.tt.getBBox().width,
box.size,
box.point
);
box.x = boxPosition.x;
box.align = 0; // 0-align to the top, 1-align to the bottom
box.target = boxPosition.y;
box.rank = pick(boxPosition.rank, box.rank);
});
}
// Distribute and put in place
H.distribute(boxes, chart.plotHeight + headerHeight);
boxes.forEach(function(box) {
var point = box.point,
series = point.series;
// Put the label in place
box.tt.attr({
visibility: box.pos === undefined ? 'hidden' : 'inherit',
x: (rightAligned || point.isHeader || options.positioner ?
box.x :
point.plotX + chart.plotLeft + tooltip.distance),
y: box.pos + distributionBoxTop,
anchorX: point.isHeader ?
point.plotX + chart.plotLeft : point.plotX + series.xAxis.pos,
anchorY: point.isHeader ?
chart.plotTop + chart.plotHeight / 2 : point.plotY + series.yAxis.pos
});
});
}
})(Highcharts);
Demo:
https://jsfiddle.net/BlackLabel/rdnoq0fs/

LSTM: Figuring out the library?

I'm using the library https://github.com/cazala/synaptic
I am trying to predict the next value (value X) in the following series:
0 0 0 1 0 0 0 1 0 0 0 X
Which should be a 1.
Here is the code:
const options = {
peepholes: Layer.connectionType.ALL_TO_ALL,
hiddenToHidden: false,
outputToHidden: false,
outputToGates: false,
inputToOutput: true,
};
// 1 input, 3 hidden layers (4 nodes per layer), 1 output
const lstm = new Architect.LSTM(1, 4, 4, 4, 1, options);
const trainingArray = [
{
input: [0],
output: [0],
},
{
input: [0],
output: [0],
},
{
input: [0],
output: [1],
},
{
input: [1],
output: [0],
},
{
input: [0],
output: [0],
},
{
input: [0],
output: [0],
},
{
input: [0],
output: [1],
},
{
input: [1],
output: [0],
},
];
const trainingOptions = {
rate: 0.1,
iterations: 100000,
error: 0.05,
cost: null,
crossValidate: null,
};
let results = lstm.trainer.train(trainingArray, trainingOptions);
console.log(results);
array = [
0,
0,
0,
1,
0,
0,
0,
1,
0,
0,
0,
];
results = lstm.activate(array);
console.log(results);
The output in the console:
{ error: 0.049765018466871494, iterations: 673, time: 392 }
[ 0.05010961302724895 ]
I was expecting the result of the activation to be a value closer to 1 than 0 (much closer). I don't know if this is the library of my lack of knowledge with LSTM. Can someone point me in the correct direction?
I read through the source code and figured it out.
const synaptic = require('synaptic');
const Architect = synaptic.Architect;
const Layer = synaptic.Layer;
const lstmOptions = {
peepholes: Layer.connectionType.ALL_TO_ALL,
hiddenToHidden: false,
outputToHidden: false,
outputToGates: false,
inputToOutput: true,
};
const lstm = new Architect.LSTM(1, 4, 4, 4, 1, lstmOptions);
const trainSet = [
{ input: [0], output: [0.1] },
{ input: [1], output: [0.2] },
{ input: [0], output: [0.3] },
{ input: [1], output: [0.4] },
{ input: [0], output: [0.5] },
];
const trainOptions = {
rate: 0.2,
iterations: 10000,
error: 0.005,
cost: null,
crossValidate: null,
};
const trainResults = lstm.trainer.train(trainSet, trainOptions);
console.log(trainResults);
const testResults = [];
testResults[0] = lstm.activate([0]);
testResults[1] = lstm.activate([1]);
testResults[2] = lstm.activate([0]);
testResults[3] = lstm.activate([1]);
testResults[4] = lstm.activate([0]);
console.log(testResults);
Results in:
{ error: 0.004982436660844655, iterations: 2010, time: 384 }
[ [ 0.18288280009908592 ],
[ 0.2948083898027347 ],
[ 0.35061782593064206 ],
[ 0.3900799575806566 ],
[ 0.49454852760556606 ] ]
Which is accurate.

Resources