위도 및 경도 Feature Engineering 케이스 정리

Data Setup

import polars as pl  
from xgboost import XGBRegressor  
from sklearn.linear_model import Ridge  
  
  
# Data taken from https://www.openml.org/search?type=data&id=43093  
df = (  
	pl.scan_csv("../data/miami-housing.csv")  
	.with_columns([  
		pl.col("SALE_PRC").alias("price"),  
		pl.col(["LATITUDE", "LONGITUDE"]).name.to_lowercase()  
	])  
	.select(pl.col(["latitude", "longitude", "price"]))  
)

TRAIN_TEST_SPLIT_FRACTION = 0.8  
  
df = (  
	df  
	  
	# Shuffle the data to avoid any issues from the data being pre-ordered...  
	.sample(fraction=1, shuffle=True)  
	  
	# ...then use row numbers as an index for separating train and test.  
	.with_row_count(name="row_number")  
	.with_columns([  
	(pl.col("row_number") < TRAIN_TEST_SPLIT_FRACTION * len(df)).alias("is_train")  
	])  
)

Feature Engineering

  • Raw Latitude and Longitude
  • Spatial Density
    • Population density is correlated with many demographic processes, and this is certainly true for rental prices and incomes.
    • Approach
      • One could use many methods for measuring the spatial density around a home: counting the number of other home sales within some radius of each home sale
      • or computing and sampling from a Kernel Density Estimate over home sale locations
      • or even pulling third party census data about population density.
    • Case(scipy's CKDTree 이용해서 Density Feature 생성)

def add_density_feature_columns_to_dataframe(geo_df: pl.DataFrame) -> pl.DataFrame:  
	tree = spatial.cKDTree(df.select(["latitude", "longitude"]))  
	result = geo_df.with_columns(  
	pl.Series(  
	"spatial_density",  
	tree.query_ball_point(geo_df.select(["latitude", "longitude"]), .005, return_length=True)  
	)  
	)  
	return result  
  
  
df_w_density = add_density_feature_columns_to_dataframe(df)
  • Geohash Target Encoding(Ref)
    • It’s a known fact — some neighborhoods are more expensive than others. So, it’s possible that giving information to the model about each home’s neighborhood (and the sale price that can be expected in that neighborhood) can add predictive power.
    • A neighborhood can be anything — a zip-code, a street, or in our case, a Geohash.
def add_geohash_column_to_df(geo_df: pl.DataFrame) -> pl.DataFrame:  
	result = (  
	df  
		.with_columns(  
			df  
				.select("latitude", "longitude")  
				.map_rows(  
					lambda x: geohash2.encode(x[0], x[1], precision=5),  
					return_dtype=pl.Utf8  
			)  
		.rename({"map": "geohash"})  
		)  
	)  
	return result  
  
def add_target_encoding_to_df(  
		dataframe: pl.DataFrame,  
		categorical_column: str = "geohash"  
	) -> pl.DataFrame:  
		category_target_means = (  
		dataframe  
		.filter(pl.col("is_train")) # Only include train data to prevent test data leakage.  
		.group_by(categorical_column)  
		.agg(  
			pl.col(MODEL_TARGET).mean().alias(f"{categorical_column}_{MODEL_TARGET}_mean")  
		)  
	)  
	result = (  
		dataframe  
		.join(  
			category_target_means,  
			how="left",  
			on=categorical_column  
		)  
	)  
	return result  
  
df_w_geohash = add_geohash_column_to_df(df)  
df_w_geohash_target_encoded = add_target_encoding_to_df(df_w_geohash)

Reference

Read more

DataFrame은 Pandera로, 모델은 Pydantic으로 데이터를 검증한다.

DataFrame은 Pandera로, 모델은 Pydantic으로 데이터를 검증한다.

Pandera: 데이터프레임 검증에 최적화된 도구 주요 장점 * Pandas와 통합: Pandas 데이터프레임에 대해 스키마 기반 검증을 수행합니다. * 유연한 검증 조건: 열 데이터 타입, 값 범위, Null 여부 등 다양한 검증 조건을 정의할 수 있습니다. * 명확한 오류 메시지: 스키마 불일치에 대한 명확한 오류 메시지를 제공합니다. 단점 * 대용량 데이터 검증에서는 속도가 느릴 수

Tobit Regression은 Censored Data에 적합한 Regression이다.

Tobit Regression은 Censored Data에 적합한 Regression이다.

Tobit Regression * Tobit 회귀(Tobit Regression)는 종속 변수가 특정 값에서 절단(Censored)된 상황에서 데이터를 분석하기 위해 사용되는 통계 기법입니다. * James Tobin이 처음 제안한 이 모델은 경제학과 사회과학 분야에서 자주 사용되며, 일반 선형 회귀로는 설명할 수 없는 상황에서 효과적으로 적용할 수 있습니다. Tobit Regression 수식 1. 관측된 종속 변수