GVKun编程网logo

Python ARIMA 模型 - 对数据进行差分后缺失值(python差分函数)

12

如果您对PythonARIMA模型-对数据进行差分后缺失值感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于PythonARIMA模型-对数据进行差分后缺失值的详细内容,我们还

如果您对Python ARIMA 模型 - 对数据进行差分后缺失值感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于Python ARIMA 模型 - 对数据进行差分后缺失值的详细内容,我们还将为您解答python差分函数的相关问题,并且为您提供关于2.3 Hive 的数据类型讲解及实际项目中如何使用 python 脚本对数据进行 ETL、ARIMA模型在新的statsmodels ARIMA for python中无法正常工作、oracle 数据库表字段的缺失值统计 -- 基于python、python sklearn与pandas实现缺失值数据预处理流程详解的有价值信息。

本文目录一览:

Python ARIMA 模型 - 对数据进行差分后缺失值(python差分函数)

Python ARIMA 模型 - 对数据进行差分后缺失值(python差分函数)

如何解决Python ARIMA 模型 - 对数据进行差分后缺失值?

我在 python 中使用 ARIMA 模型进行时间序列预测。分析数据后,我注意到数据是季节性的,有一个趋势。为了确保我的假设,我运行了 ADFuller 测试,并且数据确实不是平稳的。

为了使数据稳定,我对数​​据进行了一次差分并绘制了它。然后,我尝试再次运行 ADFuller 测试,但出现以下错误:

MissingDataError: exog 包含 inf 或 nans

这是 ADFuller 测试的代码:

result = adfuller(df[''power''])
print(result)

差分数据是否会产生缺失值?我在原始数据中没有任何缺失值。我只有在对数据进行差分后才会收到此错误。我真的不明白这是怎么回事。任何帮助表示赞赏。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

2.3 Hive 的数据类型讲解及实际项目中如何使用 python 脚本对数据进行 ETL

2.3 Hive 的数据类型讲解及实际项目中如何使用 python 脚本对数据进行 ETL

一、hive Data Types

https://cwiki. apache. org/confluence/display/HiveLanguageManual+Types

Numeric Types
        · TINYINT(1-byte signed integer, from-128 to 127)
        · SMALLINT(2-byte signed integer, from-32,768 to 32,767)
        · INT(4-byte signed integer, from-2,147,483,648 to 2,147,483,647)
        · BIGINT(8-byte signed integer, from-9,223,372,036,854,775,808 to9
        · FLOAT(4-byte single precision floating point number)
        · DOUBLE(8-byte double precision floating point number)
        · DECIMAL
                · Introduced in Hive 0.11.0 with a precision of 38 digits
                · Hive 0.13.0 introduced user definable precision and scale


Date/Time Types
        · TIMESTAMP(Note: Only available starting with Hive 0.8.0)
        · DATE(Note: Only available starting with Hive 0.12.0)


String Types
       · STRING
    · VARCHAR(Note: Only available starting with Hive 0.12.0)
    · CHAR(Note: Only available starting with Hive 0.13.0)


Misc Types
    · BOOLEAN
    · BINARY(Note: Only available starting with Hive 0.8.0)



Complex Types
    · arrays: ARRAY<data_type>(Note: negative values and non-constant expressions are allowed as of Hive 0.14.)
    · maps: MAP<primitivetype, data_type>(Note: negative values and non-constant expressions are allowed as of Hive 0.14.)
    · structs: STRUCT<col_name: datatype [ COMENT col_comment],..>
    · union: UNIONTYPE<datatype, data_type,..>(Note: Only available starting with Hive 0.7.0.)


二、Primitive Types

·Types are associated with the columns in the tables.The following Primitive types are
supported:

·Integers
    ·TINYINT-1 byte integer
    ·SMALLINT-2 byte integer
    ·INT-4 byte integer
    ·BIGINT-8 byte integer


·Boolean type
    ·BOOLEAN-TRUE/FALSE


·Floating point numbers
    ·FLOAT-single precision
    ·DOUBLE-Double precision


·String type
    ·STRING-sequence of characters in a specified character set


https://cwiki.apache.org/confluence/display/Hive/Tutorial


三、python 脚本对数据进行 ETL 流程

1)table, load           E

