You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从R的wordVectors模型提取并导出绘图X/Y坐标至CSV

Extract & Export wordVectors Plot Coordinates to CSV

Got it! Let's walk through how to pull those X/Y coordinates from your wordVectors visualization and save them as a CSV for tools like Tableau. You’ve got two reliable approaches to choose from:

Approach 1: Pull Coordinates Directly from Your Existing Plot

If you already ran plot() on your trained model to generate the visualization, this is the easiest way—since you’ll get the exact coordinates used in the graph you’re looking at:

# First, save your plot to a variable (replace `your_trained_model` with your actual model name)
my_plot <- plot(your_trained_model)

# Extract the underlying data that powers the plot
plot_coords <- my_plot$data

# Optional: Check the first few rows to confirm it has Word, x, y columns
head(plot_coords)

# Export to CSV—this will save to your current R working directory
write.csv(plot_coords, "word_vector_plot_coords.csv", row.names = FALSE)

The resulting CSV will have all the words paired with their exact X/Y positions from your R plot, ready to import straight into Tableau.

Approach 2: Manually Calculate Dimensionality Reduction (More Flexible)

If you want to tweak the dimensionality reduction method (like switching between PCA and t-SNE) or adjust parameters, you can compute the coordinates from scratch:

Step 1: Extract the Word Vectors

First, pull the raw vector matrix from your trained model:

word_vectors <- as.matrix(your_trained_model)  # Replace with your model name

Step 2: Choose Your Dimensionality Reduction Method

Option A: PCA (Default for wordVectors Plots)

This is what the plot() function uses by default:

# Run PCA to reduce vectors to 2 dimensions
pca_results <- prcomp(word_vectors, scale. = FALSE)

# Format into a data frame with word names and coordinates
pca_coords <- data.frame(
  Word = rownames(word_vectors),
  X = pca_results$x[, 1],
  Y = pca_results$x[, 2]
)

Option B: t-SNE (Better for Clustering Visualization)

If you prefer t-SNE for clearer groupings, you’ll need the Rtsne package:

# Install the package first (only need to do this once)
# install.packages("Rtsne")
library(Rtsne)

# Run t-SNE (adjust perplexity based on your dataset size—10-50 is typical)
tsne_results <- Rtsne(word_vectors, dims = 2, perplexity = 30, max_iter = 1000)

# Format into a data frame
tsne_coords <- data.frame(
  Word = rownames(word_vectors),
  X = tsne_results$Y[, 1],
  Y = tsne_results$Y[, 2]
)

Step 3: Export to CSV

Save whichever coordinate set you need:

# Export PCA coordinates
write.csv(pca_coords, "word_vector_pca_coords.csv", row.names = FALSE)

# Or export t-SNE coordinates
write.csv(tsne_coords, "word_vector_tsne_coords.csv", row.names = FALSE)

Quick Notes

  • Don’t forget to replace your_trained_model with the actual name of your wordVectors model object (e.g., my_wv_model).
  • To find where your CSV saves, run getwd() in R to check your current working directory. You can also specify a full file path in write.csv() (like "C:/MyProjects/word_coords.csv") if you want to save it somewhere specific.

内容的提问来源于stack exchange,提问作者revsequin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:05:00