admin管理员组文章数量:1030255
Python实现相关分析与线性回归分析
我们假设用户已经准备好.sav文件,已完成数据预处理。
代码语言:txt复制import pandas as pd
import pyreadstat
import statsmodels.api as sm
import matplotlib.pyplot as plt
import seaborn as sns
def load_spss_data(file_path):
"""
加载SPSS数据文件
:param file_path: SPSS数据文件的路径
:return: 包含数据的DataFrame
"""
df, _ = pyreadstat.read_sav(file_path)
return df
def descriptive_analysis(df):
"""
进行描述性统计分析
:param df: 数据DataFrame
:return: 描述性统计结果
"""
return df.describe()
def correlation_analysis(df):
"""
进行相关分析
:param df: 数据DataFrame
:return: 相关系数矩阵
"""
return df.corr()
def linear_regression(df, dependent_var, independent_vars):
"""
进行线性回归分析
:param df: 数据DataFrame
:param dependent_var: 因变量
:param independent_vars: 自变量列表
:return: 线性回归结果
"""
X = df[independent_vars]
X = sm.add_constant(X)
y = df[dependent_var]
model = sm.OLS(y, X).fit()
return model
def plot_correlation_matrix(corr_matrix):
"""
绘制相关系数矩阵热力图
:param corr_matrix: 相关系数矩阵
"""
plt.figure(figsize=(10, 8))
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm')
plt.title('Correlation Matrix')
plt.show()
def plot_regression_results(model, independent_vars):
"""
绘制线性回归结果图
:param model: 线性回归模型
:param independent_vars: 自变量列表
"""
plt.figure(figsize=(10, 8))
for var in independent_vars:
plt.scatter(model.model.exog[:, model.model.exog_names.index(var)], model.resid)
plt.axhline(y=0, color='r', linestyle='--')
plt.xlabel(var)
plt.ylabel('Residuals')
plt.title(f'Residuals vs {var}')
plt.show()
if __name__ == "__main__":
# 替换为你的SPSS数据文件路径
file_path = 'D:\\Projects\\Practice\\大学生生活满意度调查.sav'
df = load_spss_data(file_path)
# 描述性统计分析
desc_stats = descriptive_analysis(df)
print("Descriptive Statistics:")
print(desc_stats)
# 相关分析
corr_matrix = correlation_analysis(df)
print("Correlation Matrix:")
print(corr_matrix)
plot_correlation_matrix(corr_matrix)
# 线性回归分析
dependent_var = 'your_dependent_variable'
independent_vars = ['your_independent_variable_1', 'your_independent_variable_2']
model = linear_regression(df, dependent_var, independent_vars)
print("Linear Regression Results:")
print(model.summary())
plot_regression_results(model, independent_vars)
Python实现相关分析与线性回归分析
我们假设用户已经准备好.sav文件,已完成数据预处理。
代码语言:txt复制import pandas as pd
import pyreadstat
import statsmodels.api as sm
import matplotlib.pyplot as plt
import seaborn as sns
def load_spss_data(file_path):
"""
加载SPSS数据文件
:param file_path: SPSS数据文件的路径
:return: 包含数据的DataFrame
"""
df, _ = pyreadstat.read_sav(file_path)
return df
def descriptive_analysis(df):
"""
进行描述性统计分析
:param df: 数据DataFrame
:return: 描述性统计结果
"""
return df.describe()
def correlation_analysis(df):
"""
进行相关分析
:param df: 数据DataFrame
:return: 相关系数矩阵
"""
return df.corr()
def linear_regression(df, dependent_var, independent_vars):
"""
进行线性回归分析
:param df: 数据DataFrame
:param dependent_var: 因变量
:param independent_vars: 自变量列表
:return: 线性回归结果
"""
X = df[independent_vars]
X = sm.add_constant(X)
y = df[dependent_var]
model = sm.OLS(y, X).fit()
return model
def plot_correlation_matrix(corr_matrix):
"""
绘制相关系数矩阵热力图
:param corr_matrix: 相关系数矩阵
"""
plt.figure(figsize=(10, 8))
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm')
plt.title('Correlation Matrix')
plt.show()
def plot_regression_results(model, independent_vars):
"""
绘制线性回归结果图
:param model: 线性回归模型
:param independent_vars: 自变量列表
"""
plt.figure(figsize=(10, 8))
for var in independent_vars:
plt.scatter(model.model.exog[:, model.model.exog_names.index(var)], model.resid)
plt.axhline(y=0, color='r', linestyle='--')
plt.xlabel(var)
plt.ylabel('Residuals')
plt.title(f'Residuals vs {var}')
plt.show()
if __name__ == "__main__":
# 替换为你的SPSS数据文件路径
file_path = 'D:\\Projects\\Practice\\大学生生活满意度调查.sav'
df = load_spss_data(file_path)
# 描述性统计分析
desc_stats = descriptive_analysis(df)
print("Descriptive Statistics:")
print(desc_stats)
# 相关分析
corr_matrix = correlation_analysis(df)
print("Correlation Matrix:")
print(corr_matrix)
plot_correlation_matrix(corr_matrix)
# 线性回归分析
dependent_var = 'your_dependent_variable'
independent_vars = ['your_independent_variable_1', 'your_independent_variable_2']
model = linear_regression(df, dependent_var, independent_vars)
print("Linear Regression Results:")
print(model.summary())
plot_regression_results(model, independent_vars)
本文标签: Python实现相关分析与线性回归分析
版权声明:本文标题:Python实现相关分析与线性回归分析 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/jiaocheng/1747645094a2198022.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论