繁体   English   中英

线性回归模拟 scikit-learn python

[英]simulation of linear regression scikit-learn python

我想运行线性回归,但此代码从“reg = LinearRegression()”开始生成错误

import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
from scipy.stats import binom

from scipy.stats import norm
# generate random numbers from N(0,1)
x = norm.rvs(size=10000,loc=0,scale=1)
y = norm.rvs(size=10000,loc=0,scale=1)
z = binom.rvs(n=10,p=0.8,size=10000)
df = pd.DataFrame(data={'v1':x.flatten(),'target':y.flatten(),'label':z.flatten()})
df.head(10)

reg = LinearRegression()
reg.fit(df['v1'], df["target"])

错误消息:ValueError:预期 2D 数组,得到 1D 数组:array=[ 0.39507346 -0.01013895 -0.83918156... 0.47254883 0.02202747 0.50782984]。 如果您的数据具有单个特征,则使用 array.reshape(-1, 1) 重塑您的数据,如果它包含单个样本,则使用 array.reshape(1, -1) 。

关于出了什么问题的任何提示?

使用.values.reshape(-1, 1)

reg.fit(df['v1'].values.reshape(-1, 1), df["target"].values.reshape(-1, 1))

暂无
暂无

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

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