การจัดการรายการ

Oct 01 2020

ฉันมีชุดข้อมูล (ข้อมูลดาวเทียม) - นี่คือลิงค์:

https://drive.google.com/drive/folders/1qBOROSDHSRMykZjWgP7iRMtpQ3gBMceL?usp=sharing

หากต้องการอ่านส่วนหนึ่งของชุดข้อมูลให้ใช้:

LISTA = FileNames[]; (* Here you assume you have your files in one specific Directory and list them*)

data1 = Import[
   LISTA[[1]], {"Datasets", "/PRODUCT/methane_mixing_ratio"}];

การตรวจสอบโครงสร้างข้อมูลจะได้รับ

Dimensions[data1]

{1, 4173, 215}

ฉันต้องการลบพื้นหลังบางส่วน (หรือข้อมูล NaN) และใส่ 0 (ศูนย์)

Newdata1 = Flatten[ReplaceAll[data1, s_ /; s > 5000 -> 0]]

เมื่อดำเนินการเสร็จสิ้นอย่างไรก็ตามโครงสร้างข้อมูลจะถูก "ทำลาย" และกลายเป็นเพียงรายการองค์ประกอบเดียวที่มีขนาดใหญ่

Dimensions[Newdata1]

{897195}

มีวิธีแทนที่ตัวเลขเหล่านั้นและรักษาโครงสร้างไว้ (1 x 4173 x 214) หรือไม่?

ฉันได้ลองทำโดยไม่ใช้ตัวเลือก Flatten แต่ไม่ได้ผลฉันได้เห็นคำถามบางข้อที่นี่และนี่อาจเป็นกรณีของปัญหารายการซ้อนกัน แต่ฉันไม่แน่ใจว่าจะแก้ปัญหาได้อย่างไร

คำตอบ

1 user1066 Oct 01 2020 at 02:03

กรณีสำหรับClip?

Clip[RandomReal[10000, {3, 4, 5}],{5000,Infinity},{0,Infinity}]
1 kglr Oct 31 2020 at 02:20

นอกจากนี้คุณยังสามารถใช้ซึ่งสร้างความประหลาดใจของฉันจะเร็วกว่าThresholdClip

SeedRandom[1]
data1 = RandomReal[10000, {1, 4173, 215}];

การแทนที่ตัวเลขที่มากกว่า 5000 ด้วย 0:

t1a = First @ RepeatedTiming[
    res1a = Subtract[data1, Threshold[data1, 5000 + $MachineEpsilon]]];
t2a = First @ RepeatedTiming[res2a = Clip[data1, {0, 5000}, {0, 0}]];
t3a = First @ RepeatedTiming[res3a = data1 UnitStep[Subtract[5000, data1]]];
t4a = First @ RepeatedTiming[res4a = Replace[data1, s_ /; s > 5000 -> 0., All]];
t5a = First @ RepeatedTiming[res5a = ReplaceAll[data1, s_ /; s > 5000 -> 0.]];

res1a == res2a == res3a == res4a == res5a
True
Dimensions @ res1a
 {1, 4173, 215}

การแทนที่ตัวเลขน้อยกว่า 5000 ด้วย 0:

t1b = First @ RepeatedTiming[res1b = Threshold[data1, 5000 + $MachineEpsilon]];
t2b = First @ RepeatedTiming[res2b = Clip[data1, {5000, ∞}, {0, ∞}]];
t3b = First @ RepeatedTiming[res3b = data1 UnitStep[Subtract[data1, 5000]]];
t4b = First @ RepeatedTiming[res4b = Replace[data1, s_ /; s < 5000 -> 0., All]];
t5b = First @ RepeatedTiming[res5b = ReplaceAll[data1, s_ /; s < 5000 -> 0.]];

res1b == res2b == res3b == res4b == res5b
True
Dimensions @ res1b
 {1, 4173, 215}

การเปรียบเทียบเวลา

{timingsa, timingsb} = {{t1a, t2a, t3a, t4a, t5a}, {t1b, t2b, t3b, t4b, t5b}};

functionsa = {"Subtract[data1,Threshold[data1,5000+$MachineEpsilon]]", "Clip[data1,{0,5000},{0,0}]", "data1 UnitStep[Subtract[5000,data1]]", "Replace[data1,s_/;s>5000\[Rule]0., All]", "ReplaceAll[data1,s_/;s>5000\[Rule]0.]"}; functionsb = {"Threshold[data1,5000+$MachineEpsilon]", 
   "Clip[data1,{5000, Infinity},{0,Infinity}]", 
   "data1 UnitStep[Subtract[data1,5000]]", 
   "Replace[data1,s_/;s<5000\[Rule]0., All]", 
   "ReplaceAll[data1,s_/;s<5000\[Rule]0.]"};

Grid[Join[{{Item["Replace numbers greater than 5000 with 0", 
     Background -> LightBlue], SpanFromLeft}}, 
  Transpose[{functionsa, timingsa}], 
 {{Item["Replace numbers less than 5000 with 0", 
     Background -> LightBlue], SpanFromLeft}}, 
  Transpose[{functionsb, timingsb}]], 
 Alignment -> {{Left, "."}, Center}, Dividers -> All]