当前位置:网站首页>R ellipse random point generation and drawing
R ellipse random point generation and drawing
2022-06-24 09:32:00 【qq_ forty-five million seven hundred and fifty-nine thousand tw】
Generating data sets
The first thing I thought about was R Generate dataset in , That's true. python There is make_blob function , that R Is there a corresponding function in , Although I can be in python Generate data and then import it to R It will be very trouble , Because I don't just generate data once , I have to simulate many times , Finally find a satisfactory price comparison ,
https://github.com/elbamos/clusteringdatasets
This R And... Are provided in the package sklearn Inside make_blob Same function
The installation method is as follows
First download github The source file
install.packages("/Volumes/MACPAN/Chrome download /clusteringdatasets-master/",type="source",repos=NULL)
library(clusteringdatasets)
After installation, it can be used
Use cases are as follows
rm(list=ls())
library(ggplot2)
library(clusteringdatasets)
set.seed(1)
n_sample=20000
center_metrix=matrix(c(0,3,-2,0,2,0),nrow=3,byrow = T) # Set up cluster center
##
blobs <- make_blobs(n_samples = n_sample,centers=center_metrix,cluster_std = 0.2)
xx=sample(c(1,2,3),n_sample,replace=T)
blobs$labels=xx
# Because my final result is very smooth , So I want to delete some points
# radis=0.3
# r1=center_metrix[1,]
# r2=center_metrix[2,]
# r3=center_metrix[3,]
df=as.data.frame(blobs)
colnames(df)=c("x","y","cluster")
df$cluster=as.factor(df$cluster)
#plot(blobs$samples, col=rainbow(3)[blobs$labels])
p=ggplot(data=df,aes(x=x,y=y,colour=cluster))+geom_point()
print(p)
give the result as follows 
But when I draw this , For a better look , I want to draw the ellipse further , How to achieve this
https://stackoverflow.com/questions/41820683/how-to-plot-ellipse-given-a-general-equation-in-r
This question mentioned how to draw an ellipse
Actually, this ellipse is what I want , But this is just a drawing , No expression is provided , Because I essentially want to generate random points in an ellipse
pass
Then I found a similar problem
https://stackoverflow.com/questions/64847597/how-do-i-generate-data-where-points-are-repelled-if-they-land-within-a-certain
This explains how to generate randomly distributed points in a circle , But what I need is an ellipse , It can be used for reference ,
Then I found a more interesting drawing scheme , Draw the confidence interval of the ellipse directly
https://blog.csdn.net/maryyu8873/article/details/78491869
library(car)
dataEllipse(Duncan$income, Duncan$education, levels=0.95,xlim=c(-30,120),ylim=c(-30,160))
This can also be used for reference , But I found a perfect solution later
Is the use of runifdisc function
The code I implemented is as follows
################################# plot cluster1 #########################
############################################################################
############################################################################
############################################################################
n_sample=2000
center=c(0,3)
X <- runifdisc(n_sample)
Y <- affine(X, mat=diag(c(1.5,0.8)))
Z = Y
#Z = rotate(Y,45)
df = as.data.frame(matrix(nrow=n_sample,ncol=3)) # Create a 3 Empty object for column
colnames(df)=c("x","y","cluster")
df$x=Z$x
df$y=Z$y
df$x=df$x+center[1]
df$y=df$y+center[2]
df$cluster=sample(c("Batch1","Batch2","Batch3"),n_sample,replace=T)
df$cluster=as.factor(df$cluster)
df1=df
########################### plot cluster 2 ################################
############################################################################
############################################################################
############################################################################
center=c(-4,0)
X <- runifdisc(n_sample)
Y <- affine(X, mat=diag(c(1,1.5)))
#Z=Y
Z = rotate(Y,90)
#Z = rotate(Z,45)
df = as.data.frame(matrix(nrow=n_sample,ncol=3)) # Create a 3 Empty object for column
colnames(df)=c("x","y","cluster")
df$x=Z$x
df$y=Z$y
df$x=df$x+center[1]
df$y=df$y+center[2]
df$cluster=sample(c("Batch1","Batch2","Batch3"),n_sample,replace=T)
df$cluster=as.factor(df$cluster)
df2=df
########################### plot cluster 3 ################################
############################################################################
############################################################################
############################################################################
center=c(4,0)
X <- runifdisc(n_sample)
Y <- affine(X, mat=diag(c(2,1)))
Z = rotate(Y,135)
df = as.data.frame(matrix(nrow=n_sample,ncol=3)) # Create a 3 Empty object for column
colnames(df)=c("x","y","cluster")
df$x=Z$x
df$y=Z$y
df$x=df$x+center[1]
df$y=df$y+center[2]
df$cluster=sample(c("Batch1","Batch2","Batch3"),n_sample,replace=T)
df$cluster=as.factor(df$cluster)
df3=df
df=rbind(df1,df2,df3)
colnames(df)=c("x","y","BATCH")
p=ggplot(data=df,aes(x=x,y=y,colour=BATCH))+geom_point(size=1)+
theme(aspect.ratio=1)+theme_void()+
guides(colour = guide_legend(override.aes = list(size = 20,alpha=1,shape=16)))+
theme(legend.position = c(0.8, 0.7),legend.title=element_text(size=20))+
theme(legend.text=element_text(size=20))+
scale_color_manual(values=RColorBrewer::brewer.pal(3,"Set1"))
print(p)
ggsave("./simulation_plot.png",plot=p)
The final result is as follows
I can't adjust the angle of the ellipse on the left properly , I feel the rotation angle is wrong , But it looks pretty , It meets my needs , Hey
边栏推荐
- Learn Tai Chi Maker - esp8226 (12) esp8266 multitasking
- jupyter入门常见的几个坑:
- June 13-19, 2022 AI industry weekly (issue 102): career development
- Weekly recommended short video: talk about "meta universe" with a serious attitude
- 学习太极创客 — ESP8226 (十三)OTA
- ThinkPHP 5.0 模型关联详解
- 零基础自学SQL课程 | 相关子查询
- CF566E-Restoring Map【bitset】
- Go 语言项目开发实战目录
- 嵌入式 | 硬件转软件的几条建议
猜你喜欢

