import numpy as np
import matplotlib.pyplot as plt
def generate_data(num_points=100, outlier_ratio=0.2, seed=42):
np.random.seed(seed)
x = np.random.rand(num_points) * 10
y = 2 * x + 1 + np.random.randn(num_points)
# Add outliers
num_outliers = int(outlier_ratio * num_points)
outliers_index = np.random.choice(num_points, num_outliers, replace=False)
y[outliers_index] += 10 * np.random.randn(num_outliers)
return x, y
def fit_line_ransac(x, y, num_iterations=100, threshold=1.0):
best_model = None
best_inliers = None
for _ in range(num_iterations):
# Randomly sample two points
sample_indices = np.random.choice(len(x), size=2, replace=False)
x_sample = x[sample_indices]
y_sample = y[sample_indices]
# Fit a line to the sampled points
model = np.polyfit(x_sample, y_sample, deg=1)
# Calculate distances to the line for all points
distances = np.abs(y - (model[0] * x + model[1]))
# Count inliers (points within the threshold)
inliers = np.where(distances < threshold)[0]
# Update the best model if the current model has more inliers
if len(inliers) > len(best_inliers):
best_model = model
best_inliers = inliers
return best_model, best_inliers
def plot_ransac_result(x, y, model, inliers):
plt.scatter(x, y, label='Data', color='blue')
plt.scatter(x[inliers], y[inliers], label='Inliers', color='green')
if model is not None:
x_line = np.linspace(min(x), max(x), 100)
y_line = model[0] * x_line + model[1]
plt.plot(x_line, y_line, label='RANSAC Model', color='red')
plt.legend()
plt.show()
# Generate synthetic data with outliers
x_data, y_data = generate_data()
# Apply RANSAC algorithm to fit a line
best_fit, inliers_indices = fit_line_ransac(x_data, y_data)
# Plot the original data, inliers, and the RANSAC model
plot_ransac_result(x_data, y_data, best_fit, inliers_indices)
카테고리 없음