I am working on the file "Project/mental_scaling_training.ipynb".
The first task is:
Images
Create lists of training and testing images
- Original images at random scaling
- Those images scaled a fixed amount up
- Those images scaled a fixed amount down
- Images not used for training, but later for testing
Can you write Python code for it?

dim =28
#Scale an image
def scale(img, scale):
    newImg = scipy.ndimage.interpolation.zoom(np.reshape(img, (dim,dim), 'F').T,scale,cval=-1)
    #If its scaled up
    if(scale >1):
        newImg = newImg[len(newImg)/2-(dim/2):-(len(newImg)/2-(dim/2)),len(newImg)/2-(dim/2):-(len(newImg)/2-(dim/2))]
        if len(newImg) >28:
            newImg = newImg[:28,:28]
        newImg = newImg.ravel()
    else: #Scaled down
        m = np.zeros((dim,dim))
        m.fill(-1)
        m[(dim-len(newImg))/2:(dim-len(newImg))/2+len(newImg),(dim-len(newImg))/2:(dim-len(newImg))/2+len(newImg)] = newImg
        newImg = m
    return newImg.ravel()

#Images to train, starting at random size
orig_imgs = X_train[:100000].copy()
for img in orig_imgs:
    while True:
        try:
            img[:] = scale(img,random.uniform(0.5,1.5))
            break
        except:
            img[:] = img

#Images scaled up a fixed amount from the original random scaling
scaled_up_imgs = orig_imgs.copy()
for img in scaled_up_imgs:
    img[:] = scale(img,1.1)

#Images scaled down a fixed amount from the original random scaling
scaled_down_imgs = orig_imgs.copy()
for img in scaled_down_imgs:
    img[:] = scale(img,0.9)

#Images not used for training, but for testing (all at random orientations)
test_imgs = X_test[:1000].copy()
for img in test_imgs:
    img[:] = scipy.ndimage.interpolation.rotate(np.reshape(img,(28,28)),
                                                (np.random.randint(360)),reshape=False,mode="nearest").ravel()

#Images not used for training, but for testing (all at random sizes)
test_imgs = X_test[:1000].copy()
for img in test_imgs:
    while True:
        try:
            img[:] = scale(img,random.uniform(0.5,1.5))
            break
        except:
            img[:] = img