2)select, python     T

3)sub table             L

ARIMA模型在新的statsmodels ARIMA for python中无法正常工作

ARIMA模型在新的statsmodels ARIMA for python中无法正常工作

如何解决ARIMA模型在新的statsmodels ARIMA for python中无法正常工作?

我以前曾经使用过

from statsmodels.tsa.arima_model import ARIMA
model = ARIMA(log_air_passengers,order=(2,1,0))  
results_AR = model.fit(disp=-1)  
plt.plot(log_air_passengers_diff)
plt.plot(results_AR.fittedvalues,color=''red'')
plt.title(''RSS: %.4f''% sum((results_AR.fittedvalues-log_air_passengers_diff)**2))

我会得到的情节是:

enter image description here

但是,现在他们有了ARIMA的较新版本,而旧版本将在0.12发行版之后被删除。因此,我正在尝试较新的版本:

from statsmodels.tsa.arima.model import ARIMA as ARIMA2
model = ARIMA2(log_air_passengers,0),missing=''drop'')  
results_AR = model.fit()  
plt.plot(log_air_passengers_diff)
plt.plot(results_AR.fittedvalues,color=''red'')
plt.title(''RSS: %.4f''% sum((results_AR.fittedvalues-log_air_passengers_diff)**2))

剧情也很糟糕:

enter image description here

但这是行不通的。我试图弄弄它,但没有太大帮助。如何更改代码以使其正常工作?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

oracle 数据库表字段的缺失值统计 -- 基于python

oracle 数据库表字段的缺失值统计 -- 基于python

 

  参考资料:python 连接oracle -- sqlalchemy及cx_Oracle的使用详解

  oracle指定表缺失值统计 -- 基于cx_Oracle

import pandas as pd
import cx_Oracle as orcl

# 批量查询数据缺失率
def missing_count(table_name, where_condition={}, **engine):
    #where 条件参数化, str或dict
    sql_tab_columns = "select column_name from user_tab_columns \
        where table_name = ''{}''".format(table_name)

    db = ConnectOracle(**engine)
    #sql_select.encode(''utf-8'')
    columns = db.select_oracle(sql=sql_tab_columns)
    
    #生成select语句
    ss = ''''
    for col in columns.COLUMN_NAME:
        ss += ''sum(decode({},null, 1, 0)) as {}, ''.format(col, col)
    ss = ss[:-2]
    
    #生成where条件
    wh = ''''
    if where_condition:
        wh += '' where ''
        if type(where_condition)==str:
            wh += where_condition
        if type(where_condition)==dict:
            for key in where_condition.keys():
                if type(where_condition[key])!=str:
                    wh += (''t.'' + str(key) + '' = '' +
                           str(where_condition[key]) + '' and '')
                else:
                    wh += ("t." + str(key) + " = ''" +
                           str(where_condition[key]) + "'' and ")
            wh = wh[:-4]
    
    #print(ss)
    sql_select =  ''''''select count(*) as counts, {}
                    from {} t {}
                    ''''''.format(ss, table_name, wh)
    
    #print(sql_select)
    res = db.select_oracle(sql=sql_select)
    return pd.Series(res.values.tolist()[0], index=res.columns)

  缺失值统计2 -- 基于sqlalchemy

import pandas as pd
#import cx_Oracle as orcl
from sqlalchemy import create_engine

