我已经生成了一个csv文件,其中有4行对应于图表中2行的系数:
df=df[["AC_GRADE","ALPHA"]]
df=df.dropna(axis=0, how='any')
df.rename(index=str, columns={"AC_GRADE":"Access grade public","ALPHA":"Alpha_public"},inplace=True)
x=list(df[(df.columns[0])])
y=list(df[(df.columns[1])])
parameters_pub = np.polyfit(x, y, 1)
df['Regression_public'] = parameters_pub[0] * df[df.columns[0]] + parameters_pub[1]
df=df.round(decimals=2)
df_pp=df_pp[["AC_GRADE","ALPHA"]]
df_pp=df_pp.dropna(axis=0, how='any')
df_pp.rename(index=str, columns={"AC_GRADE":"Access grade private","ALPHA":"Alpha_private"},inplace=True)
x=list(df_pp[(df_pp.columns[0])])
y=list(df_pp[(df_pp.columns[1])])
parameters_pri = np.polyfit(x, y, 1)
df_pp['Regression_private'] = parameters_pri[0] * df_pp[df_pp.columns[0]] + parameters_pri[1]
df_pp=df_pp.round(decimals=2)
df_total=pd.concat([df,df_pp],axis=1)
df_total=df_total[["Access grade public","Alpha_public","Access grade private","Alpha_private",'Regression_public','Regression_private']]
df_total=df_total.round(decimals=2)
parameters=np.concatenate((parameters_pub,parameters_pri), axis=0)
np.savetxt("/home/mtintore/mysite/app/static/CSV/Chart_data/parameters.csv", parameters, delimiter=",")
df_total.to_csv("/home/mtintore/mysite/app/static/CSV/Chart_data/alpha_access_pp.csv", index=False)
parameters.csv看起来保存得很好,如下所示:
1.150517617398622156e-01
-5.234557848372338729e-01
1.275258227834331071e-01
-6.895679012729082347e-01
现在,我想显示将此代码段添加到绘制图表的JavaScript的直线方程(alpha_access_pp.js):
d3.csv('../static/CSV/Chart_data/parameters.csv?rnd='+(new Date).getTime(),function(data){
var f1 = data[0];
var f2 = data[1];
var a = f1[0];
var b = f2[0];
a = (Math.round(a*1000)/1000);
b = (Math.round(b*1000)/1000);
document.getElementById("equation9").innerHTML = "Public high schools: Alpha = " + a + "·Access grade + " + b;
var f3 = data[2];
var f4 = data[3];
var d = f3[0];
var c = f4[0];
d = (Math.round(d*1000)/1000);
c = (Math.round(c*1000)/1000);
document.getElementById("equation10").innerHTML = "Private high schools: Alpha = " + c + "·Access grade + " + d;
})
在html中,这就是我同时调用包含图表+等式显示和ID的javascript的方式:
<div id="alpha_access_pp"></div>
<div style="line-height:57%;"><br></div>
<p class="w3-container" style="margin: 0 05%" align="justify">The scatter plot compares the α and the access grade of the students and linearly approximates this relationship. The equations of the line's regressions can be used as prediction's of a students' performance once the access grade is known.
The lines that best approximate the expected alpha parameters (α) according to the access grade to University and comparing students from both public and private high schools are:</p>
<div style="line-height:57%;"><br></div>
<div class="w3-center" id="equation9"></div>
<div style="line-height:57%;"><br></div>
<div class="w3-center" id="equation10"></div>
<script type="text/javascript" src="../static/scripts/alpha_access_pp.js"></script>
但是我看到的是“ NaNs”,而不是方程式的系数。有什么帮助吗?谢谢!