5.9 Measuring Data Skew

Measuring Data Skew

Now that you know how to make histograms, did you notice how the plots have "shapes?"

These shapes are important because they can show us the distributional characteristics of the data. The first characteristic we'll look at is skew.

現(xiàn)在你已經(jīng)知道如何制作直方圖了蒙具,你注意到這些地塊有“形狀”嗎?

Skew refers to asymmetry in the data. When data is concentrated on the right side of the histogram, for example, we say it has a negative skew. When the data is concentrated on the left, we say it has a positive skew.

偏度是指數(shù)據(jù)中的不對(duì)稱(chēng)。 當(dāng)數(shù)據(jù)集中在直方圖的右側(cè)時(shí)掖桦,例如迎献,我們說(shuō)它有負(fù)偏移刑枝。 當(dāng)數(shù)據(jù)集中在左側(cè)時(shí)著拭,我們說(shuō)它有一個(gè)正的偏移唤冈。

We can measure the level of skew with the skew function. A positive value indicates a positive skew, a negative value indicates a negative skew, and a value close to zero indicates no skew.

我們可以用偏斜函數(shù)來(lái)衡量偏斜的程度帅容。 正值表示正偏斜颇象,負(fù)值表示負(fù)偏斜,接近零的值表示無(wú)偏斜并徘。

  • Assign the skew of test_scores_positive to positive_skew.
  • Assign the skew of test_scores_negative to negative_skew.
  • Assign the skew of test_scores_normal to no_skew
# We've already loaded in some numpy arrays. We'll make some plots with them.
# The arrays contain student test scores that are on a 0-100 scale.
import matplotlib.pyplot as plt

# See how there's a long slope to the left?
# The data is concentrated in the right part of the distribution, but some people also scored poorly.
# This plot has a negative skew.
plt.hist(test_scores_negative)
plt.show()

# This plot has a long slope to the right.
# Most students did poorly, but a few did really well.
# This plot has a positive skew.
plt.hist(test_scores_positive)
plt.show()

# This plot has no skew either way. Most of the values are in the center, and there is no long slope either way.
# It is an unskewed distribution.
plt.hist(test_scores_normal)
plt.show()

# We can test how skewed a distribution is using the skew function.
# A positive value means positive skew, a negative value means negative skew, and close to zero means no skew.


from scipy.stats import skew
positive_skew = skew(test_scores_positive)
negative_skew = skew(test_scores_negative)
no_skew = skew(test_scores_normal)

#######output#####
 negative_skewfloat (<class 'float'>)
-0.6093247474592195
 positive_skewfloat (<class 'float'>)
0.5376950498203763
 no_skewfloat (<class 'float'>)
0.0223645171350847


9. Checking for Outliers with Kurtosis

Kurtosis is another characteristic of distributions. Kurtosis measures whether the distribution is short and flat, or tall and skinny. In other words, it assesses the shape of the peak.

峰度是分布的另一個(gè)特征遣钳。 峰度測(cè)量分布是短而扁平的,還是高而瘦麦乞。 換句話(huà)說(shuō)蕴茴,它評(píng)估峰值的形狀。

"Shorter" distributions have a lower maximum frequency, but higher subsequent frequencies. A high kurtosis may indicate problems with outliers (very large or very small values that skew the data).

“較短”分布具有較低的最大頻率姐直,但較高的后續(xù)頻率倦淀。 高峭度可能表示異常值問(wèn)題(非常大或非常小的值會(huì)使數(shù)據(jù)偏斜)。

  • Assign the kurtosis of test_scores_platy to kurt_platy.
  • Assign the kurtosis of test_scores_lepto to kurt_lepto.
  • Assign the kurtosis of test_scores_meso to kurt_meso.
import matplotlib.pyplot as plt

# This plot is short. It is platykurtic.
# Notice how the values are distributed fairly evenly, and there isn't a large cluster in the middle.
# Student performance varied widely.
plt.hist(test_scores_platy)
plt.ylim(0,3500)
plt.xlim(0,1)
plt.show()

# This plot is tall. It is leptokurtic.
# Most students performed similarly.
plt.hist(test_scores_lepto)
plt.ylim(0,3500)
plt.xlim(0,1)
plt.show()

# The height of this plot neither short nor tall. It is mesokurtic.
plt.hist(test_scores_meso)
plt.ylim(0,3500)
plt.xlim(0,1)
plt.show()

# We can measure kurtosis with the kurtosis function.
# Negative values indicate platykurtic distributions, positive values indicate leptokurtic distributions, and values near 0 are mesokurtic.
from scipy.stats import kurtosis
kurt_platy = kurtosis(test_scores_platy)
kurt_lepto = kurtosis(test_scores_lepto)
kurt_meso = kurtosis(test_scores_meso)

##########
 kurt_leptofloat (<class 'float'>)
0.023335026722224317
 kurt_platyfloat (<class 'float'>)
-0.9283967256161696
 kurt_mesofloat (<class 'float'>)
-0.042791859857727044


platy.png

lepto.png

meso.png