# 批量查询数据缺失率
def missing_count(table_name, where_condition={}, **config):
    #where 条件参数化, str或dict
    
    #定义数据库连接
    #''oracle://qmcbrt:qmcbrt@10.85.31.20:1521/tqmcbdb''
    engine = ''oracle://{username}:{passwd}@{host}:{port}/{sid}''.format(**config)  #dbname -- 各版本语法不同
    db = create_engine(engine)
    #pd.read_sql_query(sql_tab_columns, db)
    #db.execute(''truncate table {}''.format(ttb))
    
    #查询列名 -- 用于生成select项
    sql_tab_columns = "select column_name from user_tab_columns where table_name = ''{}''".format(table_name)
    columns = pd.read_sql_query(sql_tab_columns, db)
     
    #生成select项
    ss = ''''
    for col in columns.column_name:
        ss += ''sum(decode({}, null, 1, 0)) as {}, ''.format(col, col)
    ss = ss[:-2]
     
    #生成where条件
    wh = ''''
    if where_condition:
        wh += '' where ''
        if type(where_condition)==str:
            wh += where_condition
        if type(where_condition)==dict:
            for key in where_condition.keys():
                if type(where_condition[key])!=str:
                    wh += (''t.'' + str(key) + '' = '' +
                           str(where_condition[key]) + '' and '')
                else:
                    wh += ("t." + str(key) + " = ''" +
                           str(where_condition[key]) + "'' and ")
            wh = wh[:-4]
     
    #select语句
    sql_select =  ''''''select count(*) as counts, {} from {} t {} ''''''.format(ss, table_name, wh)
     
    #pd.Series(res.values.tolist()[0], index=res.columns)
    res = pd.read_sql_query(sql_select, db)
    return res.iloc[0,:]

  示例

config = {
        ''username'':''qmcb'',
        ''passwd'':''qmcb'',
        ''host'':''localhost'',
        ''port'':''1521'',
        ''sid'':''tqmcbdb''
        }
where_condition = {
                 ''is_normal'': 1,
                 ''is_below_16'': 0,
                 ''is_xs'': 0,
                 ''is_cj'': 0,
                 ''is_dead'': 0,
                 ''AAE138_is_not_null'': 0,
                 ''is_dc'': 0,
                 ''is_px'': 0
                 }
# 计算 QMCB_KM_2019_1_31_1 表的数据缺失数
missing_count(''QMCB_KM_2019_1_31_1'', where_condition, **config)

  

  

 

python sklearn与pandas实现缺失值数据预处理流程详解

python sklearn与pandas实现缺失值数据预处理流程详解

注:代码用 jupyter notebook跑的,分割线线上为代码,分割线下为运行结果

1.导入库生成缺失值

通过pandas生成一个6行4列的矩阵,列名分别为''col1'',''col2'',''col3'',''col4'',同时增加两个缺失值数据。

import  numpy as np 
import pandas as pd
from sklearn.impute import SimpleImputer
#生成缺失数据
df=pd.DataFrame(np.random.randn(6,4),columns=[''col1'',''col2'',''col3'',''col4'']) #生成一份数据
#增加缺失值
df.iloc[1:2,1]=np.nan
df.iloc[4,3]=np.nan
df

        col1        col2        col3        col4
0    -0.480144    1.463995    0.454819    -1.531419
1    -0.418552       NaN        -0.931259    -0.534846
2    -0.028083    -0.420394    0.925346    0.975792
3    -0.144064    -0.811569    -0.013452    0.110480
4    -0.966490    -0.822555    0.228038    NaN
5    -0.017370    -0.538245    -2.083904    0.230733

2.查看哪些值缺失(第2行第2列,第5行第4列)

nan_all=df.isnull() #获得所有数据中的nan
nan_all

    col1    col2    col3    col4
0    False    False    False    False
1    False    True    False    False
2    False    False    False    False
3    False    False    False    False
4    False    False    False    True
5    False    False    False    False

3 any()方法来查找含有至少1个缺失值的列,all()方法来查找全部缺失值的列

#使用any方法
nan_col1=df.isnull().any() #获得含有nan的列
print(nan_col1)

col1    False
col2     True
col3    False
col4     True
dtype: bool

#使用all方法
nan_col2=df.isnull().all() #获得全部为nan的列
print(nan_col2)

col1    False
col2    False
col3    False
col4    False
dtype: bool

4.法一:直接丢弃缺失值

df1=df.dropna()#直接丢弃含有nan的行记录
df1

