python機(jī)器學(xué)習(xí)生物信息學(xué)系列課(博主錄制):http://dwz.date/b9vw
測(cè)試腳本
測(cè)試數(shù)據(jù)
T
is an array of durations, E
is a either boolean or binary array representing whether the “death” was observed (alternatively an individual can be censored).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import lifelines from lifelines.datasets import load_waltons df = load_waltons() # returns a Pandas DataFrame T = df[ 'T' ] E = df[ 'E' ] from lifelines import KaplanMeierFitter kmf = KaplanMeierFitter() kmf.fit(T, event_observed = E) # more succiently, kmf.fit(T,E) kmf.survival_function_ ''' Out[7]: KM_estimate timeline 0.0 1.000000 6.0 0.993865 7.0 0.987730 9.0 0.969210 13.0 0.950690 15.0 0.938344 17.0 0.932170 19.0 0.913650 22.0 0.888957 26.0 0.858090 29.0 0.827224 32.0 0.821051 33.0 0.802531 36.0 0.790184 38.0 0.777837 41.0 0.734624 43.0 0.728451 45.0 0.672891 47.0 0.666661 48.0 0.616817 51.0 0.598125 ''' kmf.median_ ''' Out[8]: 56.0 ''' kmf.plot() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import lifelines from lifelines.datasets import load_waltons from lifelines import KaplanMeierFitter df = load_waltons() # returns a Pandas DataFrame kmf = KaplanMeierFitter() T = df[ 'T' ] E = df[ 'E' ] groups = df[ 'group' ] ix = (groups = = 'miR-137' ) kmf.fit(T[~ix], E[~ix], label = 'control' ) ax = kmf.plot() kmf.fit(T[ix], E[ix], label = 'miR-137' ) kmf.plot(ax = ax) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import numpy as np import matplotlib.pyplot as plt from lifelines.plotting import plot_lifetimes from numpy.random import uniform, exponential N = 25 current_time = 10 actual_lifetimes = np.array([[exponential( 12 ), exponential( 2 )][uniform()< 0.5 ] for i in range (N)]) observed_lifetimes = np.minimum(actual_lifetimes,current_time) observed = actual_lifetimes < current_time plt.xlim( 0 , 25 ) plt.vlines( 10 , 0 , 30 ,lw = 2 , linestyles = '--' ) plt.xlabel( 'time' ) plt.title( 'Births and deaths of our population, at $t=10$' ) plot_lifetimes(observed_lifetimes, event_observed = observed) print 'Observed lifetimes at time %d:\n' % (current_time), observed_lifetimes |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import pandas as pd import lifelines from lifelines import KaplanMeierFitter import matplotlib.pyplot as plt data = lifelines.datasets.load_dd() kmf = KaplanMeierFitter() T = data[ 'duration' ] C = data[ 'observed' ] kmf.fit(T, event_observed = C ) plt.title( 'Survival function of political regimes' ) kmf.survival_function_.plot() kmf.plot() kmf.median_ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import pandas as pd import lifelines from lifelines import KaplanMeierFitter import matplotlib.pyplot as plt data = lifelines.datasets.load_dd() kmf = KaplanMeierFitter() T = data[ 'duration' ] C = data[ 'observed' ] kmf.fit(T, event_observed = C ) plt.title( 'Survival function of political regimes' ) kmf.survival_function_.plot() kmf.plot() ax = plt.subplot( 111 ) dem = (data[ 'democracy' ] = = 'Democracy' ) kmf.fit(T[dem], event_observed = C[dem], label = 'Democratic Regimes' ) kmf.plot(ax = ax, ci_force_lines = True ) kmf.fit(T[~dem], event_observed = C[~dem], label = 'Non-democratic Regimes' ) plt.ylim( 0 , 1 ); plt.title( 'Lifespans of different global regimes' ) kmf.plot(ax = ax, ci_force_lines = True ) |
應(yīng)用于保險(xiǎn)業(yè),病人治療,信用卡詐騙
信用卡拖欠
具體文檔
http://lifelines.readthedocs.io/en/latest/
https://wenku.baidu.com/view/577041d3a1c7aa00b52acb2e.html?from=search
https://wenku.baidu.com/view/a5adff8b89eb172ded63b7d6.html?from=search
https://github.com/thomas-haslwanter/statsintro_python/tree/master/ISP/Code_Quantlets/10_SurvivalAnalysis/lifelinesDemo
測(cè)試代碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | # -*- coding: utf-8 -*- # Import standard packages import numpy as np import matplotlib.pyplot as plt from numpy.random import uniform, exponential import os # additional packages from lifelines.plotting import plot_lifetimes import sys sys.path.append(os.path.join( '..' , '..' , 'Utilities' )) try : # Import formatting commands if directory 'Utilities' is available from ISP_mystyle import setFonts except ImportError: # Ensure correct performance otherwise def setFonts( * options): return # Generate some dummy data np.set_printoptions(precision = 2 ) N = 20 study_duration = 12 # Note: a constant dropout rate is equivalent to an exponential distribution! actual_subscriptiontimes = np.array([[exponential( 18 ), exponential( 3 )][uniform()< 0.5 ] for i in range (N)]) observed_subscriptiontimes = np.minimum(actual_subscriptiontimes,study_duration) observed = actual_subscriptiontimes < study_duration # Show the data setFonts( 18 ) plt.xlim( 0 , 24 ) plt.vlines( 12 , 0 , 30 , lw = 2 , linestyles = '--' ) plt.xlabel( 'time' ) plt.title( 'Subscription Times, at $t=12$ months' ) plot_lifetimes(observed_subscriptiontimes, event_observed = observed) print ( 'Observed subscription time at time %d:\n' % (study_duration), observed_subscriptiontimes) |
where k > 0 is the shape parameter and > 0 is the scale parameter of the
distribution. (It is one of the rare cases where we use a shape parameter different
from skewness and kurtosis.) Its complementary cumulative distribution function is
a stretched exponential function.
If the quantity x is a “time-to-failure,” theWeibull distribution gives a distribution
for which the failure rate is proportional to a power of time. The shape parameter, k,
is that power plus one, and so this parameter can be interpreted directly as follows:
· Avalueofk < 1 indicates that the failure rate decreases over time. This happens
if there is significant “infant mortality,” or defective items failing early and the
failure rate decreasing over time as the defective items are weeded out of the
population.
· Avalueofk D 1 indicates that the failure rate is constant over time. This might
suggest random external events are causing mortality, or failure.
· Avalueofk > 1 indicates that the failure rate increases with time. This happens
if there is an “aging” process, or parts that are more likely to fail as time goes on.
An example would be products with a built-in weakness that fail soon after the
warranty expires.
In the field of materials science, the shape parameter k of a distribution of
strengths is known as the Weibull modulus.
聯(lián)系客服