99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁大數(shù)據(jù)時代數(shù)據(jù)可視化|用散點圖進行數(shù)據(jù)分析
數(shù)據(jù)可視化|用散點圖進行數(shù)據(jù)分析
2020-07-30
收藏

散點圖大家都能繪制,平常工作匯報有時也會用散點圖讓報表看起來更美觀。但是,散點圖并不是為了展示數(shù)據(jù),而是需要數(shù)據(jù)分析,并利用數(shù)據(jù)分析的結(jié)果推動業(yè)務(wù)的增長。小編今天跟大家分享的這篇文章就是教大家如何用散點圖進行數(shù)據(jù)分析的,希望對大家有所幫助。

文章來源:林驥微信公眾號

作者:林驥

01

你好,我是林驥。

散點圖的用途有很多,我認(rèn)為它的核心價值,在于應(yīng)用相關(guān)思維,發(fā)現(xiàn)變量之間的關(guān)系。

散點圖就像一扇窗,打開它,并仔細(xì)觀察,能讓我們看見更多有價值的信息。

比如說,假設(shè)表格中有 10000 個客戶年齡和消費金額的數(shù)據(jù):

我們可以計算每一個年齡對應(yīng)的人均消費金額,比如說,所有 20 歲客戶的平均消費金額約為 1383.69 元,然后我們可以畫出一張散點圖

從圖中可以看出,客戶的年齡與人均消費金額有很強的相關(guān)性,其中應(yīng)用了線性回歸算法,得到一條擬合的直線,并用公式表示出來,  接近于 1 ,代表算法擬合的效果很好。

02

接下來,我們看看具體實現(xiàn)的步驟。

首先,導(dǎo)入所需的庫,并設(shè)置中文字體和定義顏色等。

# 導(dǎo)入所需的庫
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import Pipeline

# 正常顯示中文標(biāo)簽
mpl.rcParams['font.sans-serif'] = ['SimHei']

# 自動適應(yīng)布局
mpl.rcParams.update({'figure.autolayout': True})

# 正常顯示負(fù)號
mpl.rcParams['axes.unicode_minus'] = False

# 禁用科學(xué)計數(shù)法
pd.set_option('display.float_format', lambda x: '%.2f' % x) 

# 定義顏色,主色:藍色,輔助色:灰色,互補色:橙色
c = {'藍色':'#00589F', '深藍色':'#003867', '淺藍色':'#5D9BCF',
     '灰色':'#999999', '深灰色':'#666666', '淺灰色':'#CCCCCC',
     '橙色':'#F68F00', '深橙色':'#A05D00', '淺橙色':'#FBC171'}

其次,從 Excel 文件中讀取數(shù)據(jù),并調(diào)用 sklearn 中的算法,得到擬合的直線和評分結(jié)果。

# 數(shù)據(jù)源路徑
filepath='./data/客戶年齡和消費金額.xlsx'

# 讀取 Excel文件
df = pd.read_excel(filepath, index_col='客戶編號')

# 定義畫圖用的數(shù)據(jù):年齡和人均消費金額
df_group = df.groupby('年齡').mean()
x = np.array(df_group.index).reshape(-1, 1)
y = np.array(df_group.values)

# 用管道的方式調(diào)用算法,以便把線性回歸擴展為多項式回歸
poly_reg = Pipeline([
    ('ploy', PolynomialFeatures(degree=1)),
    ('lin_reg', LinearRegression())
])

# 擬合
poly_reg.fit(x, y)

# 斜率
coef = poly_reg.steps[1][1].coef_
# 截距
intercept = poly_reg.steps[1][1].intercept_
# 評分
score = poly_reg.score(x, y)

接下來,開始用「面向?qū)ο蟆沟姆椒ㄟM行畫圖。

# 使用「面向?qū)ο蟆沟姆椒ó媹D,定義圖片的大小
fig, ax = plt.subplots(figsize=(8, 6))

