首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >学习问题:在进行回归时发现样本数不一致的数组

学习问题:在进行回归时发现样本数不一致的数组
EN

Stack Overflow用户
提问于 2015-08-19 13:47:29
回答 5查看 24.2K关注 0票数 9

这个问题以前似乎有人问过,但我似乎无法评论是否需要进一步澄清所接受的答案,也无法找到所提供的解决办法。

我正在努力学习如何使用我自己的数据。在过去的100年里,我基本上得到了两个不同国家的GDP年变化率。我现在只是试着用一个变量来学习。我主要想做的是,用滑雪板来预测A国GDP %的变化会给出B国GDP的百分比变化。

问题是,我收到一个错误,上面写着:

ValueError:发现样本数不一致的数组:1 107个

这是我的代码:

代码语言:javascript
复制
import sklearn.linear_model as lm
import numpy as np
import scipy.stats as st
import matplotlib.pyplot as plt
import matplotlib.dates as mdates


def bytespdate2num(fmt, encoding='utf-8'):#function to convert bytes to string for the dates.
    strconverter = mdates.strpdate2num(fmt)
    def bytesconverter(b):
        s = b.decode(encoding)
        return strconverter(s)
    return bytesconverter

dataCSV = open('combined_data.csv')

comb_data = []

for line in dataCSV:
    comb_data.append(line)

date, chngdpchange, ausgdpchange = np.loadtxt(comb_data, delimiter=',', unpack=True, converters={0: bytespdate2num('%d/%m/%Y')})


chntrain = chngdpchange[:-1]
chntest = chngdpchange[-1:]

austrain = ausgdpchange[:-1]
austest = ausgdpchange[-1:]

regr = lm.LinearRegression()
regr.fit(chntrain, austrain)

print('Coefficients: \n', regr.coef_)

print("Residual sum of squares: %.2f"
      % np.mean((regr.predict(chntest) - austest) ** 2))

print('Variance score: %.2f' % regr.score(chntest, austest))

plt.scatter(chntest, austest,  color='black')
plt.plot(chntest, regr.predict(chntest), color='blue')

plt.xticks(())
plt.yticks(())

plt.show()

我做错了什么?实际上,我试图将sklearn教程(他们使用了一些糖尿病数据集)应用到我自己的简单数据中。我的数据只包含了日期,A国GDP在那一年的%变化,B国GDP在同一年的%变化。

我尝试了解决方案这里这里(基本上是试图在第一个链接中找到更多关于解决方案的信息),但只收到了完全相同的错误。

以下是完整的跟踪,以防您想要看到它:

代码语言:javascript
复制
Traceback (most recent call last):
  File "D:\My Stuff\Dropbox\Python\Python projects\test regression\tester.py", line 34, in <module>
    regr.fit(chntrain, austrain)
  File "D:\Programs\Installed\Python34\lib\site-packages\sklearn\linear_model\base.py", line 376, in fit
    y_numeric=True, multi_output=True)
  File "D:\Programs\Installed\Python34\lib\site-packages\sklearn\utils\validation.py", line 454, in check_X_y
    check_consistent_length(X, y)
  File "D:\Programs\Installed\Python34\lib\site-packages\sklearn\utils\validation.py", line 174, in check_consistent_length
    "%s" % str(uniques))
ValueError: Found arrays with inconsistent numbers of samples: [  1 107]
EN

回答 5

Stack Overflow用户

发布于 2016-07-01 13:53:41

在fit(X,y)中,输入参数X应该是二维数组.但是,如果您的数据中的X只是一维的,您可以将它重新构造成如下所示的二维数组:regr.fit(chntrain_X.reshape(len(chntrain_X), 1), chntrain_Y)

票数 6
EN

Stack Overflow用户

发布于 2015-08-19 21:26:01

代码语言:javascript
复制
regr.fit(chntrain, austrain)

这看起来不太对。fit的第一个参数应该是X,它引用一个特征向量。第二个参数应该是y,它是与X关联的正确答案(目标)向量。

例如,如果你有国内生产总值,你可能有:

代码语言:javascript
复制
X[0] = [43, 23, 52] -> y[0] = 5
# meaning the first year had the features [43, 23, 52] (I just made them up)
# and the change that year was 5

从您的名字判断,chntrainaustrain都是特征向量。从加载数据的方式判断,也许最后一列是目标?

也许你需要做这样的事情:

代码语言:javascript
复制
chntrain_X, chntrain_y = chntrain[:, :-1], chntrain[:, -1]
# you can do the same with austrain and concatenate them or test on them if this part works
regr.fit(chntrain_X, chntrain_y)

但是,如果不知道数据的确切存储格式,我们就无法判断。

票数 0
EN

Stack Overflow用户

发布于 2015-10-22 10:34:01

尝试将chntrain更改为二维数组,而不是一维数组,即将其重塑为(len(chntrain), 1).

对于预测,还可以将chntest更改为二维数组.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32097392

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档