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}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,047评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,807评论 3 386
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,501评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,839评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,951评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,117评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,188评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,929评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,372评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,679评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,837评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,536评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,168评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,886评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,129评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,665评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,739评论 2 351

推荐阅读更多精彩内容