如何将“ for-lop”值的y_pred变量结果存储在numpy数组或列表中?

问题描述 投票:0回答:2

我正在编写SVR的代码。我有超过2000行和22列的数据集。在使用for loop对y_pred进行每次迭代预测之后,因此我需要store来自y_pred的所有for loop值。我想将这些值存储在listnumpy array中。我正在编写我的代码,但是它不起作用。请帮助我以适当的方式编写此代码。我知道角色是>>

numpyarray = np.empty((1001,2000, 23))
for i in range(1001):
    numpyarray[i, :] = numpyarrayfunction

评论:但这不起作用。

我的完整代码:

import pandas as pd
import numpy as np

# Make fake dataset
dataset = pd.DataFrame(data= np.random.rand(2000,22))
dataset['age'] = np.random.randint(2, size=2000)

# Separate the target from the other features
target = dataset['age']
data = dataset.drop('age', axis = 1)

X_train, y_train = data.loc[:1000], target.loc[:1000]

X_test,  y_test  = data.loc[1001], target.loc[1001] 

X_test = np.array(X_test).reshape(1, -1)
print(X_test.shape)

SupportVectorRefModel = SVR()
SupportVectorRefModel.fit(X_train, y_train)

y_pred = SupportVectorRefModel.predict(X_test)
y_pred

for i in range(1, 1001):
    X_train, y_train = dataset.iloc[i:1000+i], target.iloc[i:1000+i]
    X_test, y_test = dataset.iloc[i], target.iloc[i]

    X_test = np.array(X_test).reshape(1, -1)
    print(X_test.shape)

    SupportVectorRefModel = SVR()
    SupportVectorRefModel.fit(X_train, y_train)
    y_pred = SupportVectorRefModel.predict(X_test)

我当前的代码:

numpyarray = np.empty((1001,2000, 23))
    for i in range(1001):
        numpyarray[i, :] = numpyarrayfunction

我正在为SVR编写代码。我有超过2000行和22列的数据集。在使用for循环对y_pred进行每次迭代预测之后,因此我需要存储所有y_pred值...

python python-3.x numpy arraylist numpy-ndarray
2个回答
0
投票

这是您想要的吗?


0
投票

似乎您正在训练一系列数据,从技术上讲,您不需要循环。

© www.soinside.com 2019 - 2024. All rights reserved.