import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
df = pd.read_csv('train.csv')
df=df.drop(['ID'],axis=1)
df=df.to_numpy()
feature=np.abs(np.fft.fft(df[:,:-1]))
from sklearn.model_selection import train_test_split
tfeature,ttest,tlabel,testlabel=train_test_split(feature,df[:,-1],test_size=0.2)
from sklearn import tree
from sklearn.metrics import accuracy_score
from sklearn.model_selection import KFold
kf=KFold(n_splits=5,shuffle=False)
from sklearn import svm
from sklearn.model_selection import cross_val_score
for k in range(30):
sum=0
sum1=0
i=0
for train_index,test_index in kf.split(df):
i=i+1
tfeature=df[train_index,:-1]
label=df[train_index,-1]
clf=tree.DecisionTreeClassifier(criterion='entropy',random_state=0,max_depth=k+1)
clf.fit(tfeature,tlabel)
l=clf.predict(tfeature)
ttest=df[test_index,:-1]
testlabel=df[test_index,-1]
l1=clf.predict(ttest)
pr=accuracy_score(tlabel, l)
pr1=accuracy_score(testlabel, l1)
sum=sum+pr
sum1=sum1+pr1
clf1=tree.DecisionTreeClassifier(criterion='entropy',random_state=0,max_depth=k+1)
scores = cross_val_score(clf1, feature, df[:,-1], cv=5)
print(k,sum/i,sum1/i,scores.mean())
clf=tree.DecisionTreeClassifier(criterion='entropy',random_state=0,max_depth=15)
clf.fit(feature,df[:,-1])
df = pd.read_csv('test.csv')
df=df.drop(['ID'],axis=1)
df=df.to_numpy()
feature=np.abs(np.fft.fft(df[:,:]))
out=clf.predict(feature)
out=pd.DataFrame(out)
out.columns = ['CLASS']
w=[]
for k in range(out.shape[0]):
w.append(k+210)
out['ID']=np.reshape(w,(-1,1))
out[['ID','CLASS']].to_csv('out3.csv',index=False)