Say, I have two arrays of data, A and B.
A = [
{name:'Mon Oct 24 2022 17:00:00', value: 1},
{name:'Mon Oct 24 2022 18:00:00', value: 2},
{name:'Mon Oct 24 2022 19:00:00', value: 3},
{name:'Mon Oct 24 2022 20:00:00', value: 4}
]
and
B = [
{name:'Mon Oct 24 2022 17:30:00', value: 5},
{name:'Mon Oct 24 2022 18:30:00', value: 4},
{name:'Mon Oct 24 2022 19:30:00', value: 3},
]
Now I want to draw two lines with different colours for these data in the same line chart with the correct x-axis, e.g. B[0] is in the middle of A[0] and A[1]. But I couldn't make it work, it's always the case that the line for B comes after the line for A the line.
It would be better if the x-axis uses [0, 'auto'] as the domain property.
Ah! Solve it. Conver time string to stamp, and make Axis type be number.
Related
I have the following array
import numpy as np
single_array =
[[ 1 80 80 80]
[ 2 80 80 89]
[ 3 52 50 90]
[ 4 39 34 54]
[ 5 37 47 32]
[ 6 42 42 27]
[ 7 42 52 27]
[ 8 38 33 28]
[ 9 42 37 42]]
and want to create another array with all unique sums of 2 rows within this single_array so that 1+2 and 2+1 are treated as duplicates and are only included once.
First I would like to update the 0th column of the array to multiply each value by 10 (so I can identify the corresponding matching), then I want to add up every 2 rows and append them into the new array.
Output should look like this:
double_array=
[[12 160 160 169]
[13 132 130 170]
[14 119 114 134]
...
[98 80 70 70]]
Can I use itertools.combinations to get a 3D array with two unique combinations and then add the rows on the corresponding 3rd axis?
This
import numpy as np
from itertools import combinations
single_array = np.array(
[[ 1, 80, 80, 80],
[ 2, 80, 80, 89],
[ 3, 52, 50, 90],
[ 4, 39, 34, 54],
[ 5, 37, 47, 32],
[ 6, 42, 42, 27],
[ 7, 42, 52, 27],
[ 8, 38, 33, 28],
[ 9, 42, 37, 42]]
)
np.vstack([single_array[i] * np.array([10, 1, 1, 1]) + single_array[j]
for i, j in combinations(range(single_array.shape[0]), 2)])
does what you ask for in terms of specified input and output; I'm not sure if it's what you actually need. I don't think it will scale to big inputs.
A 3D array to find this sum would be ragged (first "layer" would be 9 deep, next one 8, etc.); you could maybe get around this with NaNs or masking. It also wouldn't scale that well for big inputs: you'd be allocating twice as much memory as you need, and then have to index out ragged layers to get your final output.
If you have to do this fast for big arrays, I suggest a pre-allocated output array and a for-loop with Numba:
from numba import jit
#jit(nopython=True)
def unique_row_sums(a):
n = a.shape[0]
b = np.empty((n*(n-1)//2, a.shape[1]))
s = np.array([10, 1, 1, 1])
k = 0
for i in range(n):
for j in range(i+1, n):
b[k] = s * a[i] + a[j]
k += 1
return b
In my not-too-careful testing with IPython's %timeit, this took about 4µs versus 152µs for the itertools-based version with your data, and should scale better.
Ok i`ll minimize my problem
i`ve a matrix of the type
int nitem = 20; // imagine that the items are numbers from 1 to 20
int nmatch = nitem-1; // because every item can have 19 combination with other items (1 - 2, 1 - 3, etc..)
int matrix[nmatch*(nitem/2)][2]; //in this case 190 row because every cycle will put in this array 10 match that aren`t done before.
now i`ve to put in the matrix array element of the type
matrix = {{1 , 2},{3 , 4},{5 , 6}, etc..}
But from row 0 to 9 (10 to 19, 20 to 29 etc...) i want to put numbers that appears only 1 time in the actual 10 row.
Obtaining a result like:
matrix = {{1 , 2}, {3 , 4}, {5 , 6}, {7 , 8}, {9 , 10}, {11 , 12}, {13 , 14}, {15 , 16}, {17 , 18}, {19 , 20}}
On my second cycle i want to match items that aren`t matched before (the entire couple not the single item) and obtain a matrix of the type:
matrix = {{1 , 2}, {3 , 4}, {5 , 6}, {7 , 8}, {9 , 10}, {11 , 12}, {13 , 14}, {15 , 16}, {17 , 18}, {19 , 20}, {2 , 3}, {4 , 5}, {6 , 7}, {8 , 9}, {10 , 11}, {12 , 13}, {14 , 15}, {16 , 17}, {18 , 19}, {20 , 21}}
And I’d like to do this 19 time until the end of all my possible combination (and the end of the matrix obv)..
I tried to code it 2 times but my code its too big and contain a lot of variable that i use in my program so i can't post my logic error but i'll be very grateful to to anyone who helps me with this little code.
I also use this tecnique to write at the right position in this matrix
int cycle = 1;
int * ptrcycle = &cycle;
...
void myfunction(int * ptr, int matrix[][2], int nitem){
for(int i = (ptr-1)*(nitem/2); i < ptr*(nitem/2); i++){
do my things...
}
*ptr = *ptr+1
}
so every time i call my cycle, imagine at cycle = 3, i'll have i = 20 and i < 30 (both correct).
Please help meee :(
How could I solve it simply and great?
First 6 line of input file (170 lines and the shape is still the same):
6 1 0 6 2 25
392712621
6 13 31 6 18 15
442407028
6 29 39 6 34 7
712676212
I want to put it to an array like this
[[6, 1, 0, 6, 2, 25, 392712621], [6, 13, 31, 6, 18, 15, 442407028], [6, 29, 39, 6, 34, 7, 712676212]]
Try
outputs = []
for i in range(3):
inputs = list(map(int, input().split()))
inputs.append(int(input()))
outputs.append(inputs)
print(outputs)
Change the number inside range to half the number of lines you need to parse.
I have been doing date time conversions using Calendar everything was working fine for last 4 months but all of sudden am starting some strange behaviour in this,
what am doing is
1..total_days |> Enum.reduce(start_date, fn _i, acc ->
day_of_week = acc |> Calendar.Date.day_of_week_name
rec_head = get_head_tail(schedule[day_of_week])
rec_head |> Enum.each(fn(x) ->
iterate(x, acc, timezone) |> t_download(interval, t_agent)
end)
acc |> Calendar.DateTime.to_erl |> IO.inspect |> Calendar.DateTime.from_erl!(timezone, {123456, 6}) |> IO.inspect |> Calendar.DateTime.add!(86400)
end)
And the
acc |> Calendar.DateTime.to_erl |> IO.inspect |> Calendar.DateTime.from_erl!(timezone, {123456, 6}) |> IO.inspect |> Calendar.DateTime.add!(86400)
give output as
{{2016, 10, 27}, {1, 0, 0}}
%Calendar.DateTime{abbr: "IST", day: 27, hour: 1, min: 0, month: 10, sec: 0,
std_off: 3600, timezone: "Europe/Dublin", usec: {123456, 6}, utc_off: 0,
year: 2016}
{{2016, 10, 28}, {1, 0, 0}}
%Calendar.DateTime{abbr: "IST", day: 28, hour: 1, min: 0, month: 10, sec: 0,
std_off: 3600, timezone: "Europe/Dublin", usec: {123456, 6}, utc_off: 0,
year: 2016}
{{2016, 10, 29}, {1, 0, 0}}
%Calendar.DateTime{abbr: "IST", day: 29, hour: 1, min: 0, month: 10, sec: 0,
std_off: 3600, timezone: "Europe/Dublin", usec: {123456, 6}, utc_off: 0,
year: 2016}
{{2016, 10, 30}, {1, 0, 0}}
But on the last date and time instead of giving me right output as it is giving above, It is giving this
{:ambiguous, %Calendar.AmbiguousDateTime{possible_date_times: [%Calendar.DateTime{abbr: "GMT", day: 30, hour: 1, min: 0, month: 10, sec: 0, std_off: 0, timezone: "Europe/Dublin", usec: {123456, 6}, utc_off: 0, year: 2016}, %Calendar.DateTime{abbr: "IST", day: 30, hour: 1, min: 0, month: 10, sec: 0, std_off: 3600, timezone: "Europe/Dublin", usec: {123456, 6}, utc_off: 0, year: 2016}]}}
Am totally unknown to that reason as It was working fine but all of sudden its giving me these errors. any help?
UPDATE: am using https://github.com/lau/calendar Calendar.
According to the docs, this happens when the input time you provide is ambiguous:
AmbiguousDateTime provides a struct which represents an ambiguous time
and date in a certain time zone. These structs will be returned from
the DateTime.from_erl/2 function when the provided time is ambiguous.
AmbiguousDateTime contains two DateTime structs. For instance they can
represent both a DST and non-DST time. If clocks are turned back an
hour at 2:00 when going from summer to winter time then the “wall
time” between 1:00 and 2:00 happens twice. One of them is on DST and
one of them is not.
This seems to be your case. The times output are 1 a.m. Europe/Dublin time, and the 1 a.m. occurs twice on October 30th 2016: the second time is when at 2 a.m. you turn the clock back when Daylight Savings Time ends.
It seems like you can use the disamb/2 function to disambiguate the result you're getting.
How would I compare adjacent elements in:
line = 'Aug 2013-Mar 2014; Apr 2013-Aug 2013; Jun 2014-Aug 2015; Apr 2003-Nov 2004; Apr 2014-Jan 2015'
to check whether:
The first year value in the proceeding element is less than or equal to the last year value in the current element, and
The first month value in the proceeding element is less than, equal to, or one greater than the month value in the current element
If the condition is met, I would like to move the latter two values from the proceeding element to the current element in a combination or hard set value and delete the proceeding element.
#Expected result => [[2003, 4, 2004, 11], [2013, 4, 2015, 8]]
This is my attempt:
require 'date'
line = line.split(/[-,;]/)
months = []
years = []
line.each do |x|
x = DateTime.parse(x)
months << x.mon
years << x.year
end
years.zip(months).flatten.each_slice(4).to_a.sort_by{|x| [x[2],x[3]]}
#=> [[2003, 4, 2004, 11], [2013, 4, 2013, 8], [2013, 8, 2014, 3], [2014, 4, 2015, 1], [2014, 6, 2015, 8]]
Well, conditions are cumbersome, so I expect you are able to write a proper condition to select elements. The first step would be to get dates:
line = 'Aug 2013-Mar 2014; Apr 2013-Aug 2013; ' +
'Jun 2014-Aug 2015; Apr 2003-Nov 2004; Apr 2014-Jan 2015'
dates = line.split(/\s*[,;]\s*/).map do |s|
s.split('-')
end.map do |d1, d2|
[Date.parse(d1), Date.parse(d2)]
end
#⇒ [[#<Date: 2013-08-01 ((2456506j,0s,0n),+0s,2299161j)>,
#<Date: 2014-03-01 .......]]
dates.map { |dd| dd.inject(&:-) }
#⇒ [(-212/1), (-122/1), (-426/1), (-580/1), (-275/1)]
As I understood, you want to select something basing on date difference. Here you go (e. g. for one year):
dates.select do |dd|
dd.inject(&:-) < -365
end.map do |d1, d2|
[d1.year, d1.month, d2.year, d2.month]
end
#⇒ [[2014, 6, 2015, 8], [2003, 4, 2004, 11]]