簡體   English   中英

數據科學:定量和定性變量之間的相關性(Python中)

[英]Data science : correlation between quantitative and qualitative variables (in python)

我想知道是否有可能在python中測量定量變量(在我的情況下為家庭的平均日消費量)與定性變量(在我的情況下為月:1、2,...,12)之間的相關性?

一個月 avg_daily_consumption
------------------------------------------
1 | 12.11836586156116
2 | 11.713968603585668
3 | 11.902829015188159
4 | 10.12066900094302
5 | 8.879703717271864
6 | 8.384419625257689
7 | 8.146453593663365
8 | 7.961394876525876
9 | 8.748848024841289
10 | 9.820944144869841
11 | 11.247017177860053
12 | 12.069888731716086

謝謝。

我們可以使用numpymatplotlib庫來顯示是否存在任何關聯。

以下內容是在Jupyter筆記本中編寫的,但是應該可以在Python中工作,並且刪除注釋為#remove的行。

import numpy as np

#x values
x = [1,2,3,4,5,6,7,8,9,10,11,12]

# y values 
y = [12.11836586156116, 11.713968603585668, 11.902829015188159, 10.12066900094302, 8.879703717271864, 8.384419625257689, 8.146453593663365, 7.961394876525876, 8.748848024841289, 9.820944144869841, 11.247017177860053 , 12.069888731716086]

print( np.corrcoef(x, y))

這將輸出:[[1. -0.22316588] [-0.22316588 1.]],該圖顯示出較小的負相關。

然后,我們可以繪制x,y值:

import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline      # remove if not in Jupyter notebook
matplotlib.style.use('ggplot')

plt.scatter(x, y)
plt.show()

這給了我們下面的圖-指出月份和每月消費之間沒有直接的相關性。

放置x和y值

看起來這可能是周期性消耗。 假設1-12個月,那么消費似乎從年中到年底有所上升,然后下降到年中點,然后又上升。 如果是這樣的話,她可以添加前幾年的數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM