如何用Pandas在Python中创建虚拟变量

您所在的位置:网站首页 spss26如何创建虚拟变量 如何用Pandas在Python中创建虚拟变量

如何用Pandas在Python中创建虚拟变量

2024-07-07 15:30| 来源: 网络整理| 查看: 265

创建虚拟变量通常是数据分析过程中的一项必要工作。在Python中,我们可以使用Pandas库中的get_dummies()函数来创建虚拟变量。以下是创建虚拟变量的完整攻略:

1. 导入必要的库

首先,需要导入Pandas库。同时,如果要演示示例,也需要导入numpy库和matplotlib库。

import pandas as pd import numpy as np import matplotlib.pyplot as plt 2. 创建数据集

接下来,我们需要创建一个包含分类变量的数据集。在本文中,我们使用一个名为“students”的数据集,其中包含两个分类变量:性别和年级。示例中我们定义了三行数据。

students = pd.DataFrame({'gender': ['male', 'female', 'male'], 'grade': ['sophomore', 'freshman', 'junior']}) 3. 创建虚拟变量

现在是创建虚拟变量的关键步骤。我们可以使用get_dummies()函数来创建虚拟变量。让我们将所有的分类变量都转换为虚拟变量,并将它们存储在一个新的数据集中。

students_dummies = pd.get_dummies(students, columns=['gender', 'grade']) print(students_dummies)

运行后的输出如下:

gender_female gender_male grade_freshman grade_junior grade_sophomore 0 0 1 0 0 1 1 1 0 1 0 0 2 0 1 0 1 0

这里我们使用了get_dummies()函数,将“gender”和“grade”这两个列转换为虚拟变量。一个虚拟变量分配给一个分类变量的每个可能取值,该值设置为1,其他虚拟变量的值都会被设置为0。

4. 将虚拟变量合并回原数据集

通常,我们希望将虚拟变量与原数据集合并。可以使用concat()函数将原始数据集和虚拟变量数据集合并。以下是示例代码:

students = pd.concat([students, students_dummies], axis=1) print(students)

运行后的输出如下:

gender grade gender_female gender_male grade_freshman grade_junior grade_sophomore 0 male sophomore 0 1 0 0 1 1 female freshman 1 0 1 0 0 2 male junior 0 1 0 1 0 5. 示例一:应用虚拟变量进行回归分析

使用虚拟变量分析是统计学和经济学中的一个重要应用。让我们使用虚拟变量重新构建一个例子。我们创建了一个名为“sales”的数据集,其中包含两个分类变量:“性别”和“广告类型”。假设我们已经使用线性回归模型预测了“销售量”的结果,并希望分析“广告类型”和“性别”对“销售量”的影响。以下是示例代码:

# 生成包含示例数据的数据集 sales = pd.DataFrame({'gender': ['Male', 'Male', 'Female', 'Male', 'Male', 'Female', 'Female', 'Male', 'Female', 'Male', 'Male', 'Female', 'Male', 'Female', 'Female', 'Male', 'Male'], 'Ad Type': ['A', 'B', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B'], 'Sales': [13000, 17000, 18000, 10000, 19000, 13000, 15000, 13000, 18000, 9500, 18000, 13000, 18000, 16000, 18000, 17000, 18000]}) # 将分类变量转换为虚拟变量 sales_dummies = pd.get_dummies(sales[['Ad Type', 'gender']]) # 合并虚拟变量和原始数据集 sales = pd.concat([sales, sales_dummies], axis=1) # 进行线性回归,分析虚拟变量的影响 import statsmodels.api as sm X = sales[['Ad Type_A', 'Ad Type_B', 'gender_Female', 'gender_Male']] Y = sales['Sales'] X = sm.add_constant(X) model = sm.OLS(Y, X).fit() print(model.summary())

运行后的输出如下:

OLS Regression Results ============================================================================== Dep. Variable: Sales R-squared: 0.203 Model: OLS Adj. R-squared: 0.100 Method: Least Squares F-statistic: 1.971 Date: Mon, 02 Aug 2021 Prob (F-statistic): 0.143 Time: 09:47:32 Log-Likelihood: -161.78 No. Observations: 17 AIC: 333.6 Df Residuals: 12 BIC: 337.8 Df Model: 4 Covariance Type: nonrobust ================================================================================= coef std err t P>|t| [0.025 0.975] --------------------------------------------------------------------------------- const 1.553e+04 1006.409 15.434 0.000 1.33e+04 1.76e+04 Ad Type_A 1.201e+04 1.13e+04 1.067 0.306 -1.21e+04 3.61e+04 Ad Type_B 3.527e+03 1.04e+04 0.340 0.740 -1.87e+04 2.57e+04 gender_Female 3032.0513 1.49e+04 0.204 0.842 -2.97e+04 3.58e+04 gender_Male 1.25e+04 1.29e+04 0.966 0.356 -1.61e+04 4.11e+04 ============================================================================== Omnibus: 0.774 Durbin-Watson: 1.744 Prob(Omnibus): 0.679 Jarque-Bera (JB): 0.618 Skew: -0.172 Prob(JB): 0.734 Kurtosis: 1.998 Cond. No. 6.93 ============================================================================== Notes: [1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

结果表明,“广告类型”和“性别”对“销售量”几乎没有影响,因为它们在回归方程中的系数接近于零,且p值较高。例如,Ad Type A的系数是1.201e+04,Ad Type B的系数是3.527e+03,而它们的p值分别为0.306和0.740。同样地,女性和男性的系数分别为3032.0513和1.25e+04,而它们的p值分别为0.842和0.356。

6. 示例二:创建哑变量并绘制散点图

在另一个示例中,让我们使用matplotlib库绘制散点图。这次我们使用的是名为“iris”的经典数据集。

# 读取经典的Iris数据集 iris = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', header=None) iris.columns = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'] # 将分类变量'种类'转换为虚拟变量,并更改列名 species_dummies = pd.get_dummies(iris['species']) species_dummies.columns = ['setosa', 'versicolor', 'virginica'] # 将iris和species_dummies合并,并绘制散点图 iris = pd.concat([iris, species_dummies], axis=1) plt.scatter(iris['sepal_length'], iris['sepal_width'], c=iris['versicolor'], cmap='coolwarm') plt.xlabel('Sepal Length') plt.ylabel('Sepal Width') plt.show()

运行后的输出如下:

image

散点图展示了sepal_length和sepal_width两个数值变量之间的关系,同时用颜色区分了versicolor的虚拟变量。蓝色区域代表versicolor=0的情况,红色区域表示versicolor=1的情况。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:如何用Pandas在Python中创建虚拟变量 - Python技术站



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3