You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

401 lines
18 KiB
Plaintext

6 months ago
{
"cells": [
{
"cell_type": "markdown",
"id": "799f31eb",
"metadata": {},
"source": [
"线性回归预测的是连续数值,逻辑回归预测的是离散数值。"
]
},
{
"cell_type": "markdown",
"id": "fc3c8eef",
"metadata": {},
"source": [
"线性回归: \n",
"1.预测天气 \n",
"2.预测股票 \n",
"3.预测房价 \n",
"逻辑回归: \n",
"1.邮件是否是垃圾邮件 \n",
"2.猫狗分类 \n",
"3.是否贷款"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "0b62f4a6",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "efe20171",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv(\"insurance_data.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "a0336e69",
"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>age</th>\n",
" <th>bought_insurance</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>22</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>25</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>47</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>52</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>46</td>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" age bought_insurance\n",
"0 22 0\n",
"1 25 0\n",
"2 47 1\n",
"3 52 0\n",
"4 46 1"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "50fb949d",
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "e77e37f4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.collections.PathCollection at 0x2716f2d0608>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAGdCAYAAADAAnMpAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAeKElEQVR4nO3df3TV9X348VcC5AbBBCySAEahqy065EdBs5R6ejozc5yH1f06OdYVDv2xo6MOzXYmaRXadTWsHY71wMyk7dqdzUH1TNdWi2OxsOOalRHGqW4WpdLBURPgbOZi1MQln+8ffr2aAjYXE98JPB7n3GP43Pfn3vfH903u83zuzU1JlmVZAAAkUpp6AgDA2U2MAABJiREAICkxAgAkJUYAgKTECACQlBgBAJISIwBAUuNTT2AoBgYG4rnnnotzzz03SkpKUk8HABiCLMvi+PHjMXPmzCgtPfX5jzERI88991zU1NSkngYAcBoOHz4cF1xwwSmvHxMxcu6550bEawdTUVGReDYAwFDk8/moqakpPI+fypiIkddfmqmoqBAjADDG/Ly3WHgDKwCQlBgBAJISIwBAUmIEAEhKjAAASYkRACApMQIAJCVGAICkxAgAkFTRMfIv//IvsWzZspg5c2aUlJTEgw8++HP32blzZ7z//e+PXC4X73nPe+Ib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QAgKTECACQlRgCApMQIAJCUGAEAkhIjAEBSYgQASEqMAABJiREAICkxAgAkJUYAgKTECACQlBgBAJISIwBAUmIEAEhKjAAASYkRACApMQIAJCVGAICkxAgAkJQYAQCSEiMAQFJiBABISowAAEmJEQAgKTECACQlRgCApMQIAJCUGAEAkhIjAEBSYgQASEqMAABJiREAICkxAgAkJUYAgKTECACQlBgBAJISIwBAUmIEAEhKjAAASYkRACApMQIAJCVGAICkxAgAkJQYAQCSEiMAQFJiBABISowAAEmJEQAgqdOKkc2bN8fs2bOjvLw8amtrY/fu3W85/oUXXohVq1bFjBkzIpfLxXvf+954+OGHT2vCAMCZZXyxO2zbti2ampqitbU1amtrY+PGjdHQ0BD79++P6dOnnzC+r68vfuVXfiWmT58e999/f8yaNSv++7//O6ZMmTIc8wcAxriSLMuyYnaora2Nyy+/PDZt2hQREQMDA1FTUxM333xzrFmz5oTxra2t8eUvfzl+/OMfx4QJE05rkvl8PiorK6O7uzsqKipO6zYAgHfWUJ+/i3qZpq+vLzo6OqK+vv6NGygtjfr6+mhvbz/pPt/+9rejrq4uVq1aFVVVVTFv3ry48847o7+//5T309vbG/l8ftAFADgzFRUjx44di/7+/qiqqhq0vaqqKjo7O0+6zzPPPBP3339/9Pf3x8MPPxx33HFHbNiwIf7kT/7klPfT0tISlZWVhUtNTU0x0wQAxpAR/22agYGBmD59etxzzz2xePHiaGxsjM9+9rPR2tp6yn2am5uju7u7cDl8+PBITxMASKSoN7BOmzYtxo0bF11dXYO2d3V1RXV19Un3mTFjRkyYMCHGjRtX2HbJJZdEZ2dn9PX1RVlZ2Qn75HK5yOVyxUwNABijijozUlZWFosXL462trbCtoGBgWhra4u6urqT7rN06dI4cOBADAwMFLY99dRTMWPGjJOGCABwdin6ZZqmpqbYsmVLfPOb34wnn3wybrrppujp6YmVK1dGRMTy5cujubm5MP6mm26K//mf/4nVq1fHU089FQ899FDceeedsWrVquE7CgBgzCr6c0YaGxvj6NGjsXbt2ujs7IyFCxfG9u3bC29qPXToUJSWvtE4NTU18cgjj8Stt94a8+fPj1mzZsXq1av
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.scatter(df.age, df.bought_insurance, marker=\"+\", color='r')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "bf219340",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(27, 2)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.shape"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "3a6ed8a7",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "c85065da",
"metadata": {},
"outputs": [],
"source": [
"x_train, x_test, y_train, y_test = train_test_split(df[['age']], df.bought_insurance, train_size=0.9, random_state=10)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "16437565",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(24, 3)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(x_train), len(x_test)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "4d83ec8c",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.linear_model import LogisticRegression"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "61312ccd",
"metadata": {},
"outputs": [],
"source": [
"lr = LogisticRegression()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "dac8d670",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"LogisticRegression()"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lr.fit(x_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "698480b6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([1, 1, 0], dtype=int64),\n",
" 7 1\n",
" 5 1\n",
" 18 0\n",
" Name: bought_insurance, dtype: int64)"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lr.predict(x_test), y_test"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "89c91628",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lr.score(x_test, y_test)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "452bb440",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.06470723, 0.93529277],\n",
" [0.10327405, 0.89672595],\n",
" [0.92775095, 0.07224905]])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lr.predict_proba(x_test)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "1246d4de",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"D:\\envs\\stark-lin\\lib\\site-packages\\sklearn\\base.py:451: UserWarning: X does not have valid feature names, but LogisticRegression was fitted with feature names\n",
" \"X does not have valid feature names, but\"\n",
"D:\\envs\\stark-lin\\lib\\site-packages\\sklearn\\base.py:451: UserWarning: X does not have valid feature names, but LogisticRegression was fitted with feature names\n",
" \"X does not have valid feature names, but\"\n"
]
},
{
"data": {
"text/plain": [
"(array([1], dtype=int64), array([0], dtype=int64))"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lr.predict([[53]]), lr.predict([[20]])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc340fea",
"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.8.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}