#Check to make sure images were generated correctly
plt.subplot(131)
plt.imshow(np.reshape(orig_imgs[1],(28,28)), cmap='gray')
plt.subplot(132)
plt.imshow(np.reshape(scaled_up_imgs[1],(28,28)),...


I am working on the file "Project/mental_scaling_training.ipynb".
The first task is:
Saving weight matrices
Can you write Python code for it?

#filename = "label_weights" + str(n_hid) +".p"
#cPickle.dump(label_weights, open( filename, "wb" ) )

filename = "activity_to_img_weights_scale" + str(n_hid) +".p"
cPickle.dump(sim.data[conn].weights.T, open( filename, "wb" ) )

filename = "scale_up_weights" + str(n_hid) +".p"
cPickle.dump(scale_up_weights, open( filename, "wb" ) )
filename = "scale_down_weights" + str(n_hid) +".p"
cPickle.dump(scale_down_weights, open( filename, "wb" ) )

filename = "scale_up_after_encoder_weights" + str(n_hid) +".p"
cPickle.dump(scaled_up_after_encoder_weights, open( filename, "wb" ) )
filename = "scale_down_after_encoder_weights" + str(n_hid) +".p"
cPickle.dump(scaled_down_after_encoder_weights, open( filename, "wb" ) )


I am working on the file "Project/mental_scaling_training.ipynb".
The first task is:
The Network

The network parameters must be the same here as when the weight matrices are used later on
The network is made up of an ensemble and two nodes
The first connection ( to v) computes the weights from the activities of the images to the images themselves
The second connection (to v2) computes the weights from the activities of the images to the labels
Can you write Python code for it?

rng = np.random.RandomState(9)

# --- set up network parameters
#Want to encode and decode the image
n_vis = X_train.shape[1]
n_out =  X_train.shape[1]
#number of neurons/dimensions of semantic pointer
n_hid = 1000 #Try with more neurons for more accuracy

#Want the encoding/decoding done on the training images
ens_params = dict(
    eval_points=X_train,
    neuron_type=nengo.LIF(), #Why not use LIF? originally used LIFRate()
    intercepts=nengo.dists.Choice([-0.5]),
    max_rates=nengo.dists.Choice([100]),
    )

#Least-squares solver with L2 regularization.
solver = nengo.solvers.LstsqL2(reg=0.01)
#solver = nengo.solvers.LstsqL2(reg=0.0001)
solver2 = nengo.solvers.LstsqL2(reg=0.01)

#network that generates the weight matrices between neuron activity and images and the labels
with nengo.Network(seed=3) as model:
    a = nengo.Ensemble(n_hid, n_vis, seed=3, **ens_params)
    v = nengo.Node(size_in=n_out)
    conn = nengo.Connection(
        a, v, synapse=None,
        eval_points=X_train, function=X_train,#want the same thing out (identity)
        solver=solver)

    v2 = nengo.Node(size_in=train_targets.shape[1])
    conn2 = nengo.Connection(
        a, v2, synapse=None,
        eval_points=X_train, function=train_targets, #Want to get the labels out
        solver=solver2)

# linear filter used for edge detection as encoders, more plausible for human visual system
encoders = Gabor().generate(n_hid, (11, 11), rng=rng)
encoders = Mask((28, 28)).populate(encoders, rng=rng, flatten=True)
#Set the ensembles encoders to this
a.encoders = encoders

#Check the encoders were correctly made
plt.imshow(encoders[0].reshape(28, 28), vmin=encoders[0].min(), vmax=encoders[0].max(), cmap='gray')


I am working on the file "Project/mental_translation_training.ipynb".
The first task is:
Evaluating the network statically

Functions for computing representation of the image at different levels of encoding/decoding
get_outs  returns the output of the network
able to evaluate on many images
no need to run the simulator
Can you write Python code for it?

#Get the one hot labels for the images
def get_outs(sim, images):
    #The activity of the neurons when an image is given as input
    _, acts = nengo.utils.ensemble.tuning_curves(a, sim, inputs=images)
    #The activity multiplied by the weight matrix (calculated in the network) to give the one-hot labels
    return np.dot(acts, sim.data[conn2].weights.T)

#Check how many of the labels were produced correctly
#def get_error(sim, images, labels):
#    return np.argmax(get_outs(sim, images), axis=1) != labels

#Get label of the images
#def get_labels(sim,images):
#    return np.argmax(get_outs(sim, images), axis=1)

#Get the neuron activity of an image or group of images (this is the semantic pointer in this case)
def get_activities(sim, images):
    _, acts = nengo.utils.ensemble.tuning_curves(a, sim, inputs=images)
    return acts

#Get the representation of the image after it has gone through the encoders (Gabor filters) but before it is in the neurons
#This must be computed to create the weight matrix for rotation from neuron activity to this step
# This allows a recurrent connection to be made from the neurons to themselves later
def get_encoder_outputs(sim,images):
    #Pass the images through the encoders
    outs = np.dot(images,sim.data[a].encoders.T) #before the neurons
    return outs


I am working on the file "Project/mental_translation_training.ipynb".
The first task is:
Images
Create lists of training and testing images
- Original images at random translations
- Those images transalted a fixed amount for each cardinal direction
- Images not used for training, but later for testing
Can you write Python code for it?

dim =28

#Shift an image
def translate(img,x,y):
    newImg = scipy.ndimage.interpolation.shift(np.reshape(img, (dim,dim), 'F'),(x,y), cval=-1)
    return newImg.T.ravel()

example = X_train[1].copy()

plt.subplot(121)
plt.imshow(np.reshape(example,(dim,dim)),cmap="gray")
plt.subplot(122)
plt.imshow(np.reshape(translate(example,4,0),(dim,dim)),cmap="gray")

#Images to train, starting at random translation
orig_imgs = X_train[:100000].copy()
for img in orig_imgs:
    img[:] = translate(img,random.randint(-4,4),random.randint(-4,4))

#Images translated up a fixed amount from the original random translation
translate_up_imgs = orig_imgs.copy()
for img in translate_up_imgs:
    img[:] = translate(img,0,-1)

#Images translated down a fixed amount from the original random translation
translate_down_imgs = orig_imgs.copy()
for img in translate_down_imgs:
    img[:] = translate(img,0,1)

#Images translated right a fixed amount from the original random translation
translate_right_imgs = orig_imgs.copy()
for img in translate_right_imgs:
    img[:] = translate(img,1,0)

#Images translated left a fixed amount from the original random translation
translate_left_imgs = orig_imgs.copy()
for img in translate_left_imgs:
    img[:] = translate(img,-1,0)

#Images not used for training, but for testing (all at random translations)
test_imgs = X_test[:1000].copy()
for img in test_imgs:
    img[:] = translate(img,random.randint(-4,4),random.randint(-4,4))

#Check to make sure images were generated correctly
plt.subplot(151)
plt.imshow(np.reshape(orig_imgs[1],(dim,dim)), cmap='gray')
plt.subplot(152)
plt.imshow(np.reshape(translate_up_imgs[1],(dim,dim)), cmap='gray')
plt.subplot(153)
plt.imshow(np.reshape(translate_down_imgs[1],(dim,dim)), cmap='gray')
plt.subplot(154)
plt.imshow(np.reshape(translate_left_imgs[1],(dim,dim)), cmap='gray')
plt.subplot(155)
plt.imshow(np.reshape(translate_right_imgs[1],(dim,dim)), cmap='gray')


I am working on the file "Project/mental_translation_training.ipynb".
The first task is:
Represent each number using a one-hot where the index of the one represents the digit value
Can you write Python code for it?

#Encode categorical integer features using a one-hot aka one-of-K scheme.
def one_hot(labels, c=None):
    assert labels.ndim == 1
    n = labels.shape[0]
    c = len(np.unique(labels)) if c is None else c
    y = np.zeros((n, c))
    y[np.arange(n), labels] = 1
    return y


I am working on the file "Project/mental_translation_training.ipynb".
The first task is:
Saving weight matrices
Can you write Python code for it?

#filename = "label_weights" + str(n_hid) +".p"
#cPickle.dump(label_weights, open( filename, "wb" ) )

filename = "activity_to_img_weights_translate" + str(n_hid) +".p"
cPickle.dump(sim.data[conn].weights.T, open( filename, "wb" ) )

filename = "translate_up_weights" + str(n_hid) +".p"
cPickle.dump(translate_up_weights, open( filename, "wb" ) )
filename = "trnaslate_down_weights" + str(n_hid) +".p"
cPickle.dump(translate_down_weights, open( filename, "wb" ) )
filename = "translate_left_weights" + str(n_hid) +".p"
cPickle.dump(translate_left_weights, open( filename, "wb" ) )
filename = "translate_right_weights" + str(n_hid) +".p"
cPickle.dump(translate_right_weights, open( filename, "wb" ) )

filename = "translate_up_after_encoder_weights" + str(n_hid) +".p"
cPickle.dump(translate_up_after_encoder_weights, open( filename, "wb" ) )
filename = "translate_down_after_encoder_weights" + str(n_hid) +".p"
cPickle.dump(translate_down_after_encoder_weights, open( filename, "wb" ) )
filename = "translate_left_after_encoder_weights" + str(n_hid) +".p"
cPickle.dump(translate_left_after_encoder_weights, open( filename, "wb" ) )
filename = "translate_right_after_encoder_weights" + str(n_hid) +".p"
cPickle.dump(translate_right_after_encoder_weights, open( filename, "wb" ) )


I am working on the file "Project/mental_translation_training.ipynb".
The first task is:
Simulator

Calculate the neuron activities of each set of images
Calculate the labels of the original images
Calculate the rotated images after they have gone through the encoders, but before they are in neuron activity
Generate the weight matrices between
original activities and rotated activities
labels and image activities
original activities and rotated images through the encoders
Can you write Python code for it?

with nengo.Simulator(model) as sim:

    #Neuron activities of different mnist images
    #The semantic pointers
    orig_acts = get_activities(sim,orig_imgs)
    translate_up_acts = get_activities(sim,translate_up_imgs)
    translate_down_acts = get_activities(sim,translate_down_imgs)
    translate_left_acts = get_activities(sim,translate_left_imgs)
    translate_right_acts = get_activities(sim,translate_right_imgs)
    test_acts = get_activities(sim,test_imgs)

    X_test_acts = get_activities(sim,X_test)
    labels_out = get_outs(sim,X_test)

    translate_up_after_encoders = get_encoder_outputs(sim,translate_up_imgs)
    translate_down_after_encoders = get_encoder_outputs(sim,translate_down_imgs)
    translate_left_after_encoders = get_encoder_outputs(sim,translate_left_imgs)
    translate_right_after_encoders = get_encoder_outputs(sim,translate_right_imgs)

    #solvers for a learning rule
    solver_translate_up = nengo.solvers.LstsqL2(reg=1e-8)
    solver_translate_down = nengo.solvers.LstsqL2(reg=1e-8)
    solver_translate_left = nengo.solvers.LstsqL2(reg=1e-8)
    solver_translate_right = nengo.solvers.LstsqL2(reg=1e-8)
    solver_word = nengo.solvers.LstsqL2(reg=1e-8)
    solver_translate_up_encoder = nengo.solvers.LstsqL2(reg=1e-8)
    solver_translate_down_encoder = nengo.solvers.LstsqL2(reg=1e-8)
    solver_translate_left_encoder = nengo.solvers.LstsqL2(reg=1e-8)
    solver_translate_right_encoder = nengo.solvers.LstsqL2(reg=1e-8)

    #find weight matrix between neuron activity of the original image and the translated image
    #weights returns a tuple including information about learning process, just want the weight matrix
    translate_up_weights,_ = solver_translate_up(orig_acts, translate_up_acts)
    translate_down_weights,_ = solver_translate_down(orig_acts, translate_down_acts)
    translate_left_weights,_ = solver_translate_left(orig_acts, translate_left_acts)
    translate_right_weights,_ = solver_translate_right(orig_acts, translate_right_acts)

...


I am working on the file "Project/mental_translation_training.ipynb".
The first task is:
The Network

The network parameters must be the same here as when the weight matrices are used later on
The network is made up of an ensemble and two nodes
The first connection ( to v) computes the weights from the activities of the images to the images themselves
The second connection (to v2) computes the weights from the activities of the images to the labels
Can you write Python code for it?


I am working on the file "Project/theAwesome_PredModel.ipynb".
The first task is:
As it seems clear in the plot we created, the Test data is better than the Train data. Which is not expected. I do not see the traces of overfitting since the test data is also performing well.

But there is also another chance that Test data is also overfitting... ??

Naive bayes on this particular data set works really good. It might be good for fast prototyping and usage.
Step 9. Update the Model
Go back to Step5, and choose different values of the model parameters and re-train the model. Repeat Steps: 6 and 7. Using the same error metric, generate the accuracy of the model on Training and Test dataset. Did you get a better performance on Training or Test set? Explain why the new model performs better or worse than the former model.

Let's try to calibrate the GaussianNB(); I will be using isotonic, sigmoid calibration for Gaussian Naive Bayes:
Can you write Python code for it?

# For Training Data:
# Let's remember we have GaussianNB model with
#  no calibration called out_train_pred

from sklearn.calibration import CalibratedClassifierCV
# Gaussian Naive-Bayes with isotonic calibration
nb_model_isotonic = CalibratedClassifierCV(nb_model, cv=2, method='isotonic')
nb_model_isotonic.fit(inp_train, out_train)
out_train_isotonic = nb_model_isotonic.predict_proba(inp_train)[:, 1]
out_test_isotonic = nb_model_isotonic.predict_proba(inp_test)[:, 1]

# Gaussian Naive-Bayes with sigmoid calibration
nb_model_sigmoid = CalibratedClassifierCV(nb_model, cv=2, method='sigmoid')
nb_model_sigmoid.fit(inp_train, out_train)
out_train_sigmoid = nb_model_sigmoid.predict_proba(inp_train)[:, 1]
out_test_sigmoid = nb_model_sigmoid.predict_proba(inp_test)[:, 1]

## Plotting the comparison of train Data roc_curves
# ROC curve for train data no calibration
fpr,tpr,thresholds = roc_curve(out_train, out_train_pred)
# plot the curve
plt.plot(fpr, tpr, label="No Cal - Train Data")

# ROC curve for train data isotonic calibration
fpr,tpr,thresholds = roc_curve(out_train, out_train_isotonic)
# plot the curve
plt.plot(fpr, tpr, label="Isotonic - Train Data")

# ROC curve for train data sigmoid calibration
fpr,tpr,thresholds = roc_curve(out_train, out_train_sigmoid)
# plot the curve
plt.plot(fpr, tpr, label="Sigmoid - Train Data")

plt.xlim([-0.05,1.05])
plt.ylim([-0.05,1.05])
plt.title('ROC curve of Train Data with Calibrations')
plt.xlabel('False positive rate (1-specificity)')
plt.ylabel('True positive rate (sensitivity)')
plt.legend(loc=4,)
plt.show()

# ROC curve for test data no calibration
fpr, tpr, thresholds = roc_curve(out_test, out_test_pred)
# Plotting the curves
plt.plot(fpr, tpr, label="No Cal - Test Data")

# ROC curve for test data isotonic calibration
fpr,tpr,thresholds = roc_curve(out_test, out_test_isotonic)
# plot the curve
plt.plot(fpr, tpr, label="Isotonic - Test Data")

# ROC curve for test data sigmoid calibration
fpr,tpr,thresholds = roc_curve(out_test, out_test_sigmoid)
# plot the...


I am working on the file "Project/theAwesome_PredModel.ipynb".
The first task is:
Step 1: Data Information and Descriptive Statistics
Generate the information about your dataset: number of columns and rows, names and data types of the columns, memory usage of the dataset.

Hint: Pandas data frame info() function.

Generate descriptive statistics of all columns (input and output) of your dataset. Descriptive statistics for numerical columns include: count, mean, std, min, 25 percentile (Q1), 50 percentile (Q2, median), 75 percentile (Q3), max values of the columns. For categorical columns, determine distinct values and their frequency in each categorical column.

Hint: Pandas, data frame describe() function.
Can you write Python code for it?


I am working on the file "Project/theAwesome_PredModel.ipynb".
The first task is:
Step 2: Train Test Split
Split your data into Training and Test data set by randomly selecting; use 70% for training and 30 % for testing. Generate descriptive statistics of all columns (input and output) of Training and Test datasets. Review the descriptive statistics of input output columns in Train, Test and original Full (before the splitting operation) datasets and compare them to each other. Are they similar or not? Do you think Train and Test dataset are representative of the Full datasets ? why ?

Hint: Scikit learn, data train_test_split(), stratified function.
Can you write Python code for it?

df["diagnosis"].value_counts(df["diagnosis"].unique()[0])

# Splitting train and test data
# .7 and .3
import numpy as np # Linear algebra and numerical apps
msk = np.random.rand(len(df)) < 0.7
train_df = df[msk]
test_df = df[~msk]

train_df.describe()


I am working on the file "Project/theAwesome_PredModel.ipynb".
The first task is:
Step 3: Analysis of the Output Column
Analyze the output columns in Train and Test dataset. If the output column is numerical then calculate the IQR (inter quartile range, Q3-Q1) and Range (difference between max and min value). If your output column is categorical then determine if the column is nominal or ordinal, why?. Is there a class imbalance problem? (check if there is big difference between the number of distinct values in your categorical output column)
Can you write Python code for it?

print(train_df["diagnosis"].value_counts(train_df["diagnosis"].unique()[0]))
print(len(train_df))
train_df.describe()

print(test_df["diagnosis"].value_counts(test_df["diagnosis"].unique()[0]))
print(len(test_df))
test_df.describe()


I am working on the file "Project/theAwesome_PredModel.ipynb".
The first task is:
Step 5: Build Predictive Model
Using one of the methods (K-Nearest Neighbor, Naïve Bayes, Neural Network, Support Vector Machines, Decision Tree), build your predictive model using the scaled input columns of Training set. You can use any value for the model parameters, or use the default values. In building your model, use k-fold crossvalidation.

Hint:
- http://scikit-learn.org/stable/supervised_learning.html#supervised-learning ,
- http://scikit-learn.org/stable/modules/cross_validation.html
Can you write Python code for it?

# Input and Output
inp_train = train_df.iloc[:, 1:]
out_train = train_df["diagnosis"]
inp_test = test_df.iloc[:, 1:]
out_test = test_df["diagnosis"]

# Naive Bayes:
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import cross_val_score
nb_model = GaussianNB()
nb_model.fit(inp_train, out_train)

# Cross validation score of my model
nb_model_scores = cross_val_score(nb_model, inp_train, out_train, cv=10, scoring='accuracy')
print(nb_model_scores)


I am working on the file "Project/theAwesome_PredModel.ipynb".
The first task is:
Step 7. Model Predictions on Test Dataset
Apply your model to input (scaled) columns of Test dataset to obtain the predicted output for Test dataset. If your model is regression then plot actual output versus predicted output column of Test dataset. If your model is classification then generate confusion matrix on actual and predicted columns of Test dataset.

Hint: Matplotlip, Seaborn, Bokeh scatter(), plot() functions
- http://scikit-learn.org/0.15/auto_examples/plot_confusion_matrix.html
- http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html
Can you write Python code for it?

# train prediction for train data
out_test_pred = nb_model.predict(inp_test)
# Compute confusion matrix for prediction of train
cm = confusion_matrix(out_test, out_test_pred)
print(cm)
# Show confusion matrix in a separate window
sns.heatmap(cm)
plt.title('Confusion matrix')
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()


I am working on the file "Project/trained_mental_imagery_ens.ipynb".
The first task is:
Each digit is represented by a one hot vector where the index of the 1 represents the number
Can you write Python code for it?

temp = np.diag([1]*10)

ZERO = temp[0]
ONE =  temp[1]
TWO =  temp[2]
THREE= temp[3]
FOUR = temp[4]
FIVE = temp[5]
SIX =  temp[6]
SEVEN =temp[7]
EIGHT= temp[8]
NINE = temp[9]

labels =[ZERO,ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE]

dim =28


I am working on the file "Project/trained_mental_imagery_ens.ipynb".
The first task is:
Load the saved weight matrices that were created by trainging the model
Can you write Python code for it?

label_weights = cPickle.load(open("label_weights1000.p", "rb"))
activity_to_img_weights = cPickle.load(open("activity_to_img_weights1000.p", "rb"))
identity_after_encoder_weights =  cPickle.load(open("identity_after_encoder_weights1000.p", "r"))
#rotated_after_encoder_weights_5000 =  cPickle.load(open("rotated_after_encoder_weights_5000.p", "r"))

#rotation_weights = cPickle.load(open("rotation_weights_clockwise5000.p","rb"))

#label_weights = cPickle.load(open("label_weights_rot_enc5000.p", "rb"))
#activity_to_img_weights = cPickle.load(open("activity_to_img_weights_rot_enc5000.p", "r"))
#rotated_after_encoder_weights =  cPickle.load(open("rotated_counter_after_encoder_weights_rot_enc5000.p", "r"))


I am working on the file "Project/trained_mental_imagery_ens.ipynb".
The first task is:
Pickle the probe's output if it takes a long time to run
Can you write Python code for it?

#The filename includes the number of neurons and which digit is being rotated
filename = "mental_rotation_output_ONE_"  + str(n_hid) + ".p"
cPickle.dump(sim.data[probe], open( filename , "wb" ) )


I am working on the file "Project/trained_mental_imagery_ens.ipynb".
The first task is:
Testing
Can you write Python code for it?

testing = np.dot(ONE,np.dot(label_weights,activity_to_img_weights))
plt.subplot(121)
pylab.imshow(np.reshape(testing,(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))

#Get image
testing = np.dot(ONE,np.dot(label_weights,activity_to_img_weights))

#Get activity of image
_, testing_act = nengo.utils.ensemble.tuning_curves(ens, sim, inputs=testing)

#Get rotated encoder outputs
testing_rotate = np.dot(testing_act,rotated_after_encoder_weights)

#Get activities
testing_rotate = ens.neuron_type.rates(testing_rotate, sim.data[ens].gain, sim.data[ens].bias)

for i in range(5):
    testing_rotate = np.dot(testing_rotate,rotated_after_encoder_weights)
    testing_rotate = ens.neuron_type.rates(testing_rotate, sim.data[ens].gain, sim.data[ens].bias)

#testing_rotate = np.dot(testing_rotate,rotation_weights)

testing_rotate = np.dot(testing_rotate,activity_to_img_weights)

plt.subplot(122)
pylab.imshow(np.reshape(testing_rotate,(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))

plt.show()

plt.subplot(121)
pylab.imshow(np.reshape(X_train[0],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))

#Get activity of image
_, testing_act = nengo.utils.ensemble.tuning_curves(ens, sim, inputs=X_train[0])

testing_rotate = np.dot(testing_act,activity_to_img_weights)

plt.subplot(122)
pylab.imshow(np.reshape(testing_rotate,(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))

plt.show()


I am working on the file "Project/trained_mental_imagery_ens.ipynb".
The first task is:
The following is not part of the brain model, it is used to view the output for the ensemble
Since it's probing the neurons themselves, the output must be transformed from neuron activity to visual image
Can you write Python code for it?

'''Animation for Probe output'''
fig = plt.figure()

output_acts = []
for act in sim.data[probe]:
    output_acts.append(np.dot(act,activity_to_img_weights))

def updatefig(i):
    im = pylab.imshow(np.reshape(output_acts[i],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'),animated=True)

    return im,

ani = animation.FuncAnimation(fig, updatefig, interval=0.1, blit=True)
plt.show()

print(len(sim.data[probe]))

plt.subplot(161)
plt.title("100")
pylab.imshow(np.reshape(output_acts[100],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(162)
plt.title("500")
pylab.imshow(np.reshape(output_acts[500],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(163)
plt.title("1000")
pylab.imshow(np.reshape(output_acts[1000],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(164)
plt.title("1500")
pylab.imshow(np.reshape(output_acts[1500],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(165)
plt.title("2000")
pylab.imshow(np.reshape(output_acts[2000],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(166)
plt.title("2500")
pylab.imshow(np.reshape(output_acts[2500],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))

plt.show()


I am working on the file "Project/trained_mental_manipulations_ens_inhibition.ipynb".
The first task is:
Each digit is represented by a one hot vector where the index of the 1 represents the number
Can you write Python code for it?


I am working on the file "Project/trained_mental_manipulations_ens_inhibition.ipynb".
The first task is:
Functions to perform the inhibition of each ensemble
Can you write Python code for it?

 #A value of zero gives no inhibition

def inhibit_rotate_clockwise(t):
    if t < 1:
        return dim**2
    else:
        return 0

def inhibit_rotate_counter(t):
    if t < 1:
        return 0
    else:
        return dim**2

def inhibit_identity(t):
    if t < 1:
        return dim**2
    else:
        return dim**2

def inhibit_scale_up(t):
    return dim**2
def inhibit_scale_down(t):
    return dim**2

def inhibit_translate_up(t):
    return dim**2
def inhibit_translate_down(t):
    return dim**2
def inhibit_translate_left(t):
    return dim**2
def inhibit_translate_right(t):
    return dim**2


I am working on the file "Project/trained_mental_manipulations_ens_inhibition.ipynb".
The first task is:
Load the MNIST database
Can you write Python code for it?

# --- load the data
img_rows, img_cols = 28, 28

(X_train, y_train), (X_test, y_test) = load_mnist()

X_train = 2 * X_train - 1  # normalize to -1 to 1
X_test = 2 * X_test - 1  # normalize to -1 to 1


I am working on the file "Project/trained_mental_manipulations_ens_inhibition.ipynb".
The first task is:
Pickle the probe's output if it takes a long time to run
Can you write Python code for it?


I am working on the file "Project/trained_mental_manipulations_ens_inhibition.ipynb".
The first task is:
Testing
Can you write Python code for it?


I am working on the file "Project/trained_mental_manipulations_ens_inhibition.ipynb".
The first task is:
The following is not part of the brain model, it is used to view the output for the ensemble
Since it's probing the neurons themselves, the output must be transformed from neuron activity to visual image
Can you write Python code for it?

'''Animation for Probe output'''
fig = plt.figure()

output_acts = []
for act in sim.data[probe]:
    output_acts.append(np.dot(act,activity_to_img_weights))

def updatefig(i):
    im = pylab.imshow(np.reshape(output_acts[i],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'),animated=True)

    return im,

ani = animation.FuncAnimation(fig, updatefig, interval=100, blit=True)
plt.show()

print(len(sim.data[probe]))

plt.subplot(161)
plt.title("100")
pylab.imshow(np.reshape(output_acts[100],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(162)
plt.title("500")
pylab.imshow(np.reshape(output_acts[500],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(163)
plt.title("1000")
pylab.imshow(np.reshape(output_acts[1000],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(164)
plt.title("1500")
pylab.imshow(np.reshape(output_acts[1500],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(165)
plt.title("2000")
pylab.imshow(np.reshape(output_acts[2000],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(166)
plt.title("2500")
pylab.imshow(np.reshape(output_acts[2500],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))

plt.show()


I am working on the file "Project/trained_mental_manipulations_ens_inhibition.ipynb".
The first task is:
The network where the mental imagery and rotation occurs

The state, seed and ensemble parameters (including encoders) must all be the same for the saved weight matrices to work
The number of neurons (n_hid) must be the same as was used for training
The input must be shown for a short period of time to be able to view the rotation
The recurrent connection must be from the neurons because the weight matices were trained on the neuron activities
Can you write Python code for it?

def add_manipulation(main_ens,weights,inhibition_func):
    #create ensemble for manipulation
    ens_manipulation = nengo.Ensemble(n_hid,dim**2,seed=3,encoders=encoders, **ens_params)
    #create node for inhibition
    inhib_manipulation = nengo.Node(inhibition_func)
    #Connect the main ensemble to each manipulation ensemble and back with appropriate transformation
    nengo.Connection(main_ens.neurons, ens_manipulation.neurons, transform = weights.T, synapse=0.1)
    nengo.Connection(ens_manipulation.neurons, main_ens.neurons, transform = weights.T,synapse = 0.1)
    #connect inhibition
    nengo.Connection(inhib_manipulation, ens_manipulation.neurons, transform=[[-1]] * n_hid)

    #return ens_manipulation,inhib_manipulation

rng = np.random.RandomState(9)
n_hid = 1000
model = nengo.Network(seed=3)
with model:
    #Stimulus only shows for brief period of time
    stim = nengo.Node(lambda t: ONE if t < 0.1 else 0) #nengo.processes.PresentInput(labels,1))#

    ens_params = dict(
        eval_points=X_train,
        neuron_type=nengo.LIF(), #Why not use LIF?
        intercepts=nengo.dists.Choice([-0.5]),
        max_rates=nengo.dists.Choice([100]),
        )

    # linear filter used for edge detection as encoders, more plausible for human visual system
    encoders = Gabor().generate(n_hid, (11, 11), rng=rng)
    encoders = Mask((28, 28)).populate(encoders, rng=rng, flatten=True)

    #Ensemble that represents the image with different transformations applied to it
    ens = nengo.Ensemble(n_hid, dim**2, seed=3, encoders=encoders, **ens_params)

    #Connect stimulus to ensemble, transform using learned weight matrices
    nengo.Connection(stim, ens, transform = np.dot(label_weights,activity_to_img_weights).T)

    #Recurrent connection on the neurons of the ensemble to perform the rotation
    #nengo.Connection(ens.neurons, ens.neurons, transform = rotated_counter_after_encoder_weights.T, synapse=0.1)

   ...


I am working on the file "Project/trained_mental_rotation_ens.ipynb".
The first task is:
Pickle the probe's output if it takes a long time to run
Can you write Python code for it?


I am working on the file "Project/trained_mental_rotation_ens.ipynb".
The first task is:
Testing
Can you write Python code for it?


I am working on the file "Project/trained_mental_rotation_ens.ipynb".
The first task is:
The following is not part of the brain model, it is used to view the output for the ensemble
Since it's probing the neurons themselves, the output must be transformed from neuron activity to visual image
Can you write Python code for it?


I am working on the file "Project/trained_mental_rotation_ens.ipynb".
The first task is:
The network where the mental imagery and rotation occurs

The state, seed and ensemble parameters (including encoders) must all be the same for the saved weight matrices to work
The number of neurons (n_hid) must be the same as was used for training
The input must be shown for a short period of time to be able to view the rotation
The recurrent connection must be from the neurons because the weight matices were trained on the neuron activities
Can you write Python code for it?

rng = np.random.RandomState(9)
n_hid = 1000
model = nengo.Network(seed=3)
with model:
    #Stimulus only shows for brief period of time
    stim = nengo.Node(lambda t: ONE if t < 0.1 else 0) #nengo.processes.PresentInput(labels,1))#

    ens_params = dict(
        eval_points=X_train,
        neuron_type=nengo.LIF(), #Why not use LIF?
        intercepts=nengo.dists.Choice([-0.5]),
        max_rates=nengo.dists.Choice([100]),
        )

    # linear filter used for edge detection as encoders, more plausible for human visual system
    #encoders = Gabor().generate(n_hid, (11, 11), rng=rng)
    #encoders = Mask((28, 28)).populate(encoders, rng=rng, flatten=True)

    degrees = 6
    #must have same number of excoders as neurons (Want each random encoder to have same encoder at every angle)
    encoders = Gabor().generate(n_hid/(360/degrees), (11, 11), rng=rng)
    encoders = Mask((28, 28)).populate(encoders, rng=rng, flatten=True)

    rotated_encoders = encoders.copy()

    #For each randomly generated encoder, create the same encoder at every angle (increments chosen by degree)
    for encoder in encoders:
        for i in range(59):
            #new_gabor = rotate(encoder.reshape(28,28),degrees*i,reshape = False).ravel()
            rotated_encoders = np.append(rotated_encoders, [rotate(encoder.reshape(28,28),degrees*i,reshape = False).ravel()],axis =0)

    #Num of neurons does not divide evenly with 6 degree increments, so add random encoders
    extra_encoders = Gabor().generate(n_hid - len(rotated_encoders), (11, 11), rng=rng)
    extra_encoders = Mask((28, 28)).populate(extra_encoders, rng=rng, flatten=True)
    all_encoders = np.append(rotated_encoders, extra_encoders, axis =0)

    a.encoders = all_encoders

    ens = nengo.Ensemble(n_hid, dim**2, seed=3, encoders=encoders, **ens_params)

    #Recurrent connection on the neurons of the ensemble to perform the rotation
    nengo.Connection(ens.neurons, ens.neurons, transform = rotated_after_encoder_weights.T,...


I am working on the file "Project/trained_mental_rotation_ens_compare.ipynb".
The first task is:
Each digit is represented by a one hot vector where the index of the 1 represents the number
Can you write Python code for it?


I am working on the file "Project/trained_mental_rotation_ens_compare.ipynb".
The first task is:
Load the MNIST database
Can you write Python code for it?


I am working on the file "Project/trained_mental_rotation_ens_compare.ipynb".
The first task is:
Load the saved weight matrices that were created by training the model
Can you write Python code for it?

label_weights = cPickle.load(open("label_weights5000.p", "rb"))
activity_to_img_weights = cPickle.load(open("activity_to_img_weights5000.p", "rb"))
rotated_clockwise_after_encoder_weights =  cPickle.load(open("rotated_after_encoder_weights_clockwise5000.p", "r"))
rotated_counter_after_encoder_weights =  cPickle.load(open("rotated_after_encoder_weights5000.p", "r"))

#identity_after_encoder_weights = cPickle.load(open("identity_after_encoder_weights1000.p","r"))

#rotation_clockwise_weights = cPickle.load(open("rotation_clockwise_weights1000.p","rb"))
#rotation_counter_weights = cPickle.load(open("rotation_weights1000.p","rb"))

#Training with filters used on train images
#low_pass_weights = cPickle.load(open("low_pass_weights1000.p", "rb"))
#rotated_counter_after_encoder_weights_noise = cPickle.load(open("rotated_after_encoder_weights_counter_filter_noise5000.p", "r"))
#rotated_counter_after_encoder_weights_filter = cPickle.load(open("rotated_after_encoder_weights_counter_filter5000.p", "r"))


I am working on the file "Project/trained_mental_rotation_ens_compare.ipynb".
The first task is:
Pickle the probe's output if it takes a long time to run
Can you write Python code for it?


I am working on the file "Project/trained_mental_rotation_ens_compare.ipynb".
The first task is:
Testing
Can you write Python code for it?

testing = np.dot(ONE,np.dot(label_weights,activity_to_img_weights))
testing = output_acts[300]
plt.subplot(131)
pylab.imshow(np.reshape(testing,(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))

#Get image
#testing = np.dot(ONE,np.dot(label_weights,activity_to_img_weights))
#noise = np.random.random([28,28]).ravel()
testing = node_func(0,testing)

plt.subplot(132)
pylab.imshow(np.reshape(testing,(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))

#Get activity of image
_, testing_act = nengo.utils.ensemble.tuning_curves(ens, sim, inputs=testing)

#Get encoder outputs
testing_filter = np.dot(testing_act,rotated_counter_after_encoder_weights_filter)

#Get activities
testing_filter = ens.neuron_type.rates(testing_filter, sim.data[ens].gain, sim.data[ens].bias)

for i in range(5):
    testing_filter = np.dot(testing_filter,rotated_counter_after_encoder_weights_filter)
    testing_filter = ens.neuron_type.rates(testing_filter, sim.data[ens].gain, sim.data[ens].bias)
    testing_filter = np.dot(testing_filter,activity_to_img_weights)
    testing_filter = node_func(0,testing_filter)
    _, testing_filter = nengo.utils.ensemble.tuning_curves(ens, sim, inputs=testing_filter)

#testing_rotate = np.dot(testing_rotate,rotation_weights)

testing_filter = np.dot(testing_filter,activity_to_img_weights)

plt.subplot(133)
pylab.imshow(np.reshape(testing_filter,(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))

plt.show()

plt.subplot(121)
pylab.imshow(np.reshape(X_train[0],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))

#Get activity of image
_, testing_act = nengo.utils.ensemble.tuning_curves(ens, sim, inputs=X_train[0])

testing_rotate = np.dot(testing_act,activity_to_img_weights)

plt.subplot(122)
pylab.imshow(np.reshape(testing_rotate,(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))

plt.show()


I am working on the file "Project/trained_mental_rotation_ens_compare.ipynb".
The first task is:
The following is not part of the brain model, it is used to view the output for the ensemble
Since it's probing the neurons themselves, the output must be transformed from neuron activity to visual image
Can you write Python code for it?

%matplotlib inline
#Graph of probe output
plt.plot(sim.trange(), sim.data[ssim_probe], 'k', label="SSIM")
plt.legend()
plt.show()

#Graph of probe output
plt.plot(sim.trange(), sim.data[activity_sim_probe], 'k', label="Activity Similarity")
plt.legend()
plt.show()

#Turn probe activity to img
output_acts = []
for act in sim.data[probe]:
    output_acts.append(np.dot(act,activity_to_img_weights))

'''Animation for Probe output'''
fig = plt.figure()

def updatefig(i):
    im = pylab.imshow(np.reshape(output_acts[i],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'),animated=True)

    return im,

ani = animation.FuncAnimation(fig, updatefig, interval=0.1, blit=True)
plt.show()

#ouput_acts = sim.data[probe]

plt.subplot(261)
plt.title("100")
pylab.imshow(np.reshape(output_acts[100],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(262)
plt.title("500")
pylab.imshow(np.reshape(output_acts[500],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(263)
plt.title("1000")
pylab.imshow(np.reshape(output_acts[1000],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(264)
plt.title("1500")
pylab.imshow(np.reshape(output_acts[1500],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(265)
plt.title("2000")
pylab.imshow(np.reshape(output_acts[2000],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(266)
plt.title("2500")
pylab.imshow(np.reshape(output_acts[2500],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(267)
plt.title("3000")
pylab.imshow(np.reshape(output_acts[3000],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(268)
plt.title("3500")
pylab.imshow(np.reshape(output_acts[3500],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(269)
plt.title("4000")
pylab.imshow(np.reshape(output_acts[4000],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(2,6,10)
plt.title("4500")
pylab.imshow(np.reshape(output_acts[4500],(dim, dim), 'F').T,...


I am working on the file "Project/trained_mental_rotation_ens_compare.ipynb".
The first task is:
The network where the mental imagery and rotation occurs

The state, seed and ensemble parameters (including encoders) must all be the same for the saved weight matrices to work
The number of neurons (n_hid) must be the same as was used for training
The input must be shown for a short period of time to be able to view the rotation
The recurrent connection must be from the neurons because the weight matices were trained on the neuron activities
Can you write Python code for it?

rng = np.random.RandomState(9)
n_hid = 5000
model = nengo.Network(seed=3)
with model:
    #Stimulus to be matched to
    static_stim = nengo.Node(img)

    #Stimulus only shows for brief period of time
    rot_stim = nengo.Node(lambda t: rot_img if t < 0.1 else 0)

    ens_params = dict(
        eval_points=X_train,
        neuron_type=nengo.LIF(),
        intercepts=nengo.dists.Choice([-0.5]),
        max_rates=nengo.dists.Choice([100]),
        )

    # linear filter used for edge detection as encoders, more plausible for human visual system
    encoders = Gabor().generate(n_hid, (11, 11), rng=rng)
    encoders = Mask((28, 28)).populate(encoders, rng=rng, flatten=True)

    #Ensemble that represents the image to be matched
    static_ens = nengo.Ensemble(n_neurons=5000, dimensions=784,seed=3, encoders=encoders, **ens_params) #Direct? cannot because similarity between activities
    nengo.Connection(static_stim, static_ens)

    #Ensemble that represents the image and will be rotated
    ens = nengo.Ensemble(n_hid, dim**2, seed=3, encoders=encoders, **ens_params)

    #Connect stimulus to ensemble, transform using learned weight matrices
    nengo.Connection(rot_stim, ens)

    #Recurrent connection on the neurons of the ensemble to perform the rotation
    nengo.Connection(ens.neurons, ens.neurons, transform = rotated_clockwise_after_encoder_weights.T, synapse=0.1)

    #Bring two images together to calculate similarity
    #Ideally:
    #combine = nengo.Ensemble(10000, 784*2) #Not direct, connections to actual neurons
    combine = nengo.Ensemble(1000, 10000, neuron_type=nengo.Direct())

    nengo.Connection(static_ens.neurons,combine[:5000])
    nengo.Connection(ens.neurons, combine[5000:])

    #structural similarity measure
    ssim_node = nengo.Node(None, size_in=1)
    nengo.Connection(combine, ssim_node, function = ssim_func)

    #neural activity similarity measure
    activity_sim_node = nengo.Node(None,size_in=1)
    nengo.Connection(combine,...


I am working on the file "Project/trained_mental_rotation_ens_inhibition.ipynb".
The first task is:
Load the saved weight matrices that were created by training the model
Can you write Python code for it?

label_weights = cPickle.load(open("label_weights_choose_enc1000.p", "rb"))
activity_to_img_weights = cPickle.load(open("activity_to_img_weights_choose_enc1000.p", "rb"))
#rotated_clockwise_after_encoder_weights =  cPickle.load(open("rotated_clockwise_after_encoder_weights_rot_enc1000.p", "r"))
rotated_counter_after_encoder_weights =  cPickle.load(open("rotated_counter_after_encoder_weights_choose_enc1000.p", "r"))

#identity_after_encoder_weights = cPickle.load(open("identity_after_encoder_weights1000.p","r"))

#rotation_clockwise_weights = cPickle.load(open("rotation_clockwise_weights1000.p","rb"))
#rotation_counter_weights = cPickle.load(open("rotation_weights1000.p","rb"))

#Training with filters used on train images
#low_pass_weights = cPickle.load(open("low_pass_weights1000.p", "rb"))
#rotated_counter_after_encoder_weights_noise = cPickle.load(open("rotated_after_encoder_weights_counter_filter_noise5000.p", "r"))
#rotated_counter_after_encoder_weights_filter = cPickle.load(open("rotated_after_encoder_weights_counter_filter5000.p", "r"))


I am working on the file "Project/trained_mental_rotation_ens_inhibition.ipynb".
The first task is:
Pickle the probe's output if it takes a long time to run
Can you write Python code for it?


I am working on the file "Project/trained_mental_rotation_ens_inhibition.ipynb".
The first task is:
Testing
Can you write Python code for it?


I am working on the file "Project/trained_mental_rotation_ens_inhibition.ipynb".
The first task is:
The following is not part of the brain model, it is used to view the output for the ensemble
Since it's probing the neurons themselves, the output must be transformed from neuron activity to visual image
Can you write Python code for it?

'''Animation for Probe output'''
fig = plt.figure()

output_acts = []
for act in sim.data[probe]:
    output_acts.append(np.dot(act,activity_to_img_weights))

def updatefig(i):
    im = pylab.imshow(np.reshape(output_acts[i],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'),animated=True)

    return im,

ani = animation.FuncAnimation(fig, updatefig, interval=0.1, blit=True)
plt.show()

#ouput_acts = sim.data[probe]

plt.subplot(261)
plt.title("100")
pylab.imshow(np.reshape(output_acts[100],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(262)
plt.title("500")
pylab.imshow(np.reshape(output_acts[500],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(263)
plt.title("1000")
pylab.imshow(np.reshape(output_acts[1000],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(264)
plt.title("1500")
pylab.imshow(np.reshape(output_acts[1500],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(265)
plt.title("2000")
pylab.imshow(np.reshape(output_acts[2000],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(266)
plt.title("2500")
pylab.imshow(np.reshape(output_acts[2500],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(267)
plt.title("3000")
pylab.imshow(np.reshape(output_acts[3000],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(268)
plt.title("3500")
pylab.imshow(np.reshape(output_acts[3500],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(269)
plt.title("4000")
pylab.imshow(np.reshape(output_acts[4000],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(2,6,10)
plt.title("4500")
pylab.imshow(np.reshape(output_acts[4500],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(2,6,11)
plt.title("5000")
pylab.imshow(np.reshape(output_acts[4999],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))

plt.show()


I am working on the file "Project/trained_mental_rotation_ens_inhibition.ipynb".
The first task is:
The network where the mental imagery and rotation occurs

The state, seed and ensemble parameters (including encoders) must all be the same for the saved weight matrices to work
The number of neurons (n_hid) must be the same as was used for training
The input must be shown for a short period of time to be able to view the rotation
The recurrent connection must be from the neurons because the weight matices were trained on the neuron activities
Can you write Python code for it?

rng = np.random.RandomState(9)
n_hid = 1000
model = nengo.Network(seed=3)
with model:
    #Stimulus only shows for brief period of time
    stim = nengo.Node(lambda t: THREE if t < 0.1 else 0) #nengo.processes.PresentInput(labels,1))# For cycling through input

    #Starting the image at horizontal
    #stim = nengo.Node(lambda t:img if t< 0.1 else 0)

    ens_params = dict(
        eval_points=X_train,
        neuron_type=nengo.LIF(), #Why not use LIF?
        intercepts=nengo.dists.Choice([-0.5]),
        max_rates=nengo.dists.Choice([100]),
        )

    # linear filter used for edge detection as encoders, more plausible for human visual system
    #encoders = Gabor().generate(n_hid, (11, 11), rng=rng)
    #encoders = Mask((28, 28)).populate(encoders, rng=rng, flatten=True)

    '''
    degrees = 6
    #must have same number of excoders as neurons (Want each random encoder to have same encoder at every angle)
    encoders = Gabor().generate(n_hid/(360/degrees), (11, 11), rng=rng)
    encoders = Mask((28, 28)).populate(encoders, rng=rng, flatten=True)

    rotated_encoders = encoders.copy()

    #For each randomly generated encoder, create the same encoder at every angle (increments chosen by degree)
    for encoder in encoders:
        rotated_encoders = np.append(rotated_encoders, [encoder],axis =0)
        for i in range(1,59):
            #new_gabor = rotate(encoder.reshape(28,28),degrees*i,reshape = False).ravel()
            rotated_encoders = np.append(rotated_encoders, [rotate(encoder.reshape(28,28),degrees*i,reshape = False).ravel()],axis =0)
            #rotated_encoders = np.append(rotated_encoders, [encoder],axis =0)
'''
    rotated_encoders = cPickle.load(open("encoders.p", "r"))

    #Num of neurons does not divide evenly with 6 degree increments, so add random encoders
    extra_encoders = Gabor().generate(n_hid - len(rotated_encoders), (11, 11), rng=rng)
    extra_encoders = Mask((28, 28)).populate(extra_encoders, rng=rng, flatten=True)
    all_encoders =...


I am working on the file "Project/trained_mental_rotation_ens_trained_compare.ipynb".
The first task is:
Functions to perform the inhibition of each ensemble
Can you write Python code for it?

#Create stimulus at angle
weight = np.dot(label_weights,activity_to_img_weights)

img = np.dot(THREE,weight)

plt.subplot(121)
pylab.imshow(img.reshape(28,28),cmap="gray")

#img =X_train[7]
rot_img =scipy.ndimage.interpolation.rotate(img.reshape(28,28),-60,reshape=False,cval=-1).ravel()

pylab.imshow(rot_img.reshape(28,28),cmap='gray')
plt.show()

'''For direct mode and nodes
def ssim_func(x):

    img1 = np.dot(x[:1000],activity_to_img_weights)
    img2 = np.dot(x[1000:],activity_to_img_weights)
    return ssim(img1.reshape(28,28),img2.reshape(28,28))

def activity_sim_func(x):
    u=x[:1000]
    v=x[1000:]

    #a= nengo.spa.similarity(u,v,normalize=True)
    a = np.dot(u,v)

    return a
'''


I am working on the file "Project/trained_mental_rotation_ens_trained_compare.ipynb".
The first task is:
Just for fun
Can you write Python code for it?

letterO = np.dot(ZERO,np.dot(label_weights,activity_to_img_weights))
plt.subplot(161)
pylab.imshow(np.reshape(letterO,(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))

letterL = np.dot(SEVEN,label_weights)
for _ in range(30):
    letterL = np.dot(letterL,rotation_weights)
letterL = np.dot(letterL,activity_to_img_weights)
plt.subplot(162)
pylab.imshow(np.reshape(letterL,(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))

letterI = np.dot(ONE,np.dot(label_weights,activity_to_img_weights))
plt.subplot(163)
pylab.imshow(np.reshape(letterI,(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(165)
pylab.imshow(np.reshape(letterI,(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))

letterV = np.dot(SEVEN,label_weights)
for _ in range(40):
    letterV = np.dot(letterV,rotation_weights)
letterV = np.dot(letterV,activity_to_img_weights)
plt.subplot(164)
pylab.imshow(np.reshape(letterV,(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))

letterA = np.dot(SEVEN,label_weights)
for _ in range(10):
    letterA = np.dot(letterA,rotation_weights)
letterA = np.dot(letterA,activity_to_img_weights)
plt.subplot(166)
pylab.imshow(np.reshape(letterA,(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))

plt.show()


I am working on the file "Project/trained_mental_rotation_ens_trained_compare.ipynb".
The first task is:
Pickle the probe's output if it takes a long time to run
Can you write Python code for it?

#The filename includes the number of neurons and which digit is being rotated
filename = "mental_rotation_output_ONE_"  + str(n_hid) + ".p"
cPickle.dump(sim.data[probe], open( filename , "wb" ) )


I am working on the file "Project/trained_mental_rotation_ens_trained_compare.ipynb".
The first task is:
The following is not part of the brain model, it is used to view the output for the ensemble
Since it's probing the neurons themselves, the output must be transformed from neuron activity to visual image
Can you write Python code for it?

%matplotlib inline
#Graph of probe output
plt.plot(sim.trange(), sim.data[ssim_probe], 'k', label="Trained SSIM")
plt.legend()
plt.show()

#Graph of probe output
plt.plot(sim.trange(), sim.data[activity_sim_probe], 'k', label="Trained Similarity Dot Product")
plt.legend()
plt.show()

#Turn probe activity to img
output_acts = []
for act in sim.data[probe]:
    output_acts.append(np.dot(act,activity_to_img_weights))

'''Animation for Probe output'''
fig = plt.figure()

def updatefig(i):
    im = pylab.imshow(np.reshape(output_acts[i],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'),animated=True)

    return im,

ani = animation.FuncAnimation(fig, updatefig, interval=0.1, blit=True)
plt.show()

#ouput_acts = sim.data[probe]

plt.subplot(261)
plt.title("100")
pylab.imshow(np.reshape(output_acts[100],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(262)
plt.title("500")
pylab.imshow(np.reshape(output_acts[500],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(263)
plt.title("1000")
pylab.imshow(np.reshape(output_acts[1000],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(264)
plt.title("1500")
pylab.imshow(np.reshape(output_acts[1500],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(265)
plt.title("2000")
pylab.imshow(np.reshape(output_acts[2000],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(266)
plt.title("2500")
pylab.imshow(np.reshape(output_acts[2500],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(267)
plt.title("3000")
pylab.imshow(np.reshape(output_acts[3000],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(268)
plt.title("3500")
pylab.imshow(np.reshape(output_acts[3500],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(269)
plt.title("4000")
pylab.imshow(np.reshape(output_acts[4000],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.subplot(2,6,10)
plt.title("4500")
pylab.imshow(np.reshape(output_acts[4500],(dim, dim), 'F').T,...


I am working on the file "Project/trained_mental_rotation_testing.ipynb".
The first task is:
Load the weight matrices from the training
Can you write Python code for it?

#Weight matrices generated by the neural network after training

#Maps the label vectors to the neuron activity of the ensemble
label_weights = cPickle.load(open("label_weights5000.p", "rb"))
#Maps the activity of the neurons to the visual representation of the image
activity_to_img_weights = cPickle.load(open("activity_to_img_weights5000.p", "rb"))
#Maps the activity of the neurons of an image with the activity of the neurons of an image rotated 6 degrees
#rotation_weights = cPickle.load(open("rotation_weights5000.p", "rb"))

#Create the pointers for the numbers
temp = np.diag([1]*10)

ZERO = temp[0]
ONE =  temp[1]
TWO =  temp[2]
THREE= temp[3]
FOUR = temp[4]
FIVE = temp[5]
SIX =  temp[6]
SEVEN =temp[7]
EIGHT= temp[8]
NINE = temp[9]

labels =[ZERO,ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE]

#Visualize the one hot representation
print(ZERO)
print(ONE)


I am working on the file "Project/trained_mental_rotation_testing.ipynb".
The first task is:
Visualize the digit from one hot representation through the activity weight matrix to the image representation
- Image is average digit from mnist dataset
Can you write Python code for it?

def intense(img):
    newImg = img.copy()
    #for i in range(len(newImg)):
    #    newImg[i] = np.log (newImg[i] + 1.25)

    newImg[newImg < 0] = -1
    newImg[newImg > 0] = 1
    return newImg

#Change this to imagine different digits
imagine = FIVE
#Can also imagine combitnations of numbers (ZERO + ONE)

#Label to activity
test_activity = np.dot(imagine,label_weights)
#Image decoded
test_output_img = np.dot(test_activity, activity_to_img_weights)

#noise = np.random.random([28,28])
#test_output_img = noise+np.reshape(test_output_img,(28,28))

#clean = intense(test_output_img)

#clean = scipy.ndimage.median_filter(test_output_img, 3)
#clean = intense(clean)

clean = scipy.ndimage.gaussian_filter(test_output_img, sigma=1)
#clean = intense(clean)

#clean = scipy.ndimage.binary_opening(test_output_img)

#Edge detection?
#clean = scipy.ndimage.sobel(test_output_img, axis=0, mode='constant')

#Sharpening
#filter_blurred_f = scipy.ndimage.gaussian_filter(test_output_img, 1)
#alpha = 30
#clean = test_output_img + alpha * (test_output_img - filter_blurred_f)

plt.subplot(131)
plt.imshow(test_output_img.reshape(28,28),cmap='gray')
plt.subplot(132)
plt.imshow(clean.reshape(28,28),cmap='gray')

clean = intense(clean)
plt.subplot(133)
plt.imshow(clean.reshape(28,28),cmap='gray')
plt.show()

for i in range(7):
    imagine = labels[i]
    #Label to activity
    test_activity = np.dot(imagine,label_weights)
    #Image decoded
    test_output_img = np.dot(test_activity, activity_to_img_weights)

    noise = np.random.random([28,28])
    test_output_img = noise+np.reshape(test_output_img,(28,28))

    plt.subplot(131)
    plt.imshow(test_output_img.reshape(28,28),cmap='gray')

    clean = scipy.ndimage.gaussian_filter(test_output_img, sigma=1)

    plt.subplot(132)
    plt.imshow(clean.reshape(28,28),cmap='gray')

    clean = intense(clean)
    plt.subplot(133)
    plt.imshow(clean.reshape(28,28),cmap='gray')
    plt.show()


I am working on the file "Project/trained_mental_rotation_testing.ipynb".
The first task is:
Visualize the rotation of the image using the weight matrix from activity to activity
- does not use the weight matrix used on the recurrent connection
Can you write Python code for it?

#Change this to visualize different digits
imagine = FIVE

#How long the animation should go for
frames=60

#Make a list of the activation of rotated images and add first frame
rot_seq = []
rot_seq.append(np.dot(imagine,label_weights)) #Map the label vector to the activity vector
test_output_img = np.dot(rot_seq[0], activity_to_img_weights) #Map the activity to the visual representation

#add the rest of the frames, using the previous frame to calculate the current frame
for i in range(1,frames):
    rot_seq.append(np.dot(rot_seq[i-1],rotation_weights)) #add the activity of the current image to the list
    test_output_img = np.dot(rot_seq[i], activity_to_img_weights) #map the new activity to the visual image

#Animation of rotation
fig = plt.figure()

def updatefig(i):
    image_vector = np.dot(rot_seq[i], activity_to_img_weights) #map the activity to the image representation
    im = pylab.imshow(np.reshape(image_vector,(28,28), 'F').T, cmap=plt.get_cmap('Greys_r'),animated=True)

    return im,

ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True)
plt.show()

imagine = FIVE
test_output_img = np.dot(imagine,label_weights) #Map the label vector to the activity vector
test_output_img = np.dot(test_output_img,rotation_weights)
test_output_img = np.dot(test_output_img,linalg.inv(rotation_weights))
test_output_img = np.dot(test_output_img, activity_to_img_weights) #Map the activity to the visual representation

pylab.imshow(np.reshape(test_output_img,(28,28), 'F').T, cmap=plt.get_cmap('Greys_r'))
plt.show()


I am working on the file "Project/trained_mental_scaling_ens.ipynb".
The first task is:
Load the saved weight matrices that were created by trainging the model
Can you write Python code for it?

label_weights = cPickle.load(open("label_weights1000.p", "rb"))
activity_to_img_weights = cPickle.load(open("activity_to_img_weights_scale1000.p", "rb"))
scale_up_after_encoder_weights =  cPickle.load(open("scale_up_after_encoder_weights1000.p", "r"))
scale_down_after_encoder_weights =  cPickle.load(open("scale_down_after_encoder_weights1000.p", "r"))

scale_up_weights = cPickle.load(open("scale_up_weights1000.p","rb"))
scale_down_weights = cPickle.load(open("scale_down_weights1000.p","rb"))


I am working on the file "Project/trained_mental_scaling_ens.ipynb".
The first task is:
Pickle the probe's output if it takes a long time to run
Can you write Python code for it?

#The filename includes the number of neurons and which digit is being rotated
filename = "mental_scaling_output_ZERO_"  + str(n_hid) + ".p"
cPickle.dump(sim.data[probe], open( filename , "wb" ) )


I am working on the file "Project/trained_mental_scaling_ens.ipynb".
The first task is:
Testing
Can you write Python code for it?

testing = np.dot(ZERO,np.dot(label_weights,activity_to_img_weights))
plt.subplot(121)
pylab.imshow(np.reshape(testing,(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))

#Get image
testing = np.dot(ZERO,np.dot(label_weights,activity_to_img_weights))

#Get activity of image
_, testing_act = nengo.utils.ensemble.tuning_curves(ens, sim, inputs=testing)

#Get rotated encoder outputs
testing_scale = np.dot(testing_act,scale_down_after_encoder_weights)

#Get activities
testing_scale = ens.neuron_type.rates(testing_scale, sim.data[ens].gain, sim.data[ens].bias)

for i in range(2):
    testing_scale = np.dot(testing_scale,scale_down_after_encoder_weights)
    testing_scale = ens.neuron_type.rates(testing_scale, sim.data[ens].gain, sim.data[ens].bias)

#testing_rotate = np.dot(testing_rotate,rotation_weights)

testing_scale = np.dot(testing_scale,activity_to_img_weights)

plt.subplot(122)
pylab.imshow(np.reshape(testing_scale,(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'))

plt.show()


I am working on the file "Project/trained_mental_scaling_ens.ipynb".
The first task is:
The following is not part of the brain model, it is used to view the output for the ensemble
Since it's probing the neurons themselves, the output must be transformed from neuron activity to visual image
Can you write Python code for it?

'''Animation for Probe output'''
fig = plt.figure()

output_acts = []
for act in sim.data[probe]:
    output_acts.append(np.dot(act,activity_to_img_weights))

def updatefig(i):
    im = pylab.imshow(np.reshape(output_acts[i],(dim, dim), 'F').T, cmap=plt.get_cmap('Greys_r'),animated=True)

    return im,

ani = animation.FuncAnimation(fig, updatefig, interval=0.1, blit=True)
plt.show()


I am working on the file "Project/trained_mental_scaling_ens.ipynb".
The first task is:
The network where the mental imagery and scaling occurs

The state, seed and ensemble parameters (including encoders) must all be the same for the saved weight matrices to work
The number of neurons (n_hid) must be the same as was used for training
The input must be shown for a short period of time to be able to view the scaling
The recurrent connection must be from the neurons because the weight matices were trained on the neuron activities
Can you write Python code for it?

rng = np.random.RandomState(9)
n_hid = 1000
model = nengo.Network(seed=3)
with model:
    #Stimulus only shows for brief period of time
    stim = nengo.Node(lambda t: ZERO if t < 0.1 else 0) #nengo.processes.PresentInput(labels,1))#

    ens_params = dict(
        eval_points=X_train,
        neuron_type=nengo.LIF(), #Why not use LIF?
        intercepts=nengo.dists.Choice([-0.5]),
        max_rates=nengo.dists.Choice([100]),
        )

    # linear filter used for edge detection as encoders, more plausible for human visual system
    encoders = Gabor().generate(n_hid, (11, 11), rng=rng)
    encoders = Mask((28, 28)).populate(encoders, rng=rng, flatten=True)

    ens = nengo.Ensemble(n_hid, dim**2, seed=3, encoders=encoders, **ens_params)

    #Recurrent connection on the neurons of the ensemble to perform the rotation
    nengo.Connection(ens.neurons, ens.neurons, transform = scale_down_after_encoder_weights.T, synapse=0.1)

    #Connect stimulus to ensemble, transform using learned weight matrices
    nengo.Connection(stim, ens, transform = np.dot(label_weights,activity_to_img_weights).T, synapse=0.1)

    #Collect output, use synapse for smoothing
    probe = nengo.Probe(ens.neurons,synapse=0.1)

sim = nengo.Simulator(model)

sim.run(5)


I am working on the file "Project/trained_mental_scaling_testing.ipynb".
The first task is:
Visualize the rotation of the image using the weight matrix from activity to activity
- does not use the weight matrix used on the recurrent connection
Can you write Python code for it?

#Change this to visualize different digits
imagine = ZERO

#How long the animation should go for
frames=5

#Make a list of the activation of rotated images and add first frame
rot_seq = []
rot_seq.append(np.dot(imagine,label_weights)) #Map the label vector to the activity vector
test_output_img = np.dot(rot_seq[0], activity_to_img_weights) #Map the activity to the visual representation

#add the rest of the frames, using the previous frame to calculate the current frame
for i in range(1,frames):
    rot_seq.append(np.dot(rot_seq[i-1],scale_down_weights)) #add the activity of the current image to the list
    test_output_img = np.dot(rot_seq[i], activity_to_img_weights) #map the new activity to the visual image
for i in range(1,frames*2):
    rot_seq.append(np.dot(rot_seq[frames+i-2],scale_up_weights)) #add the activity of the current image to the list
    test_output_img = np.dot(rot_seq[i], activity_to_img_weights) #map the new activity to the visual image

#Animation of rotation
fig = plt.figure()

def updatefig(i):
    image_vector = np.dot(rot_seq[i], activity_to_img_weights) #map the activity to the image representation
    im = pylab.imshow(np.reshape(image_vector,(28,28), 'F').T, cmap=plt.get_cmap('Greys_r'),animated=True)

    return im,

ani = animation.FuncAnimation(fig, updatefig, interval=100, blit=True)
plt.show()


I am working on the file "Project/trained_mental_translation_testing.ipynb".
The first task is:
Load the weight matrices from the training
Can you write Python code for it?

#Weight matrices generated by the neural network after training

#Maps the label vectors to the neuron activity of the ensemble
label_weights = cPickle.load(open("label_weights1000.p", "rb"))
#Maps the activity of the neurons to the visual representation of the image
activity_to_img_weights = cPickle.load(open("activity_to_img_weights_translate1000.p", "rb"))
#Maps the activity of the neurons of an image with the activity of the neurons of an image scaled
translate_up_weights = cPickle.load(open("translate_up_weights1000.p", "rb"))
translate_down_weights = cPickle.load(open("translate_down_weights1000.p", "rb"))
translate_left_weights = cPickle.load(open("translate_left_weights1000.p", "rb"))
translate_right_weights = cPickle.load(open("translate_right_weights1000.p", "rb"))

#Create the pointers for the numbers
temp = np.diag([1]*10)

ZERO = temp[0]
ONE =  temp[1]
TWO =  temp[2]
THREE= temp[3]
FOUR = temp[4]
FIVE = temp[5]
SIX =  temp[6]
SEVEN =temp[7]
EIGHT= temp[8]
NINE = temp[9]

labels =[ZERO,ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE]

#Visualize the one hot representation
print(ZERO)
print(ONE)


I am working on the file "Project/trained_mental_translation_testing.ipynb".
The first task is:
Visualize the rotation of the image using the weight matrix from activity to activity
- does not use the weight matrix used on the recurrent connection
Can you write Python code for it?

#Change this to visualize different digits
imagine = ONE

#How long the animation should go for
frames=5

#Make a list of the activation of rotated images and add first frame
rot_seq = []
rot_seq.append(np.dot(imagine,label_weights)) #Map the label vector to the activity vector
test_output_img = np.dot(rot_seq[0], activity_to_img_weights) #Map the activity to the visual representation

#add the rest of the frames, using the previous frame to calculate the current frame
for i in range(1,frames):
    rot_seq.append(np.dot(rot_seq[i-1],translate_left_weights)) #add the activity of the current image to the list
    test_output_img = np.dot(rot_seq[i], activity_to_img_weights) #map the new activity to the visual image
for i in range(1,frames*2):
    rot_seq.append(np.dot(rot_seq[frames+i-2],translate_down_weights)) #add the activity of the current image to the list
    test_output_img = np.dot(rot_seq[i], activity_to_img_weights) #map the new activity to the visual image

#Animation of rotation
fig = plt.figure()

def updatefig(i):
    image_vector = np.dot(rot_seq[i], activity_to_img_weights) #map the activity to the image representation
    im = pylab.imshow(np.reshape(image_vector,(28,28), 'F').T, cmap=plt.get_cmap('Greys_r'),animated=True)

    return im,

ani = animation.FuncAnimation(fig, updatefig, interval=100, blit=True)
plt.show()


I am working on the file "Project0-titanic_survival_exploration/titanic_survival_exploration.ipynb".
The first task is:
从泰坦尼克号的数据样本中，我们可以看到船上每位旅客的特征

Survived：是否存活（0代表否，1代表是）
Pclass：社会阶级（1代表上层阶级，2代表中层阶级，3代表底层阶级）
Name：船上乘客的名字
Sex：船上乘客的性别
Age:船上乘客的年龄（可能存在 NaN）
SibSp：乘客在船上的兄弟姐妹和配偶的数量
Parch：乘客在船上的父母以及小孩的数量
Ticket：乘客船票的编号
Fare：乘客为船票支付的费用
Cabin：乘客所在船舱的编号（可能存在 NaN）
Embarked：乘客上船的港口（C 代表从 Cherbourg 登船，Q 代表从 Queenstown 登船，S 代表从 Southampton 登船）

因为我们感兴趣的是每个乘客或船员是否在事故中活了下来。可以将 Survived 这一特征从这个数据集移除，并且用一个单独的变量 outcomes 来存储。它也做为我们要预测的目标。
运行该代码，从数据集中移除 Survived 这个特征，并将它存储在变量 outcomes 中。
Can you write Python code for it?

# Store the 'Survived' feature in a new variable and remove it from the dataset
# 从数据集中移除 'Survived' 这个特征，并将它存储在一个新的变量中。
outcomes = full_data['Survived']
data = full_data.drop('Survived', axis = 1)

# Show the new dataset with 'Survived' removed
# 显示已移除 'Survived' 特征的数据集
display(data.head())


I am working on the file "Project0-titanic_survival_exploration/titanic_survival_exploration.ipynb".
The first task is:
回答: Predictions have an accuracy of 61.62%

我们可以使用 survival_stats 函数来看看 Sex 这一特征对乘客的存活率有多大影响。这个函数定义在名为 titanic_visualizations.py 的 Python 脚本文件中，我们的项目提供了这个文件。传递给函数的前两个参数分别是泰坦尼克号的乘客数据和乘客的 生还结果。第三个参数表明我们会依据哪个特征来绘制图形。
运行下面的代码绘制出依据乘客性别计算存活率的柱形图。
Can you write Python code for it?


I am working on the file "Project0-titanic_survival_exploration/titanic_survival_exploration.ipynb".
The first task is:
回答: Predictions have an accuracy of 78.68%.

仅仅使用乘客性别（Sex）这一特征，我们预测的准确性就有了明显的提高。现在再看一下使用额外的特征能否更进一步提升我们的预测准确度。例如，综合考虑所有在泰坦尼克号上的男性乘客：我们是否找到这些乘客中的一个子集，他们的存活概率较高。让我们再次使用 survival_stats 函数来看看每位男性乘客的年龄（Age）。这一次，我们将使用第四个参数来限定柱形图中只有男性乘客。
运行下面这段代码，把男性基于年龄的生存结果绘制出来。
Can you write Python code for it?

survival_stats(data, outcomes, 'Age', ["Sex == 'male'"])


I am working on the file "Project0-titanic_survival_exploration/titanic_survival_exploration.ipynb".
The first task is:
回答: Predictions have an accuracy of 79.35%.

添加年龄（Age）特征与性别（Sex）的结合比单独使用性别（Sex）也提高了不少准确度。现在该你来做预测了：找到一系列的特征和条件来对数据进行划分，使得预测结果提高到80%以上。这可能需要多个特性和多个层次的条件语句才会成功。你可以在不同的条件下多次使用相同的特征。Pclass，Sex，Age，SibSp 和 Parch 是建议尝试使用的特征。
使用 survival_stats 函数来观测泰坦尼克号上乘客存活的数据统计。
提示: 要使用多个过滤条件，把每一个条件放在一个列表里作为最后一个参数传递进去。例如: ["Sex == 'male'", "Age < 18"]
Can you write Python code for it?


I am working on the file "Project0-titanic_survival_exploration/titanic_survival_exploration.ipynb".
The first task is:
提示：如果你保存 iPython Notebook，代码运行的输出也将被保存。但是，一旦你重新打开项目，你的工作区将会被重置。请确保每次都从上次离开的地方运行代码来重新生成变量和函数。

预测
如果我们要预测泰坦尼克号上的乘客是否存活，但是我们又对他们一无所知，那么最好的预测就是船上的人无一幸免。这是因为，我们可以假定当船沉没的时候大多数乘客都遇难了。下面的 predictions_0 函数就预测船上的乘客全部遇难。
Can you write Python code for it?

def predictions_0(data):
    """ Model with no features. Always predicts a passenger did not survive. """

    predictions = []
    for _, passenger in data.iterrows():

        # Predict the survival of 'passenger'
        # 预测 'passenger' 的生还率
        predictions.append(0)

    # Return our predictions
    # 返回预测结果
    return pd.Series(predictions)

# Make the predictions
# 进行预测
predictions = predictions_0(data)


I am working on the file "Project0-titanic_survival_exploration/titanic_survival_exploration.ipynb".
The first task is:
观察泰坦尼克号上乘客存活的数据统计，我们可以发现大部分男性乘客在船沉没的时候都遇难了。相反的，大部分女性乘客都在事故中生还。让我们在先前推断的基础上继续创建：如果乘客是男性，那么我们就预测他们遇难；如果乘客是女性，那么我们预测他们在事故中活了下来。
将下面的代码补充完整，让函数可以进行正确预测。
提示：您可以用访问 dictionary（字典）的方法来访问船上乘客的每个特征对应的值。例如， passenger['Sex'] 返回乘客的性别。
Can you write Python code for it?

def predictions_1(data):
    """ Model with one feature:
            - Predict a passenger survived if they are female. """

    predictions = []
    for _, passenger in data.iterrows():

        # Remove the 'pass' statement below
        # 移除下方的 'pass' 声明
        # and write your prediction conditions here
        # 输入你自己的预测条件
        if passenger['Sex'] == 'female':
            predictions.append(1)
        else:
            predictions.append(0)

    # Return our predictions
    # 返回预测结果
    return pd.Series(predictions)

# Make the predictions
# 进行预测
predictions = predictions_1(data)


I am working on the file "Project0-titanic_survival_exploration/titanic_survival_exploration.ipynb".
The first task is:
问题2
当我们预测船上女性乘客全部存活，而剩下的人全部遇难，那么我们预测的准确率会达到多少？
提示：运行下面的代码来查看我们预测的准确率。
Can you write Python code for it?


I am working on the file "Project0-titanic_survival_exploration/titanic_survival_exploration.ipynb".
The first task is:
问题3
当预测所有女性以及小于10岁的男性都存活的时候，预测的准确率会达到多少？
提示：运行下面的代码来查看预测的准确率。
Can you write Python code for it?


I am working on the file "Project0/.ipynb_checkpoints/Bay_Area_Bike_Share_Analysis-checkpoint.ipynb".
The first task is:
Explore some different variables using the functions above and take note of some trends you find. Feel free to create additional cells if you want to explore the dataset in other ways or multiple ways.

Tip: In order to add additional cells to a notebook, you can use the "Insert Cell Above" and "Insert Cell Below" options from the menu bar above. There is also an icon in the toolbar for adding new cells, with additional icons for moving the cells up and down the document. By default, new cells are of the code type; you can also specify the cell type (e.g. Code or Markdown) of selected cells from the Cell menu or the dropdown in the toolbar.

One you're done with your explorations, copy the two visualizations you found most interesting into the cells below, then answer the following questions with a few sentences describing what you found and why you selected the figures. Make sure that you adjust the number of bins or the bin limits so that they effectively convey data findings. Feel free to supplement this with any additional numbers generated from usage_stats() or place multiple visualizations to support your observations.
Can you write Python code for it?

# Final Plot 1
usage_plot(trip_data, 'start_hour', boundary = 0, bin_width = 1)


I am working on the file "Project0/.ipynb_checkpoints/Bay_Area_Bike_Share_Analysis-checkpoint.ipynb".
The first task is:
In this exploration, we're going to concentrate on factors in the trip data that affect the number of trips that are taken. Let's focus down on a few selected columns: the trip duration, start time, start terminal, end terminal, and subscription type. Start time will be divided into year, month, and hour components. We will also add a column for the day of the week and abstract the start and end terminal to be the start and end city.
Let's tackle the lattermost part of the wrangling process first. Run the below code cell to see how the station information is structured, then observe how the code will create the station-city mapping. Note that the station mapping is set up as a function, create_station_mapping(). Since it is possible that more stations are added or dropped over time, this function will allow us to combine the station information across all three parts of our data when we are ready to explore everything.
Can you write Python code for it?

# Display the first few rows of the station data file.
station_info = pd.read_csv('201402_station_data.csv')
display(station_info.head())

# This function will be called by another function later on to create the mapping.
def create_station_mapping(station_data):
    """
    Create a mapping from station IDs to cities, returning the
    result as a dictionary.
    """
    station_map = {}
    for data_file in station_data:
        with open(data_file, 'r') as f_in:
            # set up csv reader object - note that we are using DictReader, which
            # takes the first row of the file as a header row for each row's
            # dictionary keys
            weather_reader = csv.DictReader(f_in)

            for row in weather_reader:
                station_map[row['station_id']] = row['landmark']
    return station_map


I am working on the file "Project0/.ipynb_checkpoints/Bay_Area_Bike_Share_Analysis-checkpoint.ipynb".
The first task is:
Looks pretty strange, doesn't it? Take a look at the duration values on the x-axis. Most rides are expected to be 30 minutes or less, since there are overage charges for taking extra time in a single trip. The first bar spans durations up to about 1000 minutes, or over 16 hours. Based on the statistics we got out of usage_stats(), we should have expected some trips with very long durations that bring the average to be so much higher than the median: the plot shows this in a dramatic, but unhelpful way.
When exploring the data, you will often need to work with visualization function parameters in order to make the data easier to understand. Here's where the third argument of the usage_plot() function comes in. Filters can be set for data points as a list of conditions. Let's start by limiting things to trips of less than 60 minutes.
Can you write Python code for it?

usage_plot(trip_data, 'duration', ['duration < 60'])


I am working on the file "Project0/.ipynb_checkpoints/Bay_Area_Bike_Share_Analysis-checkpoint.ipynb".
The first task is:
Now it's your turn to explore the new dataset with usage_stats() and usage_plot() and report your findings! Here's a refresher on how to use the usage_plot() function:

first argument (required): loaded dataframe from which data will be analyzed.
second argument (required): variable on which trip counts will be divided.
third argument (optional): data filters limiting the data points that will be counted. Filters should be given as a list of conditions, each element should be a string in the following format: '<field> <op> <value>' using one of the following operations: >, <, >=, <=, ==, !=. Data points must satisfy all conditions to be counted or visualized. For example, ["duration < 15", "start_city == 'San Francisco'"] retains only trips that originated in San Francisco and are less than 15 minutes long.

If data is being split on a numeric variable (thus creating a histogram), some additional parameters may be set by keyword.
- "n_bins" specifies the number of bars in the resultant plot (default is 10).
- "bin_width" specifies the width of each bar (default divides the range of the data by number of bins). "n_bins" and "bin_width" cannot be used simultaneously.
- "boundary" specifies where one of the bar edges will be placed; other bar edges will be placed around that value (this may result in an additional bar being plotted). This argument may be used alongside the "n_bins" and "bin_width" arguments.
You can also add some customization to the usage_stats() function as well. The second argument of the function can be used to set up filter conditions, just like how they are set up in usage_plot().
Can you write Python code for it?

usage_stats(trip_data)

usage_plot(trip_data, 'start_city', boundary = 0, bin_width = 1)

usage_plot(trip_data, 'start_hour', boundary = 0, bin_width = 1)

usage_plot(trip_data, 'start_month', boundary = 0, bin_width = 1)

usage_plot(trip_data, 'weekday', boundary = 0, bin_width = 1)


I am working on the file "Project0/.ipynb_checkpoints/Bay_Area_Bike_Share_Analysis-checkpoint.ipynb".
The first task is:
Question 4: Which five-minute trip duration shows the most number of trips? Approximately how many trips were made in this range?
Answer: Approximately 9,000 trips were made in the 5-10 minutes range.
Visual adjustments like this might be small, but they can go a long way in helping you understand the data and convey your findings to others.
Performing Your Own Analysis
Now that you've done some exploration on a small sample of the dataset, it's time to go ahead and put together all of the data in a single file and see what trends you can find. The code below will use the same summarise_data() function as before to process data. After running the cell below, you'll have processed all the data into a single data file. Note that the function will not display any output while it runs, and this can take a while to complete since you have much more data than the sample you worked with above.
Can you write Python code for it?

station_data = ['201402_station_data.csv',
                '201408_station_data.csv',
                '201508_station_data.csv' ]
trip_in = ['201402_trip_data.csv',
           '201408_trip_data.csv',
           '201508_trip_data.csv' ]
trip_out = 'babs_y1_y2_summary.csv'

# This function will take in the station data and trip data and
# write out a new data file to the name listed above in trip_out.
summarise_data(trip_in, station_data, trip_out)


I am working on the file "Project0/.ipynb_checkpoints/Bay_Area_Bike_Share_Analysis-checkpoint.ipynb".
The first task is:
Question 5a: What is interesting about the above visualization? Why did you select it?
Answer: The plot shows the usage of the service during the day. We can see that busiest hours are 7-10AM and 4-7PM, which are the time people commute to and from work/school. There are also a fair share of trips happen during the day, but there is no significant increase during lunch time (I guess people don't bike for lunch).
Can you write Python code for it?

# Final Plot 2
usage_plot(trip_data, 'duration', ['duration < 60'], boundary = 0, bin_width = 5)


I am working on the file "Project0/.ipynb_checkpoints/Bay_Area_Bike_Share_Analysis-checkpoint.ipynb".
The first task is:
Seems like there's about 50% more trips made by subscribers in the first month than customers. Let's try a different variable now. What does the distribution of trip durations look like?
Can you write Python code for it?


I am working on the file "Project0/.ipynb_checkpoints/Bay_Area_Bike_Share_Analysis-checkpoint.ipynb".
The first task is:
Since the summarise_data() function has created a standalone file, the above cell will not need to be run a second time, even if you close the notebook and start a new session. You can just load in the dataset and then explore things from there.
Can you write Python code for it?

trip_data = pd.read_csv('babs_y1_y2_summary.csv')
display(trip_data.head())


I am working on the file "Project0/.ipynb_checkpoints/Bay_Area_Bike_Share_Analysis-checkpoint.ipynb".
The first task is:
Tip: If you save a jupyter Notebook, the output from running code blocks will also be saved. However, the state of your workspace will be reset once a new session is started. Make sure that you run all of the necessary code blocks from your previous session to reestablish variables and functions before picking up where you last left off.

Exploratory Data Analysis
Now that you have some data saved to a file, let's look at some initial trends in the data. Some code has already been written for you in the babs_visualizations.py script to help summarize and visualize the data; this has been imported as the functions usage_stats() and usage_plot(). In this section we'll walk through some of the things you can do with the functions, and you'll use the functions for yourself in the last part of the project. First, run the following cell to load the data, then use the usage_stats() function to see the total number of trips made in the first month of operations, along with some statistics regarding how long trips took.
Can you write Python code for it?

trip_data = pd.read_csv('201309_trip_summary.csv')

usage_stats(trip_data)


I am working on the file "Project0/.ipynb_checkpoints/Bay_Area_Bike_Share_Analysis-checkpoint.ipynb".
The first task is:
You can now use the mapping to condense the trip data to the selected columns noted above. This will be performed in the summarise_data() function below. As part of this function, the datetime module is used to parse the timestamp strings from the original data file as datetime objects (strptime), which can then be output in a different string format (strftime). The parsed objects also have a variety of attributes and methods to quickly obtain
There are two tasks that you will need to complete to finish the summarise_data() function. First, you should perform an operation to convert the trip durations from being in terms of seconds to being in terms of minutes. (There are 60 seconds in a minute.) Secondly, you will need to create the columns for the year, month, hour, and day of the week. Take a look at the documentation for datetime objects in the datetime module. Find the appropriate attributes and method to complete the below code.
Can you write Python code for it?

def summarise_data(trip_in, station_data, trip_out):
    """
    This function takes trip and station information and outputs a new
    data file with a condensed summary of major trip information. The
    trip_in and station_data arguments will be lists of data files for
    the trip and station information, respectively, while trip_out
    specifies the location to which the summarized data will be written.
    """
    # generate dictionary of station - city mapping
    station_map = create_station_mapping(station_data)

    with open(trip_out, 'w') as f_out:
        # set up csv writer object
        out_colnames = ['duration', 'start_date', 'start_year',
                        'start_month', 'start_hour', 'weekday',
                        'start_city', 'end_city', 'subscription_type']
        trip_writer = csv.DictWriter(f_out, fieldnames = out_colnames)
        trip_writer.writeheader()

        for data_file in trip_in:
            with open(data_file, 'r') as f_in:
                # set up csv reader object
                trip_reader = csv.DictReader(f_in)

                # collect data from and process each row
                for row in trip_reader:
                    new_point = {}

                    # convert duration units from seconds to minutes
                    ### Question 3a: Add a mathematical operation below   ###
                    ### to convert durations from seconds to minutes.     ###
                    new_point['duration'] = float(row['Duration'])/60
                    # reformat datestrings into multiple columns
                    ### Question 3b: Fill in the blanks below to generate ###
                    ### the expected time values.                         ###
                    trip_date = datetime.strptime(row['Start Date'], '%m/%d/%Y %H:%M')
                    new_point['start_date']  = trip_date.strftime('%Y-%m-%d')
                    new_point['start_year']  = trip_date.year
                   ...


I am working on the file "Project0/.ipynb_checkpoints/Bay_Area_Bike_Share_Analysis-checkpoint.ipynb".
The first task is:
You should see that there are over 27,000 trips in the first month, and that the average trip duration is larger than the median trip duration (the point where 50% of trips are shorter, and 50% are longer). In fact, the mean is larger than the 75% shortest durations. This will be interesting to look at later on.
Let's start looking at how those trips are divided by subscription type. One easy way to build an intuition about the data is to plot it. We'll use the usage_plot() function for this. The second argument of the function allows us to count up the trips across a selected variable, displaying the information in a plot. The expression below will show how many customer and how many subscriber trips were made. Try it out!
Can you write Python code for it?


I am working on the file "Project0/Bay_Area_Bike_Share_Analysis.ipynb".
The first task is:
Condensing the Trip Data
The first step is to look at the structure of the dataset to see if there's any data wrangling we should perform. The below cell will read in the sampled data file that you created in the previous cell, and print out the first few rows of the table.
Can you write Python code for it?

sample_data = pd.read_csv('201309_trip_data.csv')

display(sample_data.head())


I am working on the file "Project0/Bay_Area_Bike_Share_Analysis.ipynb".
The first task is:
In this exploration, we're going to concentrate on factors in the trip data that affect the number of trips that are taken. Let's focus down on a few selected columns: the trip duration, start time, start terminal, end terminal, and subscription type. Start time will be divided into year, month, and hour components. We will also add a column for the day of the week and abstract the start and end terminal to be the start and end city.
Let's tackle the lattermost part of the wrangling process first. Run the below code cell to see how the station information is structured, then observe how the code will create the station-city mapping. Note that the station mapping is set up as a function, create_station_mapping(). Since it is possible that more stations are added or dropped over time, this function will allow us to combine the station information across all three parts of our data when we are ready to explore everything.
Can you write Python code for it?


I am working on the file "Project0/Bay_Area_Bike_Share_Analysis.ipynb".
The first task is:
Looks pretty strange, doesn't it? Take a look at the duration values on the x-axis. Most rides are expected to be 30 minutes or less, since there are overage charges for taking extra time in a single trip. The first bar spans durations up to about 1000 minutes, or over 16 hours. Based on the statistics we got out of usage_stats(), we should have expected some trips with very long durations that bring the average to be so much higher than the median: the plot shows this in a dramatic, but unhelpful way.
When exploring the data, you will often need to work with visualization function parameters in order to make the data easier to understand. Here's where the third argument of the usage_plot() function comes in. Filters can be set for data points as a list of conditions. Let's start by limiting things to trips of less than 60 minutes.
Can you write Python code for it?


I am working on the file "Project0/Bay_Area_Bike_Share_Analysis.ipynb".
The first task is:
Now it's your turn to explore the new dataset with usage_stats() and usage_plot() and report your findings! Here's a refresher on how to use the usage_plot() function:

first argument (required): loaded dataframe from which data will be analyzed.
second argument (required): variable on which trip counts will be divided.
third argument (optional): data filters limiting the data points that will be counted. Filters should be given as a list of conditions, each element should be a string in the following format: '<field> <op> <value>' using one of the following operations: >, <, >=, <=, ==, !=. Data points must satisfy all conditions to be counted or visualized. For example, ["duration < 15", "start_city == 'San Francisco'"] retains only trips that originated in San Francisco and are less than 15 minutes long.

If data is being split on a numeric variable (thus creating a histogram), some additional parameters may be set by keyword.
- "n_bins" specifies the number of bars in the resultant plot (default is 10).
- "bin_width" specifies the width of each bar (default divides the range of the data by number of bins). "n_bins" and "bin_width" cannot be used simultaneously.
- "boundary" specifies where one of the bar edges will be placed; other bar edges will be placed around that value (this may result in an additional bar being plotted). This argument may be used alongside the "n_bins" and "bin_width" arguments.
You can also add some customization to the usage_stats() function as well. The second argument of the function can be used to set up filter conditions, just like how they are set up in usage_plot().
Can you write Python code for it?


I am working on the file "Project0/Bay_Area_Bike_Share_Analysis.ipynb".
The first task is:
Question 3: Run the below code block to call the summarise_data() function you finished in the above cell. It will take the data contained in the files listed in the trip_in and station_data variables, and write a new file at the location specified in the trip_out variable. If you've performed the data wrangling correctly, the below code block will print out the first few lines of the dataframe and a message verifying that the data point counts are correct.
Can you write Python code for it?

# Process the data by running the function we wrote above.
station_data = ['201402_station_data.csv']
trip_in = ['201309_trip_data.csv']
trip_out = '201309_trip_summary.csv'
summarise_data(trip_in, station_data, trip_out)

# Load in the data file and print out the first few rows
sample_data = pd.read_csv(trip_out)
display(sample_data.head())

# Verify the dataframe by counting data points matching each of the time features.
question_3(sample_data)


I am working on the file "Project0/Bay_Area_Bike_Share_Analysis.ipynb".
The first task is:
Question 4: Which five-minute trip duration shows the most number of trips? Approximately how many trips were made in this range?
Answer: Approximately 9,000 trips were made in the 5-10 minutes range.
Visual adjustments like this might be small, but they can go a long way in helping you understand the data and convey your findings to others.
Performing Your Own Analysis
Now that you've done some exploration on a small sample of the dataset, it's time to go ahead and put together all of the data in a single file and see what trends you can find. The code below will use the same summarise_data() function as before to process data. After running the cell below, you'll have processed all the data into a single data file. Note that the function will not display any output while it runs, and this can take a while to complete since you have much more data than the sample you worked with above.
Can you write Python code for it?


I am working on the file "Project0/Bay_Area_Bike_Share_Analysis.ipynb".
The first task is:
Seems like there's about 50% more trips made by subscribers in the first month than customers. Let's try a different variable now. What does the distribution of trip durations look like?
Can you write Python code for it?


I am working on the file "Project0/Bay_Area_Bike_Share_Analysis.ipynb".
The first task is:
Tip: If you save a jupyter Notebook, the output from running code blocks will also be saved. However, the state of your workspace will be reset once a new session is started. Make sure that you run all of the necessary code blocks from your previous session to reestablish variables and functions before picking up where you last left off.

Exploratory Data Analysis
Now that you have some data saved to a file, let's look at some initial trends in the data. Some code has already been written for you in the babs_visualizations.py script to help summarize and visualize the data; this has been imported as the functions usage_stats() and usage_plot(). In this section we'll walk through some of the things you can do with the functions, and you'll use the functions for yourself in the last part of the project. First, run the following cell to load the data, then use the usage_stats() function to see the total number of trips made in the first month of operations, along with some statistics regarding how long trips took.
Can you write Python code for it?


I am working on the file "Project0/Bay_Area_Bike_Share_Analysis.ipynb".
The first task is:
You can now use the mapping to condense the trip data to the selected columns noted above. This will be performed in the summarise_data() function below. As part of this function, the datetime module is used to parse the timestamp strings from the original data file as datetime objects (strptime), which can then be output in a different string format (strftime). The parsed objects also have a variety of attributes and methods to quickly obtain
There are two tasks that you will need to complete to finish the summarise_data() function. First, you should perform an operation to convert the trip durations from being in terms of seconds to being in terms of minutes. (There are 60 seconds in a minute.) Secondly, you will need to create the columns for the year, month, hour, and day of the week. Take a look at the documentation for datetime objects in the datetime module. Find the appropriate attributes and method to complete the below code.
Can you write Python code for it?


I am working on the file "Project0/Bay_Area_Bike_Share_Analysis.ipynb".
The first task is:
You should see that there are over 27,000 trips in the first month, and that the average trip duration is larger than the median trip duration (the point where 50% of trips are shorter, and 50% are longer). In fact, the mean is larger than the 75% shortest durations. This will be interesting to look at later on.
Let's start looking at how those trips are divided by subscription type. One easy way to build an intuition about the data is to plot it. We'll use the usage_plot() function for this. The second argument of the function allows us to count up the trips across a selected variable, displaying the information in a plot. The expression below will show how many customer and how many subscriber trips were made. Try it out!
Can you write Python code for it?


I am working on the file "Project0/titanic_survival_exploration.ipynb".
The first task is:
从泰坦尼克号的数据样本中，我们可以看到船上每位旅客的特征

Survived：是否存活（0代表否，1代表是）
Pclass：社会阶级（1代表上层阶级，2代表中层阶级，3代表底层阶级）
Name：船上乘客的名字
Sex：船上乘客的性别
Age:船上乘客的年龄（可能存在 NaN）
SibSp：乘客在船上的兄弟姐妹和配偶的数量
Parch：乘客在船上的父母以及小孩的数量
Ticket：乘客船票的编号
Fare：乘客为船票支付的费用
Cabin：乘客所在船舱的编号（可能存在 NaN）
Embarked：乘客上船的港口（C 代表从 Cherbourg 登船，Q 代表从 Queenstown 登船，S 代表从 Southampton 登船）

因为我们感兴趣的是每个乘客或船员是否在事故中活了下来。可以将 Survived 这一特征从这个数据集移除，并且用一个单独的变量 outcomes 来存储。它也做为我们要预测的目标。
运行该代码，从数据集中移除 Survived 这个特征，并将它存储在变量 outcomes 中。
Can you write Python code for it?

# 从数据集中移除 'Survived' 这个特征，并将它存储在一个新的变量中。
outcomes = full_data['Survived']
data = full_data.drop('Survived', axis = 1)

# 显示已移除 'Survived' 特征的数据集
display(data.head())