Files
machine_learning_projects/共享民宿平台担保交易房子评分的影响研究/ada_reg.ipynb
T
2023-07-15 12:04:50 +00:00

361 lines
11 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"cell_type": "markdown",
"id": "9e30b402-4ed9-4e45-aa7c-a77a7d68df17",
"metadata": {},
"source": [
"# AdaBoost回归"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "1a11e27d-9c4e-47eb-958b-cf9ed1c4c603",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"from sklearn.ensemble import RandomForestRegressor, AdaBoostRegressor\n",
"from sklearn.metrics import mean_squared_error\n",
"from sklearn.preprocessing import StandardScaler\n",
"from sklearn.model_selection import train_test_split"
]
},
{
"cell_type": "markdown",
"id": "7a704744-26f2-40f1-9bc1-0e25608dd40f",
"metadata": {},
"source": [
"读入数据,由于样本量很大,直接删除有缺失值的样本。"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9f2f693d-44fd-432f-b732-7a2117106150",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>host_response_rate</th>\n",
" <th>host_acceptance_rate</th>\n",
" <th>accommodates</th>\n",
" <th>price</th>\n",
" <th>number_of_reviews</th>\n",
" <th>review_scores_rating</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1.00</td>\n",
" <td>0.33</td>\n",
" <td>2.0</td>\n",
" <td>120.0</td>\n",
" <td>90.0</td>\n",
" <td>4.50</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1.00</td>\n",
" <td>0.98</td>\n",
" <td>2.0</td>\n",
" <td>90.0</td>\n",
" <td>351.0</td>\n",
" <td>4.58</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1.00</td>\n",
" <td>0.98</td>\n",
" <td>2.0</td>\n",
" <td>66.0</td>\n",
" <td>67.0</td>\n",
" <td>4.52</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1.00</td>\n",
" <td>0.98</td>\n",
" <td>1.0</td>\n",
" <td>33.0</td>\n",
" <td>297.0</td>\n",
" <td>4.70</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>1.00</td>\n",
" <td>1.00</td>\n",
" <td>2.0</td>\n",
" <td>45.0</td>\n",
" <td>42.0</td>\n",
" <td>4.98</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>203252</th>\n",
" <td>1.00</td>\n",
" <td>0.93</td>\n",
" <td>4.0</td>\n",
" <td>152.0</td>\n",
" <td>1.0</td>\n",
" <td>4.00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>203253</th>\n",
" <td>1.00</td>\n",
" <td>0.97</td>\n",
" <td>2.0</td>\n",
" <td>45.0</td>\n",
" <td>1.0</td>\n",
" <td>3.00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>203254</th>\n",
" <td>1.00</td>\n",
" <td>0.97</td>\n",
" <td>2.0</td>\n",
" <td>40.0</td>\n",
" <td>1.0</td>\n",
" <td>1.00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>203276</th>\n",
" <td>0.99</td>\n",
" <td>0.99</td>\n",
" <td>2.0</td>\n",
" <td>43.0</td>\n",
" <td>1.0</td>\n",
" <td>5.00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>203308</th>\n",
" <td>1.00</td>\n",
" <td>1.00</td>\n",
" <td>3.0</td>\n",
" <td>110.0</td>\n",
" <td>1.0</td>\n",
" <td>5.00</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>134835 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" host_response_rate host_acceptance_rate accommodates price \\\n",
"0 1.00 0.33 2.0 120.0 \n",
"1 1.00 0.98 2.0 90.0 \n",
"2 1.00 0.98 2.0 66.0 \n",
"3 1.00 0.98 1.0 33.0 \n",
"5 1.00 1.00 2.0 45.0 \n",
"... ... ... ... ... \n",
"203252 1.00 0.93 4.0 152.0 \n",
"203253 1.00 0.97 2.0 45.0 \n",
"203254 1.00 0.97 2.0 40.0 \n",
"203276 0.99 0.99 2.0 43.0 \n",
"203308 1.00 1.00 3.0 110.0 \n",
"\n",
" number_of_reviews review_scores_rating \n",
"0 90.0 4.50 \n",
"1 351.0 4.58 \n",
"2 67.0 4.52 \n",
"3 297.0 4.70 \n",
"5 42.0 4.98 \n",
"... ... ... \n",
"203252 1.0 4.00 \n",
"203253 1.0 3.00 \n",
"203254 1.0 1.00 \n",
"203276 1.0 5.00 \n",
"203308 1.0 5.00 \n",
"\n",
"[134835 rows x 6 columns]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"variables = ['number_of_reviews', 'price', 'accommodates',\n",
" 'host_response_rate', 'host_acceptance_rate', 'review_scores_rating']\n",
"df = pd.read_csv('../data/2022-01(US_25).csv', usecols=variables)\n",
"df['price'] = df['price'].replace('\\$', '', regex=True)\n",
"df['price'] = df['price'].replace('\\,', '', regex=True).astype(float)\n",
"df[['host_response_rate', 'host_acceptance_rate']] = df[['host_response_rate',\n",
" 'host_acceptance_rate']].replace('\\%', '', regex=True).astype(float)*0.01\n",
"df[['number_of_reviews']] = df[['number_of_reviews']].astype(float)\n",
"for col in variables:\n",
" df[col] = df[col].astype(np.float32)\n",
" df = df[np.isnan(df[col]) != 1]\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "16658a50-232f-49c7-ad4c-c93a6a43e118",
"metadata": {},
"source": [
"划分测试集和训练集,测试集大小为0.3,并对数据标准化"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "00158193-2ea9-4fb8-91f1-cb5ea4b9fb96",
"metadata": {},
"outputs": [],
"source": [
"# 固定划分训练集和测试集\n",
"info = df.iloc[:, :-1].values\n",
"target = df.iloc[:, -1].values\n",
"# 标准化\n",
"stdscaler = StandardScaler()\n",
"info_train, info_test, target_train, target_test = train_test_split(\n",
" info, target, test_size=0.3,shuffle=True, random_state=420)\n",
"info_train = stdscaler.fit_transform(info_train)\n",
"info_test = stdscaler.transform(info_test)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "91622729-b48b-45bd-9cd6-b33f8d839d7a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.2976891 , 0.44020966, -0.10153121, -0.14794447, -0.62968445],\n",
" [ 0.2976891 , 0.44020966, -0.10153121, -0.27286685, 0.06075969],\n",
" [ 0.2976891 , 0.39323488, -0.8076375 , -0.25845274, -0.62968445],\n",
" ...,\n",
" [ 0.2976891 , 0.5341586 , 0.6045751 , -0.22241743, 0.03610097],\n",
" [ 0.2976891 , 0.25231144, -0.8076375 , -0.3016951 , 1.1457433 ],\n",
" [ 0.2976891 , -0.6871791 , -0.8076375 , -0.3185116 , 0.72654516]],\n",
" dtype=float32)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"info_train"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3b292dc8-9c79-45e4-8a4a-6bda192a2b64",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([3. , 4.93, 4. , ..., 4.69, 4.85, 4.83], dtype=float32)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"target_train"
]
},
{
"cell_type": "markdown",
"id": "47bf67ca-2ede-4bf3-9f77-1f49efd68bc3",
"metadata": {},
"source": [
"拟合并预测,计算在测试集上的均方误差"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "81f63b17-e62c-4775-a525-edb8430a0910",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"MSE:0.4843\n"
]
}
],
"source": [
"# AdaBoost回归\n",
"rf = RandomForestRegressor(n_estimators=10, random_state=0, max_depth=2)\n",
"ada = AdaBoostRegressor(estimator=rf,n_estimators=100, loss='square', random_state=0)\n",
"ada.fit(info_train, target_train)\n",
"target_pred = ada.predict(info_test)\n",
"print(\"MSE:%.4f\" % mean_squared_error(target_test, target_pred))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e4e758bb-8599-41c0-a6db-ef367d744505",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}