首页 文章

使用支持向量回归的预测

提问于
浏览
0

在我的问题中有四个特征(X); a,b,c,d 和两个家属(Y); e,f . 我有一个数据集,其中包含所有这些变量的一组值 . 如果给出新的 a,b,c,d 值,如何通过使用scikit learn in python中的支持向量回归来预测 e,f 变量?

我是ML的新手,我非常感谢一些指导,因为我发现很难按照SVR上的scikit学习文档 .

到目前为止,这是我在sklearn文档中的一个示例的帮助 .

train = pd.read_csv('/Desktop/test.csv')
X = train.iloc[:, 4]
y = train.iloc[:, 4:5]

svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
y_rbf = svr_rbf.fit(X, y).predict(X)

lw = 2
plt.scatter(X, y, color='darkorange', label='data')
plt.plot(X, y_rbf, color='navy', lw=lw, label='RBF model')
plt.xlabel('data')
plt.ylabel('target')
plt.title('Support Vector Regression')
plt.legend()
plt.show()

这给出了错误,

ValueError:预期的2D数组,改为获得1D数组::如果数据具有单个特征,则使用array.reshape(-1,1)重塑数据;如果包含单个样本,则使用array.reshape(1,-1)重塑数据 .

2 回答

  • 2

    我假设你的目标变量需要在这里独立预测,所以如果我错了,请纠正我 . 我稍微修改了sklearn doc示例来说明您需要做什么 . 在执行回归之前,请考虑扩展数据 .

    import numpy as np
    from sklearn import svm
    import matplotlib.pyplot as plt
    
    n_samples, n_features = 10, 4 # your four features a,b,c,d are the n_features
    np.random.seed(0)
    y_e = np.random.randn(n_samples)
    y_f = np.random.randn(n_samples)
    
    # your input array should be formatted like this.
    X = np.random.randn(n_samples, n_features)
    
    #dummy parameters - use grid search etc to find best params
    svr_rbf = svm.SVR(kernel='rbf', C=1e3, gamma=0.1)
    # Fit and predict for one target, do the same for the other
    y_pred_e = svr_rbf.fit(X, y_e).predict(X)
    
  • 0

    假设您的数据文件有6列,并且功能值在前4列中,并且目标(您称之为'dependents')在最后2列中,那么我认为您需要这样做:

    train = pd.read_csv('/Desktop/test.csv')
    X = train.iloc[:, 0:3]
    y = train.iloc[:, 4:5]
    

相关问题