使用
scipy.stats
:
import pandas as pd
from scipy import stats
import matplotlib.pyplot as plt
column_A= [132.54672, 201.3845717, 323.2654551]
column_B= [51.54671995, 96.38457166, 131.2654551]
df = pd.DataFrame({'A': column_A, 'B': column_B})
reg = stats.linregress(df.A, df.B)
plt.plot(df.A, df.B, 'bo', label='Data')
plt.plot(df.A, reg.intercept + reg.slope * df.A, 'k-', label='Linear Regression')
plt.xlabel('A')
plt.ylabel('B')
plt.legend()
plt.show()
你也可以从中找到有用的方法。
dir(reg)
,其中包括
.intercept
.pvalue
.rvalue
.slope
.stderr
见
here
。