Python Hello I need help with this code I really dont know w

Python

Hello I need help with this code I really dont know what to do, thank you in advance. I was not given a test input file, the only two files I was given, is the assignment file that I have attach below. The only reason why I attach it is because the script is under python and the input file that I have attach as a hyperlink towards the end. This are the instructions that I was given:

The main objective of this assignment is to implement the RANSAC algorithm for linear regression as we discussed in the class. RANSAC can help you find outliers. Download the code template (the code thatg i have copied and paste it below) and the input data file (the hyperlink I attach below). .

\"\"\"
The main objective of this assignment is to implement the RANSAC algorithm
for linear regression
\"\"\"

import numpy as np
from sklearn import linear_model, datasets
from matplotlib import pyplot as plt
import json

def RANSAC(observed_data,threshold):
\"\"\"
observed_data: the set of obverserved data for fitting a linear regression
model.
threshold: the threshold is used to differentiate inliers from outliers by
testing the error deviation. You can use a predefined percentage as the
threshold as we show in the class.
\"\"\"
X,y=observed_data

\"\"\"
Write your codes here
Find outliers and remove them from (X,y), return (newX,newY)
\"\"\"
  
#Keep the following code unchanged
return (newX,newy)


\"\"\"
Use the following code to test your RANSAC implementation.
DON\'T change this part.
\"\"\"
with open(\'ass3input.txt\',\'r\') as infile:
indata=json.load(infile)

X=np.array(indata[\'X\'])
y=np.array(indata[\'y\'])   

newX,newy=RANSAC((X,y),0.01)

model = linear_model.LinearRegression()
model.fit(newX, newy)
yhat=model.predict(newX)

m_coef=model.coef_
m_intercept=model.intercept_

outdata={\'coef\':m_coef.tolist(),\'intercept\':m_intercept.tolist()}
with open(\'ass3output.txt\',\'w\') as outfile:
json.dump(outdata,outfile)

plt.scatter(X, y, color=\'yellowgreen\', marker=\'.\')
plt.plot(X, yhat, color=\'navy\', linestyle=\'-\', linewidth=2,
label=\'Linear regressor\')

plt.show()

I have also attach the link of the assignment input:

-ass3input.txt

Solution

import numpy as np
import scipy
import matplotlib.pyplot as plt
import math
import sys


ransac_iterations = 20 # number of iterations
ransac_threshold = 3 # threshold
ransac_ratio = 0.6 # ratio of inliers required to assert
# that a model fits well to data


n_samples = 500 # number of input points
outliers_ratio = 0.4 # ratio of outliers

n_inputs = 1
n_outputs = 1

x = 30*np.random.random((n_samples,n_inputs) )

perfect_fit = 0.5*np.random.normal(size=(n_inputs,n_outputs) )

y = scipy.dot(x,perfect_fit)

RANSAC plot:


ef ransac_plot(n, x, y, m, c, final=False, x_in=(), y_in=(), points=()):
\"\"\" plot the current RANSAC step
:param n iteration
:param points picked up points for modeling
:param x samples x
:param y samples y
:param m slope of the line model
:param c shift of the line model
:param x_in inliers x
:param y_in inliers y
\"\"\"

fname = \"output/figure_\" + str(n) + \".png\"
line_width = 1.
line_color = \'#0080ff\'
title = \'iteration \' + str(n)

if final:
fname = \"output/final.png\"
line_width = 3.
line_color = \'#ff0000\'
title = \'final solution\'

plt.figure(\"Ransac\", figsize=(15., 15.))

# grid for the plot
grid = [min(x) - 10, max(x) + 10, min(y) - 20, max(y) + 20]
plt.axis(grid)

# put grid on the plot
plt.grid(b=True, which=\'major\', color=\'0.75\', linestyle=\'--\')
plt.xticks([i for i in range(min(x) - 10, max(x) + 10, 5)])
plt.yticks([i for i in range(min(y) - 20, max(y) + 20, 10)])

# plot input points
plt.plot(x[:,0], y[:,0], marker=\'o\', label=\'Input points\', color=\'#00cc00\', linestyle=\'None\', alpha=0.4)

# draw the current model
plt.plot(x, m*x + c, \'r\', label=\'Line model\', color=line_color, linewidth=line_width)

# draw inliers
if not final:
plt.plot(x_in, y_in, marker=\'o\', label=\'Inliers\', linestyle=\'None\', color=\'#ff0000\', alpha=0.6)

# draw points picked up for the modeling
if not final:
plt.plot(points[:,0], points[:,1], marker=\'o\', label=\'Picked points\', color=\'#0000cc\', linestyle=\'None\', alpha=0.6)

plt.title(title)
plt.legend()
plt.savefig(fname)
plt.close()

Python Hello I need help with this code I really dont know what to do, thank you in advance. I was not given a test input file, the only two files I was given,
Python Hello I need help with this code I really dont know what to do, thank you in advance. I was not given a test input file, the only two files I was given,
Python Hello I need help with this code I really dont know what to do, thank you in advance. I was not given a test input file, the only two files I was given,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site