Posts: 24
Threads: 7
Joined: Nov 2019
Feb-02-2020, 11:21 AM
(This post was last modified: Feb-02-2020, 11:21 AM by StillAnotherDave.)
Hello folks,
I have a physics report to write up and graphs need to be digitally produced. How do I add a line of best for the data below (the data points match the forward bias characteristic for a silicon diode - i.e. exponential curve):
import numpy as np
import matplotlib.pyplot as plt
plt.figure("Forward Bias Characteristic")
x = np.array([0.00, 0.53, 0.55, 0.57, 0.59, 0.61, 0.63, 0.65, 0.67, 0.69])
y = np.array([0.0, 0.0, 0.1, 0.2, 0.5, 1.0, 1.2, 2.3, 3.5, 5.4])
yerror = np.array([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1,])
plt.plot(x, y, 'rx')
plt.xlabel('$Volatge (V)$')
plt.ylabel('$Current (mA)$')
Posts: 12,031
Threads: 485
Joined: Sep 2016
Feb-02-2020, 02:19 PM
(This post was last modified: Feb-02-2020, 02:20 PM by Larz60+.)
Posts: 24
Threads: 7
Joined: Nov 2019
(Feb-02-2020, 02:19 PM)Larz60+ Wrote: perhaps reading https://pyspice.fabrice-salvaire.fr/exam...curve.html will help
Hi Larz60+,
Thanks but I think the info on the link is to do with generating a characteristic curve for forward bias ... I'm simply looking to plot a best fit curve for my actual data points that I've produced in my x and y arrays ...? The info in your link is way above my very basic grasp of python.
Posts: 24
Threads: 7
Joined: Nov 2019
Posts: 1,358
Threads: 2
Joined: May 2019
Asking for a linear regression, not sure your data will exactly fit, but here goes.
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets, linear_model
plt.figure("Forward Bias Characteristic")
x = np.array([0.00, 0.53, 0.55, 0.57, 0.59, 0.61, 0.63, 0.65, 0.67, 0.69])
y = np.array([0.0, 0.0, 0.1, 0.2, 0.5, 1.0, 1.2, 2.3, 3.5, 5.4])
yerror = np.array([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1,])
regr = linear_model.LinearRegression()
X = x.reshape(-1,1)
regr.fit(X, y)
y_pred = regr.predict(X)
plt.plot(x, y_pred, color='blue', linewidth=3)
plt.plot(x, y, 'rx')
plt.xlabel('$Volatge (V)$')
plt.ylabel('$Current (mA)$') Data looks like a curve, line is best fit line.
Posts: 24
Threads: 7
Joined: Nov 2019
Feb-05-2020, 11:05 AM
(This post was last modified: Feb-05-2020, 11:25 AM by StillAnotherDave.)
(Feb-04-2020, 12:17 PM)jefsummers Wrote: Asking for a linear regression, not sure your data will exactly fit, but here goes.
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets, linear_model
plt.figure("Forward Bias Characteristic")
x = np.array([0.00, 0.53, 0.55, 0.57, 0.59, 0.61, 0.63, 0.65, 0.67, 0.69])
y = np.array([0.0, 0.0, 0.1, 0.2, 0.5, 1.0, 1.2, 2.3, 3.5, 5.4])
yerror = np.array([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1,])
regr = linear_model.LinearRegression()
X = x.reshape(-1,1)
regr.fit(X, y)
y_pred = regr.predict(X)
plt.plot(x, y_pred, color='blue', linewidth=3)
plt.plot(x, y, 'rx')
plt.xlabel('$Volatge (V)$')
plt.ylabel('$Current (mA)$') Data looks like a curve, line is best fit line.
It's a best fit curve I need ....
Do I have to use:
def func () with the return modelling an exponential? Just sharing what google has thrown up.
Posts: 1,358
Threads: 2
Joined: May 2019
Ok. I read first post with "line of best fit" and assumed straight line. Here's something to tinker with. Support Vector Regressions using polynomial regression and RBF. Check the scikit-learn docs on regression for more ideas if needed.
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets, linear_model
from sklearn.svm import SVR
plt.figure("Forward Bias Characteristic")
x = np.array([0.00, 0.53, 0.55, 0.57, 0.59, 0.61, 0.63, 0.65, 0.67, 0.69])
y = np.array([0.0, 0.0, 0.1, 0.2, 0.5, 1.0, 1.2, 2.3, 3.5, 5.4])
yerror = np.array([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1,])
regr = linear_model.LinearRegression()
svr_rbf = SVR(kernel='rbf', C=100, gamma=0.1, epsilon=.1)
svr_poly = SVR(kernel='poly', C=100, gamma='auto', degree=3, epsilon=.1,
coef0=1)
X = x.reshape(-1,1)
regr.fit(X, y)
svr_rbf.fit(X,y)
svr_poly.fit(X,y)
y_pred = regr.predict(X)
y1_pred = svr_rbf.predict(X)
y2_pred = svr_poly.predict(X)
plt.plot(x, y_pred, color='blue', linewidth=2)
plt.plot(x, y1_pred, color='green', linewidth=2)
plt.plot(x, y2_pred, color='black', linewidth=2)
plt.plot(x, y, 'rx')
plt.xlabel('$Volatge (V)$')
plt.ylabel('$Current (mA)$')
|