深入了解 border

Lu Qi: I am most optimistic about these four major technology trends

学习太极创客 — ESP8226 (十三)OTA

cookie加密 4 rpc方法确定cookie加密

Yolox backbone -- implementation of cspparknet

零基础自学SQL课程 | 子查询
![[bug] @jsonformat has a problem that the date is less than one day when it is used](/img/09/516799972cd3c18795826199aabc9b.png)
[bug] @jsonformat has a problem that the date is less than one day when it is used

Zero foundation self-study SQL course | related sub query
Depens:*** but it is not going to be installed

【Redis實現秒殺業務①】秒殺流程概述|基本業務實現
随机推荐
LeetCode: 240. 搜索二维矩阵 II
Every (), map (), forearch () methods. There are objects in the array
什么情况下应该使用GridFS?
Zero foundation self-study SQL course | having clause
Recommendation - Secret of curiosity: how many dancing angels can stand on the tip of a needle?
June 13-19, 2022 AI industry weekly (issue 102): career development
Zero foundation self-study SQL course | syntax sequence and execution sequence of SQL statements
The border problem after the focus of input
php文件锁
【bug】@JsonFormat 使用时出现日期少一天的问题
【LeetCode】541. Reverse string II
百度AI模板 获取知识理解
Target detection series fast r-cnn
L01_ How is an SQL query executed?
RISC-V架构下 FPU Context 的动态保存和恢复
L01_一条SQL查询语句是如何执行的?
【LeetCode】387. First unique character in string
Applet cloud data, data request a method to collect data
How to import MDF and LDF files into MySQL workbench
EasyExcel单sheet页与多sheet页写出