from sklearn.preprocessing import MinMaxScaler
from scipy.stats import pearsonr
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm
# 假设X是你的特征数据
# X = [[0.0183], [0.0765], [0.1132], [0.1302], [0.1587], [0.1778], [0.1965], [0.1999], [0.2105], [0.2832]] + 2
X = np.array([0.0083, 0.1765, 0.1832, 0.1902, 0.1887, 0.1878, 0.1965, 0.1999, 0.2105, 0.2832]).reshape(-1, 1)
# X = [[0.0843], [0.0853], [0.0855], [0.0865], [0.0886], [0.0878], [0.0878], [0.0853], [0.0855], [0.0855]]
# Y = [[0.0220], [0.0863], [0.1211], [0.1332], [0.1615], [0.1801], [0.1963], [0.1981], [0.2032], [0.2815]]
Y = np.array([0.0020, 0.1863, 0.1811, 0.1832, 0.1915, 0.1901, 0.1963, 0.1981, 0.2032, 0.2815]).reshape(-1, 1)
# Y = [[0.0843], [0.0856], [0.0875], [0.0875], [0.0883], [0.0846], [0.0852], [0.0872], [0.0872], [0.0883]]
# 归一化
scaler = MinMaxScaler(feature_range=(0, 1))
# 使用fit_transform方法拟合数据,并转换它
X_scaled = scaler.fit_transform(X).flatten().tolist()
Y_scaled = scaler.fit_transform(Y).flatten().tolist()
print(X_scaled)
print(Y_scaled)
# # 计算R矩阵
# A = np.array([X_scaled, Y_scaled]).T
# W = np.array([0.5003, 0.4997]).T
# R = np.dot(A, W)
#
# print(R)
corr, p = pearsonr(X_scaled, Y_scaled)
print(f'皮尔森相关系数: {corr} p值:{p}')
# 计算X, Y均值和标准差
mean_X = np.mean(X_scaled)
std_X = np.std(X_scaled)
mean_Y = np.mean(Y_scaled)
std_Y = np.std(Y_scaled)
# 创建x值的范围
x = np.linspace(mean_X - 4 * std_X, mean_X + 4 * std_X, 1000)
# 计算两个正态分布的概率密度函数(PDF)
y1 = norm.pdf(x, mean_X, std_X)
y2 = norm.pdf(x, mean_Y, std_Y)
# 创建一个新的图形
plt.figure()
# 绘制第一个正态分布的折线图
plt.plot(x, y1, label=f'NX(μ={mean_X:.4f}, σ={std_X:.4f})')
# 绘制第二个正态分布的折线图
plt.plot(x, y2, label=f'NY(μ={mean_Y:.4f}, σ={std_Y:.4f})')
# 添加图例
plt.legend()
# 添加标题和轴标签
plt.title('Two Normal Distribution Curves with Different Means and Std Devs')
plt.xlabel('X Axis')
plt.ylabel('Probability Density')
# 显示网格
plt.grid(True)
# 显示图形
plt.show()
将y不同的两组数据画两个折线正态分布图在一个画上,并计算两个矩阵相乘
于 2024-03-05 13:43:24 首次发布