-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatures.py
More file actions
165 lines (131 loc) · 6.27 KB
/
Copy pathfeatures.py
File metadata and controls
165 lines (131 loc) · 6.27 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from __future__ import annotations
from collections.abc import Iterable
from dataclasses import asdict, is_dataclass
from typing import Any
import numpy as np
import pandas as pd
from .config import FEATURE_COLUMNS, TARGET_COLUMN, resolve_raw_data_path
def load_raw_dataset(path: str | None = None) -> pd.DataFrame:
data_path = resolve_raw_data_path() if path is None else path
return pd.read_csv(data_path)
def compute_ppi(screen_resolution: str, screen_size: float) -> float:
x_res, y_res = [int(value) for value in screen_resolution.split("x")]
return float(np.sqrt((x_res**2) + (y_res**2)) / screen_size)
def categorize_cpu(cpu_value: str) -> str:
cpu_name = " ".join(cpu_value.split()[:3])
if cpu_name in {"Intel Core i7", "Intel Core i5", "Intel Core i3"}:
return cpu_name
if cpu_name.startswith("Intel"):
return "Other Intel Processor"
if cpu_name.startswith("AMD"):
return "AMD Processor"
return "Other Processor"
def categorize_os(os_value: str) -> str:
if os_value in {"Windows 10", "Windows 7", "Windows", "Windows 10 S"}:
return "Windows"
if os_value in {"Mac", "Mac OS X", "macOS"}:
return "Mac"
return "Linux_and_others"
def _parse_memory_components(memory_series: pd.Series) -> pd.DataFrame:
normalized = memory_series.astype(str).replace("\\.0", "", regex=True)
normalized = normalized.str.replace("GB", "", regex=False)
normalized = normalized.str.replace("TB", "000", regex=False)
split_memory = normalized.str.split("+", n=1, expand=True, regex=False)
first = split_memory[0].str.strip()
second = split_memory[1].fillna("0")
first_numeric = first.str.replace(r"\D", "", regex=True).replace("", "0").astype(int)
second_numeric = second.str.replace(r"\D", "", regex=True).replace("", "0").astype(int)
frame = pd.DataFrame(index=memory_series.index)
frame["HDD"] = (
first_numeric * first.str.contains("HDD").astype(int)
+ second_numeric * second.str.contains("HDD").astype(int)
)
frame["SSD"] = (
first_numeric * first.str.contains("SSD").astype(int)
+ second_numeric * second.str.contains("SSD").astype(int)
)
frame["Hybrid"] = (
first_numeric * first.str.contains("Hybrid").astype(int)
+ second_numeric * second.str.contains("Hybrid").astype(int)
)
frame["Flash_Storage"] = (
first_numeric * first.str.contains("Flash Storage").astype(int)
+ second_numeric * second.str.contains("Flash Storage").astype(int)
)
return frame
def build_modeling_dataframe(raw_df: pd.DataFrame) -> pd.DataFrame:
df = raw_df.copy()
if "Unnamed: 0" in df.columns:
df = df.drop(columns=["Unnamed: 0"])
df = df.drop_duplicates().reset_index(drop=True)
df["Ram"] = df["Ram"].str.replace("GB", "", regex=False).astype("int64")
df["Weight"] = df["Weight"].str.replace("kg", "", regex=False).astype("float64")
df["Touchscreen"] = df["ScreenResolution"].apply(lambda value: 1 if "Touchscreen" in value else 0)
df["Ips"] = df["ScreenResolution"].apply(lambda value: 1 if "IPS" in value else 0)
resolution_xy = df["ScreenResolution"].str.extract(r"(?P<x_res>\d+)x(?P<y_res>\d+)")
df["x_res"] = resolution_xy["x_res"].astype(int)
df["y_res"] = resolution_xy["y_res"].astype(int)
df["ppi"] = np.sqrt((df["x_res"] ** 2) + (df["y_res"] ** 2)) / df["Inches"]
df["Cpu_brand"] = df["Cpu"].apply(categorize_cpu)
memory_components = _parse_memory_components(df["Memory"])
df = pd.concat([df, memory_components], axis=1)
df["gpu_brand"] = df["Gpu"].str.split().str[0]
df = df[df["gpu_brand"] != "ARM"].copy()
df["os"] = df["OpSys"].apply(categorize_os)
keep_columns = FEATURE_COLUMNS + [TARGET_COLUMN]
modeling_df = df[keep_columns].copy()
modeling_df[TARGET_COLUMN] = modeling_df[TARGET_COLUMN].astype(float)
return modeling_df.reset_index(drop=True)
def build_training_matrices(modeling_df: pd.DataFrame) -> tuple[pd.DataFrame, pd.Series]:
X = modeling_df[FEATURE_COLUMNS].copy()
y = np.log(modeling_df[TARGET_COLUMN].copy())
return X, y
def extract_ui_options(raw_df: pd.DataFrame) -> dict[str, list[Any]]:
modeling_df = build_modeling_dataframe(raw_df)
resolution_options = sorted(
{
match
for match in raw_df["ScreenResolution"].str.extract(r"(\d+x\d+)", expand=False).dropna().tolist()
}
)
return {
"companies": sorted(modeling_df["Company"].unique().tolist()),
"types": sorted(modeling_df["TypeName"].unique().tolist()),
"ram_options": sorted(int(value) for value in modeling_df["Ram"].unique().tolist()),
"screen_resolutions": resolution_options,
"cpu_brands": sorted(modeling_df["Cpu_brand"].unique().tolist()),
"hdd_options": sorted(int(value) for value in modeling_df["HDD"].unique().tolist()),
"ssd_options": sorted(int(value) for value in modeling_df["SSD"].unique().tolist()),
"gpu_brands": sorted(modeling_df["gpu_brand"].unique().tolist()),
"os_options": sorted(modeling_df["os"].unique().tolist()),
}
def _to_mapping(record: Any) -> dict[str, Any]:
if isinstance(record, dict):
return record
if hasattr(record, "model_dump"):
return record.model_dump()
if is_dataclass(record):
return asdict(record)
raise TypeError(f"Unsupported prediction record type: {type(record)!r}")
def build_inference_dataframe(records: Iterable[Any]) -> pd.DataFrame:
rows = []
for record in records:
payload = _to_mapping(record)
rows.append(
{
"Company": payload["company"],
"TypeName": payload["type_name"],
"Ram": int(payload["ram"]),
"Weight": float(payload["weight"]),
"Touchscreen": int(bool(payload["touchscreen"])),
"Ips": int(bool(payload["ips"])),
"ppi": compute_ppi(payload["screen_resolution"], float(payload["screen_size"])),
"Cpu_brand": payload["cpu_brand"],
"HDD": int(payload["hdd"]),
"SSD": int(payload["ssd"]),
"gpu_brand": payload["gpu_brand"],
"os": payload["os"],
}
)
inference_df = pd.DataFrame(rows)
return inference_df[FEATURE_COLUMNS]