数据分析 Python
dython是python中的一款数据建模库。尽管已经有了scikit-learn、statsmodels、seaborn等非常优秀的数据建模库,但实际数据分析过程中常用到的一些功能场景仍然需要编写数十行以上的代码才能实现。
而今天推荐的dython就是一款集成了诸多实用功能的数据建模工具库,更加高效地完成数据分析过程中的诸多任务:
2021-08-20-22-51-02-213550.png
通过下面两种方式均可完成对dython的安装:

  1. pip install dython

或:

  1. conda install -c conda-forge dython

dython中目前根据功能分类划分为以下几个子模块:

「data_utils」

data_utils子模块集成了一些基础性的数据探索性分析相关的API,如identify_columns_with_na()可用于快速检查数据集中的缺失值情况:

  1. >> df = pd.DataFrame({'col1': ['a', np.nan, 'a', 'a'], 'col2': [3, np.nan, 2, np.nan], 'col3': [1., 2., 3., 4.]})
  2. >> identify_columns_with_na(df)
  3. column na_count
  4. 1 col2 2
  5. 0 col1 1

identify_columns_by_type()可快速选择数据集中具有指定数据类型的字段:

  1. >> df = pd.DataFrame({'col1': ['a', 'b', 'c', 'a'], 'col2': [3, 4, 2, 1], 'col3': [1., 2., 3., 4.]})
  2. >> identify_columns_by_type(df, include=['int64', 'float64'])
  3. ['col2', 'col3']

one_hot_encode()可快速对数组进行「独热编码」:

  1. >> one_hot_encode([1,0,5])
  2. [[0. 1. 0. 0. 0. 0.]
  3. [1. 0. 0. 0. 0. 0.]
  4. [0. 0. 0. 0. 0. 1.]]

split_hist()则可以快速绘制分组直方图,帮助用户快速探索数据集特征分布:

  1. import pandas as pd
  2. from sklearn import datasets
  3. from dython.data_utils import split_hist
  4. # Load data and convert to DataFrame
  5. data = datasets.load_breast_cancer()
  6. df = pd.DataFrame(data=data.data, columns=data.feature_names)
  7. df['malignant'] = [not bool(x) for x in data.target]
  8. # Plot histogram
  9. split_hist(df, 'mean radius', split_by='malignant', bins=20, figsize=(15,7))

2021-08-20-22-51-02-317519.png

「nominal」

nominal子模块包含了一些进阶的特征相关性度量功能,例如其中的associations()可以自适应由连续型和类别型特征混合的数据集,并自动计算出相应的Pearson、Cramer’s V、Theil’s U、条件熵等多样化的系数;cluster_correlations()可以绘制出基于层次聚类的相关系数矩阵图等实用功能:
2021-08-20-22-51-02-498654.png
2021-08-20-22-51-02-668656.png

「model_utils」

model_utils子模块包含了诸多对机器学习模型进行性能评估的工具,如ks_abc()

  1. from sklearn import datasets
  2. from sklearn.model_selection import train_test_split
  3. from sklearn.linear_model import LogisticRegression
  4. from dython.model_utils import ks_abc
  5. # Load and split data
  6. data = datasets.load_breast_cancer()
  7. X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=.5, random_state=0)
  8. # Train model and predict
  9. model = LogisticRegression(solver='liblinear')
  10. model.fit(X_train, y_train)
  11. y_pred = model.predict_proba(X_test)
  12. # Perform KS test and compute area between curves
  13. ks_abc(y_test, y_pred[:,1])

2021-08-20-22-51-02-801681.png
metric_graph()

  1. import numpy as np
  2. from sklearn import svm, datasets
  3. from sklearn.model_selection import train_test_split
  4. from sklearn.preprocessing import label_binarize
  5. from sklearn.multiclass import OneVsRestClassifier
  6. from dython.model_utils import metric_graph
  7. # Load data
  8. iris = datasets.load_iris()
  9. X = iris.data
  10. y = label_binarize(iris.target, classes=[0, 1, 2])
  11. # Add noisy features
  12. random_state = np.random.RandomState(4)
  13. n_samples, n_features = X.shape
  14. X = np.c_[X, random_state.randn(n_samples, 200 * n_features)]
  15. # Train a model
  16. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5, random_state=0)
  17. classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True, random_state=0))
  18. # Predict
  19. y_score = classifier.fit(X_train, y_train).predict_proba(X_test)
  20. # Plot ROC graphs
  21. metric_graph(y_test, y_score, 'pr', class_names=iris.target_names)

2021-08-20-22-51-02-916654.png

  1. import numpy as np
  2. from sklearn import svm, datasets
  3. from sklearn.model_selection import train_test_split
  4. from sklearn.preprocessing import label_binarize
  5. from sklearn.multiclass import OneVsRestClassifier
  6. from dython.model_utils import metric_graph
  7. # Load data
  8. iris = datasets.load_iris()
  9. X = iris.data
  10. y = label_binarize(iris.target, classes=[0, 1, 2])
  11. # Add noisy features
  12. random_state = np.random.RandomState(4)
  13. n_samples, n_features = X.shape
  14. X = np.c_[X, random_state.randn(n_samples, 200 * n_features)]
  15. # Train a model
  16. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5, random_state=0)
  17. classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True, random_state=0))
  18. # Predict
  19. y_score = classifier.fit(X_train, y_train).predict_proba(X_test)
  20. # Plot ROC graphs
  21. metric_graph(y_test, y_score, 'roc', class_names=iris.target_names)

2021-08-20-22-51-03-043660.png

「sampling」

sampling子模块则包含了boltzmann_sampling()weighted_sampling()两种数据采样方法,简化数据建模流程。
2021-08-20-22-51-03-187653.png
dython作为一个处于快速开发迭代过程的Python库,陆续会有更多的实用功能引入,感兴趣的朋友们可以前往https://github.com/shakedzy/dython查看更多内容或对此项目保持关注。