{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":10418,"databundleVersionId":862236,"sourceType":"competition"},{"sourceId":4326674,"sourceType":"datasetVersion","datasetId":2547995}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#P2.1导入库\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport imageio\nfrom os import listdir\n%matplotlib inline","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-22T06:06:29.511557Z","iopub.execute_input":"2024-11-22T06:06:29.512658Z","iopub.status.idle":"2024-11-22T06:06:29.519381Z","shell.execute_reply.started":"2024-11-22T06:06:29.512599Z","shell.execute_reply":"2024-11-22T06:06:29.518017Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import sys\ndata_path='/kaggle/input/human-protein-atlas-image-classification'\npath = '../input/mlcoursechapter2/chapter2'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-22T06:06:29.521584Z","iopub.execute_input":"2024-11-22T06:06:29.522028Z","iopub.status.idle":"2024-11-22T06:06:29.531733Z","shell.execute_reply.started":"2024-11-22T06:06:29.521984Z","shell.execute_reply":"2024-11-22T06:06:29.530559Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#P2.2 读入train.csv，观察训练集\ntrain=pd.read_csv(f\"{data_path}/train.csv\")\nprint('训练集维度：{0}'.format(train.shape))\ntrain.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-22T06:06:29.533149Z","iopub.execute_input":"2024-11-22T06:06:29.533769Z","iopub.status.idle":"2024-11-22T06:06:29.590785Z","shell.execute_reply.started":"2024-11-22T06:06:29.533633Z","shell.execute_reply":"2024-11-22T06:06:29.589623Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"files=listdir(f\"{data_path}/train\")\nprint('train目录下共有{0}个图像文件'.format(len(files)))\nfor n in range(8):\n    print(files[n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-22T06:06:29.592824Z","iopub.execute_input":"2024-11-22T06:06:29.593158Z","iopub.status.idle":"2024-11-22T06:06:30.865122Z","shell.execute_reply.started":"2024-11-22T06:06:29.593124Z","shell.execute_reply":"2024-11-22T06:06:30.864055Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def load_image(basepath,image_id):\n    images=np.zeros(shape=(4,512,512))\n    images[0,:,:]=imageio.imread(basepath+image_id+\"_green\"+\".png\")\n    images[1,:,:]=imageio.imread(basepath+image_id+\"_blue\"+\".png\")\n    images[2,:,:]=imageio.imread(basepath+image_id+\"_red\"+\".png\")\n    images[3,:,:]=imageio.imread(basepath+image_id+\"_yellow\"+\".png\")\n    return images\nfig,ax=plt.subplots(1,4,figsize=(20,10))\nimages=load_image(f\"{data_path}/train/\",train['Id'][1])\nax[0].imshow(images[0],cmap=\"Greens\")\nax[1].imshow(images[1],cmap=\"Blues\")\nax[2].imshow(images[2],cmap=\"Reds\")\nax[3].imshow(images[3],cmap=\"Oranges\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-22T06:06:30.866466Z","iopub.execute_input":"2024-11-22T06:06:30.866837Z","iopub.status.idle":"2024-11-22T06:06:31.915007Z","shell.execute_reply.started":"2024-11-22T06:06:30.866806Z","shell.execute_reply":"2024-11-22T06:06:31.914161Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"type(train[\"Target\"][0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-22T06:06:31.916088Z","iopub.execute_input":"2024-11-22T06:06:31.916456Z","iopub.status.idle":"2024-11-22T06:06:31.924811Z","shell.execute_reply.started":"2024-11-22T06:06:31.916416Z","shell.execute_reply":"2024-11-22T06:06:31.923715Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_names = {\n    0:  \"Nucleoplasm\",  \n    1:  \"Nuclear membrane\",   \n    2:  \"Nucleoli\",   \n    3:  \"Nucleoli fibrillar center\",   \n    4:  \"Nuclear speckles\",\n    5:  \"Nuclear bodies\",   \n    6:  \"Endoplasmic reticulum\",   \n    7:  \"Golgi apparatus\",   \n    8:  \"Peroxisomes\",   \n    9:  \"Endosomes\",   \n    10:  \"Lysosomes\",   \n    11:  \"Intermediate filaments\",   \n    12:  \"Actin filaments\",   \n    13:  \"Focal adhesion sites\",   \n    14:  \"Microtubules\",   \n    15:  \"Microtubule ends\",   \n    16:  \"Cytokinetic bridge\",   \n    17:  \"Mitotic spindle\",   \n    18:  \"Microtubule organizing center\",   \n    19:  \"Centrosome\",   \n    20:  \"Lipid droplets\",   \n    21:  \"Plasma membrane\",   \n    22:  \"Cell junctions\",   \n    23:  \"Mitochondria\",   \n    24:  \"Aggresome\",   \n    25:  \"Cytosol\",   \n    26:  \"Cytoplasmic bodies\",   \n    27:  \"Rods & rings\"\n}\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-22T06:06:31.926366Z","iopub.execute_input":"2024-11-22T06:06:31.926807Z","iopub.status.idle":"2024-11-22T06:06:31.945166Z","shell.execute_reply.started":"2024-11-22T06:06:31.926759Z","shell.execute_reply":"2024-11-22T06:06:31.943888Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def fill_targets(row):\n#     print('row\\n ',row)\n    row.Target = np.array(row.Target.split(\" \")).astype(int)\n#     row.Target = row.Target.astype(np.int)\n    if len(row.Target.shape)==0:\n        row.Target=[row.Target]\n        \n    for num in row.Target:\n        name = label_names[int(num)]\n        row.loc[name] = 1\n    return row\n\ntrain = train.apply(fill_targets, axis=1)\ntrain.head(2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-22T06:06:31.946710Z","iopub.execute_input":"2024-11-22T06:06:31.947164Z","iopub.status.idle":"2024-11-22T06:07:02.940209Z","shell.execute_reply.started":"2024-11-22T06:06:31.947117Z","shell.execute_reply":"2024-11-22T06:07:02.939073Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# P2.6 测试集标签的向量化\ntest = pd.read_csv(f\"{path}/dataset/test.csv\")\n# test = pd.read_csv(f\"{data_path}/sample_submission.csv\")\nprint('测试集扩增列之前的维度：{0}'.format(test.shape))\nfor col in train.columns.values:\n    if col != \"Id\" and col != \"Target\":\n        test[col] = 0\ntest.head(2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-22T06:07:02.941664Z","iopub.execute_input":"2024-11-22T06:07:02.942062Z","iopub.status.idle":"2024-11-22T06:07:02.983279Z","shell.execute_reply.started":"2024-11-22T06:07:02.942032Z","shell.execute_reply":"2024-11-22T06:07:02.982027Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target_total=train.drop([\"Id\",\"Target\"],axis=1).sum(axis=0).sort_values(ascending=False)\nplt.figure(figsize=(8,6))\nsns.barplot(x=target_total.values,y=target_total.index.values,order=target_total.index)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-22T06:08:03.223026Z","iopub.execute_input":"2024-11-22T06:08:03.223416Z","iopub.status.idle":"2024-11-22T06:08:03.710659Z","shell.execute_reply.started":"2024-11-22T06:08:03.223375Z","shell.execute_reply":"2024-11-22T06:08:03.709414Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# P2.8 筛选标签集\nfilter_target = pd.DataFrame(target_total)\nfilter_target.rename(columns = {0:'counts'}, inplace=True)\nfilter_target = filter_target[filter_target.counts.between(1000,1500)]\nfilter_target","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-22T06:12:06.183760Z","iopub.execute_input":"2024-11-22T06:12:06.184186Z","iopub.status.idle":"2024-11-22T06:12:06.198153Z","shell.execute_reply.started":"2024-11-22T06:12:06.184151Z","shell.execute_reply":"2024-11-22T06:12:06.197041Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# P2.9 训练集筛选重构\nfilter_columns = filter_target.index.insert(0,'Id')\nfilter_columns = filter_columns.insert(1,'Target')\nfilter_train = train[train[filter_target.index].sum(axis=1)>0][filter_columns]\nprint('筛选训练集的维数为：{0}'.format(filter_train.shape))\nfilter_train.head(3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-22T06:13:52.049824Z","iopub.execute_input":"2024-11-22T06:13:52.050865Z","iopub.status.idle":"2024-11-22T06:13:52.085421Z","shell.execute_reply.started":"2024-11-22T06:13:52.050806Z","shell.execute_reply":"2024-11-22T06:13:52.084343Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# P2.10 新训练集的标签数量分类统计\nfilter_train['target_count'] = filter_train[filter_target.index].sum(axis=1)\ncount_percent = np.round(100 * filter_train[\"target_count\"].value_counts() / filter_train.shape[0], 2)\nplt.figure(figsize=(6,4))\ng = sns.barplot(x=count_percent.index.values, y=count_percent.values, palette=\"Reds\")\nplt.ylabel(\"% of train data\")\nfor index,row in pd.DataFrame(count_percent).iterrows():\n    g.text(row.name-1,row[0]/2+5,row[0],color=\"black\",ha=\"center\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-22T06:30:51.236484Z","iopub.execute_input":"2024-11-22T06:30:51.237416Z","iopub.status.idle":"2024-11-22T06:30:51.451653Z","shell.execute_reply.started":"2024-11-22T06:30:51.237373Z","shell.execute_reply":"2024-11-22T06:30:51.450236Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# P2.11 删除含有两种标签的样本\nfilter_train = filter_train[filter_train['target_count'] < 2]\nfilter_train.reset_index(drop=True,inplace=True)\nfilter_train.drop(['Target','target_count'],axis=1,inplace=True)\nprint(filter_train.shape)\nfilter_train.head(3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-22T06:31:55.077532Z","iopub.execute_input":"2024-11-22T06:31:55.078347Z","iopub.status.idle":"2024-11-22T06:31:55.097344Z","shell.execute_reply.started":"2024-11-22T06:31:55.078307Z","shell.execute_reply":"2024-11-22T06:31:55.096148Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}