# Agglomerative Hierarchial Clustering in python using DTW distance

U

#### user3806649

##### Guest
user3806649 Asks: Agglomerative Hierarchial Clustering in python using DTW distance
I am new to both data science and python. I have a dataset of the time-dependent samples, which I want to run agglomerative hierarchical clustering on them. I have found that Dynamic Time Warping (DTW) is a useful method to find alignments between two time series which may vary in time or speed.

I have found `dtw_std` in `mlpy` library and `scipy.cluster.hierarchy` in `SciPy` in order to cluster my data.

From the scipy docs, I find that I could use my custom distance function:

metric : str or function, optional The distance metric to use in the case that y is a collection of observation vectors; ignored otherwise. See the pdist function for a list of valid distance metrics. A custom distance function can also be used.

But I am stuck matching this information to implement clustering.

My dataset is in the format of `dataframe` which each row corresponds to a sample.

Here is my questions:

1- How can I provide distance matrics for the linkage function?

2- How to set my custom distance function?

Code:
``````import pandas as pd
import scipy.cluster.hierarchy as hac
import mlpy

dataset = pd.read_csv ( "dataset.csv",encoding='utf-8' )
X # distance matrics
cluster = hac.fcluster(Z, t=100, criterion='maxclust')

fig = plt.figure(figsize=(25, 10))
dn = dendrogram(Z)
plt.show()``````

edit:

Here is how I compute distance matrix, then I pass it to linkage:

Code:
``````# computing distance matrix
dm = pdist ( dataset ,lambda u,v: mlpy.dtw_std ( pd.Series(u).dropna().values.tolist(),pd.Series(v).dropna().values.tolist(),dist_only=True ))
cluster = hac.fcluster(z, t=100, criterion='maxclust')

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.

#### Linearly independent paths and cyclomatic complexity

jack malkovick Asks: Linearly independent paths and cyclomatic complexity
I'm trying to find out what are the linearly independent paths in this control flow graph. The cyclomatic complexity is 4, by counting the bounded areas. Let's assume that the loop is executed maximum twice.

The definition that I use is a linearly independent path is any path through the program that introduces at least one new edge that is not included in any other linearly independent paths.

Below is a list of all the paths that I see. What are the 4 linearly independent ones? Maybe the definition is incomplete or I don't know how to apply it correctly.

Code:
``````A - B - C - A - E
A - B - D - A - E
A - E
A - B - C - A - B - C - A - E
A - B - D - A - B - D - A - E
A - B - C - A - B - D - A - E
A - B - D - A - B - C - A - E``````

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.

#### [Solved] EPSG:3857 to 4326 - incorrect geometry

• Tomáš Černý
• Geography
• Replies: 0
Tomáš Černý Asks: EPSG:3857 to 4326 - incorrect geometry
I need to get simplified boundaries of all countries with some buffer around them (20 km). I am using QGIS, I converted my country polygons vector layer from 4326 to 3857, set buffer, used simplify and the result looks OK.

Then, when I try to convert the layer with the new polygons to 4326 I get some malformed polygons for Russia, Fiji, Antarctica, and Tuvalu - I think it's because they appear on both east and west sides of the map in the 4326 projection. Then, when I try to use the "split vector layer" function, it says `incorrect geometry`.

Is there any way, how to do it "correctly"? I am new to QGIS, I need this for my other project.

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, solveforum.

#### How does gravitational redshift and blueshift work?

• externo
• Physics
• Replies: 0
externo Asks: How does gravitational redshift and blueshift work?
In a gravitational field, if a source emits a signal from below (at higher potential) every second, the signal will be received above (at lower potential) with a lower apparent frequency because time elapses faster above than below. For example 1 second down = 2 seconds up

But if the time metric is changing, the frequency of the light should increase as it travels up the field as time accelerates and compensate for the difference in measured frequencies so that its frequency at the top would be measured as equal to its frequency at the bottom. (If the source travels up the field it will emit at higher and higher frequencies, so the frequency of the light should also increase and arrive at the top with the same apparent frequency as at the bottom)

So I don't understand the mechanism of gravitational redshift.

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.

#### what is resistance?