10. Modality

Modality is another characteristic of distributions. Modality refers to the number of modes, or peaks, in a distribution.

形態(tài)是分布的另一個(gè)特征声畏。 形態(tài)是指分布中的模式或峰的數(shù)量撞叽。

Real-world data is often unimodal (it has only one mode).

真實(shí)世界的數(shù)據(jù)往往是單峰分布的(它只有一種模式)。

  • Plot test_scores_multi, which has four peaks.
import matplotlib.pyplot as plt

# This plot has one mode. It is unimodal.
plt.hist(test_scores_uni)
plt.show()

# This plot has two peaks. It is bimodal.
# This could happen if one group of students learned the material and another learned something else, for example.
plt.hist(test_scores_bi)
plt.show()

# More than one peak means that the plot is multimodal.
# We can't easily measure the modality of a plot, like we can with kurtosis or skew.
# Often, the best way to detect multimodality is to examine the plot visually.
plt.hist(test_scores_multi)
plt.show()
uni.png
bi.png
multi.png

11. Measures of Central Tendency

Now that we know how to measure the characteristics of a distribution, let's look at central tendency measures.

現(xiàn)在我們知道如何衡量分布的特征插龄,我們來(lái)看看集中趨勢(shì)測(cè)度愿棋。

Central tendency measures assess how likely the data points are to cluster around a central value.

集中趨勢(shì)測(cè)量評(píng)估數(shù)據(jù)點(diǎn)圍繞中心值聚集的可能性。

The first one we'll look at is the
mean. We've calculated mean before, but let's explore it further.

我們要看的第一個(gè)是平均值均牢。 我們之前已經(jīng)計(jì)算出平均值糠雨,但讓我們進(jìn)一步探索它。
The mean is just the sum of all of the elements in an array divided by the number of elements.

  • Compute the mean of test_scores_normal, and assign it to mean_normal.
  • Compute the mean of test_scores_negative, and assign it to mean_negative.
  • Compute the mean of test_scores_positive, and assign it to mean_positive.

import matplotlib.pyplot as plt
# Let's put a line over our plot that shows the mean.
# This is the same histogram we plotted for skew a few screens ago.
plt.hist(test_scores_normal)
# We can use the .mean() method of a numpy array to compute the mean.
mean_test_score = test_scores_normal.mean()
# The axvline function will plot a vertical line over an existing plot.
plt.axvline(mean_test_score)

# Now we can show the plot and clear the figure.
plt.show()

# When we plot test_scores_negative, which is a very negatively skewed distribution, we see that the small values on the left pull the mean in that direction.
# Very large and very small values can easily skew the mean.
# Very skewed distributions can make the mean misleading.
plt.hist(test_scores_negative)
plt.axvline(test_scores_negative.mean())
plt.show()

# We can do the same with the positive side.
# Notice how the very high values pull the mean to the right more than we would expect.
plt.hist(test_scores_positive)
plt.axvline(test_scores_positive.mean())
plt.show()

mean_normal = test_scores_normal.mean()
mean_negative = test_scores_negative.mean()
mean_positive = test_scores_positive.mean()


normal.png
negative.png
positive.png
 mean_normalfloat64 (<class 'numpy.float64'>)
49.23213521195251
 mean_positivefloat64 (<class 'numpy.float64'>)
16.607018116017176
 mean_negativefloat64 (<class 'numpy.float64'>)
83.627606422256036

12. Calculating the Median

Median is another measure of central tendency. This is the midpoint of an array.

To calculate the median, we need to sort the array, then take the value in the middle. If there are two values in the middle (because there are an even number of items in the array), then we take the mean of the two middle values.

The median is less sensitive to very large or very small values (which we call outliers), and is a more realistic center of the distribution.

中位數(shù)對(duì)于非常大或非常小的值(我們稱(chēng)之為離群值)較不敏感徘跪,并且是分布的更實(shí)際的中心甘邀。

  • Plot a histogram for test_scores_positive.
  • Add a green line for the median.
  • Add a red line for the mean.
# Let's plot the mean and median side-by-side in a negatively skewed distribution.
# Unfortunately, arrays don't have a nice median method, so we have to use a numpy function to compute it.
import numpy
import matplotlib.pyplot as plt

# Plot the histogram
plt.hist(test_scores_negative)
# Compute the median
median = numpy.median(test_scores_negative)

# Plot the median in green (the color argument of "g" means green)
plt.axvline(median, color="g")

# Plot the mean in red
plt.axvline(test_scores_negative.mean(), color="r")

# Notice how the median is further to the right than the mean.
# It's less sensitive to outliers, and isn't pulled to the left.
plt.show()


plt.hist(test_scores_positive)
plt.axvline(numpy.median(test_scores_positive), color="g")
plt.axvline(test_scores_positive.mean(), color="r")
plt.show()

negative.png

positive.png

titanic DATA

Unfortunately, not all of the data is available; details such as age are missing for some passengers. Before we can analyze the data, we have to do something about the missing rows.

