auta-public/linear-regression.py

79 lines
2.6 KiB
Python
Raw Normal View History

2021-05-06 12:41:21 +02:00
import pandas as pd
2021-05-12 15:59:34 +02:00
import plotly.express as px
2021-05-06 12:41:21 +02:00
from pandas import DataFrame
from sklearn import preprocessing
2021-05-12 15:59:34 +02:00
from sklearn.linear_model import LinearRegression, Ridge, RidgeCV
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import PolynomialFeatures
2021-05-06 12:41:21 +02:00
col_names = ["price", "mileage", "year", "brand", "engine_type", "engine_cap"]
col_names_in = ["mileage", "year", "brand", "engine_type", "engine_cap"]
2021-05-12 15:59:34 +02:00
df_train = pd.read_csv(
"train/train.tsv", error_bad_lines=False, header=None, sep="\t", names=col_names
)
df = df_train
dev0 = pd.read_csv(
"dev-0/in.tsv", error_bad_lines=False, header=None, sep="\t", names=col_names_in
)
testA = pd.read_csv(
"test-A/in.tsv", error_bad_lines=False, header=None, sep="\t", names=col_names_in
)
test = pd.read_csv("dev-0/expected.tsv", error_bad_lines=False, header=None, sep="\t")
Y_a = test[0]
2021-05-06 12:41:21 +02:00
2021-05-12 15:59:34 +02:00
brands = df.brand.value_counts()[:35].index.tolist()
2021-05-06 12:41:21 +02:00
2021-05-12 15:59:34 +02:00
def preprocess_data(df: DataFrame, brands: list) -> DataFrame:
2021-05-06 12:41:21 +02:00
"""Prepare dataset to linear regression"""
2021-05-12 15:59:34 +02:00
df.brand = df.brand.apply(lambda x: x if x in brands else "0")
df["year"] = df.year / 2000
df["mileage"] = df.mileage ** 0.3
df["engine_cap"] = df.engine_cap * 0.3
df["brand"] = df["brand"].str.lower()
scaler = preprocessing.RobustScaler()
df = pd.get_dummies(df, columns=["brand", "engine_type"])
# takes 1k rmse more ;(
df[["mileage", "year", "engine_cap", "year"]] = scaler.fit_transform(
df[["mileage", "year", "engine_cap", "year"]]
)
poly = PolynomialFeatures(2, interaction_only=True)
df = poly.fit_transform(df)
2021-05-06 12:41:21 +02:00
return df
2021-05-12 15:59:34 +02:00
indexes = df_train[(df_train.price < 1000) & (df_train.price > 1)].index
df_train.drop(indexes, inplace=True)
2021-05-06 12:41:21 +02:00
2021-05-12 15:59:34 +02:00
index = df_train[(df_train.mileage > 900000)].index
df_train.drop(index, inplace=True)
2021-05-06 12:41:21 +02:00
Y_train = df_train["price"]
2021-05-12 15:59:34 +02:00
df_train.drop("price", axis=1, inplace=True)
# df_train = df_train[df_train.price not in range (2, 1000)]
df_train = preprocess_data(df_train, brands)
dev0 = preprocess_data(dev0, brands)
testA = preprocess_data(testA, brands)
# fig = px.imshow(df_train.corr())
# fig.show()
2021-05-06 12:41:21 +02:00
lm_model = LinearRegression()
2021-05-12 15:59:34 +02:00
# clf = RidgeCV(alphas=[0.1, 0.01, 0.001, 0.00001, 1e-1], cv=10, fit_intercept=True, normalize=True)
# clf.fit(df_train, Y_train)
lm_model.fit(df_train, Y_train)
dev0_predicted = lm_model.predict(dev0)
testA_predicted = lm_model.predict(testA)
# dev0_predicted2 = clf.predict(dev0)
2021-05-06 12:41:21 +02:00
pd.Series(dev0_predicted).to_csv("dev-0/out.tsv", sep="\t", index=False, header=False)
pd.Series(testA_predicted).to_csv("test-A/out.tsv", sep="\t", index=False, header=False)
2021-05-12 15:59:34 +02:00
print(mean_squared_error(Y_a, dev0_predicted, squared=False))