分析結果迴圈寫入csv

2021-10-07 07:14:48 字數 1606 閱讀 6384

30戶的暖氣的分析結果複製貼上下來發給隊友

(好像是有點多了乙個對話方塊都過不去)

被嫌棄了

你就不能給我打包個excel!!!

好吧

import csv

with open("result.csv","w") as csvfile:

writer = csv.writer(csvfile)

#先寫入columns_name

writer.writerow(['index','住戶','模型權重','截距','回歸方程','訓練集r^2','測試集r^2','均方誤差(mse)','根均方誤差(rmse)','"平均絕對值誤差(mae)'])

def all(x):

data2=data[(data.address_2nd==x)]

x ,y= data2[['power','in_temperature','溫度']],data2['room_temperature']

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=0)

lr = linearregression()

lr.fit(x_train, y_train)

y_hat = lr.predict(x_test)

df1=x

df2=lr.coef_

df3=lr.intercept_

list(df2)

df4=('y = %f x1 + %f x2 +%f x3 +%f'%(df2[0],df2[1],df2[2],df3))

df5=r2_score(y_train, lr.predict(x_train))

df6=r2_score(y_test, y_hat)

df7=mean_squared_error(y_test, y_hat)

df8=np.sqrt(mean_squared_error(y_test, y_hat))

df9=mean_absolute_error(y_test, y_hat)

writer.writerows([[i,df1,df2,df3,df4,df5,df6,df7,df8,df9]])

address = [15311251,15310819,15310804,15311234,15311289,15311072,15311061,15310846,15311065,15311342,15311245,15310966,

15311191,15311361,15310827,15311196,15311235,15311233,15310808,15311473,15310839,15310815,15310845,

15311082]

for i in range(len(address)):

x = address[i]

all(x)

for i in address:

df1=x

df2=lr.coef_

df3=lr.intercept_

df4=r2_score(y_train, lr.predict(x_train))

i+=1

CSV的多行寫入和迴圈追加

def writ in csv global lats global lons global soil m 三個全域性變數用於存放經緯度,土壤數值 count 0with open r hecdata pm.csv a newline as f a表示在文末追加,newline用於去除間隔的空行 w...

python使用csv寫入csv檔案

沒什麼好說的,直接上 吧 with open file.csv w encoding utf 8 newline as csvfile writer csv.writer csvfile 首先是表頭 writer.writerow id name gender birthday rating 然後是...

python資料寫入CSV

下面我將介紹什麼時候適合使用pandas,什麼時候適合使用csv庫。主要區別是乙個按行儲存方便,乙個按列訪問方便。假設第一列為 1,1,1,1 第二列為 2,2,2,2 第三列的值為 3,3,3 列名 表頭名字 為column1,column2,column3。如下 import pandas as...