Очевидно, я давал неправильные имена папок, которые нужно было добавить, и в коде были и другие мелкие ошибки. Я должен был дать точные имена каждой папки для запуска программы, и это сработало, вот правильный код
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sklearn
from glob import glob
import os
import itertools
import tensorflow as tf
print(os.listdir("A_DeviceMotion_data"))
Activety_Types = {'dws_1':1,'jog_9':2,'sit_5':3,'std_6':4,'ups_3':5,'wlk_7':6, 'dws_2':7, 'dws_11':8, 'jog_16':9, 'sit_13':10, 'std_14':11, 'ups_4':12, 'ups_12':13,'wlk_8':14,'wlk_15':15}
listDict = list(Activety_Types.keys())
Folders = glob('C:/Users/TechProBox/Desktop/A_DeviceMotion_data/*_*')
Folders = [s for s in Folders if "csv" not in s]
Df_all_list = []
Exp = 0
Segment_Size = 400
for j in Folders:
print("Folder {}\n".format(j))
Csv = glob(j + '/*' )
for i in Csv:
print (i)
print (j)
print("HERE")
df = pd.read_csv(i)
print(Activety_Types[j[48:56]])
df['Activity'] = Activety_Types[j[48:56]]
print("HERE1")
df['Sub_Num'] = i[len(j)+5:-4]
df['Exp_num'] = 1
ExpNum = np.zeros((df.shape[0]))
for i in range(0,df.shape[0]-Segment_Size,Segment_Size):
ExpNum[range(i,i+Segment_Size)] = i/Segment_Size +Exp*100
df['Exp_num'] = ExpNum
Df_all_list.append(df)
Exp += 1
Df_all = pd.concat(Df_all_list,axis=0)
Df_all.head()
plt.plot([1,2,3])
for i in range(6):
D = Df_all[Df_all['Activity']==i+1]
plt.subplot(3,2,i+1)
plt.plot(D['userAcceleration.z'][:200])
plt.title(listDict[i])
plt.ylim([-1, 1])
plt.tight_layout()
df_sum = Df_all.groupby('Exp_num', axis=0).mean().reset_index()
df_sum.columns = df_sum.columns.str.replace('.','_sum_')
df_sum_SS = np.power(Df_all.astype(float),2).groupby('Exp_num', axis=0).median().reset_index()
df_sum_SS.columns = df_sum_SS.columns.str.replace('.','_sumSS_')
df_max = Df_all.groupby('Exp_num', axis=0).max().reset_index()
df_max.columns = df_max.columns.str.replace('.','_max_')
df_min = Df_all.groupby('Exp_num', axis=0).min().reset_index()
df_min.columns = df_min.columns.str.replace('.','_min_')
df_skew = Df_all.groupby('Exp_num', axis=0).skew().reset_index()
df_skew.columns = df_skew.columns.str.replace('.','_skew_')
df_std = Df_all.groupby('Exp_num', axis=0).std().reset_index()
df_std.columns = df_std.columns.str.replace('.','_std_')
Df_Features = pd.concat([ df_max , df_sum[df_sum.columns[2:-2]],
df_min[df_min.columns[2:-2]], df_sum_SS[df_sum_SS.columns[2:-2]],
df_std[df_std.columns[2:-2]], df_skew[df_skew.columns[2:-2]]], axis=1)
Df_Features_1 = Df_Features.drop(['Exp_num','Unnamed: 0','Activity','Sub_Num'],axis=1)
Labels = Df_Features['Activity']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(Df_Features_1, Labels, test_size=0.25, random_state=42)
X_shape = Df_Features_1.shape[1]
Cluss_Num = len(Activety_Types)
X = tf.placeholder(dtype=tf.float32,shape=[None,X_shape])
y = tf.placeholder(dtype=tf.float32,shape = [None,Cluss_Num])
Hold_prob = tf.placeholder(tf.float32)
def Init_Wightes(shape1):
W = tf.truncated_normal(shape1,stddev=0.1)
return tf.Variable(W)
def Init_bias(shape1):
b = tf.constant(0.1,shape=shape1)
return tf.Variable(b)
def FC_layer(input1,shape1):
W = Init_Wightes(shape1)
B = Init_bias([shape1[1]])
Wx = tf.matmul(input1,W)
Wx_b = tf.add(Wx,B)
return tf.nn.relu(Wx_b)
H1_size = 54
H2_size = 24
H1 = FC_layer(X, [X_shape,H1_size])
H1_drop = tf.nn.dropout(H1,keep_prob=Hold_prob)
y_pred = FC_layer(H1_drop,[H1_size,len(Activety_Types)])
Err = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y,logits=y_pred))
Optimaizer = tf.train.AdamOptimizer()
Train = Optimaizer.minimize(Err)
Init = tf.global_variables_initializer()
BatchSize = 64
label_binarizer = sklearn.preprocessing.LabelBinarizer()
label_binarizer.fit(range(Cluss_Num))
X_train_Np = np.array(X_train,dtype=np.float32)
Y_train_Np = np.array(y_train,dtype=np.float32)
X_test_Np = np.array(X_test,dtype=np.float32)
Y_test_Np = np.array(y_test,dtype=np.float32)
Y_test_OH = label_binarizer.transform(Y_test_Np)
Y_test_OH = np.array(Y_test_OH,dtype=np.float32)
Batches = np.array(range(0,X_train_Np.shape[0]-BatchSize,BatchSize))
accuracy = 10
misclass = 1
steps = 5000
with tf.Session() as sess:
sess.run(Init)
for i in range(steps):
BastchNum = np.mod(i,len(Batches)-1)
batch_x = X_train_Np[Batches[BastchNum] : Batches[BastchNum+1] ,:]
batch_y = Y_train_Np[Batches[BastchNum] : Batches[BastchNum+1] ]
batch_y_OH = label_binarizer.transform(batch_y)
batch_y_OH = np.array(batch_y_OH,dtype=np.float32)
sess.run(Train,feed_dict={X:batch_x,y:batch_y_OH,Hold_prob:0.5})
if i%100 == 0:
print('Currently on step {}'.format(i))
print('Accuracy is:')
# Test the Train Model
matches = tf.equal(tf.argmax(y_pred,1),tf.argmax(y,1))
acc = tf.reduce_mean(tf.cast(matches,tf.float32))
print(sess.run(acc,feed_dict={X:X_test_Np,y:Y_test_OH,Hold_prob:1.0}))
print('\n')
Conf = tf.confusion_matrix(tf.arg_max(y_pred,1),tf.arg_max(y,1))
C1 = sess.run(Conf,feed_dict={X:X_test_Np,y:Y_test_OH,Hold_prob:1.0})
def plot_confusion_matrix(cm,
target_names,
title='Confusion matrix',
cmap=None,
normalize=True):
accuracy = np.trace(cm) / float(np.sum(cm))
misclass = 1 - accuracy
if cmap is None:
cmap = plt.get_cmap('Blues')
plt.figure(figsize=(8, 6))
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
if target_names is not None:
tick_marks = np.arange(len(target_names))
plt.xticks(tick_marks, target_names, rotation=45)
plt.yticks(tick_marks, target_names)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
thresh = cm.max() / 1.5 if normalize else cm.max() / 2
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
if normalize:
plt.text(j, i, "{:0.4f}".format(cm[i, j]),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
else:
plt.text(j, i, "{:,}".format(cm[i, j]),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label\naccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, misclass))
plt.show()
Conf = tf.confusion_matrix(tf.arg_max(y_pred,1),tf.arg_max(y,1))
plot_confusion_matrix(C1,target_names=[*Activety_Types])