I have a data set which results in a series of non-parallel linear trends on a scatter plot.

[![enter image description here](https://i.stack.imgur.com/HJgs5.png)](https://i.stack.imgur.com/HJgs5.png)

I'm trying to find a way to **classify each data point into its closest corresponding linear trend**. There are six linear trends in this data set.

This [blog post](http://blog.yatis.io/how-to-measure-clutch-riding-with-obd-iot-device/) appears to have [already done that](http://blog.yatis.io/wp-content/uploads/2016/10/rpm-vs-speed-clustered-300x210.png) using [K-means clustering](http://disq.us/p/1l8gnts).

My attempt at clustering the data using K-means resulted in the following:

[![enter image description here](https://i.stack.imgur.com/8cj6a.png)](https://i.stack.imgur.com/8cj6a.png)

Here is the code I am using (as well as some dummy data):

```
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

data = {'a': {0: 2.0,  1: 3.0,  2: 4.0,  3: 5.0,  4: 6.0,  5: 7.0,  6: 8.0,  7: 9.0,  8: 10.0,  9: 11.0,  
              10: 1.0,  11: 1.5,  12: 2.0,  13: 2.5,  14: 3.0,  15: 3.5,  16: 4.0,  17: 4.5,  18: 5.0,  
              19: 5.5,  20: 6.0,  21: 6.5,  22: 7.0,  23: 7.5,  24: 8.0,  25: 8.5,  26: 9.0,  27: 9.5,  
              28: 10.0,  29: 10.5,  30: 11.0, 31: 1.0,  32: 1.2,  33: 1.4,  34: 1.6,  35: 1.8,  36: 2.0}, 
        'b': {0: 110,  1: 140,  2: 170,  3: 200,  4: 230,  5: 260,  6: 290,  7: 320,  8: 350,  9: 380,  
              10: 160,  11: 190,  12: 220,  13: 250,  14: 280,  15: 310,  16: 340,  17: 370,  18: 400,  
              19: 430,  20: 460,  21: 490,  22: 520,  23: 550,  24: 580,  25: 610,  26: 640,  27: 670,  
              28: 700,  29: 730,  30: 760,  31: 300,  32: 350,  33: 400,  34: 450,  35: 500,  36: 550}}

df = pd.DataFrame.from_dict(data)

kmeans = KMeans(n_clusters=3)
kmeans.fit(df)

plt.scatter(df['a'], df['b'],c=kmeans.labels_)

```

The result from the above code on the dummy data:

[![enter image description here](https://i.stack.imgur.com/zXGIG.png)](https://i.stack.imgur.com/zXGIG.png)

The correct result would have the clusters corresponding to each line.

**How can I reliably classify these trends**?