सेंट्रॉइड की गणना
बाउंडिंग बॉक्स का आकार एक सिटी ब्लॉक से लेकर पूरे राज्य या यहाँ तक कि किसी देश तक हो सकता है. सरलता के लिए, इन डेटा को संभालने का एक तरीका यह है कि बाउंडिंग बॉक्स को एक तथाकथित सेंट्रॉइड में बदल दें, यानी बाउंडिंग बॉक्स का केंद्र. सेंट्रॉइड की गणना सीधी है — हम latitude और longitude से बनी लाइनों के मध्यबिंदु निकालते हैं.
numpy को np के रूप में इम्पोर्ट किया गया है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में सोशल मीडिया डेटा का विश्लेषण
अभ्यास निर्देश
- place JSON से पहले सेट के कोऑर्डिनेट्स प्राप्त करें.
- लॉन्गिट्यूड की लिस्ट को जोड़कर और दो से भाग देकर केंद्रीय लॉन्गिट्यूड निकालें.
- यही कदम latitude के लिए दोहराएँ.
placeकॉलम परcalculateCentroid()फंक्शन अप्लाई करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
def calculateCentroid(place):
""" Calculates the centroid from a bounding box."""
# Obtain the coordinates from the bounding box.
coordinates = place[____][____][0]
longs = np.unique( [x[0] for x in coordinates] )
lats = np.unique( [x[1] for x in coordinates] )
if len(longs) == 1 and len(lats) == 1:
# return a single coordinate
return (longs[0], lats[0])
elif len(longs) == 2 and len(lats) == 2:
# If we have two longs and lats, we have a box.
central_long = ____.____(____) / ____
central_lat = ____.____(____) / ____
else:
raise ValueError("Non-rectangular polygon not supported: %s" %
",".join(map(lambda x: str(x), coordinates)) )
return (central_long, central_lat)
# Calculate the centroids of place
centroids = tweets_sotu[____].apply(____)