Python 支持向量回归预测
在我的问题中有四个特征(X)<代码>a、b、c、d和两名家属(Y)<代码>e,f。我有一个数据集,包含所有这些变量的一组值。在给定新的Python 支持向量回归预测,python,machine-learning,scikit-learn,svm,prediction,Python,Machine Learning,Scikit Learn,Svm,Prediction,在我的问题中有四个特征(X)a、b、c、d和两名家属(Y)e,f。我有一个数据集,包含所有这些变量的一组值。在给定新的a、b、c、d值时,如何使用python中的scikit learn通过支持向量回归预测e、f变量的值 我对ML非常陌生,我非常希望能得到一些指导,因为我发现很难理解关于ML的scikit学习文档 这就是我在sklearn文档中的一个示例的帮助下所做的 train = pd.read_csv('/Desktop/test.csv') X = train.iloc[:, 4] y
a、b、c、d
值时,如何使用python中的scikit learn通过支持向量回归预测e、f
变量的值
我对ML非常陌生,我非常希望能得到一些指导,因为我发现很难理解关于ML的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数组:
:
使用数组重塑数据。如果数据具有单个特征或数组,则重塑(-1,1)。如果数据包含单个样本,则重塑(1,-1)
我假设你的目标变量需要在这里独立预测,所以如果我错了,请纠正我。我稍微修改了sklearndoc示例,以说明您需要做什么。请在执行回归之前考虑对数据进行缩放。
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)
我假设你的目标变量需要在这里独立预测,所以如果我错了,请纠正我。我稍微修改了sklearndoc示例,以说明您需要做什么。请在执行回归之前考虑对数据进行缩放。
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)
假设您的数据文件有6列,特征值位于前4列,目标(您称之为“依赖项”)位于最后2列,那么我认为您需要这样做:
train = pd.read_csv('/Desktop/test.csv')
X = train.iloc[:, 0:3]
y = train.iloc[:, 4:5]
假设您的数据文件有6列,特征值位于前4列,目标(您称之为“依赖项”)位于最后2列,那么我认为您需要这样做:
train = pd.read_csv('/Desktop/test.csv')
X = train.iloc[:, 0:3]
y = train.iloc[:, 4:5]
你到底需要什么帮助?拟合还是预测?您希望同时预测
e和f
,例如,新样本N具有e,f
=[0,1]或独立??对于您的X变量,此处的索引显示错误-您选择了一列感谢您指出。我将其更改为选择正确的列。您到底需要什么帮助?拟合还是预测?您希望同时预测e和f
,例如,新样本N具有e,f
=[0,1]或独立??对于您的X变量,此处的索引显示错误-您选择了一列感谢您指出。我将其更改为选择正确的列。@KrishiH将该数据从csv加载到变量X和y并使用它。@KrishiH将该数据从csv加载到变量X和y并使用它。