col1    col2    col3    col4
0    -0.480144    1.463995    0.454819    -1.531419
2    -0.028083    -0.420394    0.925346    0.975792
3    -0.144064    -0.811569    -0.013452    0.110480
5    -0.017370    -0.538245    -2.083904    0.230733

5.法二:使用sklearn将缺失值替换为特定值

首先通过SimpleImputer创建一个预处理对象,缺失值替换方法默认用均值替换,及strategy=mean,还可以使用中位数median,众数most_frequent进行替换,接着使用预处理对象的fit_transform对df进行处理,代码如下:

#使用sklearn将缺失值替换为特定值
nan_mean=SimpleImputer(strategy=''mean'') #用均值填补
nan_median=SimpleImputer(strategy=''median'') #用中位数填补
nan_0=SimpleImputer(strategy=''constant'',fill_value=0) #用0填补
#应用模型
nan_mean_result=nan_mean.fit_transform(df)
nan_median_result=nan_median.fit_transform(df)
nan_0_result=nan_0.fit_transform(df)
print(nan_mean_result)
print(nan_median_result)
print(nan_0_result)

 [-0.48014389  1.46399462  0.45481856 -1.53141863]
 [-0.4185523  -0.22575384 -0.93125874 -0.53484561]
 [-0.02808329 -0.42039426  0.925346    0.97579191]
 [-0.14406438 -0.81156913 -0.0134516   0.11048025]
 [-0.96649028 -0.82255505  0.22803842 -0.14985173]
 [-0.01737047 -0.53824538 -2.0839036   0.23073341]
 
 [-0.48014389  1.46399462  0.45481856 -1.53141863]
 [-0.4185523  -0.53824538 -0.93125874 -0.53484561]
 [-0.02808329 -0.42039426  0.925346    0.97579191]
 [-0.14406438 -0.81156913 -0.0134516   0.11048025]
 [-0.96649028 -0.82255505  0.22803842  0.11048025]
 [-0.01737047 -0.53824538 -2.0839036   0.23073341]
 
 [-0.48014389  1.46399462  0.45481856 -1.53141863]
 [-0.4185523   0.         -0.93125874 -0.53484561]
 [-0.02808329 -0.42039426  0.925346    0.97579191]
 [-0.14406438 -0.81156913 -0.0134516   0.11048025]
 [-0.96649028 -0.82255505  0.22803842  0.        ]
 [-0.01737047 -0.53824538 -2.0839036   0.23073341]

6.法三:使用pandas将缺失值替换为特定值

pandas对缺失值处理方法是df.fillna(),该方法的两个主要参数是value和method。前者通过固定或手动指定的值替换缺失值,后者使用pandas提供的方法替换缺失值。以下是method支持的方法:

(1)pad和ffill:使用前面的值替换缺失值

(2)backfill和bfill:使用后面的值替换缺失值

(3)大多数情况下用均值、众数、中位数的方法较为常用

#使用pandas将缺失值替换为特定值
nan_result_pd1=df.fillna(method=''backfill'')
nan_result_pd2=df.fillna(method=''bfill'',limit=1)#用后面的值替换缺失值,限制每列只能替换一个缺失值
nan_result_pd3=df.fillna(method=''pad'')
nan_result_pd4=df.fillna(0)
nan_result_pd5=df.fillna({''col2'':1.1,''col4'':1.2}) #手动指定两个缺失值分别为1.1,1.2
nan_result_pd6=df.fillna(df.mean()[''col2'':''col4''])
nan_result_pd7=df.fillna(df.median()[''col2'':''col4''])
print(nan_result_pd1)
print(nan_result_pd2)
print(nan_result_pd3)
print(nan_result_pd4)
print(nan_result_pd5)
print(nan_result_pd6)
print(nan_result_pd7)

  col1      col2      col3      col4
