-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht-SNE_Script.R
More file actions
158 lines (139 loc) · 6.95 KB
/
Copy patht-SNE_Script.R
File metadata and controls
158 lines (139 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
R version 3.1.1 (2014-07-10) -- "Sock it to Me"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin10.8.0 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
[Workspace loaded from ~/.RData]
> setwd("~/Desktop/t-SNE 151124")
#Load library
> library(tsne)
#Loading data
> mydata <- read.table("CombinedCells_zscores151106.csv", header=TRUE, sep=",")
> mydata2 <- mydata[,-1]
# t-sne function
> x <- 0
> epc <- function(x) {
+ x <<- x + 1
+ filename <- paste("d:\\plot", x, "jpg", sep=".")
+ cat("> Plotting TSNE to ", filename, " ")
+
+ # plot to d:\\plot.x.jpg file of 2400x1800 dimension
+ jpeg(filename, width=2400, height=1800)
+
+ plot(x, t='n', main="T-SNE")
+ text(x, labels=rownames(mydata))
+ dev.off()
+ }
# Running the tsne
> tsne_data <- tsne(mydata2, k=5, epoch_callback=epc, max_iter=500, epoch=100)
#K means clustering
> kClust <- kmeans(tsne_data, center =10)
#Plotting the tsne data
> plot(tsne_data, col=kClust$cluster)
#I wanted to also have a 3D plot.So I loaded rgl and plot3D required for 3D
> library(rgl)
> library(plot3D)
Warning message:
package ‘plot3D’ was built under R version 3.1.2
> image(z=t(1:10),col=rainbow(kClust$cluster))
> image(z=t(1:10),col=rainbow(10))
> image(z=t(1:10),col=rainbow(kClust$cluster))
> tsNE1_2_3 = data.frame(x=tsne_data[,1],y=tsne_data[,2],tsne_data=DM[,3])
Error in data.frame(x = tsne_data[, 1], y = tsne_data[, 2], tsne_data = DM[, :
object 'DM' not found
> tsNE1_2_3 = data.frame(x=tsne_data[,1],y=tsne_data[,2],z=tsne_data[,3])
> open3d()
glX
1
> colors = rainbow(kClust$cluster)
> plot3d(tsNE1_2_3,col=colors[10],xlab="DM1",ylab="DM2",zlab="DM3",size=.4,type="s")
> numClusters = 6
> numClusters = 10
> image(z=t(1:10),col=rainbow(numClusters))
> kClust <- kmeans(tsne_data, numClusters)
> KM <- kmeans(tsne_data, numClusters)
> plot3d(tsNE1_2_3,col=colors[KM$cluster],xlab="DM1",ylab="DM2",zlab="DM3",size=.4,type="s")
> outData = data.frame(cellID = rawData$X,cluster=KM$cluster)
Error in data.frame(cellID = rawData$X, cluster = KM$cluster) :
object 'rawData' not found
> write.csv(KM$numClusters, 'clusters_result.csv')
> kClust <- kmeans(tsne_data, center =10)
> plot(tsne_data, col=kClust$cluster)
> kClust$size
[1] 95 156 64 68 73 123 56 199 124 158
> numClusters
[1] 10
> kClust <- kmeans(tsne_data, numClusters)
> kClust <- kmeans(tsne_data, numClusters)
> plot3d(tsNE1_2_3,col=colors[KClust$cluster],xlab="DM1",ylab="DM2",zlab="DM3",size=.4,type="s")
Error in f(...) : object 'KClust' not found
> plot3d(tsNE1_2_3,col=colors[kClust$cluster],xlab="DM1",ylab="DM2",zlab="DM3",size=.4,type="s")
> plot(tsne_data, col=kClust$cluster)
> write.csv(kClust$cluster, 'clusters_results.csv')
> tsNE1_2_3 = data.frame(x=tsne_data[,1],y=tsne_data[,2],z=tsne_data[,3])
> open3d()
glX
2
> colors = rainbow(kClust$cluster)
> plot3d(tsNE1_2_3,col=colors[kClust$cluster],xlab="DM1",ylab="DM2",zlab="DM3",size=.4,type="s")
> tsne1_2 = data.frame(x=tsne_data[,1],y=tsne_data[,2])
> tsne1_3 = data.frame(x=tsne_data[,1],y=tsne_data[,3])
> tsne1_4 = data.frame(x=tsne_data[,1],y=tsne_data[,4])
> tsne1_5 = data.frame(x=tsne_data[,1],y=tsne_data[,5])
> tsne2_3 = data.frame(x=tsne_data[,2],y=tsne_data[,3])
> tsne2_4 = data.frame(x=tsne_data[,2],y=tsne_data[,4])
> tsne2_5 = data.frame(x=tsne_data[,2],y=tsne_data[,5])
> tsne3_4 = data.frame(x=tsne_data[,3],y=tsne_data[,4])
> tsne3_5 = data.frame(x=tsne_data[,3],y=tsne_data[,5])
> tsne4_5 = data.frame(x=tsne_data[,4],y=tsne_data[,5])
> plot(tsne1_2, col=kClust$cluster, pch=19, xlab ='Dimension 1', ylab='Dimension 2')
> plot(tsne1_3, col=kClust$cluster, pch=19, xlab ='Dimension 1', ylab='Dimension 3')
> plot(tsne1_4, col=kClust$cluster, pch=19, xlab ='Dimension 1', ylab='Dimension 4')
> plot(tsne1_5, col=kClust$cluster, pch=19, xlab ='Dimension 1', ylab='Dimension 5')
> plot(tsne2_3, col=kClust$cluster, pch=19, xlab ='Dimension 2', ylab='Dimension 3')
> plot(tsne2_4, col=kClust$cluster, pch=19, xlab ='Dimension 2', ylab='Dimension 4')
> plot(tsne2_5, col=kClust$cluster, pch=19, xlab ='Dimension 2', ylab='Dimension 5')
> plot(tsne3_4, col=kClust$cluster, pch=19, xlab ='Dimension 3', ylab='Dimension 4')
> plot(tsne3_5, col=kClust$cluster, pch=19, xlab ='Dimension 3', ylab='Dimension 5')
> plot(tsne4_5, col=kClust$cluster, pch=19, xlab ='Dimension 4', ylab='Dimension 5')
> numClusters = 12
> kClust <- kmeans(tsne_data, numClusters)
> kClust <- kmeans(tsne_data, numClusters)
> plot(tsne_data, col=kClust$cluster)
> tsNE1_2_3 = data.frame(x=tsne_data[,1],y=tsne_data[,2],z=tsne_data[,3])
> open3d()
glX
3
> colors = rainbow(kClust$cluster)
> plot3d(tsNE1_2_3,col=colors[kClust$cluster],xlab="DM1",ylab="DM2",zlab="DM3",size=.4,type="s")
> tsne1_2 = data.frame(x=tsne_data[,1],y=tsne_data[,2])
> tsne1_3 = data.frame(x=tsne_data[,1],y=tsne_data[,3])
> tsne1_4 = data.frame(x=tsne_data[,1],y=tsne_data[,4])
> tsne1_5 = data.frame(x=tsne_data[,1],y=tsne_data[,5])
> tsne2_3 = data.frame(x=tsne_data[,2],y=tsne_data[,3])
> tsne2_4 = data.frame(x=tsne_data[,2],y=tsne_data[,4])
> tsne2_5 = data.frame(x=tsne_data[,2],y=tsne_data[,5])
> tsne3_4 = data.frame(x=tsne_data[,3],y=tsne_data[,4])
> tsne3_5 = data.frame(x=tsne_data[,3],y=tsne_data[,5])
> tsne4_5 = data.frame(x=tsne_data[,4],y=tsne_data[,5])
> plot(tsne1_2, col=kClust$cluster, pch=19, xlab ='Dimension 1', ylab='Dimension 2')
> plot(tsne1_3, col=kClust$cluster, pch=19, xlab ='Dimension 1', ylab='Dimension 3')
> plot(tsne1_4, col=kClust$cluster, pch=19, xlab ='Dimension 1', ylab='Dimension 4')
> plot(tsne1_5, col=kClust$cluster, pch=19, xlab ='Dimension 1', ylab='Dimension 5')
> plot(tsne2_3, col=kClust$cluster, pch=19, xlab ='Dimension 2', ylab='Dimension 3')
> plot(tsne2_4, col=kClust$cluster, pch=19, xlab ='Dimension 2', ylab='Dimension 4')
> plot(tsne2_5, col=kClust$cluster, pch=19, xlab ='Dimension 2', ylab='Dimension 5')
> plot(tsne3_4, col=kClust$cluster, pch=19, xlab ='Dimension 3', ylab='Dimension 4')
> plot(tsne3_5, col=kClust$cluster, pch=19, xlab ='Dimension 3', ylab='Dimension 5')
> plot(tsne4_5, col=kClust$cluster, pch=19, xlab ='Dimension 4', ylab='Dimension 5')
> write.csv(kClust$cluster, 'clusters12_results.csv')
> savehistory("~/Desktop/t-SNE 151124/Rscript.Rhistory")
#Note: I always have a problem copying and pasting the epc <- function for t-SNE. It does not run when I copy and paste. I always have to go back to the original site that has the code and copy it from there: http://www.codeproject.com/Tips/788739/Visualization-of-High-Dimensional-Data-using-t-SNE.