Sep-11-2020, 09:48 PM
I am supposed to plot angle = (slope)*voltage + (intercept) using linear regression
and angle and voltage are given.
angle (radians) = [-1.57, -1.26, -0.94, -0.63, -0.31, 0., 0.31, 0.65, 0.96, 1.23, 1.52]
voltage (volts) = [1.176, 1.648, 1.874, 2.175, 2.438, 2.503, 2.925, 3.067, 3.450, 3.686, 4.014]
I need to find slope, r^2 and interscept and plot this.
this is my code.
and angle and voltage are given.
angle (radians) = [-1.57, -1.26, -0.94, -0.63, -0.31, 0., 0.31, 0.65, 0.96, 1.23, 1.52]
voltage (volts) = [1.176, 1.648, 1.874, 2.175, 2.438, 2.503, 2.925, 3.067, 3.450, 3.686, 4.014]
I need to find slope, r^2 and interscept and plot this.
this is my code.
import numpy as np import matplotlib.pyplot as plt import csv import pandas as pd from sklearn.linear_model import LinearRegression #%%------------------------------------- #number 1 voltage= np.array([1.176,1.648,1.874,2.175,2.438,2.503,2.925,3.067,3.450,3.686,4.014]).reshape(1,-1) angle = np.array([-1.57,-1.26,-0.94,-0.63,-0.31,0.,0.31,0.65,0.96,1.23,1.52]).reshape(1,-1) # Linear Regression model = LinearRegression().fit(voltage, angle.reshape(1,-1)) Rsquared = model.score(angle, voltage) slope, intercept = model.coef_, model.intercept_ print('intercept =', intercept) print('slope =', slope) print('coefficient of determination =', Rsquared) # Plot the raw data and regression using a scatter plot plt.figure(1) plt.scatter(voltage,angle, c="r", marker='x') plt.plot(voltage,slope*voltage + intercept) plt.xlabel('voltage [volts]') plt.ylabel('Angles [rad]') plt.legend(['Lin. Reg.','Raw Data']) plt.title('Regression of Sensor Data') plt.grid() plt.show()and this is what I get
intercept = [-1.57 -1.26 -0.94 -0.63 -0.31 0. 0.31 0.65 0.96 1.23 1.52] slope = [[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
Error:ValueError: x and y must have same first dimension, but have shapes (1, 11) and (11, 11)
I did get a plot too but without any labels. Any idea?