# 設(shè)置標(biāo)題
ax.set_title('\n客戶每年長一歲,人均消費金額增加' + '%.2f' % coef[0][1] + '元\n', loc='left', size=26, color=c['深灰色'])

# 畫氣泡圖
ax.scatter(x, y, color=c['藍色'], marker='.', s=100, zorder=1)

# # 繪制預(yù)測線
y2 = poly_reg.predict(x)
ax.plot(x, y2, '-', c=c['橙色'], zorder=2)

# 隱藏邊框
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

# 隱藏刻度線
ax.tick_params(axis='x', which='major', length=0)
ax.tick_params(axis='y', which='major', length=0)

ax.set_ylim(15, 65)
ax.set_ylim(1000, 5000)

# 設(shè)置坐標(biāo)標(biāo)簽字體大小和顏色
ax.tick_params(labelsize=16, colors=c['深灰色'])
ax.text(ax.get_xlim()[0]-6, ax.get_ylim()[1], '人\n均\n消\n費\n金\n額', va='top', fontsize=16, color=c['深灰色'])

# 設(shè)置坐標(biāo)軸的標(biāo)題
ax.text(ax.get_xlim()[0]+1, ax.get_ylim()[0]-300, '年齡', ha='left', va='top', fontsize=16, color=c['深灰色'])

# 預(yù)測 55 歲的人均消費金額
predict = poly_reg.predict([[55]])

# 標(biāo)注公式
formula = r'$\mathcal{Y} = ' + '%.2f' % coef[0][1] + '\mathcal{X}' + '%+.2f$' % intercept[0] + '\n' + r'$\mathcal{R}^2 = ' + '%.5f$' % score
ax.annotate(formula, xy=(55, predict), xytext=(55, predict+500), ha='center', fontsize=12, color=c['深灰色'], arrowprops=dict(arrowstyle='->', color=c['橙色']))

plt.show()

你可以前往 https://github.com/linjiwx/mp 下載數(shù)據(jù)文件和完整代碼。

03

當(dāng)業(yè)務(wù)指標(biāo)很多的時候,應(yīng)該挑選什么指標(biāo)來進行分析,這件事很考驗分析者的功力,往往需要對業(yè)務(wù)有比較深刻的理解。

為什么很多人精通各種工具技術(shù),手上也有很多各種各樣的數(shù)據(jù),卻沒有做出讓領(lǐng)導(dǎo)滿意的圖表?

好的圖表,就像是給近視的人戴了一副眼鏡,讓讀者以更清楚的方式去理解數(shù)據(jù)。

好的圖表,就像是神奇的催化劑,加快了從數(shù)據(jù)到?jīng)Q策的過程,讓決策者更加快速地掌握有助于做出決策的信息。

好的圖表,能把復(fù)雜的問題簡單化,幫我們更精準(zhǔn)地理解業(yè)務(wù)的現(xiàn)狀,甚至預(yù)測未來。

我們應(yīng)該記住,無論多么漂亮的圖表,如果不能從中獲取有價值的信息,那么也是一張沒有「靈魂」的圖表。

很多時候,我們面對的問題,并不是沒有數(shù)據(jù),而是數(shù)據(jù)太多,卻不知道怎么用。

熟悉數(shù)據(jù)分析的思維,能幫我們找到更重要的數(shù)據(jù),排除過多雜亂數(shù)據(jù)的干擾。

如果把數(shù)據(jù)分析比作醫(yī)生看病的過程,那么可以分為以下 4 個階段:

(1)描述:檢查身體,描述指標(biāo)值是否正常。

(2)診斷:詢問病情,找到疾病的產(chǎn)生原因。

(3)預(yù)測:分析病情,預(yù)測病情的發(fā)展趨勢。

(4)指導(dǎo):開出藥方,提出有效的治療建議。

我們要盡可能地理解業(yè)務(wù)并提供價值,從數(shù)據(jù)的加工者,轉(zhuǎn)變成故事的講述者,甚至是問題的解決者。

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }