pandas 类型错误:x 的预期一维向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33177548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: expected 1D vector for x
提问by grove park
I am getting the error:
我收到错误:
TypeError: expected 1D vector for x
类型错误:x 的预期一维向量
with regards to this line:
关于这一行:
coefficients = np.polyfit(x1, y1, 1)
系数 = np.polyfit(x1, y1, 1)
coefficients = np.polyfit(x1, y1, 1)
polynomial = np.poly1d(coefficients)
ys = polynomial(x1)
x1 & y1 are;
x1 & y1 是;
x = frame_query("select * from table",db)
y = frame_query("select * from table",db)
x1 = np.array(x)
y1 = np.array(y)
Consisting of 736 rows of data. I want to regress one row onto the other. Could someone help please?
由 736 行数据组成。我想将一行回归到另一行。有人可以帮忙吗?
Thanks.
谢谢。
回答by amd
You'll want to turn the data frame into a 1D array. First let me create a data frame
您需要将数据框转换为一维数组。首先让我创建一个数据框
import pandas
d = pandas.DataFrame([[1,2],[3,4],[5,6]],columns=['x1','y1'])
I think the following does what you want:
我认为以下做你想要的:
import numpy
x1 = numpy.array(d['x1'])
y1 = numpy.array(d['y1'])
numpy.polyfit(x1,y1,1)
I think the problem you are having is that the arrays you're creating have an additional dimension. For example, the arrays aand bbelow "look" just like x1and y1,
我认为您遇到的问题是您创建的数组有一个额外的维度。例如,数组a和b下面的“看起来”就像x1and y1,
a = numpy.array([[1,3,5]])
b = numpy.array([[2,4,6]])
but because of the double bracket ([[...]]) they are actually two-dimensional. I can reduce the dimension by selecting just the 0th column:
但由于双括号 ( [[...]]) 它们实际上是二维的。我可以通过只选择第 0 列来减少维度:
x1 = a[0,:]
y1 = b[0,:]
Then the polyfit will work. EDIT: if you look at the shape of the arrays (e.g. x1.shape) you should be able to tell if you have "extra dimensions."
然后 polyfit 将起作用。编辑:如果您查看数组的形状(例如x1.shape),您应该能够判断您是否有“额外的维度”。
回答by Marco
Try to make x1 and y1 np.array:
尝试制作 x1 和 y1 np.array:
x1 = np.array([45,34,12])
y1 = np.array([19,46,22])

