Created
March 21, 2023 10:45
-
-
Save joonas-yoon/46b662cbe47d7ccf6ddfe3ee5b17b4b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# %% | |
import pandas as pd | |
import seaborn as sns | |
from sklearn.model_selection import train_test_split | |
from sklearn.preprocessing import StandardScaler | |
from sklearn.linear_model import LinearRegression | |
from sklearn.metrics import r2_score | |
# %% | |
df = pd.read_csv('./vertex_latlng.csv') | |
df.head(5) | |
# %% | |
df_train_full, df_test = train_test_split(df[['x', 'y', 'lat', 'lng']], test_size=0.2) | |
df_train, df_valid = train_test_split(df_train_full, test_size=0.3) | |
# %% | |
scaler = StandardScaler() | |
scaler.fit(df_train[['x', 'y']]) | |
x_train = scaler.transform(df_train[['x', 'y']]) | |
# %% | |
def fit_model(y_true): | |
lr = LinearRegression() | |
lr.fit(x_train, y_true) | |
return lr | |
def predict(lr, x_feat): | |
x_input = scaler.transform(x_feat) | |
return lr.predict(x_input) | |
# %% | |
lr_lng = fit_model(df_train['lng']) | |
lr_lat = fit_model(df_train['lat']) | |
lng_pred = predict(lr_lng, df_valid[['x', 'y']]) | |
lat_pred = predict(lr_lat, df_valid[['x', 'y']]) | |
r2_score(df_valid['lng'], lng_pred), r2_score(df_valid['lat'], lat_pred) | |
# %% | |
lng_pred = predict(lr_lng, df_test[['x', 'y']]) | |
lat_pred = predict(lr_lat, df_test[['x', 'y']]) | |
df_result = pd.DataFrame({ | |
'x': df_test['x'], | |
'y': df_test['y'], | |
'lat': lat_pred, | |
'lng': lng_pred | |
}) | |
df_result.to_csv('transformed.csv', index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment