ds: secp256k1, 2
This commit is contained in:
@ -1,152 +1,106 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <cmath>
|
||||
|
||||
extern "C" __device__ void add_u16(
|
||||
ulonglong2 *out_c,
|
||||
ulonglong2 in_a,
|
||||
ulonglong2 in_b
|
||||
);
|
||||
|
||||
extern "C" __device__ void sub_u16(
|
||||
ulonglong2 *out_c,
|
||||
ulonglong2 in_a,
|
||||
ulonglong2 in_b
|
||||
);
|
||||
|
||||
extern "C" __device__ void add_u32(
|
||||
ulonglong4 *out_c,
|
||||
ulonglong4 in_a,
|
||||
ulonglong4 in_b
|
||||
);
|
||||
|
||||
extern "C" __device__ void sub_u32(
|
||||
ulonglong4 *out_c,
|
||||
ulonglong4 in_a,
|
||||
ulonglong4 in_b
|
||||
);
|
||||
|
||||
__device__ void mul_u16(
|
||||
ulonglong2 *out_c,
|
||||
ulonglong2 in_a,
|
||||
ulonglong2 in_b
|
||||
) {
|
||||
uint64_t ax_ay = in_a.x + in_a.y;
|
||||
uint64_t bx_by = in_b.x + in_b.y;
|
||||
uint64_t axbx = in_a.x * in_b.x;
|
||||
uint64_t ayby = in_a.y * in_b.y;
|
||||
out_c->x = ax_ay * bx_by - axbx - ayby;
|
||||
out_c->y = ayby;
|
||||
}
|
||||
|
||||
__device__ void mul_u32(
|
||||
ulonglong4 *out_c,
|
||||
ulonglong4 in_a,
|
||||
ulonglong4 in_b
|
||||
) {
|
||||
auto ax = (ulonglong2 *)&in_a.x;
|
||||
auto ay = (ulonglong2 *)&in_a.z;
|
||||
auto bx = (ulonglong2 *)&in_b.x;
|
||||
auto by = (ulonglong2 *)&in_b.z;
|
||||
ulonglong2 ax_ay, bx_by, paren, axbx, ayby;
|
||||
add_u16(&ax_ay, *ax, *ay);
|
||||
add_u16(&bx_by, *bx, *by);
|
||||
mul_u16(&paren, ax_ay, bx_by);
|
||||
mul_u16(&axbx, *ax, *bx);
|
||||
mul_u16(&ayby, *ay, *by);
|
||||
sub_u16(&paren, paren, axbx);
|
||||
sub_u16(&paren, paren, ayby);
|
||||
out_c->x = paren.x;
|
||||
out_c->y = paren.y;
|
||||
out_c->z = ayby.x;
|
||||
out_c->w = ayby.y;
|
||||
}
|
||||
|
||||
__device__ bool equ_u16(ulonglong2 a, ulonglong2 b) {
|
||||
return a.x == b.x && a.y == b.y;
|
||||
}
|
||||
|
||||
__device__ bool equ_u32(ulonglong4 a, ulonglong4 b) {
|
||||
return a.x == b.x &&
|
||||
a.y == b.y &&
|
||||
a.z == b.z &&
|
||||
a.w == b.w;
|
||||
}
|
||||
|
||||
__device__ void print_u16(ulonglong2 a) {
|
||||
printf("0x%016llx.%016llx\n", a.x, a.y);
|
||||
}
|
||||
|
||||
__device__ void print_u32(ulonglong4 a) {
|
||||
printf("0x%016llx.%016llx.%016llx.%016llx\n", a.x, a.y, a.z, a.w);
|
||||
}
|
||||
|
||||
#define U8_MAX 0xFFFFFFFFFFFFFFFF
|
||||
#define _U16_MAX {U8_MAX, U8_MAX}
|
||||
#define _U32_MAX {U8_MAX, U8_MAX, U8_MAX, U8_MAX}
|
||||
|
||||
__global__ void test(bool *passed) {
|
||||
*passed = false;
|
||||
{
|
||||
ulonglong4 a = _U32_MAX;
|
||||
ulonglong4 b = {0, 0, 0, 1};
|
||||
ulonglong4 c = {0, 0, 0, 0};
|
||||
add_u32(&a, a, b);
|
||||
if (!equ_u32(a, c)) {
|
||||
printf("add_u32 ");
|
||||
print_u32(a);
|
||||
return;
|
||||
template <typename T, int TILE_SIZE>
|
||||
__global__ void mat_mul(T *A, T *B, T *C, int N, int M, int K) {
|
||||
__shared__ T sA[TILE_SIZE][TILE_SIZE];
|
||||
__shared__ T sB[TILE_SIZE][TILE_SIZE];
|
||||
|
||||
int bx = blockIdx.x, by = blockIdx.y;
|
||||
int tx = threadIdx.x, ty = threadIdx.y;
|
||||
|
||||
int row = by * TILE_SIZE + ty;
|
||||
int col = bx * TILE_SIZE + tx;
|
||||
|
||||
T sum = 0;
|
||||
|
||||
for (int tile = 0; tile < ceil((float)M/TILE_SIZE); tile++) {
|
||||
if (row < N && (tile * TILE_SIZE + tx) < M) {
|
||||
sA[ty][tx] = A[row * M + (tile * TILE_SIZE + tx)];
|
||||
} else {
|
||||
sA[ty][tx] = 0;
|
||||
}
|
||||
}
|
||||
{
|
||||
ulonglong4 a = {0, 0, 0, 0};
|
||||
ulonglong4 b = {0, 0, 0, 1};
|
||||
ulonglong4 c = _U32_MAX;
|
||||
sub_u32(&a, a, b);
|
||||
if (!equ_u32(a, c)) {
|
||||
printf("sub_32 ");
|
||||
print_u32(a);
|
||||
return;
|
||||
|
||||
if ((tile * TILE_SIZE + ty) < M && col < K) {
|
||||
sB[ty][tx] = B[(tile * TILE_SIZE + ty) * K + col];
|
||||
} else {
|
||||
sB[ty][tx] = 0;
|
||||
}
|
||||
}
|
||||
{
|
||||
ulonglong2 a = _U16_MAX;
|
||||
ulonglong2 b = {0, U8_MAX};
|
||||
ulonglong2 c = {U8_MAX, 1};
|
||||
mul_u16(&a, a, b);
|
||||
if (!equ_u16(a, c)) {
|
||||
printf("mul_16 ");
|
||||
print_u16(a);
|
||||
return;
|
||||
__syncthreads();
|
||||
|
||||
for (int k = 0; k < TILE_SIZE; k++) {
|
||||
sum += sA[ty][k] * sB[k][tx];
|
||||
}
|
||||
__syncthreads();
|
||||
}
|
||||
{
|
||||
ulonglong4 a = _U32_MAX;
|
||||
ulonglong4 b = {0, 0, U8_MAX, U8_MAX};
|
||||
ulonglong4 c = {U8_MAX, U8_MAX, 0, 1};
|
||||
mul_u32(&a, a, b);
|
||||
if (!equ_u32(a, c)) {
|
||||
printf("mul_32 ");
|
||||
print_u32(a);
|
||||
return;
|
||||
|
||||
if (row < N && col < K) {
|
||||
C[row * K + col] = sum;
|
||||
}
|
||||
}
|
||||
|
||||
#define MAT_TYPE int
|
||||
#define MAT_FMT "%d\t"
|
||||
#define N 5
|
||||
#define M 7
|
||||
#define K 3
|
||||
#define A_LEN (N * M)
|
||||
#define B_LEN (M * K)
|
||||
#define C_LEN (N * K)
|
||||
#define A_SIZE (sizeof(MAT_TYPE) * N * M)
|
||||
#define B_SIZE (sizeof(MAT_TYPE) * M * K)
|
||||
#define C_SIZE (sizeof(MAT_TYPE) * N * K)
|
||||
|
||||
#include <cstdio>
|
||||
#include <random>
|
||||
|
||||
template <typename T>
|
||||
void mat_print(T *a, const char *fmt, int n, int m) {
|
||||
for (auto row = 0; row < n; row++) {
|
||||
for (auto col = 0; col < m; col++) {
|
||||
printf(fmt, a[row * m + col]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
*passed = true;
|
||||
}
|
||||
|
||||
int main() {
|
||||
bool test_passed, *d_test_passed;
|
||||
cudaMalloc(&d_test_passed, sizeof(bool));
|
||||
|
||||
test<<<1, 1>>>(d_test_passed);
|
||||
cudaDeviceSynchronize();
|
||||
std::random_device rd;
|
||||
std::mt19937 engine(rd());
|
||||
std::uniform_int_distribution<MAT_TYPE> dist(1, 10);
|
||||
|
||||
cudaMemcpy(&test_passed, d_test_passed, sizeof(bool), cudaMemcpyDeviceToHost);
|
||||
cudaFree(d_test_passed);
|
||||
|
||||
if (!test_passed) {
|
||||
printf("test not passed\n");
|
||||
return 1;
|
||||
MAT_TYPE buf[A_LEN + B_LEN + C_LEN];
|
||||
for (auto i = 0; i < A_LEN + B_LEN; i++) {
|
||||
buf[i] = dist(engine);
|
||||
}
|
||||
|
||||
return 0;
|
||||
MAT_TYPE *a = buf;
|
||||
MAT_TYPE *b = a + A_LEN;
|
||||
MAT_TYPE *c = b + B_LEN;
|
||||
|
||||
printf("\na\n");
|
||||
mat_print(a, MAT_FMT, N, M);
|
||||
printf("\nb\n");
|
||||
mat_print(b, MAT_FMT, M, K);
|
||||
|
||||
MAT_TYPE *d_a, *d_b, *d_c;
|
||||
cudaMalloc(&d_a, A_SIZE);
|
||||
cudaMalloc(&d_b, B_SIZE);
|
||||
cudaMalloc(&d_c, C_SIZE);
|
||||
|
||||
cudaMemcpy(d_a, a, A_SIZE, cudaMemcpyHostToDevice);
|
||||
cudaMemcpy(d_b, b, B_SIZE, cudaMemcpyHostToDevice);
|
||||
|
||||
dim3 blockDim(4, 4);
|
||||
dim3 threadDim(4, 4);
|
||||
mat_mul<MAT_TYPE, 4><<<blockDim, threadDim>>>(d_a, d_b, d_c, N, M, K);
|
||||
|
||||
cudaMemcpy(c, d_c, C_SIZE, cudaMemcpyDeviceToHost);
|
||||
cudaDeviceSynchronize();
|
||||
|
||||
cudaFree(a);
|
||||
cudaFree(b);
|
||||
cudaFree(c);
|
||||
|
||||
printf("\nc\n");
|
||||
mat_print(c, MAT_FMT, N, K);
|
||||
}
|
||||
@ -36,15 +36,14 @@
|
||||
ld.param.v2.u64 {%ra1, %ra0}, [in_a];
|
||||
ld.param.v2.u64 {%rb1, %rb0}, [in_b];
|
||||
|
||||
add.cc.u64 %ra0, %ra0, %rb0;
|
||||
addc.u64 %ra1, %ra1, %rb1;
|
||||
sub.cc.u64 %ra0, %ra0, %rb0;
|
||||
subc.u64 %ra1, %ra1, %rb1;
|
||||
|
||||
st.v2.u64 [%rdc], {%ra1, %ra0};
|
||||
|
||||
ret;
|
||||
}
|
||||
|
||||
|
||||
.visible .func add_u32(
|
||||
.param .b64 out_c,
|
||||
.param .align 16 .b8 in_a[32],
|
||||
@ -95,4 +94,78 @@
|
||||
st.v2.u64 [%rdc + 16], {%ra1, %ra0};
|
||||
|
||||
ret;
|
||||
}
|
||||
|
||||
.visible .func mul_lo_u16(
|
||||
.param .b64 out_c,
|
||||
.param .align 16 .b8 in_a[16],
|
||||
.param .align 16 .b8 in_b[16]
|
||||
) {
|
||||
.reg .u64 %a, %b, %c, %d, %a_b, %c_d;
|
||||
.reg .u64 %ac, %bd_hi, %bd_lo, %p;
|
||||
.reg .b64 %rdc;
|
||||
|
||||
ld.param.b64 %rdc, [out_c];
|
||||
|
||||
ld.param.v2.u64 {%a, %b}, [in_a];
|
||||
ld.param.v2.u64 {%c, %d}, [in_b];
|
||||
|
||||
mul.lo.u64 %ac, %a, %c;
|
||||
mul.lo.u64 %bd_lo, %b, %d;
|
||||
mul.hi.u64 %bd_hi, %b, %d;
|
||||
|
||||
add.u64 %a_b, %a, %b;
|
||||
add.u64 %c_d, %c, %d;
|
||||
|
||||
mul.lo.u64 %p, %a_b, %c_d;
|
||||
|
||||
sub.u64 %p, %p, %ac;
|
||||
sub.u64 %p, %p, %bd_lo;
|
||||
|
||||
add.u64 %p, %p, %bd_hi;
|
||||
|
||||
st.v2.u64 [%rdc], {%p, %bd_lo};
|
||||
|
||||
ret;
|
||||
}
|
||||
|
||||
.visible .func mul_u16(
|
||||
.param .b64 out_c_hi,
|
||||
.param .b64 out_c_lo,
|
||||
.param .align 16 .b8 in_a[16],
|
||||
.param .align 16 .b8 in_b[16]
|
||||
) {
|
||||
.reg .u64 %a, %b, %c, %d;
|
||||
.reg .u64 %a_b_hi, %a_b_lo, %c_d_hi, %c_d_lo;
|
||||
.reg .u64 %p_hi, %p_lo, %p_hi2, %p_lo2;
|
||||
.reg .u64 %ac_hi, %ac_lo, %bd_hi, %bd_lo;
|
||||
.reg .b64 %rdc_hi, %rdc_lo;
|
||||
|
||||
ld.param.b64 %rdc_hi, [out_c_hi];
|
||||
ld.param.b64 %rdc_lo, [out_c_lo];
|
||||
|
||||
ld.param.v2.u64 {%a, %b}, [in_a];
|
||||
ld.param.v2.u64 {%c, %d}, [in_b];
|
||||
|
||||
mul.lo.u64 %ac_lo, %a, %c;
|
||||
mul.hi.u64 %ac_hi, %a, %c;
|
||||
mul.lo.u64 %bd_lo, %b, %d;
|
||||
mul.hi.u64 %bd_hi, %b, %d;
|
||||
|
||||
add.cc.u64 %a_b_lo, %a, %b;
|
||||
addc.u64 %a_b_hi, %a, %b;
|
||||
add.cc.u64 %c_d_lo, %c, %d;
|
||||
addc.u64 %c_d_hi, %c, %d;
|
||||
|
||||
mul.lo.u64 %p_lo, %a_b_lo, %c_d_lo;
|
||||
mul.hi.u64 %p_hi, %a_b_lo, %c_d_lo;
|
||||
mul.lo.u64 %p_hi2, %a_b_hi, %c_d_hi;
|
||||
|
||||
|
||||
|
||||
|
||||
st.v2.u64 [%rdc_lo], {%p_hi, %p_lo};
|
||||
st.v2.u64 [%rdc_hi], {%a_b_lo, %p_hi2};
|
||||
|
||||
ret;
|
||||
}
|
||||
205
ds/25-1/1e/secp256k1.cu
Normal file
205
ds/25-1/1e/secp256k1.cu
Normal file
@ -0,0 +1,205 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
extern "C" __device__ void add_u16(
|
||||
ulonglong2 *out_c,
|
||||
ulonglong2 in_a,
|
||||
ulonglong2 in_b
|
||||
);
|
||||
|
||||
extern "C" __device__ void sub_u16(
|
||||
ulonglong2 *out_c,
|
||||
ulonglong2 in_a,
|
||||
ulonglong2 in_b
|
||||
);
|
||||
|
||||
extern "C" __device__ void add_u32(
|
||||
ulonglong4 *out_c,
|
||||
ulonglong4 in_a,
|
||||
ulonglong4 in_b
|
||||
);
|
||||
|
||||
extern "C" __device__ void sub_u32(
|
||||
ulonglong4 *out_c,
|
||||
ulonglong4 in_a,
|
||||
ulonglong4 in_b
|
||||
);
|
||||
|
||||
extern "C" __device__ void mul_lo_u16(
|
||||
ulonglong2 *out_c,
|
||||
ulonglong2 in_a,
|
||||
ulonglong2 in_b
|
||||
);
|
||||
|
||||
extern "C" __device__ void mul_u16(
|
||||
ulonglong2 *out_c_hi,
|
||||
ulonglong2 *out_c_lo,
|
||||
ulonglong2 in_a,
|
||||
ulonglong2 in_b
|
||||
);
|
||||
|
||||
__device__ bool equ_u16(ulonglong2 a, ulonglong2 b) {
|
||||
return a.x == b.x && a.y == b.y;
|
||||
}
|
||||
|
||||
__device__ bool equ_u32(ulonglong4 a, ulonglong4 b) {
|
||||
return a.x == b.x &&
|
||||
a.y == b.y &&
|
||||
a.z == b.z &&
|
||||
a.w == b.w;
|
||||
}
|
||||
|
||||
__device__ int cmp_u32(ulonglong4 a, ulonglong4 b) {
|
||||
if (a.x < b.x)
|
||||
return -1;
|
||||
else if (a.x > b.x)
|
||||
return 1;
|
||||
|
||||
if (a.y < b.y)
|
||||
return -1;
|
||||
else if (a.y > b.y)
|
||||
return 1;
|
||||
|
||||
if (a.z < b.z)
|
||||
return -1;
|
||||
else if (a.z > b.z)
|
||||
return 1;
|
||||
|
||||
if (a.w < b.w)
|
||||
return -1;
|
||||
else if (a.w > b.w)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
__device__ void mul_lo_u32(
|
||||
ulonglong4 *out_c,
|
||||
ulonglong4 in_a,
|
||||
ulonglong4 in_b
|
||||
) {
|
||||
auto a = (ulonglong2 *)&in_a.x;
|
||||
auto b = (ulonglong2 *)&in_a.z;
|
||||
auto c = (ulonglong2 *)&in_b.x;
|
||||
auto d = (ulonglong2 *)&in_b.z;
|
||||
ulonglong2 a_b, c_d, ac, bd_hi, bd_lo, p;
|
||||
|
||||
mul_lo_u16(&ac, *a, *c);
|
||||
mul_u16(&bd_hi, &bd_lo, *b, *d);
|
||||
|
||||
add_u16(&a_b, *a, *b);
|
||||
add_u16(&c_d, *c, *d);
|
||||
|
||||
mul_lo_u16(&p, a_b, c_d);
|
||||
|
||||
sub_u16(&p, p, ac);
|
||||
sub_u16(&p, p, bd_lo);
|
||||
add_u16(&p, p, bd_hi);
|
||||
|
||||
out_c->x = p.x;
|
||||
out_c->y = p.y;
|
||||
out_c->z = bd_lo.x;
|
||||
out_c->w = bd_lo.y;
|
||||
}
|
||||
|
||||
__device__ void print_u16(ulonglong2 a) {
|
||||
printf("0x%016llx.%016llx\n", a.x, a.y);
|
||||
}
|
||||
|
||||
__device__ void print_u32(ulonglong4 a) {
|
||||
printf("0x%016llx.%016llx.%016llx.%016llx\n", a.x, a.y, a.z, a.w);
|
||||
}
|
||||
|
||||
#define U8_MAX 0xFFFFFFFFFFFFFFFF
|
||||
#define U16_MAX {U8_MAX, U8_MAX}
|
||||
#define U32_MAX {U8_MAX, U8_MAX, U8_MAX, U8_MAX}
|
||||
|
||||
__global__ void test(bool *passed) {
|
||||
*passed = true;
|
||||
{
|
||||
ulonglong4 a = U32_MAX;
|
||||
ulonglong4 b = {0, 0, 0, 1};
|
||||
ulonglong4 c = {0, 0, 0, 0};
|
||||
add_u32(&a, a, b);
|
||||
if (!equ_u32(a, c)) {
|
||||
printf("add_u32\n");
|
||||
print_u32(a);
|
||||
*passed = false;
|
||||
}
|
||||
}
|
||||
{
|
||||
ulonglong4 a = {0, 0, 0, 0};
|
||||
ulonglong4 b = {0, 0, 0, 1};
|
||||
ulonglong4 c = U32_MAX;
|
||||
sub_u32(&a, a, b);
|
||||
if (!equ_u32(a, c)) {
|
||||
printf("sub_u32\n");
|
||||
print_u32(a);
|
||||
*passed = false;
|
||||
}
|
||||
}
|
||||
{
|
||||
ulonglong2 a = U16_MAX;
|
||||
ulonglong2 b = {0, U8_MAX};
|
||||
ulonglong2 c = {U8_MAX, 1};
|
||||
mul_lo_u16(&a, a, b);
|
||||
if (!equ_u16(a, c)) {
|
||||
printf("mul_lo_u16\n");
|
||||
print_u16(a);
|
||||
*passed = false;
|
||||
}
|
||||
}
|
||||
{
|
||||
ulonglong2 a = U16_MAX;
|
||||
ulonglong2 b = {0, U8_MAX};
|
||||
ulonglong2 c_hi = {0, U8_MAX - 1};
|
||||
ulonglong2 c_lo = {U8_MAX, 1};
|
||||
mul_u16(&a, &b, a, b);
|
||||
if (!equ_u16(a, c_hi) || !equ_u16(b, c_lo)) {
|
||||
printf("mul_u16\n");
|
||||
print_u16(a);
|
||||
print_u16(b);
|
||||
*passed = false;
|
||||
}
|
||||
a = U16_MAX;
|
||||
b = U16_MAX;
|
||||
c_hi = {U8_MAX, U8_MAX - 1};
|
||||
c_lo = {0, 1};
|
||||
mul_u16(&a, &b, a, b);
|
||||
if (!equ_u16(a, c_hi) || !equ_u16(b, c_lo)) {
|
||||
printf("mul_u16\n");
|
||||
print_u16(a);
|
||||
print_u16(b);
|
||||
*passed = false;
|
||||
}
|
||||
}
|
||||
{
|
||||
ulonglong4 a = U32_MAX;
|
||||
ulonglong4 b = {0, 0, U8_MAX, U8_MAX};
|
||||
ulonglong4 c = {U8_MAX, U8_MAX, 0, 1};
|
||||
mul_lo_u32(&a, a, b);
|
||||
if (!equ_u32(a, c)) {
|
||||
printf("mul_lo_u32\n");
|
||||
print_u32(a);
|
||||
*passed = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
bool test_passed, *d_test_passed;
|
||||
cudaMalloc(&d_test_passed, sizeof(bool));
|
||||
|
||||
test<<<1, 1>>>(d_test_passed);
|
||||
cudaDeviceSynchronize();
|
||||
|
||||
cudaMemcpy(&test_passed, d_test_passed, sizeof(bool), cudaMemcpyDeviceToHost);
|
||||
cudaFree(d_test_passed);
|
||||
|
||||
if (!test_passed) {
|
||||
printf("test not passed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -2,16 +2,21 @@ U8_MAX = 0xFFFFFFFFFFFFFFFF
|
||||
U16_MAX = U8_MAX << 64 | U8_MAX
|
||||
U32_MAX = U16_MAX << 128 | U16_MAX
|
||||
|
||||
|
||||
def dothex(num):
|
||||
strhex = hex(num)[2:]
|
||||
dothex = strhex[-16:]
|
||||
strhex = strhex[:-16]
|
||||
|
||||
|
||||
while len(strhex) > 0:
|
||||
dothex = strhex[-16:] + '.' + dothex
|
||||
strhex = strhex[:-16]
|
||||
|
||||
return '0x' + dothex
|
||||
|
||||
print('mul16', dothex(U16_MAX * 2 % (U16_MAX + 1)))
|
||||
print('mul32', dothex(U32_MAX * U16_MAX % (U32_MAX + 1)))
|
||||
print('mul_u16', dothex((U16_MAX * U8_MAX >> 128) %
|
||||
(U16_MAX + 1)), dothex(U16_MAX * U8_MAX % (U16_MAX + 1)))
|
||||
print('mul_u16', dothex((U16_MAX * U16_MAX >> 128) %
|
||||
(U16_MAX + 1)), dothex(U16_MAX * U16_MAX % (U16_MAX + 1)))
|
||||
print('mul_lo_u32', dothex(U32_MAX * U16_MAX % (U32_MAX + 1)))
|
||||
print('div_lo_u32', dothex(U32_MAX // U8_MAX), dothex(U32_MAX - U32_MAX // U8_MAX))
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -227,7 +227,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"dbscan = cuml.DBSCAN(eps=5000)eps=5000)\n",
|
||||
"dbscan = cuml.DBSCAN(eps=5000)\n",
|
||||
"# dbscan = cuml.DBSCAN(eps=10000)\n",
|
||||
"\n",
|
||||
"infected_df = gdf[gdf['infected'] == 1].reset_index()\n",
|
||||
|
||||
818
ds/25-1/2/4-02_find_infected.ipynb
Normal file
818
ds/25-1/2/4-02_find_infected.ipynb
Normal file
@ -0,0 +1,818 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<a href=\"https://www.nvidia.com/dli\"><img src=\"images/DLI_Header.png\" alt=\"Header\" style=\"width: 400px;\"/></a>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Week 1: Find Clusters of Infected People"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<span style=\"color:red\">\n",
|
||||
"**URGENT WARNING**\n",
|
||||
"\n",
|
||||
"We have been receiving reports from health facilities that a new, fast-spreading virus has been discovered in the population. To prepare our response, we need to understand the geospatial distribution of those who have been infected. Find out whether there are identifiable clusters of infected individuals and where they are. \n",
|
||||
"</span>\n",
|
||||
"\n",
|
||||
"Your goal for this notebook will be to estimate the location of dense geographic clusters of infected people using incoming data from week 1 of the simulated epidemic."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Imports"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 65,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"The cudf.pandas extension is already loaded. To reload it, use:\n",
|
||||
" %reload_ext cudf.pandas\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%load_ext cudf.pandas\n",
|
||||
"import pandas as pd\n",
|
||||
"import cuml\n",
|
||||
"\n",
|
||||
"import cupy as cp"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Load Data"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Begin by loading the data you've received about week 1 of the outbreak into a cuDF-accelerated pandas DataFrame. The data is located at `'./data/week1.csv'`. For this notebook you will only need the `'lat'`, `'long'`, and `'infected'` columns. Either drop the columns after loading, or use the `pd.read_csv` named argument `usecols` to provide a list of only the columns you need."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 66,
|
||||
"metadata": {
|
||||
"scrolled": true
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<div>\n",
|
||||
"<style scoped>\n",
|
||||
" .dataframe tbody tr th:only-of-type {\n",
|
||||
" vertical-align: middle;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe tbody tr th {\n",
|
||||
" vertical-align: top;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead th {\n",
|
||||
" text-align: right;\n",
|
||||
" }\n",
|
||||
"</style>\n",
|
||||
"<table border=\"1\" class=\"dataframe\">\n",
|
||||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>lat</th>\n",
|
||||
" <th>long</th>\n",
|
||||
" <th>infected</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>54.522511</td>\n",
|
||||
" <td>-1.571896</td>\n",
|
||||
" <td>False</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>54.554031</td>\n",
|
||||
" <td>-1.524968</td>\n",
|
||||
" <td>False</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>54.552483</td>\n",
|
||||
" <td>-1.435203</td>\n",
|
||||
" <td>False</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>54.537186</td>\n",
|
||||
" <td>-1.566215</td>\n",
|
||||
" <td>False</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>54.528210</td>\n",
|
||||
" <td>-1.588462</td>\n",
|
||||
" <td>False</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" lat long infected\n",
|
||||
"0 54.522511 -1.571896 False\n",
|
||||
"1 54.554031 -1.524968 False\n",
|
||||
"2 54.552483 -1.435203 False\n",
|
||||
"3 54.537186 -1.566215 False\n",
|
||||
"4 54.528210 -1.588462 False"
|
||||
]
|
||||
},
|
||||
"execution_count": 66,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('./data/week1.csv', dtype = {\n",
|
||||
" 'lat': 'float32',\n",
|
||||
" 'long': 'float32',\n",
|
||||
" 'infected': 'category',\n",
|
||||
"}, usecols = ['lat', 'long', 'infected'])\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Make Data Frame of the Infected"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Make a new DataFrame `infected_df` that contains only the infected members of the population.\n",
|
||||
"\n",
|
||||
"**Tip**: Reset the index of `infected_df` with `.reset_index(drop=True)`. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[28928759 28930512 28930904 ... 57410428 57411005 57411919]\n",
|
||||
"[ 0 1 2 ... 18145 18146 18147]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<div>\n",
|
||||
"<style scoped>\n",
|
||||
" .dataframe tbody tr th:only-of-type {\n",
|
||||
" vertical-align: middle;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe tbody tr th {\n",
|
||||
" vertical-align: top;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead th {\n",
|
||||
" text-align: right;\n",
|
||||
" }\n",
|
||||
"</style>\n",
|
||||
"<table border=\"1\" class=\"dataframe\">\n",
|
||||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>lat</th>\n",
|
||||
" <th>long</th>\n",
|
||||
" <th>infected</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>54.472767</td>\n",
|
||||
" <td>-1.654932</td>\n",
|
||||
" <td>True</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>54.529720</td>\n",
|
||||
" <td>-1.667143</td>\n",
|
||||
" <td>True</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>54.512981</td>\n",
|
||||
" <td>-1.589866</td>\n",
|
||||
" <td>True</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>54.522320</td>\n",
|
||||
" <td>-1.380694</td>\n",
|
||||
" <td>True</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>54.541656</td>\n",
|
||||
" <td>-1.613490</td>\n",
|
||||
" <td>True</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" lat long infected\n",
|
||||
"0 54.472767 -1.654932 True\n",
|
||||
"1 54.529720 -1.667143 True\n",
|
||||
"2 54.512981 -1.589866 True\n",
|
||||
"3 54.522320 -1.380694 True\n",
|
||||
"4 54.541656 -1.613490 True"
|
||||
]
|
||||
},
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"infected_df = df[df['infected'] == True]\n",
|
||||
"print(infected_df.index.values)\n",
|
||||
"\n",
|
||||
"infected_df = infected_df.reset_index(drop=True)\n",
|
||||
"\n",
|
||||
"print(infected_df.index.values)\n",
|
||||
"infected_df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Make Grid Coordinates for Infected Locations"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Provided for you in the next cell (which you can expand by clicking on the \"...\" and contract again after executing by clicking on the blue left border of the cell) is the lat/long to OSGB36 grid coordinates converter you used earlier in the workshop. Use this converter to create grid coordinate values stored in `northing` and `easting` columns of the `infected_df` you created in the last step."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"metadata": {
|
||||
"jupyter": {
|
||||
"source_hidden": true
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# https://www.ordnancesurvey.co.uk/docs/support/guide-coordinate-systems-great-britain.pdf\n",
|
||||
"\n",
|
||||
"def latlong2osgbgrid_cupy(lat, long, input_degrees=True):\n",
|
||||
" '''\n",
|
||||
" Converts latitude and longitude (ellipsoidal) coordinates into northing and easting (grid) coordinates, using a Transverse Mercator projection.\n",
|
||||
" \n",
|
||||
" Inputs:\n",
|
||||
" lat: latitude coordinate (N)\n",
|
||||
" long: longitude coordinate (E)\n",
|
||||
" input_degrees: if True (default), interprets the coordinates as degrees; otherwise, interprets coordinates as radians\n",
|
||||
" \n",
|
||||
" Output:\n",
|
||||
" (northing, easting)\n",
|
||||
" '''\n",
|
||||
" \n",
|
||||
" if input_degrees:\n",
|
||||
" lat = lat * cp.pi/180\n",
|
||||
" long = long * cp.pi/180\n",
|
||||
"\n",
|
||||
" a = 6377563.396\n",
|
||||
" b = 6356256.909\n",
|
||||
" e2 = (a**2 - b**2) / a**2\n",
|
||||
"\n",
|
||||
" N0 = -100000 # northing of true origin\n",
|
||||
" E0 = 400000 # easting of true origin\n",
|
||||
" F0 = .9996012717 # scale factor on central meridian\n",
|
||||
" phi0 = 49 * cp.pi / 180 # latitude of true origin\n",
|
||||
" lambda0 = -2 * cp.pi / 180 # longitude of true origin and central meridian\n",
|
||||
" \n",
|
||||
" sinlat = cp.sin(lat)\n",
|
||||
" coslat = cp.cos(lat)\n",
|
||||
" tanlat = cp.tan(lat)\n",
|
||||
" \n",
|
||||
" latdiff = lat-phi0\n",
|
||||
" longdiff = long-lambda0\n",
|
||||
"\n",
|
||||
" n = (a-b) / (a+b)\n",
|
||||
" nu = a * F0 * (1 - e2 * sinlat ** 2) ** -.5\n",
|
||||
" rho = a * F0 * (1 - e2) * (1 - e2 * sinlat ** 2) ** -1.5\n",
|
||||
" eta2 = nu / rho - 1\n",
|
||||
" M = b * F0 * ((1 + n + 5/4 * (n**2 + n**3)) * latdiff - \n",
|
||||
" (3*(n+n**2) + 21/8 * n**3) * cp.sin(latdiff) * cp.cos(lat+phi0) +\n",
|
||||
" 15/8 * (n**2 + n**3) * cp.sin(2*(latdiff)) * cp.cos(2*(lat+phi0)) - \n",
|
||||
" 35/24 * n**3 * cp.sin(3*(latdiff)) * cp.cos(3*(lat+phi0)))\n",
|
||||
" I = M + N0\n",
|
||||
" II = nu/2 * sinlat * coslat\n",
|
||||
" III = nu/24 * sinlat * coslat ** 3 * (5 - tanlat ** 2 + 9 * eta2)\n",
|
||||
" IIIA = nu/720 * sinlat * coslat ** 5 * (61-58 * tanlat**2 + tanlat**4)\n",
|
||||
" IV = nu * coslat\n",
|
||||
" V = nu / 6 * coslat**3 * (nu/rho - cp.tan(lat)**2)\n",
|
||||
" VI = nu / 120 * coslat ** 5 * (5 - 18 * tanlat**2 + tanlat**4 + 14 * eta2 - 58 * tanlat**2 * eta2)\n",
|
||||
"\n",
|
||||
" northing = I + II * longdiff**2 + III * longdiff**4 + IIIA * longdiff**6\n",
|
||||
" easting = E0 + IV * longdiff + V * longdiff**3 + VI * longdiff**5\n",
|
||||
"\n",
|
||||
" return(northing, easting)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 69,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<div>\n",
|
||||
"<style scoped>\n",
|
||||
" .dataframe tbody tr th:only-of-type {\n",
|
||||
" vertical-align: middle;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe tbody tr th {\n",
|
||||
" vertical-align: top;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead th {\n",
|
||||
" text-align: right;\n",
|
||||
" }\n",
|
||||
"</style>\n",
|
||||
"<table border=\"1\" class=\"dataframe\">\n",
|
||||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>lat</th>\n",
|
||||
" <th>long</th>\n",
|
||||
" <th>infected</th>\n",
|
||||
" <th>northing</th>\n",
|
||||
" <th>easting</th>\n",
|
||||
" <th>cluster</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>54.472767</td>\n",
|
||||
" <td>-1.654932</td>\n",
|
||||
" <td>True</td>\n",
|
||||
" <td>508670.609809</td>\n",
|
||||
" <td>422359.747233</td>\n",
|
||||
" <td>-1</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>54.529720</td>\n",
|
||||
" <td>-1.667143</td>\n",
|
||||
" <td>True</td>\n",
|
||||
" <td>515003.452959</td>\n",
|
||||
" <td>421538.534748</td>\n",
|
||||
" <td>-1</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>54.512981</td>\n",
|
||||
" <td>-1.589866</td>\n",
|
||||
" <td>True</td>\n",
|
||||
" <td>513167.311551</td>\n",
|
||||
" <td>426549.871569</td>\n",
|
||||
" <td>-1</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>54.522320</td>\n",
|
||||
" <td>-1.380694</td>\n",
|
||||
" <td>True</td>\n",
|
||||
" <td>514305.528712</td>\n",
|
||||
" <td>440081.234190</td>\n",
|
||||
" <td>-1</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>54.541656</td>\n",
|
||||
" <td>-1.613490</td>\n",
|
||||
" <td>True</td>\n",
|
||||
" <td>516349.193146</td>\n",
|
||||
" <td>425002.998690</td>\n",
|
||||
" <td>-1</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" lat long infected northing easting cluster\n",
|
||||
"0 54.472767 -1.654932 True 508670.609809 422359.747233 -1\n",
|
||||
"1 54.529720 -1.667143 True 515003.452959 421538.534748 -1\n",
|
||||
"2 54.512981 -1.589866 True 513167.311551 426549.871569 -1\n",
|
||||
"3 54.522320 -1.380694 True 514305.528712 440081.234190 -1\n",
|
||||
"4 54.541656 -1.613490 True 516349.193146 425002.998690 -1"
|
||||
]
|
||||
},
|
||||
"execution_count": 69,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"cupy_lat = cp.asarray(infected_df['lat'])\n",
|
||||
"cupy_long = cp.asarray(infected_df['long'])\n",
|
||||
"\n",
|
||||
"infected_df['northing'], infected_df['easting'] = latlong2osgbgrid_cupy(cupy_lat, cupy_long)\n",
|
||||
"infected_df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Find Clusters of Infected People"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Use DBSCAN to find clusters of at least 25 infected people where no member is more than 2000m from at least one other cluster member. Create a new column in `infected_df` which contains the cluster to which each infected person belongs."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 70,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"<cudf.core.groupby.groupby.DataFrameGroupBy object at 0x7f55ea949240>"
|
||||
]
|
||||
},
|
||||
"execution_count": 70,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"dbscan = cuml.DBSCAN(eps = 2000, min_samples = 25)\n",
|
||||
"infected_df['cluster'] = dbscan.fit_predict(infected_df[['northing', 'easting']])\n",
|
||||
"infected_df.groupby('cluster')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Find the Centroid of Each Cluster"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Use grouping to find the mean `northing` and `easting` values for each cluster identified above."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 71,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<div>\n",
|
||||
"<style scoped>\n",
|
||||
" .dataframe tbody tr th:only-of-type {\n",
|
||||
" vertical-align: middle;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe tbody tr th {\n",
|
||||
" vertical-align: top;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead th {\n",
|
||||
" text-align: right;\n",
|
||||
" }\n",
|
||||
"</style>\n",
|
||||
"<table border=\"1\" class=\"dataframe\">\n",
|
||||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>northing</th>\n",
|
||||
" <th>easting</th>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>cluster</th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>-1</th>\n",
|
||||
" <td>378094.622647</td>\n",
|
||||
" <td>401880.682473</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>397661.319575</td>\n",
|
||||
" <td>371410.021738</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>436475.527827</td>\n",
|
||||
" <td>332980.449214</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>347062.477357</td>\n",
|
||||
" <td>389386.823243</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>359668.552556</td>\n",
|
||||
" <td>379638.020362</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>391630.403390</td>\n",
|
||||
" <td>431158.137254</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>5</th>\n",
|
||||
" <td>386471.397432</td>\n",
|
||||
" <td>426559.085587</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>6</th>\n",
|
||||
" <td>434970.462486</td>\n",
|
||||
" <td>406985.278520</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>7</th>\n",
|
||||
" <td>412772.652344</td>\n",
|
||||
" <td>410069.663793</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>8</th>\n",
|
||||
" <td>415808.971615</td>\n",
|
||||
" <td>414713.750256</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>9</th>\n",
|
||||
" <td>417322.530166</td>\n",
|
||||
" <td>409583.737652</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>10</th>\n",
|
||||
" <td>334208.471668</td>\n",
|
||||
" <td>435937.777721</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>11</th>\n",
|
||||
" <td>300568.023792</td>\n",
|
||||
" <td>391901.514790</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>12</th>\n",
|
||||
" <td>291539.540205</td>\n",
|
||||
" <td>401640.663845</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>13</th>\n",
|
||||
" <td>289855.069902</td>\n",
|
||||
" <td>394518.295606</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" northing easting\n",
|
||||
"cluster \n",
|
||||
"-1 378094.622647 401880.682473\n",
|
||||
" 0 397661.319575 371410.021738\n",
|
||||
" 1 436475.527827 332980.449214\n",
|
||||
" 2 347062.477357 389386.823243\n",
|
||||
" 3 359668.552556 379638.020362\n",
|
||||
" 4 391630.403390 431158.137254\n",
|
||||
" 5 386471.397432 426559.085587\n",
|
||||
" 6 434970.462486 406985.278520\n",
|
||||
" 7 412772.652344 410069.663793\n",
|
||||
" 8 415808.971615 414713.750256\n",
|
||||
" 9 417322.530166 409583.737652\n",
|
||||
" 10 334208.471668 435937.777721\n",
|
||||
" 11 300568.023792 391901.514790\n",
|
||||
" 12 291539.540205 401640.663845\n",
|
||||
" 13 289855.069902 394518.295606"
|
||||
]
|
||||
},
|
||||
"execution_count": 71,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"centroids_df = infected_df[['northing', 'easting', 'cluster']].groupby('cluster').mean()\n",
|
||||
"centroids_df"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Find the number of people in each cluster by counting the number of appearances of each cluster's label in the column produced by DBSCAN."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 72,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"cluster\n",
|
||||
"-1 8451\n",
|
||||
" 0 8638\n",
|
||||
" 1 68\n",
|
||||
" 2 403\n",
|
||||
" 3 25\n",
|
||||
" 4 66\n",
|
||||
" 5 43\n",
|
||||
" 6 27\n",
|
||||
" 7 39\n",
|
||||
" 8 92\n",
|
||||
" 9 21\n",
|
||||
" 10 64\n",
|
||||
" 11 68\n",
|
||||
" 12 72\n",
|
||||
" 13 71\n",
|
||||
"dtype: int64"
|
||||
]
|
||||
},
|
||||
"execution_count": 72,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"infected_df.groupby(['cluster']).size()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Find the Centroid of the Cluster with the Most Members ##"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Use the cluster label for with the most people to filter `centroid_df` and write the answer to `my_assessment/question_1.json`. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 78,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"/opt/conda/lib/python3.10/site-packages/cudf/io/json.py:194: UserWarning: Using CPU via Pandas to write JSON dataset\n",
|
||||
" warnings.warn(\"Using CPU via Pandas to write JSON dataset\")\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"centroids_df.loc[0].to_json('my_assessment/question_1.json')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Check Submission ##"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 79,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"{\"northing\":397661.3195752321,\"easting\":371410.0217381102}"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"!cat my_assessment/question_1.json"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Tip**: Your submission file should contain one line of text, similar to: \n",
|
||||
"\n",
|
||||
"```\n",
|
||||
"{'northing':XXX.XX,'easting':XXX.XX}\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<div align=\"center\"><h2>Please Restart the Kernel</h2></div>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import IPython\n",
|
||||
"app = IPython.Application.instance()\n",
|
||||
"app.kernel.do_shutdown(True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<a href=\"https://www.nvidia.com/dli\"><img src=\"images/DLI_Header.png\" alt=\"Header\" style=\"width: 400px;\"/></a>"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.15"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
3718
ds/25-1/2/4-03_nearest_facilities.ipynb
Normal file
3718
ds/25-1/2/4-03_nearest_facilities.ipynb
Normal file
File diff suppressed because it is too large
Load Diff
1212
ds/25-1/2/4-04_identify_risk_factors.ipynb
Normal file
1212
ds/25-1/2/4-04_identify_risk_factors.ipynb
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user