y X 40 30 20 10 0 -10 -20 Linear Regression - data with noise and outliers Training data with noise and outliers LR mod

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

y X 40 30 20 10 0 -10 -20 Linear Regression - data with noise and outliers Training data with noise and outliers LR mod

Post by answerhappygod »

Y X 40 30 20 10 0 10 20 Linear Regression Data With Noise And Outliers Training Data With Noise And Outliers Lr Mod 1
Y X 40 30 20 10 0 10 20 Linear Regression Data With Noise And Outliers Training Data With Noise And Outliers Lr Mod 1 (152.84 KiB) Viewed 21 times
Y X 40 30 20 10 0 10 20 Linear Regression Data With Noise And Outliers Training Data With Noise And Outliers Lr Mod 2
Y X 40 30 20 10 0 10 20 Linear Regression Data With Noise And Outliers Training Data With Noise And Outliers Lr Mod 2 (183.36 KiB) Viewed 21 times
from matplotlib import pyplot as pltimport numpy as npimport csv
def ReadCSV(filename): with open(filename) as csv_file: csv_reader = csv.reader(csv_file,delimiter=',') x_mylist = [] y_mylist = [] for row in csv_reader: x_mylist.append(float(row[0]) ) y_mylist.append(float(row[1]) ) return x_mylist, y_mylist
# ******************_x, _y = ReadCSV('ParabolaWithNoiseOutlier.csv')number_of_samples = len(_x)x_pos = np.array(_x)x_pos = x_pos.reshape(number_of_samples, 1)y_pos = np.array(_y)y_pos = y_pos.reshape(number_of_samples, 1)
# *****************************plt.scatter( x_pos, y_pos, color="gold", marker=".",label="Training data with noise and outliers")
plt.xlabel('x', fontsize=15)plt.ylabel('y', fontsize=15)
# ***************************
t1 = x_pos ** 2t1 = t1.reshape(number_of_samples, 1)t2 = x_pos.reshape(number_of_samples, 1)K = np.concatenate( (t1, t2), axis=1 )
# ************************from sklearn.linear_model import LinearRegressionreg = LinearRegression().fit(K, y_pos)
print( f'Linear regression model: Coefficients = {reg.coef_} intercept = {reg.intercept_}' )print( f' y = {reg.coef_[0][0]} * x**2 + {reg.coef_[0][1]} * x +{reg.intercept_}' )
# ****************************#Draw the fitted curvemin_x = min(x_pos)max_x = max(x_pos)
numOfPoint = 300temp_x = np.linspace(min_x, max_x, num = numOfPoint)xpoints = temp_x.reshape(numOfPoint, 1)xxpoints = xpoints ** 2H = np.concatenate( (xxpoints, xpoints), axis=1)prediction = reg.predict(H)
plt.plot(xpoints, prediction, color="blue", linewidth=1,label="LR model")
plt.legend(loc='lower left', fontsize=12)plt.title('Linear Regression - data with noise and outliers')plt.show()# ******************************prediction = reg.predict( K )residual = prediction - y_posplt.hist(residual, bins=30)plt.xlabel('Residual', fontsize=15)plt.ylabel('Count', fontsize=15)plt.title('Residual histogram (data with noise andoutliers)')plt.show()
# ****************************# Predict y value given x value of 40.0
x_test = np.array([[40.0 ** 2, 40.0],])y_result = reg.predict(x_test)
print(f'When x = {x_test[0]}, y = {y_result[0]}')
How to find the physical equation in Python language tocalculate the blue line and explain what is used in the language?please
y X
40 30 20 10 0 -10 -20 Linear Regression - data with noise and outliers Training data with noise and outliers LR model 20 40 60 80
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply