Return the gradient and y intercept (m, b) to create two lines to best fit the data

I have been working on this task for a few hours now and have been unsuccessful with getting the target result. I have tried using multiple methods of trying to split the dataset using different clustering methods and logistical regression with no luck. I thought noncontinuous piecewise linear regression may work however found no good resources on how to implement it.

The taks is given a 2D NumPy array of x, y data points determine the gradient and y-intercept for exactly two lines that best fit the data. Here is an example photo where the two blue lines show what a solved solution looks like.

Any help would be greatly appreciated.

Topic linear-regression graphs dataset

Category Data Science


One option could be to take a hierarchical approach:

  1. Separate the values into two clusters. Since connectiveness within a cluster is important, use an algorithm like spectral clustering.

  2. Fit a separate regression line to each respective cluster.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.