{"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":3136,"databundleVersionId":26502,"sourceType":"competition"},{"sourceId":8587,"databundleVersionId":868304,"sourceType":"competition"},{"sourceId":14999,"databundleVersionId":653984,"sourceType":"competition"},{"sourceId":17000,"databundleVersionId":895292,"sourceType":"competition"},{"sourceId":872336,"sourceType":"datasetVersion","datasetId":464163}],"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# 1. Import Libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:34:15.899630Z","iopub.execute_input":"2024-11-25T04:34:15.900175Z","iopub.status.idle":"2024-11-25T04:34:19.077273Z","shell.execute_reply.started":"2024-11-25T04:34:15.900123Z","shell.execute_reply":"2024-11-25T04:34:19.075769Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 2. Read Data","metadata":{}},{"cell_type":"code","source":"data = pd.read_csv(\"/kaggle/input/college-data/data.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:34:19.079364Z","iopub.execute_input":"2024-11-25T04:34:19.079920Z","iopub.status.idle":"2024-11-25T04:34:19.108081Z","shell.execute_reply.started":"2024-11-25T04:34:19.079882Z","shell.execute_reply":"2024-11-25T04:34:19.106675Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head()\n\n# private: 是否是私立\n# app: 申请人数\n# accept: 接收人数\n# enroll: 注册人数\n# top10perc: top10学生比例\n# top25perc: top20学生比例\n# f_undergrad: 全日制本科生人数\n# p_undergrad: 非全日\n# outstate: 州外学费\n# room_board: 伙食费\n#books: 书本费\n#personal: 个人开销\n#phd: 持博士学位的教师比例\n#terminal: 持终极学位的教师比例\n#s_f_ratio: 师生比（student-to-faculty ratio）\n#perc_alumni: 校友捐赠比例（percent of alumni who donate）\n#expend: 每名学生的教育支出（instructional expenditure per student）\n#grad_rate: 毕业率（graduation rate）","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-25T04:35:56.947748Z","iopub.execute_input":"2024-11-25T04:35:56.948175Z","iopub.status.idle":"2024-11-25T04:35:56.979038Z","shell.execute_reply.started":"2024-11-25T04:35:56.948140Z","shell.execute_reply":"2024-11-25T04:35:56.977560Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.tail()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-25T04:35:57.589324Z","iopub.execute_input":"2024-11-25T04:35:57.589740Z","iopub.status.idle":"2024-11-25T04:35:57.605741Z","shell.execute_reply.started":"2024-11-25T04:35:57.589702Z","shell.execute_reply":"2024-11-25T04:35:57.604249Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:35:58.444559Z","iopub.execute_input":"2024-11-25T04:35:58.445088Z","iopub.status.idle":"2024-11-25T04:35:58.480866Z","shell.execute_reply.started":"2024-11-25T04:35:58.445036Z","shell.execute_reply":"2024-11-25T04:35:58.479789Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#pd.set_option('display.float', '{:.2f}'.format)\ndata.describe()","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:36:01.549517Z","iopub.execute_input":"2024-11-25T04:36:01.549921Z","iopub.status.idle":"2024-11-25T04:36:01.605161Z","shell.execute_reply.started":"2024-11-25T04:36:01.549885Z","shell.execute_reply":"2024-11-25T04:36:01.603987Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 3. Exploratory Data Analysis (EDA)","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nsns.scatterplot(x='room_board', y='grad_rate', data=data, hue='private')","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:36:27.925891Z","iopub.execute_input":"2024-11-25T04:36:27.926300Z","iopub.status.idle":"2024-11-25T04:36:28.433288Z","shell.execute_reply.started":"2024-11-25T04:36:27.926264Z","shell.execute_reply":"2024-11-25T04:36:28.432164Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nsns.scatterplot(x='outstate', y='f_undergrad', data=data, hue='private')","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:36:28.759141Z","iopub.execute_input":"2024-11-25T04:36:28.759576Z","iopub.status.idle":"2024-11-25T04:36:29.218275Z","shell.execute_reply.started":"2024-11-25T04:36:28.759534Z","shell.execute_reply":"2024-11-25T04:36:29.217071Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(12, 8))\n\ndata.loc[data.private == 'Yes', 'outstate'].hist(label=\"Private College\", bins=30)\ndata.loc[data.private == 'No', 'outstate'].hist(label=\"Non Private College\", bins=30)\n\nplt.xlabel('Outstate')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:36:53.818822Z","iopub.execute_input":"2024-11-25T04:36:53.819234Z","iopub.status.idle":"2024-11-25T04:36:54.284346Z","shell.execute_reply.started":"2024-11-25T04:36:53.819197Z","shell.execute_reply":"2024-11-25T04:36:54.282826Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(12, 8))\n\ndata.loc[data.private == 'Yes', 'grad_rate'].hist(label=\"Private College\", bins=30)\ndata.loc[data.private == 'No', 'grad_rate'].hist(label=\"Non Private College\", bins=30)\n\nplt.xlabel('Graduation Rate')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:36:54.687226Z","iopub.execute_input":"2024-11-25T04:36:54.687664Z","iopub.status.idle":"2024-11-25T04:36:55.124415Z","shell.execute_reply.started":"2024-11-25T04:36:54.687628Z","shell.execute_reply":"2024-11-25T04:36:55.123300Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.loc[data.grad_rate > 100]","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:37:02.121321Z","iopub.execute_input":"2024-11-25T04:37:02.121694Z","iopub.status.idle":"2024-11-25T04:37:02.142088Z","shell.execute_reply.started":"2024-11-25T04:37:02.121661Z","shell.execute_reply":"2024-11-25T04:37:02.140784Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.loc[data.grad_rate > 100, 'grad_rate'] = 100","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:37:04.433880Z","iopub.execute_input":"2024-11-25T04:37:04.434956Z","iopub.status.idle":"2024-11-25T04:37:04.441209Z","shell.execute_reply.started":"2024-11-25T04:37:04.434911Z","shell.execute_reply":"2024-11-25T04:37:04.439906Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(12, 8))\n\ndata.loc[data.private == 'Yes', 'grad_rate'].hist(label=\"Private College\", bins=30)\ndata.loc[data.private == 'No', 'grad_rate'].hist(label=\"Non Private College\", bins=30)\n\nplt.xlabel('Graduation Rate')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:37:04.888598Z","iopub.execute_input":"2024-11-25T04:37:04.889167Z","iopub.status.idle":"2024-11-25T04:37:05.262338Z","shell.execute_reply.started":"2024-11-25T04:37:04.889129Z","shell.execute_reply":"2024-11-25T04:37:05.261090Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 4. Build model","metadata":{}},{"cell_type":"code","source":"from sklearn.cluster import KMeans","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:37:07.075676Z","iopub.execute_input":"2024-11-25T04:37:07.076124Z","iopub.status.idle":"2024-11-25T04:37:07.563295Z","shell.execute_reply.started":"2024-11-25T04:37:07.076087Z","shell.execute_reply":"2024-11-25T04:37:07.562073Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"kmeans = KMeans(2)","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:37:07.860958Z","iopub.execute_input":"2024-11-25T04:37:07.861382Z","iopub.status.idle":"2024-11-25T04:37:07.866747Z","shell.execute_reply.started":"2024-11-25T04:37:07.861349Z","shell.execute_reply":"2024-11-25T04:37:07.865606Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"kmeans.fit(data.drop('private', axis=1))","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:37:08.811065Z","iopub.execute_input":"2024-11-25T04:37:08.811487Z","iopub.status.idle":"2024-11-25T04:37:09.811334Z","shell.execute_reply.started":"2024-11-25T04:37:08.811451Z","shell.execute_reply":"2024-11-25T04:37:09.810384Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"kmeans.cluster_centers_","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:37:09.813976Z","iopub.execute_input":"2024-11-25T04:37:09.814347Z","iopub.status.idle":"2024-11-25T04:37:09.825248Z","shell.execute_reply.started":"2024-11-25T04:37:09.814314Z","shell.execute_reply":"2024-11-25T04:37:09.823951Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 5. Evaluation","metadata":{}},{"cell_type":"code","source":"#将yes no字符文本类型转换成0，1数值类型\n\ndata['private'] = data.private.astype(\"category\").cat.codes","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:37:11.928175Z","iopub.execute_input":"2024-11-25T04:37:11.928555Z","iopub.status.idle":"2024-11-25T04:37:11.935287Z","shell.execute_reply.started":"2024-11-25T04:37:11.928524Z","shell.execute_reply":"2024-11-25T04:37:11.933882Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.private","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:38:09.377877Z","iopub.execute_input":"2024-11-25T04:38:09.378315Z","iopub.status.idle":"2024-11-25T04:38:09.387985Z","shell.execute_reply.started":"2024-11-25T04:38:09.378279Z","shell.execute_reply":"2024-11-25T04:38:09.386626Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:38:10.216479Z","iopub.execute_input":"2024-11-25T04:38:10.216914Z","iopub.status.idle":"2024-11-25T04:38:10.234207Z","shell.execute_reply.started":"2024-11-25T04:38:10.216870Z","shell.execute_reply":"2024-11-25T04:38:10.232917Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"kmeans.labels_","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:38:10.879093Z","iopub.execute_input":"2024-11-25T04:38:10.879488Z","iopub.status.idle":"2024-11-25T04:38:10.888485Z","shell.execute_reply.started":"2024-11-25T04:38:10.879454Z","shell.execute_reply":"2024-11-25T04:38:10.887238Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import classification_report, confusion_matrix\n\nprint(confusion_matrix(data.private, kmeans.labels_))\nprint(classification_report(data.private, kmeans.labels_))","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:38:15.291270Z","iopub.execute_input":"2024-11-25T04:38:15.291677Z","iopub.status.idle":"2024-11-25T04:38:15.312400Z","shell.execute_reply.started":"2024-11-25T04:38:15.291638Z","shell.execute_reply":"2024-11-25T04:38:15.310888Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, confusion_matrix\n\nprint(accuracy_score(data.private, kmeans.labels_))\nprint(pd.DataFrame(classification_report(data.private, kmeans.labels_, output_dict=True)))","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:38:22.477110Z","iopub.execute_input":"2024-11-25T04:38:22.477522Z","iopub.status.idle":"2024-11-25T04:38:22.497826Z","shell.execute_reply.started":"2024-11-25T04:38:22.477485Z","shell.execute_reply":"2024-11-25T04:38:22.496582Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 6. Scaling the data","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n\nscalar = StandardScaler()\n\nX = data.drop('private', axis=1)\ny = data.private\n\nX = scalar.fit_transform(X)\n\nkmeans = KMeans(2)\nkmeans.fit(X)\n\nprint(kmeans.cluster_centers_)\n\nprint(accuracy_score(y, kmeans.labels_))\nprint(confusion_matrix(y, kmeans.labels_))\nprint(classification_report(y, kmeans.labels_))","metadata":{"execution":{"iopub.status.busy":"2024-11-25T04:38:23.745230Z","iopub.execute_input":"2024-11-25T04:38:23.745641Z","iopub.status.idle":"2024-11-25T04:38:25.473620Z","shell.execute_reply.started":"2024-11-25T04:38:23.745606Z","shell.execute_reply":"2024-11-25T04:38:25.472755Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Not so bad considering the algorithm is purely using the features to cluster the universities into 2 distinct groups! Hopefully you can begin to see how K Means is useful for clustering un-labeled data!\n\n## Great Job!","metadata":{}}]}