Keep eliminating data points until good correlation coefficient is obtained-using Python I have been trying to find out a way in order to eliminate outliers from a dataset. The outliers are removed the following way: Any value which results into a 10% reduction in R2 value needs to be removed. When 4.2 in A-data set got replaced with 1.3 (in B-dataset), it changed the R2 >10% and thus was eliminated in the C-dataset.
However, when 0.7 in A was replaced with 0.9, it would not change the correlation coefficient by 10% and thus was not removed from C-dataset.
An image is attached herewith.

In the image, -plot A has R2 of 1.0 -plot B has R2 of 0.8294 (1.3 is the outlier since it causes >10% lowering of R2 values) -plot C has R2 of 1.0 (on removing 1.3 from the dataset)
How do I go about this issue. I need to use python to get to the solution. Out of the 10 data points a maximum of only 3 data points can be removed inorder to improve the correlation.
I apologize if this question was asked before. Thanks a ton for the help!
 A: This should be similar to what you are looking for:
import numpy, scipy, matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import scipy.stats
import copy

xyDataPairs = [[1.1, 0.1], [2.2, 2.1],[3.3, 3.4], [4.4, 4.2],[5.0, 5.6],[6.7,6.9]]

minDataPoints = len(xyDataPairs) - 3

# utility function
def UniqueCombinations(items, n):
    if n==0:
        yield []
    else:
        for i in range(len(items)):
            for cc in UniqueCombinations(items[i+1:],n-1):
                yield [items[i]]+cc

bestR2 = 0.0
bestDataPairCombination = []
bestParameters = []

for pairs in UniqueCombinations(xyDataPairs, minDataPoints):
    x = []
    y = []
    for pair in pairs:
        x.append(pair[0])
        y.append(pair[1])
    fittedParameters = numpy.polyfit(x, y, 1) # straight line
    modelPredictions = numpy.polyval(fittedParameters, x)
    absError = modelPredictions - y
    Rsquared = 1.0 - (numpy.var(absError) / numpy.var(y))
    if Rsquared > bestR2:
        bestR2 = Rsquared
        bestDataPairCombination = copy.deepcopy(pairs)
        bestParameters = copy.deepcopy(fittedParameters)


##########################################################
# graphics output section
def ModelAndScatterPlot(graphWidth, graphHeight):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
    axes = f.add_subplot(111)

    x = []
    y = []
    for pair in bestDataPairCombination:
        x.append(pair[0])
        y.append(pair[1])

    # first the raw data as a scatter plot
    axes.plot(x, y,  'D')

    # create data for the fitted equation plot
    xModel = numpy.linspace(min(x), max(x))
    yModel = numpy.polyval(bestParameters, xModel)

    # now the model as a line plot
    axes.plot(xModel, yModel)

    axes.set_xlabel('X Data') # X axis data label
    axes.set_ylabel('Y Data') # Y axis data label

    plt.show()
    plt.close('all') # clean up after using pyplot

graphWidth = 800
graphHeight = 600
ModelAndScatterPlot(graphWidth, graphHeight)
print('best parameters"', bestParameters)
print('best R-squared:', bestR2)

