import time
import random
import numpy as np
import pandas as pd
def create_data():
data_x = []
data_y = []
data = []
for i in range(300000):
x = random.randrange(0, 300000)
y = random.randrange(-1500, 1500)
data_x.append(x)
data_y.append(y)
data.append([x, y])
return data_x, data_y, data
def calculate_density(gridSize, bounds):
data_x, data_y, data = create_data()
x = np.arange(bounds[0][0],
bounds[1][0] + gridSize,
gridSize)
y = np.arange(bounds[0][1],
bounds[1][1] + gridSize,
gridSize)
grid = pd.DataFrame(0, index=x[:-1], columns=y[:-1])
for point in data:
if point[0] < bounds[0][0] \
or point[0] > bounds[1][0] \
or point[1] < bounds[0][1] \
or point[1] > bounds[1][1]:
continue
x_index = int((point[0] - bounds[0][0]) // gridSize)
y_index = int((point[1] - bounds[0][1]) // gridSize)
grid.iloc[x_index, y_index] += 1
densities = grid.to_numpy() / (gridSize * gridSize)
for point in data:
if point[0] < bounds[0][0] \
or point[0] > bounds[1][0] \
or point[1] < bounds[0][1] \
or point[1] > bounds[1][1]:
continue
x_index = int((point[0] - bounds[0][0]) // gridSize)
y_index = int((point[1] - bounds[0][1]) // gridSize)
point.append(densities[x_index, y_index])
return densities, data
if __name__ == "__main__":
start_time = time.time()
densities, data = calculate_density(100,
[[0, -1500], [300000, 1500]])
end_time = time.time()
print("消耗的时间:", end_time - start_time)
print(densities)
function createData() {
let data = []
for (i = 0; i < 10000; i++) {
let data_y = (Math.random() - Math.random())
let data_x = i
data.push([data_x, data_y])
}
return data
}
function getMax(data){
let len = data.length
let max = -Infinity
while(len--){
max = data[len] > max ? data[len] : max
}
return max
}
function getMin(data){
let len = data.length
let min = Infinity
while(len--){
min = data[len] < min ? data[len] : min
}
return min
}
function calculateDensity(gridSize){
let data = createData()
let x = data.map(v => v[0])
let y = data.map(v => v[1])
let min_x = getMin(x)
let max_x = getMax(x)
let min_y = getMin(y)
let max_y = getMax(y)
let width = max_x - min_x
let height = max_y - min_y
let x_count = data.length / 100
let y_count = 100
let x_gridSize = width / (x_count - 1)
let y_gridSize = height / (y_count - 1)
let counts = Array.from(Array(x_count), () => Array.from(Array(y_count), () => new Array(3).fill(0)))
console.log(counts)
data.forEach((v) => {
if(v[0] < min_x || v[0] > max_x || v[1] < min_y || v[1] > max_y){
return
}
let x_index = Math.floor((v[0] - min_x) / x_gridSize)
let y_index = Math.floor((v[1] - min_y) / y_gridSize)
counts[x_index][y_index][2] += 1
})
let densities = counts.map((count, x_index) => count.map((v, y_index) => {
v[2] /= gridSize
v[0] = x_index
v[1] = y_index
return v
}))
console.log(densities)
return densities
}
let start_time = Date.now()
calculateDensity(10)
let end_time = Date.now()
console.log('消耗的时间:', end_time - start_time)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)