2016-08-10 3 views
0

координаты = [(259, 168), (62, 133), (143, 163), (174, 270), (321, 385)]Как найти выбросы в заданном наборе данных с использованием Python

склон = 0,76083799
перехватывать = 77,87127406

enter image description here

координата с коричневым маркером является потенциальным останец для меня, и, следовательно, должны быть удалены. На данный момент я нахожусь , попробовав, чтобы использовать остатки студента и остатки лебедки, чтобы удалить эти выбросы. Однако я не могу вычислить эти остатки, учитывая набор данных, который у меня есть.

Было бы очень полезно, если бы вы помогли мне найти остатки и как это сделать в приведенном выше наборе данных.

КОД

import numpy as np 
import matplotlib.pyplot as plt 

coordinates = [(259, 168), (62, 133), (143, 163), (174, 270), (321, 385)] 

x=[x1[0] for x1 in coordinates] 
y=[x1[1] for x1 in coordinates] 

for x1,y1 in coordinates: 
    plt.plot(x1,y1,marker="o",color="brown") 
plt.show() 

# using numpy polyfit method to find regression line slope and intercept 
z = np.polyfit(x,y,1) 
print(z) 
slope = z[0] 
intercept =z[1] 

newx = np.linspace(62,321,200) 
newy = np.poly1d(z) 
plt.plot(x,y, 'o', newx, newy(newx),color="black") 
# plt.plot() 
plt.plot(259,168,marker="o",color="brown") 
plt.show() 

#TODO 
#remove the outliers and then display 
+0

какой код вы пробовали до сих пор? –

+0

@OscarSmith Я добавил код – kkk

ответ

1

х и у помещают в np.ndarrays на старте.

Вход:

import numpy as np 
import matplotlib.pyplot as plt 

coordinates = [(259, 168), (62, 133), (143, 163), (174, 270), (321, 385)] 

x=np.array([x1[0] for x1 in coordinates]) #Placed into array 
y=np.array([x1[1] for x1 in coordinates]) #Placed into array 

for x1,y1 in coordinates: 
    plt.plot(x1,y1,marker="o",color="brown") 
plt.show() 

# using numpy polyfit method to find regression line slope and intercept 
z = np.polyfit(x,y,1) 
print(z) 
slope = z[0] 
intercept =z[1] 

newx = np.linspace(62,321,200) 
newy = np.poly1d(z) 
plt.plot(x,y, 'o', newx, newy(newx),color="black") 
# plt.plot() 
plt.plot(259,168,marker="o",color="brown") 
plt.show() 

Дополнительный код:

print("old y: " + repr(y)) #Display original array of y values 
print("old x: " + repr(x)) 
residual_array = abs(y - (intercept + slope * x)) #Create an array of residuals 
max_accept_deviation = 100 #An arbitrary value of "acceptable deviation" 
mask = residual_array >= max_accept_deviation #Create an array of TRUE/FALSE values. TRUE where residual array is larger than deviation 
rows_to_del = tuple(te for te in np.where(mask)[0]) #np.where converts the mask to a list of row numbers which is converted to a tuple 
cleaned_y = np.delete(y,rows_to_del) #np.delete deletes all row numbers in the earlier tuple 
cleaned_x = np.delete(x,rows_to_del) 
print("new y: " + repr(cleaned_y)) #Print the cleaned values 
print("new x: " + repr(cleaned_x)) 

Выход:

[ 0.76083799 77.87127406] 
old y: array([168, 133, 163, 270, 385]) 
old x: array([259, 62, 143, 174, 321]) 
new y: array([133, 163, 270, 385]) 
new x: array([ 62, 143, 174, 321]) 
+1

большое спасибо за ваш ответ, не могли бы вы немного объяснить, что было сделано в коде. – kkk

+0

Хорошо, я добавил комментарии к дополнительному коду! Просто дайте мне знать, если все, о чем вы не уверены! – Yarnspinner