5002 assignment1

2.1 Wavelet Transform

Question 1

step1: Calculate the average of input samples

step2: Calculate the difference of input samples

step3: Regard averages as input sequence. Repeat step1 and step2 until only one output average.

Quention 2

Original Signal 1 4 2 3 -2 -1 2 1
1.Iteration 3.53 3.53 -2.12 2.12 -2.12 -0.71 -0.71 0.71
2.Iteration 5.00 0.00 0.00 -3.00
3.Iteration 3.54 3.54
Haar wavelet transformation 3.54 3.54 0.00 -3.00 -2.12 -0.71 -0.71 0.71

2.2 Principal Components Analysis

import numpy as np

dimension = 3
data_num = 7
data = [[-1, -2, -3, 1, 2, 3, 1], [-1, -1, -2, 1, 1, 2, 2],
        [1, 4, -2, 1, 2, 1, 4]]

data_mean = [np.mean(data[i]) for i in range(dimension)]

# normalize data by average
for i in range(0, dimension):
    for j in range(0, len(data[0])):
        data[i][j] -= data_mean[i]

# calculate covariance matrix according to definition
cov = []
for i in range(dimension):
    for j in range(dimension):
        sumOfRowMulti = 0
        for k in range(0, data_num):
            sumOfRowMulti += data[i][k] * data[j][k]
        cov.append(sumOfRowMulti / (data_num - 1))

cov_matrix = np.asarray(cov).reshape(dimension, dimension)
print 'covariance matrix is:'
print cov_matrix

# calculate eigen value and eigen vector using numpy function
eig_val, eig_vec = np.linalg.eig(cov_matrix)

for i in range(dimension):
    print 'eigen value', i, eig_val[
        i], ' corresponding eigen vector:', eig_vec[:, i]

# sort eigen value in descending order
eig_val_sorted = np.sort(eig_val)[::-1]

# calcuate propotion
# propotion=sum of eigen value of first k components/sum of all n eigen values
propotion = (eig_val_sorted[0] + eig_val_sorted[1]) / np.sum(eig_val_sorted)

print 'proportion of total population variance explained by the first two components: %.4f' % propotion

The result is:

image.png

3 Pattern Discovery

Question 1

Step 1: Discretization

Web page

  1. Sorted data for web pages : 7,9,9,11,13,14,18,35,36,45
  2. Partition into 4 equal-frequency (equal-depth) bins:
  • web1: 7,9,9
  • web2: 11,13
  • web3: 14,18,35
  • web4: 36,45

Session length

  1. Find the max and min value of session length: 267,2017
  2. Calculate the width of intervals: width = ( 2017 – 267 )/ 3≈583.4
  3. Partition into 3 equal-frequency (equal-width) bins:
  • len1 (267~850.4): 267,598,672
  • len2 (850.3~1433.8): 898,998,1213,1345
  • len3(1433.8~2017.2): 1543,1702,2017

Transactions table after discretization

Session ID Country Session Length Web Pages Buy
1 NA len2 web1 no
2 AS len3 web2 yes
3 EU len1 web3 yes
4 EU len2 web4 no
5 NA len1 web1 no
6 AS len2 web3 yes
7 AS len3 web3 yes
8 AS len1 web1 no
9 NA len3 web2 no
10 EU len2 web4 yes

Step 2: Apriori algorithm

1st scan

generate length 1 candidate itemsets

itemset sup
NA 0.3
AS 0.4
EU 0.3
len1 0.3
len2 0.4
len3 0.3
web1 0.3
web2 0.2
web3 0.3
web4 0.2
yes 0.5
no 0.5

prune infrequent itemsets

itemset sup
NA 0.3
AS 0.4
EU 0.3
len1 0.3
len2 0.4
len3 0.3
web1 0.3
web3 0.3
yes 0.5
no 0.5

2nd scan

generate length 2 candidate itemsets

itemset sup itemset sup
NA,len1 0.1 EU,yes 0.2
NA,len2 0.1 EU,no 0.1
NA,len3 0.1 len1,web1 0.2
AS,len1 0.1 len2,web1 0.1
AS,len2 0.1 len3,web1 0
AS,len3 0.2 len1,web3 0.1
EU,len1 0.1 len2,web3 0.1
EU,len2 0.2 len3,web3 0.1
EU,len3 0 len1,yes 0.1
NA,web1 0.2 len2,yes 0.2
NA,web3 0 len3,yes 0.2
AS,web1 0.1 len1,no 0.2
AS,web3 0.2 len2,no 0.2
EU,web1 0 len3,no 0.1
EU,web3 0.1 web1,yes 0
NA,yes 0 web1,no 0.3
NA,no 0.3 web3,yes 0.3
AS,yes 0.3 web3,no 0
AS,no 0.1

prune infrequent itemsets

itemset sup
NA,no 0.3
AS,yes 0.3
web1,no 0.3
web3,yes 0.3

3rd scan

generate length 3 candidate itemsets
no length 3 candidate

Step 3: Get frequent itemsets

{web3} (support = 0.3)
{web1} (support = 0.3)
{len3} (support = 0.3)
{len1} (support = 0.3)
{NA} (support = 0.3)
{EU} (support = 0.3)
{len2} (support = 0.4)
{AS} (support = 0.4)
{yes} (support = 0.5)
{no} (support = 0.5)
{web3,yes} (support = 0.3)
{web1,no} (support = 0.3)
{NA,no} (support = 0.3)
{AS,yes} (support = 0.3)

Question 2

