Python數據分析及可視化實例之基礎-交叉驗證及預測(波士頓房價)(22)

系列文章總目錄:Python數據分析及可視化實例目錄


源碼分為兩部分:一部分是基於matplotlib,一部分基於Bokeh。

# coding: utf-8# In[1]:get_ipython().magic("matplotlib inline")# ### 交叉驗證預測# In[2]:from sklearn import datasetsfrom sklearn.model_selection import cross_val_predictfrom sklearn import linear_modelimport matplotlib.pyplot as pltlr = linear_model.LinearRegression()boston = datasets.load_boston() # 載入SKlearn自帶數據集:波士頓房價y = boston.targetpredicted = cross_val_predict(lr, boston.data, y, cv=10)fig, ax = plt.subplots()ax.scatter(y, predicted, edgecolors=(0, 0, 0))ax.plot([y.min(), y.max()], [y.min(), y.max()], "k--", lw=4)ax.set_xlabel("Measured")ax.set_ylabel("Predicted")plt.show()

# ### 用Bokeh進行可視化# In[3]:from bokeh.io import output_notebook, showfrom bokeh.plotting import figureoutput_notebook()# In[4]:p = figure(plot_width=600, plot_height=400)# In[5]:p.circle(y, predicted, fill_color="white", line_color="green", legend="預測值",size=8)p.line([y.min(), y.max()], [y.min(), y.max()],line_color="firebrick", legend="回歸線",line_dash=[10,10],line_width=4)p.legend.location = "top_left"p.yaxis.axis_label = "預測值"p.xaxis.axis_label = "測定值"show(p)

有人問我為什麼用Bokeh?

一來這貨做出來的圖顏值高,

二來可以和Flask搞(交)基(互),

嗯,你也可以用seaborn或者ggplot2

膠水語言博大精深,

本主只得一二為新人帶路,

老鳥可去另一專欄:Python中文社區

新手可查閱歷史目錄:

Python數據分析及可視化實例目錄


最後,別只收藏不關注哈

推薦閱讀:

數據測量與分析:入門完全指南
[長篇小說]數據分析俠A的成長故事【2017.2.25更】
數據挖掘八年經驗,是不是很難找到合適的工作?
數據分析--百年大電影

TAG:数据分析 | 数据可视化 | Python |