How to cluster a list of multiple sets using clustering data-mining algorithms? - cluster-analysis

I have a list of multiple sets as an input, for example:
[
{{0, 1, 3}, {2, 4, 5, 8}, {6, 7, 9, 10}},
{{0, 1, 2, 3}, {4, 5, 8}, {6, 7, 9}, {10}},
{{0, 1, 2, 3}, {4, 5, 8}, {6, 7, 9}, {10}},
{{0}, {1, 2, 3}, {4, 5, 6, 7, 8, 9, 10}},
...
]
Every row in the list is a set, which contains multiple sets that aggregate the numbers 1~10.
I want to cluster these rows so that the rows that cluster numbers 1-10 following a similar pattern will be clustered.
I have been contemplating for a long time, still can't come up with any ideas of how to make these rows clusterable by clustering algorithm like k-means.
Please give me hints, thank you very much.

Related

MongoDB: Can I store stock data in this way?

{
{
"symbol": "MSFT",
"close": [0, 1, 2, 3, 4, 5],
"open": [0, 1, 2, 3, 4, 5],
"high": [0, 1, 2, 3, 4, 5],
"low": [0, 1, 2, 3, 4, 5],
"volume": [0, 1, 2, 3, 4, 5],
"dates": ["2022-01-01", "2022-01-02", "2022-01-03", "2022-01-04", "2022-01-05", "2022-01-06"],
"date_to_index": {
"2022-01-01": 0,
"2022-01-02": 1,
"2022-01-03": 2,
"2022-01-04": 3,
"2022-01-05": 4,
"2022-01-06": 5
}
}
when I need the data of MicroSoft from 2022-01-03 to 2022-01-05, I will get the start and end indices from date_to_index and then retrieve the slice from index 2 to index 4 of the data arrays I want.
You can certainly store data this way, but
looks you'll need to fetch the entire object each time you want to extract only a part of data or do two queries. Either way, it looks not ideal.
Gut feeling says there's a risk of not fitting into document size limit when using real world data (MSFT, for example, has decades of stock data history). Having sub-day resolution increases this risk even further.
Overall, I'd explore alternate strategies.

MongoDB - Find how many documents have the same characteristics

I'm saying sorry for the title and for not providing an example, but I'm very new to MongoDB and, after trying to accomplish this result using MySQL, I moved to MongoDB because I think that can be simpler to archive this result :(
I'll need to find how many documents have the same "characteristics".
I try to expose this with a restaurant example:
I need to find the most popular dishes that a family ordered
This is the dataset, where persons and withChildren is the criteria of the group by:
{"persons": 4, "dish1": 3, "dish2": 4},
{"persons": 4, "dish1": 3, "dish2": 4},
{"persons": 4, "dish1": 3, "dish2": 4},
{"persons": 4, "withChilden": true, "dish1": 3, "dish2": 4},
{"persons": 4, "dish1": 3, "dish2": 2},
{"persons": 4, "dish1": 3, "dish2": 2},
{"persons": 4, "dish1": 3, "dish2": 2, "dish3": 6},
I make a separation to the rows to better show the difference:
(4 persons) has ordered (dish1=3 / dish2=4) three times
(4 persons withChilden) has ordered (dish1=3 / dish2=4) one time
(4 persons has ordered) has ordered (dish1=3 / dish2=2) two times
(4 persons has ordered) has ordered (dish1=3 / dish2=2 / dish3=6) one time
The goal is to produce documents that expose the previous rows, like that:
{
{ "type": {"persons": 4} },
"dish1": 3,
"dish2": 4,
"tot": 3
}
For the type with children, will be:
{
{ "type": {"persons": 4, "withChildren": true} },
"dish1": 3,
"dish2": 4,
"tot": 1
}
I'll already try to read this solutions, that seems to be a little similar on what I need to accomplish, but because I'm very new to MongoDB I don't know if it's possible to have this result with a single query, if I need to write a script and so on.
The nested object in the result is not trivial, so the result could be a plain object too, like that:
{
"persons": 4,
"dish1": 3,
"dish2": 4,
"tot": 3
}
Thanks a lot for your help and understanding

RxDart convert Stream based on previous value

Lets say I have a Stream that emits a List followed by single elements like:
Stream.fromIterable([
[1, 2, 3],
4,
5,
]);
How to convert it to a Stream that updates the previous element with current value and emits:
[1, 2, 3],
[1, 2, 3, 4],
[1, 2, 3, 4, 5],

How to generate combinations of r elements in a given array of size n in swift?

I want total permutation pairs and its count
Like.
If input array is {1, 2, 3, 4} and r is 2.
then output should be {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4} and {3, 4}.
Actually the total permutation pairs are
[[1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4, 2], [4, 3]]
There's no need to reinvent the wheel. Apple provides a collection of useful and optimized algorithms.
Add the package Swift Algorithms to your project
Then write
import Algorithms
let array = [1, 2, 3, 4]
let permutations = array.permutations(ofCount: 2)
print(Array(permutations), permutations.count)

MapThread, Manipulate, Filter in Mathematica

I hope to be able to name that question properly soon.
Please Consider :
list1 = Tuples[Range[1, 5], 2];
list2 = Tuples[Range[3, 7], 2];
*I use the below mechanism to display all filtered eye fixations during a display. *
Manipulate[Row[
MapThread[Function[{list},
Graphics[
Point[{#[[1]], #[[2]]}]& /# Select[list,
(#[[1]] > f1 && #[[2]] > f2) &],
Frame -> True, PlotRange -> {{0, 10}, {0, 10}}]],
{{list1, list2}}]],
{f1, 0, 10}, {f2, 0, 10}]
Now, I would like to display each fixation (point) one at a time, cumulatively.
That is :
Given
list1 = {{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 1}, {2, 2}, {2, 3}, {2, 4},
{2, 5}, {3, 1}, {3, 2}, {3, 3}, {3, 4}, {3, 5}, {4, 1}, {4, 2}, {4, 3},
{4, 4}, {4, 5}, {5, 1}, {5, 2}, {5, 3}, {5, 4}, {5, 5}}
Use a slider to display the 1 to 25 Points here. But after filter the 1 to Length#Filtered Data
The Slider that control the Fixation number has yet a fixed boundary (25) , whereas it should have one equal to the Length of the filtered list.
But there is 2 due to Mapthread.
And I cannot extend the Mapthread to the Manipulate Control, could I ?
Manipulate[Row[MapThread[Function[{list},
Graphics[
Point[{#[[1]], #[[2]]}]& /# Select[list,
(#[[1]] > f1 && #[[2]] > f2) &]
[[1 ;; dd, All]],
Frame -> True, PlotRange -> {{0, 10}, {0, 10}}]],
{{list1, list2}}]],
{f1, 0, 10}, {f2, 0, 10},{dd,0,25}]
Perhaps something like:
(Beware of code efficiency)
list1 = Tuples[Range[1, 5], 2];
list2 = Tuples[Range[3, 7], 2];
f = (Select[#, (#[[1]] > f1 && #[[2]] > f2) &] &);
Manipulate[
Row#Graphics[Point##, Frame -> True, PlotRange -> {{0, 10}, {0, 10}}] & /#
Map[Map[f, {#}][[All, 1 ;; Min[dd, Length ## Map[f, {#}]], All]] &,
{list1, list2}],
{f1, 0, 10}, {f2, 0, 10}, {dd, 0, 25, 1}]
Try it with {dd, 0, 25, 1}. This both allows it to parse correctly (closing brace) and keeps it real, so to speak, by preventing dd from being real valued.