1. 程式人生 > 其它 >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()兩種資料取樣方法,簡化資料建模流程。

  dython作為一個處於快速開發迭代過程的Python庫,陸續會有更多的實用功能引入,感興趣的朋友們可以前往https://github.com/shakedzy/dython檢視更多內容或對此專案保持關注。


  以上就是本文的全部內容,歡迎在評論區與我進行討論~