简体   繁体   English

从 numpy arrays 创建 dataframe

[英]Create a dataframe from numpy arrays

I have the following code and I am stuck in creating a pandas dataframe by some numpy arrays.我有以下代码,我被一些 numpy ZA3CBC3F9D0CE2F2C19CZE1B66714 创建一个 pandas dataframe。

def gradient_descent_classification_prediction(x_test):
    y_pred = pd.DataFrame()
    for i in range(0 , 10):
        print(i)
        b = pd.read_csv("theta"+str(i)+".csv" , delimiter=",")
        y = pred(x_test , b) #outputs are 2d-s numpy arrays (10000 , 1) 
        y_pred= y_pred.append(y.tolist()) 
    y_pred.to_csv("./y_pred.csv" , index=False)

I would like to have a column in the dataframe for each y which is calculated by the function pred(x_test, b)我想在 dataframe 中有一个列,每个 y 由 function pred(x_test, b) 计算

The output that I get is a single column dataframe with just an output of the function pred.我得到的 output 是一个单列 dataframe ,只有 ZC1C4145268E683845 的 output 的 ZC1C1145268E68384DZ。

Could you help me?你可以帮帮我吗?

There are many ways to do this but essentially append a list to dataframe does not ensure its shape.有很多方法可以做到这一点,但基本上 append 到 dataframe 的列表并不能确保其形状。 It's easier to keep it as a dictionary or list, then convert to data frame before writing.将其保存为字典或列表更容易,然后在写入之前转换为数据框。 Below I use a dictionary:下面我使用字典:

def gradient_descent_classification_prediction(x_test):
    y_pred = {}
    for i in range(0 , 10):

        b = pd.read_csv("theta"+str(i)+".csv" , delimiter=",")
        y = pred(x_test , b) #outputs are 2d-s numpy arrays (10000 , 1) 
        y_pred["theta"+str(i)] = y
    
    pd.DataFrame(y_pred).to_csv("./y_pred.csv" , index=False)

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

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