Weitere ähnliche Inhalte
Ähnlich wie Datamining r 4th (20)
Datamining r 4th
- 1. R:
k
apply
sesejun@is.ocha.ac.jp
2009/11/19
- 3. ImageName Class 0,0 0,1 0,2 0,3 0,4
img_2_00_02 1 0 0 0 0 0
img_2_00_03 1 0 38 22 0 0
img_2_00_05 1 13 0 64 13 42
...
img_0_00_09 -1 34 53 0 38 0
img_0_00_28 -1 0 64 0 98 93
img_0_01_08 -1 13 0 0 59 13
img_0_03_05 -1 34 34 0 0 0
- 7. Apply Family
• , ,
• for
•
apply(X, 1, )
apply(X, 2, )
apply(X, c(1,2), )
lapply(X, )
dataframe
sapply(X, )
table
sweep(X, M,V) X (M=1) (M=2) (M=c(1,2)) V
7
- 8. 1
> m <- matrix((1:9)**2, nrow=3) > l <- list(a=1:3, b=4:6)
> m > l
[,1] [,2] [,3] $a
[1,] 1 16 49 [1] 1 2 3
[2,] 4 25 64
[3,] 9 36 81 $b
> apply(m, 1, sum) [1] 4 5 6
[1] 66 93 126
> apply(m, 2, sum) > lapply(l, sum)
[1] 14 77 194 $a
> apply(m, c(1,2), sqrt) [1] 6
[,1] [,2] [,3]
[1,] 1 4 7 $b
[2,] 2 5 8 [1] 15
[3,] 3 6 9
> sapply(l, sum)
a b
6 15
8
- 9. 2
> m <- matrix((1:9)**2, nrow=3) > l <- list(a=1:3, b=4:6)
> m > l
[,1] [,2] [,3] $a
[1,] 1 16 49 [1] 1 2 3
[2,] 4 25 64
[3,] 9 36 81 $b
> apply(m, 1, sum) [1] 4 5 6
[1] 66 93 126
> apply(m, 2, sum) > lapply(l, sum)
[1] 14 77 194 $a
> apply(m, c(1,2), sqrt) [1] 6
[,1] [,2] [,3]
[1,] 1 4 7 $b
[2,] 2 5 8 [1] 15
[3,] 3 6 9
> sapply(l, sum)
a b
6 15
9
- 10. K-NN
•
> iris.train <- read.table("iris_train.csv", sep=",", header=T)
> iris.test <- read.table("iris_test.csv", sep=",", header=T)
> q <- iris.test[1,1:4]
> diff <- sweep(iris.train[1:4], 2, t(q))
> diff * diff
> distquery <- apply(diff * diff, 1, sum)
> sort(distquery)
> order(distquery)
10
- 12. > knnpredict <- function(train,class,query,k) {
+ diff <- sweep(train,2,query)
+ distquery <- apply(diff * diff, 1, sum)
+ knnclasses <- class[order(distquery)[1:k]]
+ sortedtable <- sort(as.factor(table(knnclasses)), decreasing=T)
+ labels(sortedtable)[1]
+ }
> knnpredict(iris.train[1:4], iris.train$Class, t(iris.test[1,1:4]),
5)
> knnpredict(iris.train[1:4], iris.train$Class, t(iris.test[10,1:4]),
1)
> for (i in 1:length(rownames(iris.test))) {
+ pred <- knnpredict(iris.train[1:4], iris.train$Class, t(iris.test
[i,1:4]),10)
+ result <- pred == iris.test[i,]$Class
+ cat(paste(pred, iris.test[i,]$Class, result, sep="t"))
+ cat("n")
+ } 12
- 13. > resvec <- c()
> for (i in 1:30) {
+ pred <- knnpredict(iris.train[1:4], iris.train$Class, t(iris.test
[i,1:4]),10)
+ resvec <- append(resvec, pred == iris.test[i,]$Class)
+ }
> sum(resvec)/length(resvec)
13
- 14. 3
1. IRIS
1. IRIS 4 ("Sepal.length","Sepal.width",
"Petal.length","Petal.width")
2. IRIS
K-NN
2. USPS
1. USPS 5-NN (0-9)
2. K-NN K
3. USPS K-NN
K-NN K
4. USPS
14