0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rで人工データを作る

Posted at

はじめに

Pythonではrandomライブラリを用いて人工データを簡単に生成できるが、Rでは(とくに離散データの生成に)ひと手間いるようなのでその手順を書く。

連続データの生成

正規分布・一様分布にしたがうデータは以下コードで生成できる。

continuous.R
rnorm(n=5, mean=50, sd=10)  # 平均50、標準偏差10の正規分布から5個データを生成
runif(n=5, min=0, max=100)  # 最小値0、最大値100の一様分布から5個データを生成

rnorm関数はmean, sdを指定しない場合が標準正規分布、runif関数はmin, maxを指定しない場合0から1の値となる。

離散データの生成

上記で作成した連続データをcut関数でカテゴリ分けし、level関数で値をふることで離散データを生成する。

discrete.R
x <- runif(n=5)
x.dis <- cut(x, seq(0,1,0.2))
levels(x.dis) <- 1:5

0から1の間で生成された連続データを0.2刻みでカテゴリ分けし、それらのカテゴリに1から5までの値を割り当てている。ちなみにpythonであればrandomライブラリをインポートして x.dis = [random.randint(1, 5) for i in range(5)] で簡単に生成できる。Pythonは便利。

0
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?