代码之家  ›  专栏  ›  技术社区  ›  Sreedev

追加数组时Numpy返回错误的问题

  •  -1
  • Sreedev  · 技术社区  · 6 年前

    x 是一个大小为[16754,3]的数据集 a 是一个大小为[16754,1]的数组。据我所知,轴心是完全匹配的。

    # Importing the libraries
    import numpy as np
    import matplotlib.pyplot as plt
    import pandas as pd
    
    # Importing the dataset
    dataset = pd.read_csv('data_monthly_rainfall.csv')
    x = dataset.iloc[:, [0,1,2]].values
    y = dataset.iloc[:, 3].values
    
    # Apending a coloumn y with 1 for the equation
    import statsmodels.api as sm
    a = np.ones((16754, 0)).astype(int)
    x = np.append(arr = a,values = x, axis = 1)
    

    谁能告诉我我做错什么了吗?在学习阶段,我对python和ML非常陌生。如果需要更多信息,请告诉我。

    Link to the dataset

    1 回复  |  直到 6 年前
        1
  •  2
  •   Gabriel Avendaño    6 年前

    问题是 x 是(16755,3),你正在创建 a (16755, 1) :

    a = np.ones((16755, 1)).astype(int)
    

    通过保存变量中的行数,可以完全避免这种情况。

    m = x.shape[0]
    a = np.ones((m, 1)).astype(int)
    x = np.append(arr = a,values = x, axis = 1)
    
    推荐文章