自己的python示例代码
# Required Packages
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import datasets, linear_model
if __name__ == "__main__":
zhfont1 = matplotlib.font_manager.FontProperties(fname='C:\Windows\Fonts\simhei.ttf')
test_data = '../data/line.xlsx'
data = pd.read_excel(test_data)
m = data.__len__() # 行数
n = data.columns.size # 列数
print (m,n)
X_data = []
Y_data = []
X_data_temp = data.loc[:m,u'X']
Y_data_temp = data.loc[:m, u'Y']
for i in range(0,len(X_data_temp)):
#print (X_data_temp[i])
X_data.append([X_data_temp[i]])
for j in range(0,len(Y_data_temp)):
#print(Y_data_temp[i])
Y_data.append(Y_data_temp[j])
print (X_data)
print (Y_data)
#######创建线性模型#######
regr = linear_model.LinearRegression()
regr.fit(X_data, Y_data)
predictions = {}
#predictions['intercept'] = regr.intercept_
#predictions['coefficient'] = regr.coef_
b = regr.intercept_
a = regr.coef_[0]
print (a,',',b)
plt.scatter(X_data, Y_data, color='blue')
plt.plot(X_data, regr.predict(X_data), color='red', linewidth=4,label='H(x) ='+str(a)+' X+ '+str(b))
plt.xlabel('全站点总面雨量',fontproperties=zhfont1)
plt.ylabel('洪峰值',fontproperties=zhfont1)
plt.legend(prop=zhfont1)
plt.xticks(())
plt.yticks(())
plt.show()