<jupyter_start><jupyter_text>Inverse distance weighting (IDW) - One of Geographic information system interpolation- Goal : (latitude, longitude, air pollution <- interpolation!)  1. Import Packages<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import idw
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
import os<jupyter_output><empty_output><jupyter_text>2. Ground Truth<jupyter_code>dataset = pd.read_csv("air_pollution_new/Hour_1.csv", header = None)
data = dataset.values[:,:]

latitude = data[:,1]
longitude = data[:,2]
z = data[:,3]
coo = []

for i in range(len(z)):
    coo.append([latitude[i], longitude[i]])
zz = []
for i in range(len(z)):
    zz.append([z[i]])
coo = np.array(coo)
zz = np.array(zz)
coo1 = np.array(coo[:,0])
coo2 = np.array(coo[:,1])
print(coo.shape)
print(coo1.shape)
print(coo2.shape)
print(zz.shape)
# create sample points with structured scores
X1 = coo
z1 = zz
print("Shape :", X1.shape)
print("MAX :", np.max(X1))
print("MIN :", np.min(X1))<jupyter_output>Shape : (314, 2)
MAX : 1271.7
MIN : 33.256<jupyter_text>3. Train and Test<jupyter_code>idw_tree = idw.tree(X1, z1)
new_lat = np.linspace(34,37.5,100)
new_lon = np.linspace(126.5,130,100)

print(new_lat.shape)
print(new_lon.shape)
X2 = np.meshgrid(new_lat, new_lon)
np.shape(X2)
grid_shape = X2[0].shape
X2 = np.reshape(X2, (2, -1)).T
X2.shape
z2 = idw_tree(X2)
print(X2.shape)
print(z2.shape)<jupyter_output>(10000, 2)
(10000,)<jupyter_text>4. Results<jupyter_code>plt.rcParams["figure.figsize"] = (12,12)
fig = plt.figure()
ax1 = fig.gca(projection = '3d')
ax1.scatter(coo2,coo1, z1)
ax1.axis([126, 130, 34, 38])

plt.title("Air pollution data Ground Truth", fontsize=20)
plt.savefig("fig4.jpg")
plt.show()
X21, X22 = np.meshgrid(new_lat, new_lon)

plt.rcParams["figure.figsize"] = (12,12)
fig = plt.figure()
ax3 = fig.gca(projection = '3d')
ax3.plot_surface(X22, X21 , z2.reshape(grid_shape))

plt.title("Air pollution data after IDW Reconstruction", fontsize=20)
# plt.savefig("fig5.jpg")
plt.show()<jupyter_output><empty_output><jupyter_text>5. Make Dataset<jupyter_code>print(X21.shape)
print(X22.shape)
print(z2.reshape(grid_shape).shape)
np.savetxt('Interpolation_air_pollution/Hour1_inter_lat.txt', X21)
np.savetxt('Interpolation_air_pollution/Hour1_inter_lon.txt', X22)
np.savetxt('Interpolation_air_pollution/Hour1_inter_p.txt', z2.reshape(grid_shape))<jupyter_output><empty_output><jupyter_text>6. Save for 24 hours<jupyter_code>for hour in range(24) :
    path = os.path.join(os.getcwd(), 'air_pollution_new', 'Hour_{}'.format(hour+1) + '.csv')
    dataset = pd.read_csv(path, encoding = 'cp949', header = None)
    data = dataset.values[:,:]

    latitude = data[:,1]
    longitude = data[:,2]
    z = data[:,3]
    
    coo = []

    for i in range(len(z)):
        coo.append([latitude[i], longitude[i]])
        
    zz = []
    for i in range(len(z)):
        zz.append([z[i]])
    
    coo = np.array(coo)
    zz = np.array(zz)
    coo1 = np.array(coo[:,0])
    coo2 = np.array(coo[:,1])
    
    X1 = coo
    z1 = zz
    
    idw_tree = idw.tree(X1, z1)
    
#     new_lat = np.arange(34, 37.51, 0.0351)
#     new_lon = np.arange(126, 130.001, 0.0403)  
    new_lat = np.linspace(34,37.5,100)
    new_lon = np.linspace(126.5,130,100)

    
    X21, X22 = np.meshgrid(new_lat, new_lon)
    
    X2 = np.meshgrid(new_lat, new_lon)
    grid_shape = X2[0].shape
    X2 = np.reshape(X2, (2, -1)).T
    z2 = idw_tree(X2)
    
    print(z2)
    np.savetxt('Interpolation_air_pollution/' + 'Hour_{}'.format(hour+1) + '_inter_lat.txt', X21)
    np.savetxt('Interpolation_air_pollution/' + 'Hour_{}'.format(hour+1) + '_inter_lon.txt', X22)
    np.savetxt('Interpolation_air_pollution/' + 'Hour_{}'.format(hour+1) + '_inter_p.txt', z2.reshape(grid_shape))<jupyter_output>[30.88866497 29.82948917 29.62499847 ... 53.21792513 53.36336991
 53.50597942]
[32.0601678  31.17615205 27.51836312 ... 46.57736466 46.7050854
 46.83119597]
[27.34881286 26.93492746 23.70147327 ... 40.20395264 40.11906613
 40.03910194]
[23.01460683 22.98391465 22.65177818 ... 27.60187573 27.51546952
 27.43440579]
[31.44004912 31.00970781 29.47737179 ... 20.90313207 20.82426913
 20.75015788]
[39.82605578 38.96356338 38.6339731  ... 29.1240884  29.06512453
 29.00756421]
[43.52460513 42.7281445  39.50130908 ... 47.36784082 47.4619871
 47.54659537]
[50.67773784 49.35720517 45.95777592 ... 53.59013595 53.70689907
 53.81401095]
[45.0356591  44.69150797 48.6628944  ... 57.54774108 57.66573698
 57.77481353]
[52.38220245 52.05294515 54.0358825  ... 56.94321502 56.94736629
 56.94943891]
[62.03203982 61.356747   64.00091572 ... 55.60896233 55.54084971
 55.47385208]
[58.88097738 58.85538533 56.4824386  ... 69.81669981 69.88527085
 69.94991849]
[66.95718397 66.65404766 64.24547366 ... 73.07792082 7[...]
{'fraction_non_alphanumeric': 0.16614615865084323, 'fraction_numerical': 0.20195711013949616, 'mean_word_length': 4.256017505470459, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '646514', 'license': 'MIT', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2137, 'n_words': 350, 'path': '3_Vector_interpolation_with_wind_and_air_pollution_dataset/Inverse_distance_weighting_(IDW)_with_air_pollution.ipynb', 'repo_name': 'OH-Seoyoung/Forecasting_on_air_pollution_with_Numerical_modeling_and_ML', 'size': '4803'}