kaggle-top50
top50
的数据是kaggle官网上关于一个音乐
的数据集。
There are 50 songs and 13 variables to be explored
新知识
数据本身是比较完美的,没有涉及到太多的数据预处理的工作,主要是学习到了多种图形的绘制
- 直方图
- 直方图+折线
- 热力图
- 饼图
- 等高线图
属性
分析过程
导入库和包
1 | import pandas as pd |
数据查看
1 | filename='/Users/piqianchao/data-visualization/top50.csv' |
属性重命名rename
1 | data.rename(columns={'Track.Name':'track_name','Artist.Name':'artist_name','Beats.Per.Minute':'beats_per_minute','Loudness..dB..':'Loudness(dB)','Valence.':'Valence','Length.':'Length', 'Acousticness..':'Acousticness','Speechiness.':'Speechiness'},inplace=True) |
Calculating the number of songs of each genre
1 | popular_genre = data.groupby('Genre').size() # 根据类别分组,再统计每个类别多少首歌 |
Calculating the number of songs by each of the artists
1 | popular_artist = data.groupby('artist_name').size() # 统计每个作家几首歌 |
查看属性的统计信息
1 | pd.set_option('precision', 3) # 设置最多显示的小数位 |
Finding out the skew for each attribute
找出每个属性的偏度skew
1 | skew = data.skew() # skew是偏态,偏态系数 |
1 | transform = np.asarray(data[['Liveness']].values) # 取出每个Liveness的值,转成ndarray型数据 |
如何在直方图的基础上画出折线趋势
1 | transform1 = np.asarray(data[['Popularity']].values) |
Bar graph to see the number of songs of each genre
1 | fig, ax = plt.subplots(figsize=(30,12)) # 指定画布大小 |
相关系数correction
如何求解相关系数
1 | pd.set_option('display.width', 100) # 每行最多显示的数据量为100,多的话就隔行再显示 |
8.2 根据相关系数画出热力图
1 | plt.figure(figsize=(10,10)) |
barh of most popular artists
1 | fig, ax=plt.subplots(figsize=(12,12)) |
Analysing the relationship between energy and loudness
1 | fig = plt.subplots(figsize=(10,10)) |
Dependence between energy and popularity
1 | fig = plt.subplots(figsize=(10,10)) |
1 | plt.figure(figsize=(14,8)) |
Pie charts 饼图
通过每个歌手和其歌曲数目制作饼图
1 | labels = data.artist_name.value_counts().index # 每小块的标签 |
Linear Regression
数据构建和TTS
1 | # 构建训练集和测试集 |
预测
1 | # 进行预测,真实值和预测值之间的比较 |
1 | # 计算LR的准确率:MAE:mean absolute error;MSE: mean sqaured error |
交叉验证
1 | x = data.loc[:, ['Energy', 'Danceability']].values |