• sanya
• Physics
• Replies: 0
According to my book resistance is the number of collisions free electrons suffer in any conducting device. Now insulators have no free electrons so how do they have high resistance like what collisions are taking place here? Am I getting resistance wrong or am I missing a point. Can someone explain this to me theoretically, numerically im aware of ohms law and how conductance and resistance are inversely related but what's the intuition behind it what is resistance as a whole?

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.

#### How can a vacuum suction cup/handling lift a workpiece?

• Jawel7
• Physics
• Replies: 0
Jawel7 Asks: How can a vacuum suction cup/handling lift a workpiece?
I am not asking how a suction cup can hang on a workpiece without falling down. I know explanation that atmosphere pressure is higher than inner pressure inside suction cups, so it hangs there, which is fine.

Let's imagine that there is a workpiece on the ground and I put a suction cup on it, then I'm lifting the suction cup. So my question is why the workpiece goes up with the suction cup? I can't see any reason for it, I'm just pulling the suction cup, not the workpiece.

If the suction cup and the workpiece were attached to each other physically, I would understand it. But in this case, there's no relationship between them, I just put the suction cup on it, that's all.

How does the suction cup transmit force I apply on it to the workpiece and lift it?

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.

#### Label or Callout the Index of a List or Array of Points using ListPointPlot3D

jacobian Asks: Label or Callout the Index of a List or Array of Points using ListPointPlot3D
I have found several examples of Callouts and Labels using Text command and/or mapping data to plotted points both in Mathematica documentation and through various forums including this one.

I am relatively new to Mathematica and do not quite understand how to adapt those examples to what I desire.

I am plotting Polytopes to better understand their symmetry and structure.

When I plot them though, sometimes I get confused on which vertices correspond to which faces.

I want to label the vertices with the index number from the vertices list (1,2,3..etc.,) so that when I construct the grouping for the faces to use with the Polygon function, I can easily troubleshoot/fix the faces based on the 3D Plot. That is I can visually see the labeled vertices around the faces and not have to consider which quadrants, etc., I am working in.

I did try the generic Labeling Function and while it shows the label of the points, it shows their values, not the index to them. The values help, but it is not as intuitive or as easy as mapping index numbers to faces. The points are also too cluttered to be useful.

Edit: it would also be useful to plot the index for the faces as well to make it even easier to troubleshoot.

Here is example code to illustrate what I am plotting. I want to move on to more complex Polytopes, but I want to figure this out first to save time and headaches later.

Code:
``````C0 = 1;
C1 = 2;
groupedVertices = {{{0, 0, C1}, {0, 0, -C1}, {C1, 0, 0}, {-C1, 0, 0}, {0,
C1, 0}, {0, -C1, 0}}, {{C0, C0, C0}, {C0, C0, -C0}, {C0, -C0,
C0}, {C0, -C0, -C0}, {-C0, C0, C0}, {-C0, C0, -C0}, {-C0, -C0,
C0}, {-C0, -C0, -C0}}};
vertices = Flatten[groupedVertices, 1];
faces = {{7, 1, 9, 3}, {7, 3, 8, 5}, {7, 5, 11, 1}, {10, 2, 8,
3}, {10, 3, 9, 6}, {10, 6, 14, 2}, {12, 2, 14, 4}, {12, 4, 11,
5}, {12, 5, 8, 2}, {13, 1, 11, 4}, {13, 4, 14, 6}, {13, 6, 9,
1}};
Show[
ListPointPlot3D[groupedVertices,
PlotStyle -> {Directive[Red, PointSize[0.05]],
Directive[Blue, PointSize[0.05]]}, AxesLabel -> {x, y, z}],
Graphics3D[
Polygon[
Table[vertices[[(#1[[j]])]], {j, 1, 4}] &, {faces}]]],
BoxRatios -> Automatic]``````

Polytope Plot in 3D

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.

#### Combining two region with boundaries: combining rectangle:

a019 Asks: Combining two region with boundaries: combining rectangle:
If I have the following two regions:

Code:
``````r1 = Rectangle[{0, 0}, {5, 10}]
r2 = Rectangle[{5, 0}, {15, 10}]``````

to combine them I can use,

Code:
``RegionPlot[RegionUnion[r1, r2]]``

which gives,

how can we add two regions with their boundaries still showing, for example in the following,

I added the black line by myself.

SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your thoughts here to help others.