How to plot precision recall curves using .csv values? - python-3.x

I have the values of precision and recall at every epoch in .csv format? Now, I want to plot this values in form of precision_recall curve.
I mean precision on Y-axis and Recall on X-axis.
How can I visualise it in python3?

Supposing your file is named 'my_precision_recall.csv' and looks like:
Recall,Precision
0.836,0.4672
0.8501,0.4447
...
You could plot your curve with 'Recall' as x-axis and 'Precision' as y-axis:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('my_precision_recall.csv')
df.plot('Recall', 'Precision')
plt.show()

Related

plotting time and temperature in xy plot

I want to plot a xy plot where x axis contain temperature values(first column) and y axis contain time in hr:min:sec(second column) .
8.8900 06:09:95.50
9.4500 06:09:00.56
10.5800 08.06:95.48
11.6500 09:07:73.58
56.3650 00:08:00.47
85.7823 07:01:03.23
I just want to plot a xy plot.
I tried code
import numpy as np
import matplotlib.pyplot as plt
data=np.loadtxt("inpdata.txt")
plt.plot(data[:,0],data[:,1])
plt.show()
But it does not give plot.hope experts may help.Thanks.
The simplest approach would be to use pandas.
Load the file as whitespace delimited file into pandas.DataFrame object as:
import pandas as pd
from matplotlib import pyplot as plt
df = pd.read_csv('inpdata.txt', names=['temp', 'time'], delim_whitespace=True)
Then create a line plot with time as x axis:
df.plot.line(x='time')
and show the plot
plt.show()

Nyquist Plot using Python with certain parameters

I am trying to draw the Nyquist plot using python but I have no clue what all parameters are required by python to do plot that curve.
Here is a glimpse of the parameters that I have:
Channel_ID,Step_ID,Cycle_ID,Test_Time,EIS_Test_ID,EIS_Data_Point,Frequency,Zmod,Zphz,Zreal,Zimg,OCV,AC_Amp_RMS
4,7,1,36966.3072,0,0,200015.6,0.4933,70.9969,0.1606,0.4664,3.6231,0.35
4,7,1,36966.3072,0,1,158953.1,0.412,70.8901,0.1349,0.3893,3.6231,0.35
4,7,1,36966.3072,0,2,126234.4,0.3437,70.7115,0.1135,0.3244,3.6231,0.35
4,7,1,36966.3072,0,3,100265.6,0.2869,70.6312,0.0951,0.2706,3.6231,0.35
4,7,1,36966.3072,0,4,79640.63,0.2364,70.2418,0.0799,0.2224,3.6231,0.35
and above are the values to those parameters.
Based on the above parameters that are
Test_Time, Frequency, Zmod, Zphz, Zreal, Zimg, OCV, AC_Amp_RMS where Zmod is the absolute value of Zreal and Zimg, I need to draw a Nyquist plot. I have no clue how these parameters could be used for the plot.
PS: I tried to plot the curve by making use of the real and imaginary part that is Zimg and Zreal
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
train_df = pd.read_csv("above_data_with_around_100_rows.csv")
plt.figure()
plt.plot(train_df["Zreal"], train_df["Zimg"], "b")
plt.plot(train_df["Zreal"], -train_df["Zimg"], "r")
plt.show()
Can this be the useful for Nyquist plot?

Reduce xtick frequency when stepping with a Series

I am trying to plot a large dataset using a CSV file I had gathered previously. When I plot the data using plt.step every xtick is marked making it unreadable. How do I reduce the number of displayed xticks but keep the same graph?
I have tried using plt.xticks with np.arrange but I keep getting errors as I am using a Dataframe rather than an array. Additionally the x values I'm working with is time in the following format (%H:%M:%S).
import pandas as pd
import numpy as np
import datetime
import matplotlib
matplotlib.use("TkAgg")
from matplotlib import pyplot as plt
df = pd.read_csv(thisFile)
plt.figure(figsize=(16,6))
plt.step(df.Time, df.HR)
print(df.Time)
Output
0 13:40:34
1 13:40:44
2 14:18:29
3 14:19:15
4 14:20:58
5 14:21:17
plt.ylabel('Heart Rate (BPM)')
plt.xlabel('Time')
plt.show()
Displaying the plot is mostly correct except for the xtick frequency. It appears too often and collides with each other as shown in my graph output.
Ideally I would like for the xticks to display from 12AM to 12PM with a xtick frequency of an hour.

Y-axis values not showing in matplotlib.pyplot plot

My plot is not showing any indication of what the order of magnitude of my y-values are on the axis. How do I force python to indicate some values on the y-axis?
import numpy as np
import matplotlib.pyplot as plt
BERfinal = [0.4967843137254903, 0.49215686274509757, 0.4938823529411763,
0.49170588235294116, 0.48852941176470605, 0.48203921568627417,
0.4797058823529405, 0.47454901960784257, 0.4795686274509802,
0.474901960784313, 0.4732549019607838, 0.4703137254901953,
0.4705490196078425]
x = np.linspace(-4,8,len(BERfinal))
plt.semilogy(x,BERfinal)
plt.title("BER vs SNR")
plt.ylabel("Bit Error Rate(BER)")
plt.xlabel("Signal-to-Noise Ratio(SNR)[dB]")
plt.xlim(-4,8)
I ended up playing around with:
plt.ylim(4.7*10**-1, 5*10**-1)
and changed the values until I found an appropriate range. It now shows 5x10^-1 on the y-axis.

Plot polynomial regression in Python with Scikit-Learn

I am writing a python code for investigating the over-fiting using the function sin(2.pi.x) in range of [0,1]. I first generate N data points by adding some random noise using Gaussian distribution with mu=0 and sigma=1. I fit the model using M-th polynomial. Here is my code
import matplotlib.pyplot as plt
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
# generate N random points
N=30
X= np.random.rand(N,1)
y= np.sin(np.pi*2*X)+ np.random.randn(N,1)
M=2
poly_features=PolynomialFeatures(degree=M, include_bias=False)
X_poly=poly_features.fit_transform(X) # contain original X and its new features
model=LinearRegression()
model.fit(X_poly,y) # Fit the model
# Plot
X_plot=np.linspace(0,1,100).reshape(-1,1)
X_plot_poly=poly_features.fit_transform(X_plot)
plt.plot(X,y,"b.")
plt.plot(X_plot_poly,model.predict(X_plot_poly),'-r')
plt.show()
Picture of polynomial regression
I don't know why I have M=2 lines of m-th polynomial line? I think it should be 1 line regardless of M. Could you help me figure out this problem.
Your data after polynomial feature transformation is of shape (n_samples,2).
So pyplot is plotting the predicted variable with both columns.
Change the plot code to
plt.plot(X_plot_poly[:,i],model.predict(X_plot_poly),'-r')
where i your column number

Resources