您好,欢迎访问一九零五行业门户网

Python For Data Analysis学习之路

在引言章节里,介绍了movielens 1m数据集的处理示例。书中介绍该数据集来自grouplens research(),该地址会直接跳转到,这里面提供了来自movielens网站的各种评估数据集,可以下载相应的压缩包,我们需要的movielens 1m数据集也在里面。
下载解压后的文件夹如下:
这三个dat表都会在示例中用到。我所阅读的《python for data analysis》中文版(pdf)是2014年第一版的,里面所有示例都是基于python 2.7和pandas 0.8.2所写的,而我安装的是python 3.5.2与pandas 0.20.2,里面的一些函数与方法会有较大的不同,有些是新版本中参数改变了,而有些是新版本里弃用了某些旧版本的函数,这导致我运行按照书中示例代码时,会遇到一些error和warning。在测试movielens 1m数据集代码时,在和一样我的配置环境下,会遇到如下几个问题。
在将dat数据读入到pandas dataframe对象中时,书中给出代码为: 
users = pd.read_table('ml-1m/users.dat', sep='::', header=none, names=unames) rnames = ['user_id', 'movie_id', 'rating', 'timestamp'] ratings = pd.read_table('ml-1m/ratings.dat', sep='::', header=none, names=rnames) mnames = ['movie_id', 'title', 'genres'] movies = pd.read_table('ml-1m/movies.dat', sep='::', header=none, names=mnames)
直接运行会出现warning:
f:/python/helloworld/dataanalysisbypython-1.py:4: parserwarning: falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.   users = pd.read_table('ml-1m/users.dat', sep='::', header=none, names=unames) f:/python/helloworld/dataanalysisbypython-1.py:7: parserwarning: falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.   ratings = pd.read_table('ml-1m/ratings.dat', sep='::', header=none, names=rnames) f:/python/helloworld/dataanalysisbypython-1.py:10: parserwarning: falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.   movies = pd.read_table('ml-1m/movies.dat', sep='::', header=none, names=mnames)
虽然也能运行,但是作为完美强迫症的我还是想要解决这个warning。这个警告是说因为'c'引擎不支持,只能退回到'python'引擎,而刚好pandas.read_table方法里有个engine参数,用来设置使用哪种解析引擎,有'c'和'python'这两个选项。既然'c'引擎不支持,我们只需把engine设为'python'就可以了。
users = pd.read_table('ml-1m/users.dat', sep='::', header=none, names=unames, engine = 'python') rnames = ['user_id', 'movie_id', 'rating', 'timestamp'] ratings = pd.read_table('ml-1m/ratings.dat', sep='::', header=none, names=rnames, engine = 'python') mnames = ['movie_id', 'title', 'genres'] movies = pd.read_table('ml-1m/movies.dat', sep='::', header=none, names=mnames, engine = 'python')
使用pivot_table方法来对聚合后的数据按性别计算每部电影的平均得分,书中给出的代码为:
mean_ratings = data.pivot_table('rating', rows='title', cols='gender', aggfunc='mean')
直接运行会报错,这段代码无法运行:
traceback (most recent call last):   file f:/python/helloworld/dataanalysisbypython-1.py, line 19, in <module>mean_ratings = data.pivot_table('rating', rows='title', cols='gender', aggfunc='mean') typeerror: pivot_table() got an unexpected keyword argument 'rows'
typeerror说明这里的'rows'参数并不是方法里可用的关键字参数,这是这么回事呢?去官网上查了下pandas的api使用文档(),发现是因为0.20.2版的pandas.pivot_table里关键字参数变了,为了实现同样效果,只需把rows换成index就可以了,同时也没有cols参数,要用columns来代替。
mean_ratings = data.pivot_table('rating', index='title', columns='gender', aggfunc='mean')
为了了解女性观众最喜欢的电影,使用dataframe的方法对f列进行降序排序,书中的示例代码为:
top_female_ratings = mean_ratings.sort_index(by='f', ascending=false)
这里也只是给出一个warning,并不会干扰程序进行:
f:/python/helloworld/dataanalysisbypython-1.py:32: futurewarning: by argument to sort_index is deprecated, pls use .sort_values(by=...)   top_female_ratings = mean_ratings.sort_index(by='f', ascending=false)
这里是说进行排序的sort_index方法在将来语言或者库中可能发生改变,建议改为使用sort_values。在api使用文档中,对pandas.dataframe.sort_index的描述为“sort object by labels (along an axis)”,而对pandas.dataframe.sort_values的描述为“sort by the values along either axis”,两者能达到同样效果,那我就直接替换成sort_values就可以了。在后面的“计算评分分歧”中也会用到sort_index,也可以替换成sort_values。
top_female_ratings = mean_ratings.sort_values(by='f', ascending=false)
最后一个错误还是和排序有关。在“计算评分分歧”中计算得分数据的标准差之后,根据过滤后的值对series进行降序排序,书中的代码为:
print(rating_std_by_title.order(ascending=false)[:10])
这里的错误是:
traceback (most recent call last):   file f:/python/helloworld/dataanalysisbypython-1.py, line 47, in <module>print(rating_std_by_title.order(ascending=false)[:10])   file e:\program files\python35\lib\site-packages\pandas\core\generic.py, line 2970, in __getattr__return object.__getattribute__(self, name) attributeerror: 'series' object has no attribute 'order'
居然已经没有这个order的方法了,只好去api文档中找替代的方法用。有两个,sort_index和sort_values,这和dataframe中的方法一样,为了保险起见,我选择使用sort_values:
print(rating_std_by_title.sort_values(ascending=false)[:10]
得到的结果和数据展示的结果一样,可以放心使用。
第三方库不同版本间的差异还是挺明显的,建议是使用最新的版本,在使用时配合官网网站上的api使用文档,轻松解决各类问题~
以上就是python for data analysis学习之路的详细内容。
其它类似信息

推荐信息