简体   繁体   中英

Create a dataframe from numpy arrays

I have the following code and I am stuck in creating a pandas dataframe by some numpy arrays.

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)

The output that I get is a single column dataframe with just an output of the function pred.

Could you help me?

There are many ways to do this but essentially append a list to dataframe does not ensure its shape. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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