网站首页 > 开源技术 正文
如何根据变量相关性画出热力图?
调用 corr 来实现变量相关性的计算,同时绘制热力图,颜色越深的话,代表相关性越 强!
1-在电脑前工作
2-站立、走路和上下楼梯
3-站立
4-走路
5-上下楼梯
6-与人边走边聊
7-站立着说话
import pandas as pd
from sklearn.preprocessing import PolynomialFeatures
df = pd.read_csv('./data/activity_recognizer/1.csv', header=None) df.columns = ['index','x','y','z','activity']
x = df[['x','y','z']]
# 多项式扩充数值变量
poly = PolynomialFeatures(degree=2, include_bias=False,interaction_only=False)
x_poly = poly.fit_transform(x)
pd.DataFrame(x_poly, columns=poly.get_feature_names()).head()
# 查看热力图(颜色越深代表相关性越强) %matplotlib inline
import seaborn as snsy = df['activity']
如何把分布修正为类正态分布?
数据下载地址:https://www.kaggle.com/c/house-prices-advanced-regression- techniques/data
import pandas as pd
import numpy as np
# Plots
import seaborn as sns
import matplotlib.pyplot as plt
# 读取数据集
train = pd.read_csv(data') 分布情况,可以调用以下的方法来进行绘制: sns.set_style("white") sns.set_color_codes(palette='deep')
f, ax = plt.subplots(figsize=(8, 7))
#Check the new distribution sns.distplot(train['SalePrice'], color="b"); ax.xaxis.grid(False) ax.set(ylabel="Frequency") ax.set(xlabel="SalePrice") ax.set(title="SalePrice distribution") sns.despine(trim=True, left=True)
plt.show()
从结果可以看出,销售价格是右偏,而大多数机器学习模型都不能很好地处理非正态分布 数据,所以我们可以应用 log(1+x)转换来进行修正。
# log(1+x) 转换
train["SalePrice_log"] = np.log1p(train["SalePrice"])
sns.set_style("white") sns.set_color_codes(palette='deep') f, ax = plt.subplots(figsize=(8, 7))
sns.distplot(train['SalePrice_log'] , fit=norm, color="b");
# 得到正态分布的参数
(mu, sigma) = norm.fit(train['SalePrice_log'])
plt.legend(['Normal dist. ($\mu=$ {:.2f} and $\sigma=$ {:.2f} )'.format(mu, sigma)],
loc='best')
ax.xaxis.grid(False) ax.set(ylabel="Frequency") ax.set(xlabel="SalePrice") ax.set(title="SalePrice distribution") sns.despine(trim=True, left=True)
plt.show()
怎么简单使用 PCA 来划分数据且可视化呢?
PCA,全称为 Principal Component Analysis,也就是主成分分析方法,是一种降维算 法,其功能就是把 N 维的特征,通过转换映射到 K 维上(K<N),这些由原先 N 维的投射 后的 K 个正交特征,就被称为主成分。
使用的数据集 iris # 导入相关库
from sklearn.datasets import load_iris import matplotlib.pyplot as plt
from sklearn.decomposition import PCA %matplotlib inline
#解决中文显示问题,Mac %matplotlib inline
from matplotlib.font_manager import FontProperties # 设置显示的尺寸
plt.rcParams['font.family'] = ['Arial Unicode MS'] #正常显示中文
# 导入数据集
iris = load_iris()
iris_x, iris_y = iris.data, iris.target
# 实例化
pca = PCA(n_components=2)
# 训练数据 pca.fit(iris_x)
pca.transform(iris_x)[:5,]
# 自定义一个可视化的方法
label_dict = {i:k for i,k in enumerate(iris.target_names)} def plot(x,y,title,x_label,y_label):
ax = plt.subplot(111)
for label,marker,color in zip( range(3),('^','s','o'),('blue','red','green')):
plt.scatter(x=x[:,0].real[y == label], y = x[:,1].real[y == label],
color = color,
alpha = 0.5,
label = label_dict[label] )
plt.xlabel(x_label) plt.ylabel(y_label)
leg = plt.legend(loc='upper right', fancybox=True) leg.get_frame().set_alpha(0.5)
plt.title(title)
# 可视化
plot(iris_x, iris_y,"原始的 iris 数据集","sepal length(cm)","sepal width(cm)")
plt.show()
plot(pca.transform(iris_x), iris_y,"PCA 转换后的头两个正交特征 ","PCA1","PCA2")
猜你喜欢
- 2024-10-06 前端开发:JavaScript 实用的幻灯片(二)
- 2024-10-06 lightbox弹出层导致顶部滚动、图片抖动、去掉横线
- 2024-10-06 Hugo博客添加图片放大效果(hugo博客搭建)
- 2024-10-06 机器学习理论类常见面试题(四)(机器学习与技术面试题)
- 2024-10-06 Web端页面加载卡顿缓慢问题解决方法分享
- 2024-06-27 情人节之后,你可能会独自流浪地球……
- 2024-06-27 每年情人节,都有一些礼物让人猝不及防!
- 2024-06-27 《摩斯探长前传》第六季依然精彩,顺带推荐另一部相关剧
- 2024-06-27 Python 可视化处理 Excel 数据
- 2024-06-27 好“吃”的糖果大盘点,这几款你一定要试
你 发表评论:
欢迎- 最近发表
-
- 后端服务太慢?试试这 7 招(后端 服务端 区别)
- 做一个适合二次开发的低代码平台,把程序员从curd中解脱出来-1
- Caffeine缓存 最快缓存 内存缓存(caffeine缓存使用)
- Java性能优化的10大策略(java性能调优从哪几个方面入手)
- New Balance M576PGT 全新配色设计
- x-cmd pkg | qrencode - 二维码生成工具
- 平和精英抽奖概率是多少 平和精英抽奖物品一览
- x-cmd pkg | tmux - 开源终端多路复用器(terminal multiplexer)
- 漫威官方App中文版上线:全站漫画限时免费
- macOS Monterey 12.7.4 (21H1123) 正式版发布,ISO、IPSW、PKG 下载
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)