0 -0.480144  1.463995  0.454819 -1.531419
1 -0.418552 -0.420394 -0.931259 -0.534846
2 -0.028083 -0.420394  0.925346  0.975792
3 -0.144064 -0.811569 -0.013452  0.110480
4 -0.966490 -0.822555  0.228038  0.230733
5 -0.017370 -0.538245 -2.083904  0.230733
       col1      col2      col3      col4
0 -0.480144  1.463995  0.454819 -1.531419
1 -0.418552 -0.420394 -0.931259 -0.534846
2 -0.028083 -0.420394  0.925346  0.975792
3 -0.144064 -0.811569 -0.013452  0.110480
4 -0.966490 -0.822555  0.228038  0.230733
5 -0.017370 -0.538245 -2.083904  0.230733
       col1      col2      col3      col4
0 -0.480144  1.463995  0.454819 -1.531419
1 -0.418552  1.463995 -0.931259 -0.534846
2 -0.028083 -0.420394  0.925346  0.975792
3 -0.144064 -0.811569 -0.013452  0.110480
4 -0.966490 -0.822555  0.228038  0.110480
5 -0.017370 -0.538245 -2.083904  0.230733
       col1      col2      col3      col4
0 -0.480144  1.463995  0.454819 -1.531419
1 -0.418552  0.000000 -0.931259 -0.534846
2 -0.028083 -0.420394  0.925346  0.975792
3 -0.144064 -0.811569 -0.013452  0.110480
4 -0.966490 -0.822555  0.228038  0.000000
5 -0.017370 -0.538245 -2.083904  0.230733
       col1      col2      col3      col4
0 -0.480144  1.463995  0.454819 -1.531419
1 -0.418552  1.100000 -0.931259 -0.534846
2 -0.028083 -0.420394  0.925346  0.975792
3 -0.144064 -0.811569 -0.013452  0.110480
4 -0.966490 -0.822555  0.228038  1.200000
5 -0.017370 -0.538245 -2.083904  0.230733
       col1      col2      col3      col4
0 -0.480144  1.463995  0.454819 -1.531419
1 -0.418552 -0.225754 -0.931259 -0.534846
2 -0.028083 -0.420394  0.925346  0.975792
3 -0.144064 -0.811569 -0.013452  0.110480
4 -0.966490 -0.822555  0.228038 -0.149852
5 -0.017370 -0.538245 -2.083904  0.230733
       col1      col2      col3      col4
0 -0.480144  1.463995  0.454819 -1.531419
1 -0.418552 -0.538245 -0.931259 -0.534846
2 -0.028083 -0.420394  0.925346  0.975792
3 -0.144064 -0.811569 -0.013452  0.110480
4 -0.966490 -0.822555  0.228038  0.110480
5 -0.017370 -0.538245 -2.083904  0.230733

另外,如果是直接替换为特定值,也可以考虑用pandas的replace功能,例如本示例可直接使用df.replace(np.nan,0),这种方法简单粗暴,但也能达到效果。当然replace的出现是为了解决各种替换用的,缺失值只是其中一种应用而已。

到此这篇关于python sklearn与pandas实现缺失值数据预处理流程详解的文章就介绍到这了,更多相关python 数据预处理内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

您可能感兴趣的文章:
  • Python sklearn分类决策树方法详解
  • Python sklearn库三种常用编码格式实例
  • Python sklearn转换器估计器和K-近邻算法
  • python机器学习sklearn实现识别数字
  • 一文搞懂Python Sklearn库使用
  • python库sklearn常用操作
  • Python 第三方库 Pandas 数据分析教程
  • Python Pandas读写txt和csv文件的方法详解
  • python pandas 数据排序的几种常用方法

今天关于Python ARIMA 模型 - 对数据进行差分后缺失值python差分函数的介绍到此结束,谢谢您的阅读,有关2.3 Hive 的数据类型讲解及实际项目中如何使用 python 脚本对数据进行 ETL、ARIMA模型在新的statsmodels ARIMA for python中无法正常工作、oracle 数据库表字段的缺失值统计 -- 基于python、python sklearn与pandas实现缺失值数据预处理流程详解等更多相关知识的信息可以在本站进行查询。

本文标签: