python散点图及相关系数矩阵计算
# -*- coding:utf-8 -*-
# __author__ = "LQ"
import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as stats
#自定义方法调用,上一篇有该方法
from DataAnalysis.TeachingModel.dbc import dbcConnect
def main(sql):
df = dbcConnect.selectDf(sql)
print(df)
# pearson相关系数矩阵
corr = df.corr()
print(corr)
#计算某一个元素相关
print(df.corr()[u'avg_getscore'])
# 输出结果第一个值为pearsonr相关系数,
# 第二个为p-value,所以这里Guba列和Value值是显著相关的
pearsonr = stats.pearsonr(df['avg_total_afterrequire_num'], df['avg_getscore'])
print(pearsonr)
# 散点图矩阵
pd.plotting.scatter_matrix(df, figsize=(8, 8),
c='k',
marker='+',
diagonal='hist',
alpha=0.8,
range_padding=0.1)
plt.show()
if __name__ == '__main__':
sql = '''SELECT
avg_total_afterrequire_num,
avg_total_beforerequire_num,
avg_total_homework_num,
avg_actual_homework_num,
avg_total_exam_num,
avg_actual_exam_num,
avg_evaluate_num,
avg_question_answer_num,
avg_note_num,
avg_error_num,
avg_login_log_num,
avg_question_times,
avg_getscore
FROM
t_study_behavior_class
WHERE
avg_getscore > 0
AND avg_getscore < 100'''
main(sql)
自定义方法dbcConnect
在下面博客中
https://blog.csdn.net/qq_30868737/article/details/103995174
来源:CSDN
作者:安然烟火
链接:https://blog.csdn.net/qq_30868737/article/details/103995270