简体   繁体   English

Python - 如何重塑两个向量并将其转换为元组?

[英]Python - how to reshape two vectors and transform it into a tuple?

I have a problem.我有个问题。 I get a task.我得到一个任务。

Create LinearRegression X to Y.创建线性回归 X 到 Y。

fit() a to reshape X and Y vectors new shape: (-1, 1). fit() a 重塑 X 和 Y 向量的新形状:(-1, 1)。

This is part of my code这是我的代码的一部分

tuple1 = tuple(zip(X,Y))
np.reshape(tuple1, (-1, 1))
reg = LinearRegression().fit(tuple1)

I don't understand the question.我不明白这个问题。 The problem is the three last lines in my code.问题是我的代码中的最后三行。 So first I should merge X and Y into a tuple to make reshape?所以首先我应该将 X 和 Y 合并成一个元组来重塑? But then I must use linear regression so I need X and Y which are not merged.但是我必须使用线性回归,所以我需要未合并的 X 和 Y。 I don't get it.我不明白。

As the method fit() accepts properly shaped arrays, ...由于方法 fit() 接受形状正确的 arrays,...

The way it is defined, X is a 1D vector ( X.shape gives (5,) )它的定义方式,X 是一维向量( X.shape给出(5,)

as scikit-learn fit() methods in general expect an array of vectors作为 scikit-learn fit() 方法通常需要一个向量数组

So X is a problem, because that's not an array of vectors, but just a 1D vector.所以X是个问题,因为那不是向量数组,而只是一维向量。

reshape X and Y vectors by using the method reshape() and passing to it a tuple with a new shape: (-1, 1)使用 reshape() 方法重塑 X 和 Y 向量,并将具有新形状的元组传递给它: (-1, 1)

X.reshape(-1, 1).shape gives (5, 1) , which is what we need. X.reshape(-1, 1).shape给出(5, 1) ,这是我们需要的。 I see where you got confused: The "tuple" refers to the arguments of the reshape function (literally the tuple (-1, 1) ), not to the result of the transformation.我知道您在哪里感到困惑:“元组”是指重塑 function 的 arguments (字面意思是元组(-1, 1) ),而不是转换的结果。

Perform the reshaping on site (in the function call), keep the original vectors as they are.在现场进行整形(在 function 调用中),保持原始向量不变。

Reshape in the function call: reg = LinearRegression().fit(X.reshape(-1, 1), Y) , ie don't mess with the variables beforehand.在 function 调用中重塑: reg = LinearRegression().fit(X.reshape(-1, 1), Y) ,即不要事先弄乱变量。

Note: X can stay the way it is, because that's ok as a 1D vector (only one dependent variable);注意: X可以保持原样,因为它可以作为一维向量(只有一个因变量); so "you will have to reshape X and Y vectors" is not correct.所以“你将不得不重塑 X 和 Y 向量”是不正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM