<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Week 7
Step1:  This model is by no means great, but it does predict with .63 recall and .54 precision.
Step2:  This is a much worse model, and it is always predicting a value of 7.
Step3:  Again, this is a lousy model like the previous one, as it just predicts 9.
Step4:  These neural network based models are giving the same results, except for the convolutional network.  We'll try it again for a logistic regression, but pre-flattening the data.
Step5:  And the same results as with the previous logistic regression.
Step6:  With SciKitLearn's logistic regression algorithm, we get much better (but not good) precision than with the 
previous methods (excluding convolutional neural networks), but worse recall.
Step7:  The random forest is actually working much better than the neural networks and logistic regression models, and better precision than even the convolutional neural network.  The convolutional neural network still has the best recall, however.  It's still not a great model, but it has precision and recall better then 50%, however, most test records are being predicted as 9 as in the other models.  Apparently, our data is unbalanced among the classes - there are tons of category 'plain' compared to others.
<ASSISTANT_TASK:>
Python Code:
# re-load the saved data if needed
A = np.load('/home/nick/Documents/LewisUniversity/MachineLearning/Project/visionmatrix.npy')
#Let's start with the model parameters defined in the Week6 notebook for this data, changing the input shape as appropriate.
from keras.models import Sequential
from keras.layers import Dense, Dropout, Convolution2D, MaxPooling2D, Flatten
from keras.regularizers import l2, l1
from keras.optimizers import SGD
from keras import backend as K
K.set_image_dim_ordering('th')
model = Sequential()
model.add(Convolution2D(32, 5, 5, border_mode='valid', input_shape=(3, 50, 50), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(17, activation='softmax'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
#Because of time constraints, we'll just run 10 epochs instead of say, 20.
model = model.fit(X_train, y_train,  batch_size = 256,
          nb_epoch = 10, verbose=2, validation_data=(X_test,y_test))
predicted = model.model.predict_classes(X_test)
y_train.shape
predicted
from sklearn.metrics import classification_report, f1_score, accuracy_score, confusion_matrix
# put the y_test back into a format of non-one-hot encoded for comparison
y_test_orig = pd.DataFrame(y_test).idxmax(1)
print "Convolution Network Results\nConfusion Matrix: "
print confusion_matrix(y_test_orig,predicted)
print "\n Classifcation Report"
print classification_report(y_test_orig,predicted)
Explanation: Week 7: Implement Model
End of explanation
# Let's try with the Keras documentation example
# apply a 5x5 convolution with 32 output filters on a 50x50 image:
model = Sequential()
model.add(Convolution2D(16, 3, 3, border_mode='valid', input_shape=(3, 50, 50), activation='relu'))
# add a 3x3 convolution on top, with 16 output filters:
#model.add(Convolution2D(16, 3, 3, border_mode='same', activation='relu'))
# and flatten it and add a dense layer to get it to match the 17 categories.
model.add(Flatten())
model.add(Dense(17, activation='relu'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
model = model.fit(X_train, y_train,  batch_size = 256,
          nb_epoch = 5, verbose=2, validation_data=(X_test,y_test))
predicted = model.model.predict_classes(X_test)
# put the y_test back into a format of non-one-hot encoded for comparison
y_test_orig = pd.DataFrame(y_test).idxmax(1)
print "Convolution Network Results\nConfusion Matrix: "
print confusion_matrix(y_test_orig,predicted)
print "\n Classifcation Report"
print classification_report(y_test_orig,predicted)
Explanation: This model is by no means great, but it does predict with .63 recall and .54 precision.
End of explanation
# Let's try something totally different, a neural network based on the homework from week 5: 
model = Sequential()
model.add(Flatten(input_shape=(3, 50, 50)))
model.add(Dense(output_dim=100,
                activation='sigmoid', W_regularizer=l2(0.01)))
model.add(Dense(output_dim=500, activation='sigmoid', W_regularizer=l2(0.01)))
model.add(Dense(output_dim=17, activation='sigmoid', W_regularizer=l2(0.01)))
# Compile model
sgd = SGD(lr=0.1)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
model.summary()
# Fit the model
model = model.fit(X_train, y_train, batch_size = 256,
          nb_epoch = 20, verbose=2, validation_data=(X_test,y_test))
predicted = model.model.predict_classes(X_test)
# put the y_test back into a format of non-one-hot encoded for comparison
y_test_orig = pd.DataFrame(y_test).idxmax(1)
print "Neural Network Results\nConfusion Matrix: "
print confusion_matrix(y_test_orig,predicted)
print "\n Classifcation Report"
print classification_report(y_test_orig,predicted)
Explanation: This is a much worse model, and it is always predicting a value of 7.
End of explanation
# let's try logistic regression.
# Stochastic Logistic Regression
model = Sequential()
# validation loss 
model.add(Flatten(input_shape=(3, 50, 50)))
model.add(Dense(output_dim=17, activation='sigmoid', W_regularizer=l1(0.01)))
# Compile model
sgd = SGD(lr=0.1)
model.compile(loss='mean_squared_error', optimizer=sgd, metrics=['accuracy'])
model.summary()
# Fit the model
model = model.fit(X_train, y_train, batch_size = 256,
          nb_epoch = 100, verbose=0, validation_data=(X_test,y_test))
predicted = model.model.predict_classes(X_test)
# put the y_test back into a format of non-one-hot encoded for comparison
y_test_orig = pd.DataFrame(y_test).idxmax(1)
print "Logistic Regression Results\nConfusion Matrix: "
print confusion_matrix(y_test_orig,predicted)
print "\n Classifcation Report"
print classification_report(y_test_orig,predicted)
Explanation: Again, this is a lousy model like the previous one, as it just predicts 9.
End of explanation
# reshape the training and test data
X_train_new = np.empty(shape=[X_train.shape[0]] + [7500], dtype='float32')
for i in range(X_train.shape[0]):
    X_train_new[i,:] = X_train[i,:,:,:].flatten()
X_test_new = np.empty(shape=[X_test.shape[0]] + [7500], dtype='float32')
for i in range(X_test.shape[0]):
    X_test_new[i,:] = X_test[i,:,:,:].flatten()
print X_test_new.shape
print X_train_new.shape
# Stochastic Logistic Regression
model = Sequential()
# validation loss 
model.add(Dense(output_dim=17, input_shape=[7500], 
                activation='sigmoid', W_regularizer=l2(0)))
# Compile model
sgd = SGD(lr=0.1)
model.compile(loss='categorical_crossentropy', optimizer=sgd)
model.summary()
# Fit the model
model = model.fit(X_train_new, y_train, batch_size = 256,
          nb_epoch = 100, verbose=0, validation_data=(X_test_new,y_test))
predicted = model.model.predict_classes(X_test_new)
# put the y_test back into a format of non-one-hot encoded for comparison
y_test_orig = pd.DataFrame(y_test).idxmax(1)
print "Logistic Regression Results\nConfusion Matrix: "
print confusion_matrix(y_test_orig,predicted)
print "\n Classifcation Report"
print classification_report(y_test_orig,predicted)
Explanation: These neural network based models are giving the same results, except for the convolutional network.  We'll try it again for a logistic regression, but pre-flattening the data.
End of explanation
# We will try sklearn's logistic regression.
from sklearn.linear_model import LogisticRegression
# regular
y_train_orig = pd.DataFrame(y_train).idxmax(1)
y_test_orig = pd.DataFrame(y_test).idxmax(1)
lr = LogisticRegression()
lr.fit(X_train_new, y_train_orig)
predicted = lr.predict(X_test_new)
print "Logistic Regression Results\nConfusion Matrix: "
print confusion_matrix(y_test_orig,predicted)
print "\n Classifcation Report"
print classification_report(y_test_orig,predicted)
Explanation: And the same results as with the previous logistic regression.
End of explanation
#Finally, we'll try the random forest, which is not known for being great with image data.
from sklearn.ensemble import RandomForestClassifier
from sklearn.grid_search import GridSearchCV
rfc = RandomForestClassifier(random_state=47, n_estimators=100)
rfc.fit(X_train_new, y_train_orig)
predicted = rfc.predict(X_test_new)
print "Decision Tree Results\nConfusion Matrix: "
print confusion_matrix(y_test_orig,predicted)
print "\n Classifcation Report"
print classification_report(y_test_orig,predicted)
Explanation: With SciKitLearn's logistic regression algorithm, we get much better (but not good) precision than with the 
previous methods (excluding convolutional neural networks), but worse recall.
End of explanation
import matplotlib.pyplot as plt
g=imgdf.groupby(['category']).count()['_unit_id']
objects = g.index
y_pos = np.arange(len(objects))
performance = g
plt.bar(y_pos, performance, align='center', alpha=0.5)
#plt.xticks(y_pos, objects)
plt.xticks(y_pos, objects, rotation='vertical')
plt.ylabel('Class Counts')
plt.title('Dress Classes in Data')
 
plt.show()
Explanation: The random forest is actually working much better than the neural networks and logistic regression models, and better precision than even the convolutional neural network.  The convolutional neural network still has the best recall, however.  It's still not a great model, but it has precision and recall better then 50%, however, most test records are being predicted as 9 as in the other models.  Apparently, our data is unbalanced among the classes - there are tons of category 'plain' compared to others.
End of explanation
<END_TASK>