#import pandas as pd from sklearn.model_selection import train_test_split, GridSearchCV from sklearn.preprocessing import StandardScaler from sklearn.metrics import confusion_matrix, roc_curve, auc from sklearn.svm import SVC import matplotlib.pyplot as plt from sklearn.metrics import accuracy_score #%% get data data_df = utils.load_object('faps_for_train.pkl') valence = utils.load_object('valence.pkl') arousals = utils.load_object('arousal.pkl') valence_list = valence['valence'].tolist() arousal_list = arousals['arousal'].tolist() data_df = pu.match_label_with_sample(data_df, valence_list) data_df = pu.match_label_with_sample(data_df, arousal_list, col_name='arousal') label = data_df['label'].values.astype(np.int64) data = data_df.drop(columns=['ori_idx', 'label', 'arousal']).values.astype( np.float32) #test_df = utils.load_object('valence_for_train.pkl') #test_df = pu.match_label_with_sample(test_df,valence_list) # #label_test = test_df['label'].values.astype(np.int64) #data_test = test_df[['slope_qr','delta_pq','delta_qr']].values.astype(np.float32) # split train test data #X_train , X_test, y_train, y_test = train_test_split(data,label,test_size=0.1) X_train, X_test, y_train, y_test = train_test_split(data,
cufflinks.go_offline(connected=True) init_notebook_mode(connected=True) #%% from sklearn.preprocessing import StandardScaler #%% get data import utils import preprocessing.pre_utils as pu data_df = utils.load_object('faps_for_train.pkl') valence = utils.load_object('valence.pkl') #arousals = utils.load_object('arousal.pkl') valence_list = valence['valence'].tolist() #arousal_list = arousals['arousal'].tolist() data_df = pu.match_label_with_sample(data_df, valence_list) #data_df = pu.match_label_with_sample(data_df,arousal_list,col_name='arousal') #%% plot scatter matrix fig = ff.create_scatterplotmatrix(data_df[[0, 1, 12, 13, 'label']], diag='histogram', index='label', height=1000, width=1000) plotly.offline.plot(fig) #%% ##iaps_class = iaps(r"C:\Users\DSPLab\Research\affective-monitor-model\preprocessing\IAPSinfoFile_Final.txt") iaps_class = iaps(r"E:\Research\affective-monitor-model\preprocessing") sample_list_from_pic_id = iaps_class.get_sample_idx(2141)
import numpy as np import preprocessing.pre_utils as pu #import pandas as pd from sklearn.model_selection import train_test_split, GridSearchCV from sklearn.preprocessing import StandardScaler from sklearn.metrics import confusion_matrix, roc_curve, auc from sklearn.svm import SVC import matplotlib.pyplot as plt from sklearn.metrics import accuracy_score #%% get data data_df = utils.load_object('pd_for_train.pkl') arousals = utils.load_object('arousal.pkl') arousals_list = arousals['arousal'].tolist() data_df = pu.match_label_with_sample(data_df, arousals_list) label = data_df['label'].values.astype(np.int64) data = data_df[['slope_qr', 'delta_pq', 'delta_qr']].values.astype(np.float32) test_df = utils.load_object('pd_for_test.pkl') test_df = pu.match_label_with_sample(test_df, arousals_list) label_test = test_df['label'].values.astype(np.int64) data_test = test_df[['slope_qr', 'delta_pq', 'delta_qr']].values.astype(np.float32) # split train test data #X_train , X_test, y_train, y_test = train_test_split(data,label,test_size=0.1) X_train, X_test, y_train, y_test = train_test_split(data, label,
#%% get data for valence import utils import preprocessing.pre_utils as pu import numpy as np data_df = utils.load_object('faps_for_train.pkl') valence = utils.load_object('valence.pkl') valence_list = valence['valence'].tolist() data_df = pu.match_label_with_sample(data_df,valence_list) data_df = data_df.drop_duplicates('ori_idx') label = data_df['label'].values.astype(np.int64) data = data_df.drop(columns=['ori_idx','label']).values.astype(np.float32) ori_idx = data_df['ori_idx'].tolist() #X_train , X_test, y_train, y_test = train_test_split(data,label,test_size=0,random_state=42) #%% get model from sklearn.externals import joblib from sklearn.metrics import confusion_matrix, accuracy_score import pandas as pd filename = 'save_model/valence_svm.pkl' loaded_model = joblib.load(filename) valence_predicted = loaded_model.predict(data).tolist() print(confusion_matrix(label, valence_predicted)) print('accuracy: {:.2f}'.format(accuracy_score(label, valence_predicted))) val_np = np.array([valence_predicted,ori_idx]).transpose() pred_val_df = pd.DataFrame(val_np,columns=['valence','ori_idx']) #%% metrics
#%% visualize pqr #ppd.plot_pqr_slideshow(pd_pqr_df,51,smooth=True) #%% data selection #sel_pic_list = utils.load_object('selected_idx_list.pkl') #pd_pqr_df = pd_pqr_df[pd_pqr_df['ori_idx'].isin(sel_pic_list)] #%% data selection import preprocessing.pre_utils as pu import pandas as pd arousals = utils.load_object('arousal.pkl') arousals_list = arousals['arousal'].tolist() pd_sel_df = pd_pqr_df.copy() pd_sel_df = pu.match_label_with_sample(pd_sel_df, arousals_list) pd_ar_df = pd_sel_df[((pd_sel_df['label'] == 1) & (pd_sel_df['area_ql'] > 20))] pd_nar_df = pd_sel_df[((pd_sel_df['label'] == 2) & (pd_sel_df['area_ql'] <= 20))] pd_nar_df = pd_nar_df.sample(pd_ar_df.shape[0]) samples = pd.concat([pd_ar_df, pd_nar_df], ignore_index=True) samples = samples.sample(frac=1) # plot slide show of data #ppd.plot_pqr_slideshow(samples,'all',smooth=False,label=samples['label'].tolist()) # drop label before save it to pickle samples = samples.drop('label', axis=1)