总由于Pandas严格的数据结构要求让你感觉到很苦恼?为了实现一个简单的操做也要查阅不少的文档而头疼?html
DaPy来解放你啦!你能够用DaPy流利地实现脑子早已思索好的想法,再也不由于找不到API或者数据格式报错而打断你的思路!DaPy是一个从设计开始就很是关注易用性的数据分析框架,它专为数据分析师而设计,而不是程序员。对于数据分析师而言,你的价值是解决问题思路!而不是害得你996的几百行代码!mysql
不要小看浏览数据的方式!对于数据分析师而言,感知数据是很是重要的!git
>>> from DaPy.datasets import iris
>>> sheet, info = iris()
- read() in 0.001s.
>>> sheet
sheet:data
==========
sepal length: <5.1, 4.9, 4.7, 4.6, 5.0, ... ,6.7, 6.3, 6.5, 6.2, 5.9>
sepal width: <3.5, 3.0, 3.2, 3.1, 3.6, ... ,3.0, 2.5, 3.0, 3.4, 3.0>
petal length: <1.4, 1.4, 1.3, 1.5, 1.4, ... ,5.2, 5.0, 5.2, 5.4, 5.1>
petal width: <0.2, 0.2, 0.2, 0.2, 0.2, ... ,2.3, 1.9, 2.0, 2.3, 1.8>
class: <setos, setos, setos, setos, setos, ... ,virginic, virginic, virginic, virginic, virginic>
>>> sheet.info
sheet:data
==========
1. Structure: DaPy.SeriesSet
2. Dimensions: Lines=150 | Variables=5
3. Miss Value: 0 elements
Descriptive Statistics
=======================================================================================
Title | Miss | Min | Mean | Max | Std | Mode
--------------+------+-----------+-----------+-------------+--------------+------------
sepal length | 0 | 4.300001 | 5.8433333 | 7.900000095 | 0.8253012767 | 5
sepal width | 0 | 2 | 3.0540000 | 4.400000095 | 0.4321465798 | 3
petal length | 0 | 1 | 3.7586666 | 6.900000095 | 1.758529178 | 1.5
petal width | 0 | 0.1000000 | 1.1986666 | 2.5 | 0.7606126088 | 0.2
class | 0 | - | - | - | - | setos
=======================================================================================
>>> sheet.show(5)
sheet:data
==========
sepal length | sepal width | petal length | petal width | class
--------------+-------------+--------------+-------------+----------
5.1 | 3.5 | 1.4 | 0.2 | setos
4.9 | 3.0 | 1.4 | 0.2 | setos
4.7 | 3.2 | 1.3 | 0.2 | setos
4.6 | 3.1 | 1.5 | 0.2 | setos
5.0 | 3.6 | 1.4 | 0.2 | setos
.. Omit 140 Ln ..
6.7 | 3.0 | 5.2 | 2.3 | virginic
6.3 | 2.5 | 5.0 | 1.9 | virginic
6.5 | 3.0 | 5.2 | 2.0 | virginic
6.2 | 3.4 | 5.4 | 2.3 | virginic
5.9 | 3.0 | 5.1 | 1.8 | virginic
复制代码
按行处理数据是符合咱们每个人想法的,所以几乎全部的数据库设计都是按照按行存储的。因为Pandas最先是为了处理时间序列数据而开发的,因此他的数据是以列进行的存储。虽然这种存储方式在全局处理上表现出了不错的性能,但没优化状况下行操做却让人较为难以忍受的。因为没有什么更好的替代品,人们不得不花不少时间去适应Pandas的编程思惟。好比,Pandas不支持对于DataFrame.iterrows()
迭代出来的行进行赋值操做。这个功能即便如此经常使用,在NumPy中也是原生支持的功能在Pandas里倒是被禁止的。程序员
针对这类由行操做引起的问题,DaPy经过引入“视图”的概念从新优化了按行操做这个符合人们习惯的操做方式。github
>>> import DaPy as dp
>>> sheet = dp.SeriesSet({'A': [1, 2, 3], 'B': [4, 5, 6]})
>>> for row in sheet:
print(row.A, row[0]) # 按照下标或者列名访问行数据的值
row[1] = 'b' # 用下表为行赋值
1, 1
2, 2
3, 3
>>> sheet.show() # 你的操做会映射到原表中
A | B
---+---
1 | b
2 | b
3 | b
>>> row0 = sheet[0] # 拿到行的索引
>>> row0
[1, 'b']
>>> sheet.append_col(series=[7, 8, 9], variable_name='newColumn') # 为表添加新列
>>> sheet.show()
A | B | newColumn
---+---+-----------
1 | b | 7
2 | b | 8
3 | b | 9
>>> row0 # 表的操做会时时刻刻反映到行上
[1, 'b', 7]
复制代码
让咱们来作一个稍微有趣点的链式表达! 我但愿对于经典的鸢尾花数据集在一行代码中完成下面的6个操做。sql
(1)对于每一列数据分别进行标准化操做;数据库
(2)而后找到在标准化之后知足sepal length小于petal length的记录;编程
(3)对于筛选出来的数据集按照鸢尾花的类别class进行分组;bash
(4)对于每一个分组都按照petal width进行升序排序;数据结构
(5)对于排好序后的分组选取前10行记录;
(6)对于每一个由前十行记录构成的子数据集进行描述性统计;
>>> from DaPy.datasets import iris
>>> sheet, info = iris()
- read() in 0.001s.
>>> sheet.normalized().query('sepal length < petal length').groupby('class').sort('petal width')[:10].info
- normalized() in 0.005s.
- query() in 0.000s.
- groupby() in 0.000s.
- sort() in 0.000s.
sheet:('virginic',)
===================
1. Structure: DaPy.SeriesSet
2. Dimensions: Lines=10 | Variables=5
3. Miss Value: 0 elements
Descriptive Statistics
=======================================================================================
Title | Miss | Min | Mean | Max | Std | Mode
--------------+------+-----------+----------+------------+--------------+--------------
sepal length | 0 | 0.16666 | 0.572218 | 0.83333331 | 0.177081977 | 0.5555555173
sepal width | 0 | 0.0833333 | 0.295832 | 0.41666665 | 0.102824685 | 0.3749999851
petal length | 0 | 0.593220 | 0.747457 | 0.89830505 | 0.0852358577 | 0.8135593089
petal width | 0 | 0.541666 | 0.654166 | 0.70833331 | 0.0619419457 | 0.7083333332
class | 0 | - | - | - | - | virginic
=======================================================================================
sheet:('setos',)
================
1. Structure: DaPy.SeriesSet
2. Dimensions: Lines=6 | Variables=5
3. Miss Value: 0 elements
Descriptive Statistics
=======================================================================================
Title | Miss | Min | Mean | Max | Std | Mode
--------------+------+-----------+----------+-----------+--------------+---------------
sepal length | 0 | -5.29e-08 | 0.050925 | 0.138888 | 0.0465272020 | 0.02777772553
sepal width | 0 | 0.375 | 0.45833 | 0.583333 | 0.0680413746 | 0.4166666501
petal length | 0 | 0.0169 | 0.070621 | 0.152542 | 0.0419945431 | 0.05084745681
petal width | 0 | -6.20e-10 | 0.034722 | 0.0416666 | 0.0155282505 | 0.04166666607
class | 0 | - | - | - | - | setos
=======================================================================================
sheet:('versicolo',)
====================
1. Structure: DaPy.SeriesSet
2. Dimensions: Lines=10 | Variables=5
3. Miss Value: 0 elements
Descriptive Statistics
=======================================================================================
Title | Miss | Min | Mean | Max | Std | Mode
--------------+------+----------+----------+------------+--------------+--------------
sepal length | 0 | 0.16666 | 0.308333 | 0.47222217 | 0.10126514 | 0.1944443966
sepal width | 0 | 0 | 0.16666 | 0.29166665 | 0.0790569387 | 0.16666666
petal length | 0 | 0.338983 | 0.442372 | 0.52542370 | 0.0564434752 | 0.3898305022
petal width | 0 | 0.375 | 0.38749 | 0.41666665 | 0.0190940608 | 0.3749999996
class | 0 | - | - | - | - | versicolo
=======================================================================================
复制代码
>>> sheet.A + sheet.B # 下标访问列而且作四则运算
>>> sheet[sheet.A > sheet.B] # 这个很是Pythonic的切片写法!
复制代码
咱们都会遇到过一个问题,怎么把csv转换成Excel;或者反过来,Excel转回csv?
>>> from DaPy.datasets import iris
>>> sheet, info = iris()
>>> sheet.groupby('class').save('iris.xls') # 对!直接链式表达转成了xls! 别忘了Excel是支持多子表的,因此刚刚groupby以后DaPy给你存了三个子表!
- groupby() in 0.000s.
- save() in 0.241s.
>>> import DaPy as dp
>>> dp.read('iris.xls').shape # DaPy居然又一次性读完了三个表!!!
- read() in 0.004s.
sheet:('virginic',)
===================
sheet(Ln=50, Col=5)
sheet:('setos',)
================
sheet(Ln=50, Col=5)
sheet:('versicolo',)
====================
sheet(Ln=50, Col=5)
复制代码
你觉得read函数就这点水平吗?让咱们来看看更骚的!!!
>>> import DaPy as dp
>>> dp.read('iris.xls').save('iris.db') # Excel 转 Sqlite3
>>> dp.read('iris.sav').save('iris.html') # SPSS 转 HTML
>>> dp.read('https://sofifa.com/players').save('mysql://root:123123@localhost:3306/fifa_db') # 爬取FIFA球员数据并存入MySQL数据库
>>> dp.read('mysql://root:123123@localhost:3306/fifa_db').save('fifa.csv') # MySQL数据库 转 CSV
复制代码
先来一些数据预处理的
>>> sheet.drop_duplicates(keep='first') #删除重复记录
>>> sheet.fillna(method='linear') #线性插值法填充缺失值
>>> sheet.drop('ID', axis=1) # 删除无用变量
>>> sheet.count_values('gender') # 对于某个变量进行计数统计
复制代码
再来一些特征工程的
>>> sheet.get_date_label('birth') # 对日期变量作变化,会自动生成一大堆周期性变量
>>> sheet.get_categories(cols='age', cutpoints=[18, 30, 50], group_name=['青年', '壮年', '中年', '老年']) # 对于连续型变量进行封箱操做
>>> sheet.get_dummies(['city', 'education']) # 对于分类变量进行虚拟变量的引入
>>> sheet.get_interactions(n_power=3, col=['income', 'age', 'gender', 'education']) # 为你选定的变量之间构成高阶交叉项,阶数n_power能够随便填!!!
复制代码
在DaPy里面,已经内置了四个模型,分别是线性回归、逻辑回归、多层感知机和C4.5决策树。在模型这一块的话,DaPy的开发团队认为sklearn和tensorflow已经作得很好了。出于开发团队主要成员是统计系学生的关系,他们的思路是增长更多的统计学检验报告~ 咱们先看看一个demo级别的样例好了
>>> from DaPy.datasets import iris
>>> sheet, info = iris()
- read() in 0.001s.
>>> sheet = sheet.shuffle().normalized()
- shuffle() in 0.001s.
- normalized() in 0.005s.
>>> X, Y = sheet[:'petal width'], sheet['class']
>>>
>>> from DaPy.methods.classifiers import MLPClassifier
>>> mlp = MLPClassifier().fit(X[:120], Y[:120])
- Structure | Input:4 - Dense:4 - Output:3
- Finished: 0.2% Epoch: 1 Rest Time: 0.24s Accuracy: 0.33%
### 这里我删掉了一些日志 ###
- Finished: 99.8% Epoch: 500 Rest Time: 0.00s Accuracy: 0.88%
- Finish Train | Time:1.9s Epoch:500 Accuracy:88.33%
>>>
>>> from DaPy.methods.evaluator import Performance
>>> Performance(mlp, X[120:], Y[120:], mode='clf') # 性能测试包括了正确率、kappa系数和混淆矩阵,二分类任务会包含AUC
- Classification Accuracy: 86.6667%
- Classification Kappa: 0.8667
┏ ┓
┃ 11 0 0 11 ┃
┃ 0 8 1 9 ┃
┃ 0 3 7 10 ┃
┃11.0 11.0 8.0 30.0┃
┗ ┛
复制代码