forked from JayYu0116/MLIP_Lab6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prediction_demo.py
45 lines (35 loc) · 1.37 KB
/
prediction_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
def data_preparation(df: pd.DataFrame):
# select target column and feature columns
target = "price"
features = ["furnishingstatus"]
feature_df = pd.get_dummies(
df.loc[:,features],
columns=["furnishingstatus"])
target_series = df.loc[:,target]
return feature_df, target_series
def data_split(features: pd.DataFrame, target:pd.Series):
X_train, X_test, y_train, y_test = \
train_test_split(features,
target,
test_size=0.33,
random_state=42)
return X_train, X_test, y_train, y_test
def train_model(X_train: np.ndarray,
y_train: np.ndarray):
reg = LinearRegression().fit(X_train, y_train)
return reg
def eval_model(X_test: np.ndarray,
y_test: np.ndarray,
model: LinearRegression):
return model.score(X_test,y_test)
if __name__ == "__main__":
df_raw = pd.read_csv('Housing.csv')
feature_df, target_series = data_preparation(df_raw)
X_train, X_test, y_train, y_test = data_split(feature_df, target_series)
reg=train_model(X_train,y_train)
eval_score=eval_model(X_test,y_test,reg)
print(f"Trained model score is: {eval_score}")