原因:路徑問題,將字串作為路徑直接使用,需要跳脫
出錯程式碼:pd.read_csv(‘duringopereation\patients\patients_csv\23483949.csv’, encoding = 「gb2312」)
修改程式碼:pd.read_csv(‘duringopereation\patients\patients_csv\23483949.csv’, encoding = 「gb2312」)
將路徑刪掉重新手敲一遍解決(之前是檔案屬性裡複製的路徑)
import pandas as pd
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
# 讀取資料
dfx_train = pd.read_csv(r"E:/新網銀行杯/data_b_train.csv")
dfx_test = pd.read_csv(r"E:/新網銀行杯/data_b_test.csv")
dfy_train = pd.read_csv(r"E:/新網銀行杯/y_train.csv")
print(dfx_train)
# 字典中的key轉換為列表
key_value = list(dfx_train.keys())
print('字典中的key轉換為列表:', key_value)
x1_train = np.array(list(dfx_train['x_num_0']))
# x2_train = np.array(list(dfx_train['x_num_1']))
x3_train = np.array(list(dfx_train['x_num_2']))
# x4_train = np.array(list(dfx_train['x_num_3']))
# x5_train = np.array(list(dfx_train['x_num_4']))
# x6_train = np.array(list(dfx_train['x_num_5']))
# x7_train = np.array(list(dfx_train['x_num_6']))
# x8_train = np.array(list(dfx_train['x_num_7']))
x_train=np.array([x1_train,x3_train])
x_train=x_train.T
y_train = np.array(list(dfy_train['target']))
y_train=y_train.T
#
x1_test = np.array(list(dfx_test['x_num_0']))
# x2_test = np.array(list(dfx_test['x_num_1']))
x3_test = np.array(list(dfx_test['x_num_2']))
# x4_test = np.array(list(dfx_test['x_num_3']))
# x5_test = np.array(list(dfx_test['x_num_4']))
x_test=np.array([x1_test,x3_test])
x_test=x_test.T
#==================================
# KNN
#==================================
knn = KNeighborsClassifier()
knn.fit(x_train,y_train)
y_test=knn.predict(x_test)
print(len(y_test))
#==================================
# SVM
#==================================
# from sklearn.svm import SVC
# model = SVC(kernel='rbf', probability=True)#probability=False時,沒辦法呼叫 model.predict_proba()函數
# model.fit(x_train,y_train)
# pre = model.predict_proba(C)
# print(pre)
# y_test = model.predict(x_test)
# print(y_test)
#==================================
# 匯出
#==================================
data_df = pd.DataFrame(y_test)
# change the index and column name
data_df.columns = ['target']
data_df.index = list(range(0,5767))
# create and writer pd.DataFrame to excel
writer = pd.ExcelWriter('E:/新網銀行杯/y_test.xlsx')
data_df.to_excel(writer,'page_1',float_format='%.5f') # float_format 控制精度
writer.save()
import numpy as np
import pandas as pd
import random
a=[]
for i in range(1,5767):
a.append(random.randint(0,1))
a = np.array(a)
# prepare for data
data = a.reshape((5766,1))
print(data)
data_df = pd.DataFrame(data)
# change the index and column name
data_df.columns = ['target']
data_df.index = list(range(1,5767))
# create and writer pd.DataFrame to excel
writer = pd.ExcelWriter('Save_Excel.xlsx')
data_df.to_excel(writer,'page_1',float_format='%.5f') # float_format 控制精度
writer.save()
#pnn感知器分類
from sklearn.linear_model import Perceptron
ppn = Perceptron()
ppn.fit(x_train, y_train)
y_test = ppn.predict(x_test)
python: