{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-08-05T22:05:51.187710Z","iopub.execute_input":"2022-08-05T22:05:51.189488Z","iopub.status.idle":"2022-08-05T22:05:51.195048Z","shell.execute_reply.started":"2022-08-05T22:05:51.189395Z","shell.execute_reply":"2022-08-05T22:05:51.193694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/precio-de-portatiles/train.csv\",index_col=\"id\")\ntrain = train.drop([\"laptop_ID\",\"Product\"],axis=1)\nprint(f'training exemples : {len(train)}')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T22:05:51.324138Z","iopub.execute_input":"2022-08-05T22:05:51.324566Z","iopub.status.idle":"2022-08-05T22:05:51.363142Z","shell.execute_reply.started":"2022-08-05T22:05:51.324527Z","shell.execute_reply":"2022-08-05T22:05:51.362305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"there is no reason to use the \"laptop_ID\" to predict the price, as well as \"Product\" given that it's a summary of the rest","metadata":{}},{"cell_type":"code","source":"train['Company'] = pd.Categorical(train.Company)\ntrain['TypeName'] = pd.Categorical(train.TypeName)\ntrain['OpSys'] = pd.Categorical(train.OpSys)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T22:05:51.452645Z","iopub.execute_input":"2022-08-05T22:05:51.453798Z","iopub.status.idle":"2022-08-05T22:05:51.464378Z","shell.execute_reply.started":"2022-08-05T22:05:51.453756Z","shell.execute_reply":"2022-08-05T22:05:51.463092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def res_to_num(string):\n    nums = string.split(\" \")[-1]\n    a,b = nums.split(\"x\")\n    return max(int(a),int(b))\n\ndef ram_to_num(string):\n    return int(string.split(\"G\")[0])\n\ndef weight_to_num(string):\n    return float(string.split(\"k\")[0])\n\ndef gb_tb_to_n(string):\n    if string[-2:] == \"GB\":\n        return float(string[:-2])\n    \n    if string[-2:] == \"TB\":\n        return float(string[:-2])*1000\n    return float(string)\n\ndef mem_to_num(string):\n    ssd,hdd,hyb = \"0\",\"0\",\"0\"\n    if \"SSD\" in string:\n        lists = string.split(\" \")\n        ssd = lists[lists.index(\"SSD\")-1]\n    if \"Flash Storage\" in string:\n        lists = string.split(\" \")\n        ssd = lists[lists.index(\"Flash\")-1]\n    if \"HDD\" in string:\n        lists = string.split(\" \")\n        hdd = lists[lists.index(\"HDD\")-1]\n    if \"Hybrid\" in string:\n        lists = string.split(\" \")\n        hyb = lists[lists.index(\"Hybrid\")-1]\n    if len(string.split(\"SSD\"))>2:\n        a,b,_ = string.split(\"SSD\")\n        a = a.split(\" \")[0]\n        b = b.split(\" \")[-2]\n        ssd = str(gb_tb_to_n(a) + gb_tb_to_n(b))+\"GB\"\n    if len(string.split(\"HDD\"))>2:\n        a,b,_ = string.split(\"HDD\")\n        a = a.split(\" \")[0]\n        b = b.split(\" \")[-2]\n        hdd = str(gb_tb_to_n(a) + gb_tb_to_n(b))+\"GB\"\n    if len(string.split(\"Hybrid\"))>2:\n        a,b,_ = string.split(\"Hybrid\")\n        a = a.split(\" \")[0]\n        b = b.split(\" \")[-2]\n        hyb = str(gb_tb_to_n(a) + gb_tb_to_n(b))+\"GB\"\n    return gb_tb_to_n(ssd),gb_tb_to_n(hdd),gb_tb_to_n(hyb)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-05T22:05:51.558383Z","iopub.execute_input":"2022-08-05T22:05:51.558844Z","iopub.status.idle":"2022-08-05T22:05:51.578304Z","shell.execute_reply.started":"2022-08-05T22:05:51.558802Z","shell.execute_reply":"2022-08-05T22:05:51.576789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def transform_ds(ds):\n    ssd,hdd,hyb = [],[],[]\n    for x in ds[\"Memory\"]:\n        s,h,hy = mem_to_num(x)\n        ssd.append(s)\n        hdd.append(h)\n        hyb.append(hy)\n    ds[\"SSD\"] = ssd\n    ds[\"HDD\"] = hdd\n    ds[\"HYB\"] = hyb\n    ds = ds.drop([\"laptop_ID\",\"Product\",\"Memory\"],axis=1)\n    ds['Company'] = pd.Categorical(ds.Company)\n    ds['TypeName'] = pd.Categorical(ds.TypeName)\n    ds['OpSys'] = pd.Categorical(ds.OpSys)\n    ds['Cpu'] = pd.Categorical(ds.Cpu)\n    ds['Gpu'] = pd.Categorical(ds.Gpu)\n    #ds['Memory'] = pd.Categorical(ds.Memory)\n    ds['ScreenResolution'] = [res_to_num(x) for x in ds.ScreenResolution]\n    ds['Ram'] = [ram_to_num(x) for x in ds.Ram]\n    ds['Weight'] = [weight_to_num(x) for x in ds.Weight]\n    return ds\n\ntrain = transform_ds(pd.read_csv(\"../input/precio-de-portatiles/train.csv\",index_col=\"id\"))\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T22:05:51.674779Z","iopub.execute_input":"2022-08-05T22:05:51.675778Z","iopub.status.idle":"2022-08-05T22:05:51.736914Z","shell.execute_reply.started":"2022-08-05T22:05:51.675713Z","shell.execute_reply":"2022-08-05T22:05:51.735610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Split data**","metadata":{}},{"cell_type":"code","source":"X = train.drop([\"Price_euros\"],axis=1)\ny = train[\"Price_euros\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-05T22:05:51.776408Z","iopub.execute_input":"2022-08-05T22:05:51.776903Z","iopub.status.idle":"2022-08-05T22:05:51.785205Z","shell.execute_reply.started":"2022-08-05T22:05:51.776854Z","shell.execute_reply":"2022-08-05T22:05:51.783502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model**","metadata":{}},{"cell_type":"code","source":"!pip3 install auto-sklearn > /dev/null","metadata":{"execution":{"iopub.status.busy":"2022-08-05T22:05:51.893668Z","iopub.execute_input":"2022-08-05T22:05:51.894125Z","iopub.status.idle":"2022-08-05T22:06:04.292022Z","shell.execute_reply.started":"2022-08-05T22:05:51.894085Z","shell.execute_reply":"2022-08-05T22:06:04.290600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install auto-sklearn > /dev/null","metadata":{"execution":{"iopub.status.busy":"2022-08-05T22:06:04.295304Z","iopub.execute_input":"2022-08-05T22:06:04.295865Z","iopub.status.idle":"2022-08-05T22:06:18.988614Z","shell.execute_reply.started":"2022-08-05T22:06:04.295786Z","shell.execute_reply":"2022-08-05T22:06:18.987445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try :\n    import autosklearn\nexcept :\n    try :\n        import autosklearn\n    except :\n        pass","metadata":{"execution":{"iopub.status.busy":"2022-08-05T22:06:18.990163Z","iopub.execute_input":"2022-08-05T22:06:18.990624Z","iopub.status.idle":"2022-08-05T22:06:18.996180Z","shell.execute_reply.started":"2022-08-05T22:06:18.990582Z","shell.execute_reply":"2022-08-05T22:06:18.995311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from autosklearn.regression import AutoSklearnRegressor\nfrom autosklearn.metrics import mean_absolute_error","metadata":{"execution":{"iopub.status.busy":"2022-08-05T22:06:18.999841Z","iopub.execute_input":"2022-08-05T22:06:19.000303Z","iopub.status.idle":"2022-08-05T22:06:19.009518Z","shell.execute_reply.started":"2022-08-05T22:06:19.000269Z","shell.execute_reply":"2022-08-05T22:06:19.008204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"automl2 = AutoSklearnRegressor(\n    time_left_for_this_task=3*60*60, #3h\n    per_run_time_limit=60*4,\n    n_jobs=4,\n    metric=mean_absolute_error\n    )","metadata":{"execution":{"iopub.status.busy":"2022-08-05T22:06:19.010731Z","iopub.execute_input":"2022-08-05T22:06:19.011594Z","iopub.status.idle":"2022-08-05T22:06:19.021251Z","shell.execute_reply.started":"2022-08-05T22:06:19.011551Z","shell.execute_reply":"2022-08-05T22:06:19.020073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"automl2.fit(X, y)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T22:06:19.024475Z","iopub.execute_input":"2022-08-05T22:06:19.024940Z","iopub.status.idle":"2022-08-05T22:16:17.040737Z","shell.execute_reply.started":"2022-08-05T22:06:19.024890Z","shell.execute_reply":"2022-08-05T22:16:17.039851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"automl2.leaderboard(detailed=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T22:16:17.042013Z","iopub.execute_input":"2022-08-05T22:16:17.042795Z","iopub.status.idle":"2022-08-05T22:16:17.083573Z","shell.execute_reply.started":"2022-08-05T22:16:17.042758Z","shell.execute_reply":"2022-08-05T22:16:17.081581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(automl2.sprint_statistics())","metadata":{"execution":{"iopub.status.busy":"2022-08-05T22:16:17.086540Z","iopub.execute_input":"2022-08-05T22:16:17.087164Z","iopub.status.idle":"2022-08-05T22:16:17.120531Z","shell.execute_reply.started":"2022-08-05T22:16:17.087110Z","shell.execute_reply":"2022-08-05T22:16:17.119162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"automl2.performance_over_time_.plot(\n        x='Timestamp',\n        kind='line',\n        legend=True,\n        title='Auto-sklearn accuracy over time',\n        grid=True,\n    );","metadata":{"execution":{"iopub.status.busy":"2022-08-05T22:16:17.122215Z","iopub.execute_input":"2022-08-05T22:16:17.122589Z","iopub.status.idle":"2022-08-05T22:16:17.625568Z","shell.execute_reply.started":"2022-08-05T22:16:17.122555Z","shell.execute_reply":"2022-08-05T22:16:17.624408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Pred / sub**","metadata":{}},{"cell_type":"code","source":"test = transform_ds(pd.read_csv(\"../input/precio-de-portatiles/test.csv\",index_col=\"id\"))\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T22:16:17.628931Z","iopub.execute_input":"2022-08-05T22:16:17.629683Z","iopub.status.idle":"2022-08-05T22:16:17.670766Z","shell.execute_reply.started":"2022-08-05T22:16:17.629640Z","shell.execute_reply":"2022-08-05T22:16:17.669412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = automl2.predict(test)\ntest[\"Price_euros\"] = pred\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T22:16:17.672415Z","iopub.execute_input":"2022-08-05T22:16:17.672921Z","iopub.status.idle":"2022-08-05T22:16:18.105130Z","shell.execute_reply.started":"2022-08-05T22:16:17.672883Z","shell.execute_reply":"2022-08-05T22:16:18.103675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = test[\"Price_euros\"]\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T22:16:18.107579Z","iopub.execute_input":"2022-08-05T22:16:18.108557Z","iopub.status.idle":"2022-08-05T22:16:18.123327Z","shell.execute_reply.started":"2022-08-05T22:16:18.108496Z","shell.execute_reply":"2022-08-05T22:16:18.121107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-05T22:16:18.126600Z","iopub.execute_input":"2022-08-05T22:16:18.127740Z","iopub.status.idle":"2022-08-05T22:16:18.141948Z","shell.execute_reply.started":"2022-08-05T22:16:18.127674Z","shell.execute_reply":"2022-08-05T22:16:18.140087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}