with
open
(filein)
as f:
data =
[line.strip(
).split(
' ')
for line in f]
with
open
(filein)
as f:
fcsv = f.readlines(
)# fcsv是乙個陣列,裡面的元素是每一行字串
data =
[line.strip(
).split(
' ')
for line in fcsv]
import csv
with
open
(filein)
as f:
freader = csv.reader(f)
# 檔案需要是csv檔案,也就是逗號分隔符
headers =
next
(freader)
# 讀走標題,相當於不寫入標題
gini =
[i for i in freader]
# del gini[0] # 刪除標題
import pandas as pd
from sklearn.ensemble import adaboostclassifier
data = pd.read_csv(filein1, header=
none
)# header=none是為了不加上列名, data是dataframe型別
m, n = data.shape
x_train = data.ix[:,
:n-2
]# x_train是dataframe型別
y_train = data.ix[
:, n-1]
# y_train是series型別
data = pd.read_csv(filein2, header=
none
)m, n = data.shape
x_test = data.ix[:,
:n-2
]y_test = data.ix[
:, n-1]
clf = adaboostclassifier(n_estimators=
200, learning_rate=
0.8)
clf.fit(x_train, y_train)
y_pred = clf.predict(x_test)
# y_pred是ndarray型別
"""遍歷series型別的y_test和ndarray型別的y_pred,讓他們都變成list型別"""
y_pred =
[int
(i)for i in y_pred]
y_test =
[int
(i)for i in y_test]
"""計算平均準確率"""
# counter(y_test)得到result=counter(),這是二分類問題,只有兩個標籤
oneclasslen = counter(y_test)[1
]# 得到oneclasslen=337
twoclasslen = counter(y_test)[2
]# 得到twoclasslen=70
oneclassnum =
0twoclassnum =
0for i in
range
(len
(y_test)):
if y_pred[i]
== y_test[i]
:if y_pred[i]==1
: oneclassnum = oneclassnum +
1elif y_pred[i]==2
: twoclassnum = twoclassnum +
1oneaccuracy = oneclassnum/oneclasslen
twoaccuracy = twoclassnum/twoclasslen
print
("一類的召回率為"
, oneprecision)
print
("二類的召回率為"
, twoprecision)
print
("平均準確率為"
,(oneprecision + twoprecision)/2
)
data =
['hello world'
,'hi, world'
]fileout = r'c:\users\administrator\desktop\test.txt'
with
open
(fileout,
'w')
as f:
for row in data:
f.write(row+
'\n'
)
import csv
data =[[
1,2]
,[3,
4]]with
open
(fileout,
'w', newline='')
as f:
# newline=''是為了防止在每一行下面生成一行空白行
# fwriter = csv.writer(f, lineterminator='\n') # lineterminator='\n'是為了防止在每一行下面生成一行空白行
fwriter = csv.writer(f)
for row in data:
fwriter.writerow(row)
import pandas as pd
data =[[
1,2]
,[3,
4]]# header=false, index=false是為了不將頭名和索引寫進檔案中
pd.dataframe(data)
.to_csv(fileout, header=
false
, index=
false
)
Python檔案讀寫
今天在看python檔案讀寫操作,發現python file name mode buffering file 函式用於建立乙個file物件,它有乙個別名叫open 可能更形象一些,它們是內建函式。來看看它的引數。它引數都是以字串的形式傳遞的。name是檔案的名字。mode 是開啟的模式,可選的值為...
python檔案讀寫
檔案讀寫模式 模式 描述 r以讀方式開啟檔案,可讀取檔案資訊。w以寫方式開啟檔案,可向檔案寫入資訊。如檔案存在,則清空該檔案,再寫入新內容 a以追加模式開啟檔案 即一開啟檔案,檔案指標自動移到檔案末尾 如果檔案不存在則建立 r 以讀寫方式開啟檔案,可對檔案進行讀和寫操作。w 消除檔案內容,然後以讀寫...
python 讀寫檔案
python讀寫檔案在文字不大的情況可以用正常的 open 然後讀入 readline行讀入 或者整體讀入 read readlines 基本知識 file open path,r 說明 第乙個引數是檔名稱,包括路徑 第二個引數是開啟的模式mode r 唯讀 預設。如果檔案不存在,則丟擲錯誤 w 只...