dython:Python数据建模宝藏库

简介: dython:Python数据建模宝藏库

尽管已经有了scikit-learnstatsmodelsseaborn等非常优秀的数据建模库,但实际数据分析过程中常用到的一些功能场景仍然需要编写数十行以上的代码才能实现。

而今天要给大家推荐的dython就是一款集成了诸多实用功能的数据建模工具库,帮助我们更加高效地完成数据分析过程中的诸多任务:

通过下面两种方式均可完成对dython的安装:

pip install dython

或:

conda install -c conda-forge dython

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

  • 「data_utils」

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

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

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

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

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

>> one_hot_encode([1,0,5])
[[0. 1. 0. 0. 0. 0.]
 [1. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 1.]]

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

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

  • 「nominal」

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

  • 「model_utils」

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

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

metric_graph()

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

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

  • 「sampling」

sampling子模块则包含了boltzmann_sampling()weighted_sampling()两种数据采样方法,简化数据建模流程。

相关文章
|
10天前
|
JavaScript 前端开发 Python
用python执行js代码:PyExecJS库
文章讲述了如何使用PyExecJS库在Python环境中执行JavaScript代码,并提供了安装指南和示例代码。
54 1
用python执行js代码:PyExecJS库
|
7天前
|
Python
以下是一些常用的图表类型及其Python代码示例,使用Matplotlib和Seaborn库。
以下是一些常用的图表类型及其Python代码示例,使用Matplotlib和Seaborn库。
|
7天前
|
SQL 关系型数据库 MySQL
MySQL操作利器——mysql-connector-python库详解
MySQL操作利器——mysql-connector-python库详解
36 0
|
7天前
|
机器学习/深度学习 数据处理 Python
从NumPy到Pandas:轻松转换Python数值库与数据处理利器
从NumPy到Pandas:轻松转换Python数值库与数据处理利器
21 0
|
10天前
|
Python
turtle库的几个案例进阶,代码可直接运行(python经典编程案例)
该文章展示了使用Python的turtle库进行绘图的进阶案例,包括绘制彩色圆形和复杂图案的代码示例。
51 6
turtle库的几个案例进阶,代码可直接运行(python经典编程案例)
|
2天前
|
调度 开发者 网络架构
探索Python中的异步编程:深入理解asyncio库
【9月更文挑战第32天】在现代软件开发中,异步编程已成为提升性能和响应性的关键策略之一。本文将深入探讨Python的asyncio库,一个强大的异步I/O框架,它允许开发者编写单线程并发代码,同时处理多个任务而无需复杂的多线程或多进程编程。通过本文,你将学习到如何利用asyncio来构建高效、可扩展的应用程序,并了解其背后的原理和设计哲学。
7 2
|
7天前
|
Linux 开发者 iOS开发
Python中使用Colorama库输出彩色文本
Python中使用Colorama库输出彩色文本
|
5天前
|
数据挖掘 Python
【Python】应用:pyproj地理计算库应用
这篇博客介绍了 `pyproj` 地理计算库的应用,涵盖地理坐标系统转换与地图投影。通过示例代码展示了如何进行经纬度与UTM坐标的互转,并利用 `pyproj.Geod` 计算两点间的距离及方位角,助力地理数据分析。 安装 `pyproj`:`pip install pyproj`。更多内容欢迎关注本博客,一起学习进步! Pancake 🍰 不迷路。😉*★,°*:.☆( ̄▽ ̄)/$:*.°★* 😏
10 1
|
10天前
|
Python
turtle库的几个简单案例,代码可直接运行(python经典编程案例)
该文章提供了多个使用Python的turtle库绘制不同图形的简单示例代码,如画三角形、正方形、多边形等,展示了如何通过turtle进行基本的绘图操作。
17 5
|
7天前
|
Linux Android开发 iOS开发
开源的Python库,用于开发多点触控应用程序
Kivy是一款开源Python库,专为开发多点触控应用设计,支持Android、iOS、Linux、OS X和Windows等平台。本文将指导你使用Kivy创建“Hello World”应用并打包成Android APK。首先通过`pip install kivy`安装Kivy,然后创建并运行一个简单的Python脚本。接着,安装Buildozer并通过`buildozer init`生成配置文件,修改相关设置后,运行`buildozer -v android debug`命令打包应用。完成构建后,你将在`./bin/`目录下找到类似`your-app-debug.apk`的文件。
14 2
下一篇
无影云桌面