2

我有一个由 10 万条唯一数据记录组成的数据集,为了对代码进行基准测试,我需要测试具有 500 万条唯一记录的数据,我不想生成随机数据。我想使用我拥有的 100k 数据记录作为基础数据集,并生成与它类似的剩余数据,并为某些列提供唯一值,我该如何使用 python 或 Scala 来做到这一点?

这是示例数据

latitude   longitude  step count
25.696395   -80.297496  1   1
25.699544   -80.297055  1   1
25.698612   -80.292015  1   1
25.939942   -80.341607  1   1
25.939221   -80.349899  1   1
25.944992   -80.346589  1   1
27.938951   -82.492018  1   1
27.944691   -82.48961   1   3
28.355484   -81.55574   1   1

每对纬度和经度在生成的数据中应该是唯一的,我也应该能够为这些列设置最小值和最大值

4

2 回答 2

3

您可以使用R轻松生成符合正态分布的数据,您可以按照以下步骤操作

#Read the data into a dataframe
library(data.table)
data = data = fread("data.csv", sep=",", select = c("latitude", "longitude"))

#Remove duplicate and null values
df = data.frame("Lat"=data$"latitude", "Lon"=data$"longitude")
df1 = unique(df[1:2])
df2  <- na.omit(df1)

#Determine the mean and standard deviation of latitude and longitude values
meanLat = mean(df2$Lat)
meanLon = mean(df2$Lon)
sdLat = sd(df2$Lat)
sdLon = sd(df2$Lon)

#Use Normal distribution to generate new data of 1 million records

newData = list()
newData$Lat = sapply(rep(0, 1000000), function(x) (sum(runif(12))-6) * sdLat + meanLat)
newData$Lon = sapply(rep(0, 1000000), function(x) (sum(runif(12))-6) * sdLon + meanLon)

finalData = rbind(df2,newData)

now final data contains both old records and new records

将 finalData 数据帧写入 CSV 文件,您可以从 Scala 或 python 读取它

于 2018-04-22T17:19:11.077 回答
1

如果你只想在scala中生成数据,试试这种方式。

val r = new scala.util.Random   //create scala random object
val new_val = r.nextFloat() // for generating next random float between 0 to 1 for every call

并将这个 new_val 添加到数据中纬度的最大值。无论如何,独特的纬度使配对独一无二。

您可以使用带有 Scala 的 Spark 尝试此选项。

val latLongDF = ss.read.option("header", true).option("delimiter", ",").format("csv").load(mypath)   // loaded your sample data in your question as Dataframe
+---------+----------+----+-----+
| latitude| longitude|step|count|
+---------+----------+----+-----+
|25.696395|-80.297496|   1|    1|
|25.699544|-80.297055|   1|    1|
|25.698612|-80.292015|   1|    1|


val max_lat = latLongDF.select(max("latitude")).first.get(0).toString().toDouble // got max latitude value

val r = new scala.util.Random // scala random object to get random numbers

val nextLat = udf(() => (28.355484 + 0.000001 + r.nextFloat()).toFloat) // udf to give random latitude more than the existing maximum latitude

在上面的行toFloat中,浮动可能会导致重复值。如果您可以在纬度中使用更多十进制值(超过 6 个),请删除它以获得完整的随机值。或者在经度上使用相同的方法也可以获得更好的唯一性。

val new_df = latLongDF.withColumn("new_lat", nextLat()).select(col("new_lat").alias("latitude"),$"longitude",$"step",$"count").union(latLongDF) // creating new dataframe and Union with existing dataframe 

新生成的数据样本。

+----------+----------+----+-----+
|latitude| longitude|step|count|
+----------+----------+----+-----+
| 28.446129|-80.297496|   1|    1|
| 28.494934|-80.297055|   1|    1|
| 28.605234|-80.292015|   1|    1|
| 28.866316|-80.341607|   1|    1|
于 2018-04-07T06:47:02.057 回答