The easiest way to address them is to just remove all of the rows with missing data. This isn't necessarily the best solution in all cases, but we'll learn about other ways to handle these situations later on.

  • Remove the NaN values in the "age"and "sex" columns.
  • Assign the result to new_titanic_survival.
import pandas
f = "titanic_survival.csv"
titanic_survival = pandas.read_csv(f)

# Luckily, pandas DataFrames have a method that can drop rows that have missing data
# Let's look at how large the DataFrame is first
print(titanic_survival.shape)

# There were 1,310 passengers on the Titanic, according to our data
# Now let's drop any rows that have missing data
# The DataFrame dropna method will do this for us
# It will remove any rows with that contain missing values
new_titanic_survival = titanic_survival.dropna()

# Hmm, it looks like we were too zealous with dropping rows that contained NA values
# We now have no rows in our DataFrame
# This is because some of the later columns, which aren't immediately relevant to our analysis, contain a lot of missing values
print(new_titanic_survival.shape)

# We can use the subset keyword argument to the dropna method so that it only drops rows if there are NA values in certain columns
# This line of code will drop any row where the embarkation port (where people boarded the Titanic) or cabin number is missing
new_titanic_survival = titanic_survival.dropna(subset=["embarked", "cabin"])

# This result is much better. We've only removed the rows we needed to.
print(new_titanic_survival.shape)


new_titanic_survival = titanic_survival.dropna(subset=["age", "sex"])
  • Plot a histogram of the "age" column in new_titanic_survival.
    • Add a green line for the median.
    • Add a red line for the mean.
# We've loaded the clean version of the data into the variable new_titanic_survival
import matplotlib.pyplot as plt
import numpy


plt.hist(new_titanic_survival["age"])
plt.axvline(numpy.median(new_titanic_survival["age"]), color="g")
plt.axvline(new_titanic_survival["age"].mean(), color="r")
plt.show()

image.png
  • Assign the mean of the "age" column of new_titanic_survival to mean_age.
  • Assign the median of the "age"column of new_titanic_survival to median_age.
  • Assign the skew of the "age" column of new_titanic_survival to skew_age.
  • Assign the kurtosis of the "age"column of new_titanic_survival to kurtosis_age.

import numpy
from scipy.stats import skew
from scipy.stats import kurtosis
mean_age = new_titanic_survival["age"].mean()
median_age = numpy.median(new_titanic_survival["age"])
skew_age = skew(new_titanic_survival["age"])
kurtosis_age = kurtosis(new_titanic_survival["age"])

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市垮庐,隨后出現(xiàn)的幾起案子松邪,更是在濱河造成了極大的恐慌,老刑警劉巖突硝,帶你破解...
    沈念sama閱讀 211,194評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件测摔,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)锋八,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,058評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門(mén)浙于,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人挟纱,你說(shuō)我怎么就攤上這事羞酗。” “怎么了紊服?”我有些...
    開(kāi)封第一講書(shū)人閱讀 156,780評(píng)論 0 346
  • 文/不壞的土叔 我叫張陵檀轨,是天一觀(guān)的道長(zhǎng)。 經(jīng)常有香客問(wèn)我欺嗤,道長(zhǎng)参萄,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,388評(píng)論 1 283
  • 正文 為了忘掉前任煎饼,我火速辦了婚禮讹挎,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘吆玖。我一直安慰自己筒溃,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,430評(píng)論 5 384
  • 文/花漫 我一把揭開(kāi)白布沾乘。 她就那樣靜靜地躺著怜奖,像睡著了一般。 火紅的嫁衣襯著肌膚如雪翅阵。 梳的紋絲不亂的頭發(fā)上歪玲,一...
    開(kāi)封第一講書(shū)人閱讀 49,764評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音怎顾,去河邊找鬼读慎。 笑死,一個(gè)胖子當(dāng)著我的面吹牛槐雾,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播幅狮,決...
    沈念sama閱讀 38,907評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼募强,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了崇摄?” 一聲冷哼從身側(cè)響起擎值,我...
    開(kāi)封第一講書(shū)人閱讀 37,679評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎逐抑,沒(méi)想到半個(gè)月后鸠儿,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,122評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,459評(píng)論 2 325
  • 正文 我和宋清朗相戀三年进每,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了汹粤。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,605評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡田晚,死狀恐怖嘱兼,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情贤徒,我是刑警寧澤芹壕,帶...
    沈念sama閱讀 34,270評(píng)論 4 329
  • 正文 年R本政府宣布,位于F島的核電站接奈,受9級(jí)特大地震影響踢涌,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜序宦,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,867評(píng)論 3 312
  • 文/蒙蒙 一斯嚎、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧挨厚,春花似錦堡僻、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,734評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至巢价,卻和暖如春牲阁,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背壤躲。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,961評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工城菊, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人碉克。 一個(gè)月前我還...
    沈念sama閱讀 46,297評(píng)論 2 360
  • 正文 我出身青樓凌唬,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親漏麦。 傳聞我的和親對(duì)象是個(gè)殘疾皇子客税,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,472評(píng)論 2 348