Step 1: Discretization

same as question question 1

Step 2: Deduce the ordered frequent items

item sup
no 0.5
yes 0.5
AS 0.4
len2 0.4
EU 0.3
NA 0.3
len1 0.3
len3 0.3
web1 0.3
web3 0.3
ID Frequent Items
1 no,len2,NA,web1
2 yes,AS,len3
3 yes,EU,len1,web3
4 no,len2,EU
5 no,NA,len1,web1
6 yes,AS,len2,web3
7 yes,AS,len3,web3
8 no,AS,len1,web1
9 no,NA,len3
10 yes,len2,EU

Step 3: Construct FP tree

FP-tree

Step 4: construct conditional FP-tree for each item

Cond. FP-tree on "web3"

{web3} (support = 0.3)


web3

Cond. FP-tree on "web1"

{web1} (support = 0.3)


web1

Cond. FP-tree on "len3"

{len3} (support = 0.3)


len3

Cond. FP-tree on "len1"

{len1} (support = 0.3)


len1

Cond. FP-tree on "NA"

{NA} (support = 0.3)


NA

Cond. FP-tree on "EU"

{EU} (support = 0.3)


EU

Cond. FP-tree on "len2"

{len2} (support = 0.4)


len2

Cond. FP-tree on "AS"

{AS} (support = 0.4)


AS

Cond. FP-tree on "yes"

{yes} (support = 0.5)


yes

Cond. FP-tree on "no"

{no} (support = 0.5)


no

Step 5: Determine frequent patterns

{web3} (support = 0.3)
{web3,yes} (support = 0.3)
{web1} (support = 0.3)
{web1,no} (support = 0.3)
{len3} (support = 0.3)
{len1} (support = 0.3)
{NA} (support = 0.3)
{NA,no} (support = 0.3)
{EU} (support = 0.3)
{len2} (support = 0.4)
{AS} (support = 0.4)
{AS,yes} (support = 0.3)
{yes} (support = 0.5)
{no} (support = 0.5)

Question 3

closed frequent patterns

{web3,yes}
{web1,no}
{len3}
{len1}
{NA,no}
{EU}
{len2}
{AS}
{AS,yes}
{yes}
{no}

maximal frequent patterns

{web3,yes}
{web1,no}
{len3}
{len1}
{NA,no}
{EU}
{len2}
{AS,yes}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末晓殊,一起剝皮案震驚了整個濱河市腊敲,隨后出現(xiàn)的幾起案子喷舀,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,482評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件薇溃,死亡現(xiàn)場離奇詭異赁咙,居然都是意外死亡,警方通過查閱死者的電腦和手機葡公,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,377評論 2 382
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來条霜,“玉大人催什,你說我怎么就攤上這事≡姿” “怎么了蒲凶?”我有些...
    開封第一講書人閱讀 152,762評論 0 342
  • 文/不壞的土叔 我叫張陵,是天一觀的道長拆内。 經(jīng)常有香客問我旋圆,道長,這世上最難降的妖魔是什么麸恍? 我笑而不...
    開封第一講書人閱讀 55,273評論 1 279
  • 正文 為了忘掉前任灵巧,我火速辦了婚禮,結(jié)果婚禮上抹沪,老公的妹妹穿的比我還像新娘刻肄。我一直安慰自己,他們只是感情好融欧,可當(dāng)我...
    茶點故事閱讀 64,289評論 5 373
  • 文/花漫 我一把揭開白布敏弃。 她就那樣靜靜地躺著,像睡著了一般噪馏。 火紅的嫁衣襯著肌膚如雪麦到。 梳的紋絲不亂的頭發(fā)上绿饵,一...
    開封第一講書人閱讀 49,046評論 1 285
  • 那天,我揣著相機與錄音瓶颠,去河邊找鬼拟赊。 笑死,一個胖子當(dāng)著我的面吹牛步清,可吹牛的內(nèi)容都是我干的要门。 我是一名探鬼主播,決...
    沈念sama閱讀 38,351評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼廓啊,長吁一口氣:“原來是場噩夢啊……” “哼欢搜!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起谴轮,我...
    開封第一講書人閱讀 36,988評論 0 259
  • 序言:老撾萬榮一對情侶失蹤炒瘟,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后第步,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體疮装,經(jīng)...
    沈念sama閱讀 43,476評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,948評論 2 324
  • 正文 我和宋清朗相戀三年粘都,在試婚紗的時候發(fā)現(xiàn)自己被綠了廓推。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,064評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡翩隧,死狀恐怖樊展,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情堆生,我是刑警寧澤专缠,帶...
    沈念sama閱讀 33,712評論 4 323
  • 正文 年R本政府宣布,位于F島的核電站淑仆,受9級特大地震影響涝婉,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蔗怠,卻給世界環(huán)境...
    茶點故事閱讀 39,261評論 3 307
  • 文/蒙蒙 一墩弯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧寞射,春花似錦最住、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,264評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽轧粟。三九已至策治,卻和暖如春脓魏,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背通惫。 一陣腳步聲響...
    開封第一講書人閱讀 31,486評論 1 262
  • 我被黑心中介騙來泰國打工茂翔, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人履腋。 一個月前我還...
    沈念sama閱讀 45,511評論 2 354
  • 正文 我出身青樓珊燎,卻偏偏與公主長得像,于是被迫代替她去往敵國和親遵湖。 傳聞我的和親對象是個殘疾皇子悔政,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,802評論 2 345

推薦閱讀更多精彩內(nèi)容