I am working on the file "Machine learning Nanodegree/creating_customer_segments/customer_segments.ipynb".
The first task is:
Data Exploration
In this section, you will begin exploring the data through visualizations and code to understand how each feature is related to the others. You will observe a statistical description of the dataset, consider the relevance of each feature, and select a few sample data points from the dataset which you will track through the course of this project.
Run the code block below to observe a statistical description of the dataset. Note that the dataset is composed of six important product categories: 'Fresh', 'Milk', 'Grocery', 'Frozen', 'Detergents_Paper', and 'Delicatessen'. Consider what each category represents in terms of products you could purchase.
Can you write Python code for it?

# Display a description of the dataset
display(data.describe())


I am working on the file "Machine learning Nanodegree/creating_customer_segments/customer_segments.ipynb".
The first task is:
Implementation: Data Recovery
Each cluster present in the visualization above has a central point. These centers (or means) are not specifically data points from the data, but rather the averages of all the data points predicted in the respective clusters. For the problem of creating customer segments, a cluster's center point corresponds to the average customer of that segment. Since the data is currently reduced in dimension and scaled by a logarithm, we can recover the representative customer spending from these data points by applying the inverse transformations.
In the code block below, you will need to implement the following:
 - Apply the inverse transform to centers using pca.inverse_transform and assign the new centers to log_centers.
 - Apply the inverse function of np.log to log_centers using np.exp and assign the true centers to true_centers.
Can you write Python code for it?

# TODO: Inverse transform the centers
log_centers = None

# TODO: Exponentiate the centers
true_centers = None

# Display the true centers
segments = ['Segment {}'.format(i) for i in range(0,len(centers))]
true_centers = pd.DataFrame(np.round(true_centers), columns = data.keys())
true_centers.index = segments
display(true_centers)


I am working on the file "Machine learning Nanodegree/creating_customer_segments/customer_segments.ipynb".
The first task is:
Implementation: Dimensionality Reduction
When using principal component analysis, one of the main goals is to reduce the dimensionality of the data — in effect, reducing the complexity of the problem. Dimensionality reduction comes at a cost: Fewer dimensions used implies less of the total variance in the data is being explained. Because of this, the cumulative explained variance ratio is extremely important for knowing how many dimensions are necessary for the problem. Additionally, if a signifiant amount of variance is explained by only two or three dimensions, the reduced data can be visualized afterwards.
In the code block below, you will need to implement the following:
 - Assign the results of fitting PCA in two dimensions with good_data to pca.
 - Apply a PCA transformation of good_data using pca.transform, and assign the reuslts to reduced_data.
 - Apply a PCA transformation of the sample log-data log_samples using pca.transform, and assign the results to pca_samples.
Can you write Python code for it?

# TODO: Apply PCA by fitting the good data with only two dimensions
pca = None

# TODO: Transform the good data using the PCA fit above
reduced_data = None

# TODO: Transform the sample log-data using the PCA fit above
pca_samples = None

# Create a DataFrame for the reduced data
reduced_data = pd.DataFrame(reduced_data, columns = ['Dimension 1', 'Dimension 2'])


I am working on the file "Machine learning Nanodegree/creating_customer_segments/customer_segments.ipynb".
The first task is:
Implementation: Outlier Detection
Detecting outliers in the data is extremely important in the data preprocessing step of any analysis. The presence of outliers can often skew results which take into consideration these data points. There are many "rules of thumb" for what constitutes an outlier in a dataset. Here, we will use Tukey's Method for identfying outliers: An outlier step is calculated as 1.5 times the interquartile range (IQR). A data point with a feature that is beyond an outlier step outside of the IQR for that feature is considered abnormal.
In the code block below, you will need to implement the following:
 - Assign the value of the 25th percentile for the given feature to Q1. Use np.percentile for this.
 - Assign the value of the 75th percentile for the given feature to Q3. Again, use np.percentile.
 - Assign the calculation of an outlier step for the given feature to step.
 - Optionally remove data points from the dataset by adding indices to the outliers list.
NOTE: If you choose to remove any outliers, ensure that the sample data does not contain any of these points!
Once you have performed this implementation, the dataset will be stored in the variable good_data.
Can you write Python code for it?

# For each feature find the data points with extreme high or low values
for feature in log_data.keys():

    # TODO: Calculate Q1 (25th percentile of the data) for the given feature
    Q1 = None

    # TODO: Calculate Q3 (75th percentile of the data) for the given feature
    Q3 = None

    # TODO: Use the interquartile range to calculate an outlier step (1.5 times the interquartile range)
    step = None

    # Display the outliers
    print "Data points considered outliers for the feature '{}':".format(feature)
    display(log_data[~((log_data[feature] >= Q1 - step) & (log_data[feature] <= Q3 + step))])

# OPTIONAL: Select the indices for data points you wish to remove
outliers  = []

# Remove the outliers, if any were specified
good_data = log_data.drop(log_data.index[outliers]).reset_index(drop = True)


I am working on the file "Machine learning Nanodegree/creating_customer_segments/customer_segments.ipynb".
The first task is:
Implementation: Selecting Samples
To get a better understanding of the customers and how their data will transform through the analysis, it would be best to select a few sample data points and explore them in more detail. In the code block below, add three indices of your choice to the indices list which will represent the customers to track. It is suggested to try different sets of samples until you obtain customers that vary significantly from one another.
Can you write Python code for it?

# TODO: Select three indices of your choice you wish to sample from the dataset
indices = []

# Create a DataFrame of the chosen samples
samples = pd.DataFrame(data.loc[indices], columns = data.keys()).reset_index(drop = True)
print "Chosen samples of wholesale customers dataset:"
display(samples)


I am working on the file "Machine learning Nanodegree/creating_customer_segments/customer_segments.ipynb".
The first task is:
Observation
After applying a natural logarithm scaling to the data, the distribution of each feature should appear much more normal. For any pairs of features you may have identified earlier as being correlated, observe here whether that correlation is still present (and whether it is now stronger or weaker than before).
Run the code below to see how the sample data has changed after having the natural logarithm applied to it.
Can you write Python code for it?

# Display the log-transformed sample data
display(log_samples)


I am working on the file "Machine learning Nanodegree/creating_customer_segments/customer_segments.ipynb".
The first task is:
Observation
Run the code below to see how the log-transformed sample data has changed after having a PCA transformation applied to it using only two dimensions. Observe how the values for the first two dimensions remains unchanged when compared to a PCA transformation in six dimensions.
Can you write Python code for it?

# Display sample log-data after applying PCA transformation in two dimensions
display(pd.DataFrame(np.round(pca_samples, 4), columns = ['Dimension 1', 'Dimension 2']))


I am working on the file "Machine learning Nanodegree/creating_customer_segments/customer_segments.ipynb".
The first task is:
Question 1
Consider the total purchase cost of each product category and the statistical description of the dataset above for your sample customers.
What kind of establishment (customer) could each of the three samples you've chosen represent?
Hint: Examples of establishments include places like markets, cafes, and retailers, among many others. Avoid using names for establishments, such as saying "McDonalds" when describing a sample customer as a restaurant.
Answer:
Implementation: Feature Relevance
One interesting thought to consider is if one (or more) of the six product categories is actually relevant for understanding customer purchasing. That is to say, is it possible to determine whether customers purchasing some amount of one category of products will necessarily purchase some proportional amount of another category of products? We can make this determination quite easily by training a supervised regression learner on a subset of the data with one feature removed, and then score how well that model can predict the removed feature.
In the code block below, you will need to implement the following:
 - Assign new_data a copy of the data by removing a feature of your choice using the DataFrame.drop function.
 - Use sklearn.cross_validation.train_test_split to split the dataset into training and testing sets.
   - Use the removed feature as your target label. Set a test_size of 0.25 and set a random_state.
 - Import a decision tree regressor, set a random_state, and fit the learner to the training data.
 - Report the prediction score of the testing set using the regressor's score function.
Can you write Python code for it?

# TODO: Make a copy of the DataFrame, using the 'drop' function to drop the given feature
new_data = None

# TODO: Split the data into training and testing sets using the given feature as the target
X_train, X_test, y_train, y_test = (None, None, None, None)

# TODO: Create a decision tree regressor and fit it to the training set
regressor = None

# TODO: Report the score of the prediction using the testing set
score = None


I am working on the file "Machine learning Nanodegree/creating_customer_segments/customer_segments.ipynb".
The first task is:
Question 8
Consider the total purchase cost of each product category for the representative data points above, and reference the statistical description of the dataset at the beginning of this project. What set of establishments could each of the customer segments represent?
Hint: A customer who is assigned to 'Cluster X' should best identify with the establishments represented by the feature set of 'Segment X'.
Answer:
Question 9
For each sample point, which customer segment from Question 8 best represents it? Are the predictions for each sample point consistent with this?
Run the code block below to find which cluster each sample point is predicted to be.
Can you write Python code for it?

# Display the predictions
for i, pred in enumerate(sample_preds):
    print "Sample point", i, "predicted to be in Cluster", pred


I am working on the file "Machine learning Nanodegree/student_intervention/student_intervention.ipynb".
The first task is:
Implementation: Data Exploration
Let's begin by investigating the dataset to determine how many students we have information on, and learn about the graduation rate among these students. In the code cell below, you will need to compute the following:
- The total number of students, n_students.
- The total number of features for each student, n_features.
- The number of those students who passed, n_passed.
- The number of those students who failed, n_failed.
- The graduation rate of the class, grad_rate, in percent (%).
Can you write Python code for it?

# TODO: Calculate number of students
n_students = None

# TODO: Calculate number of features
n_features = None

# TODO: Calculate passing students
n_passed = None

# TODO: Calculate failing students
n_failed = None

# TODO: Calculate graduation rate
grad_rate = None

# Print the results
print "Total number of students: {}".format(n_students)
print "Number of features: {}".format(n_features)
print "Number of students who passed: {}".format(n_passed)
print "Number of students who failed: {}".format(n_failed)
print "Graduation rate of the class: {:.2f}%".format(grad_rate)


I am working on the file "Machine learning Nanodegree/student_intervention/student_intervention.ipynb".
The first task is:
Implementation: Training and Testing Data Split
So far, we have converted all categorical features into numeric values. For the next step, we split the data (both features and corresponding labels) into training and test sets. In the following code cell below, you will need to implement the following:
- Randomly shuffle and split the data (X_all, y_all) into training and testing subsets.
  - Use 300 training points (approximately 75%) and 95 testing points (approximately 25%).
  - Set a random_state for the function(s) you use, if provided.
  - Store the results in X_train, X_test, y_train, and y_test.
Can you write Python code for it?

# TODO: Import any additional functionality you may need here

# TODO: Set the number of training points
num_train = None

# Set the number of testing points
num_test = X_all.shape[0] - num_train

# TODO: Shuffle and split the dataset into the number of training and testing points above
X_train = None
X_test = None
y_train = None
y_test = None

# Show the results of the split
print "Training set has {} samples.".format(X_train.shape[0])
print "Testing set has {} samples.".format(X_test.shape[0])


I am working on the file "Machine learning Nanodegree/student_intervention/student_intervention.ipynb".
The first task is:
Preparing the Data
In this section, we will prepare the data for modeling, training and testing.
Identify feature and target columns
It is often the case that the data you obtain contains non-numeric features. This can be a problem, as most machine learning algorithms expect numeric data to perform computations with.
Run the code cell below to separate the student data into feature and target columns to see if any features are non-numeric.
Can you write Python code for it?

# Extract feature columns
feature_cols = list(student_data.columns[:-1])

# Extract target column 'passed'
target_col = student_data.columns[-1]

# Show the list of columns
print "Feature columns:\n{}".format(feature_cols)
print "\nTarget column: {}".format(target_col)

# Separate the data into feature data and target data (X_all and y_all, respectively)
X_all = student_data[feature_cols]
y_all = student_data[target_col]

# Show the feature information by printing the first five rows
print "\nFeature values:"
print X_all.head()


I am working on the file "Machine learning Nanodegree/student_intervention/student_intervention.ipynb".
The first task is:
Tabular Results
Edit the cell below to see how a table can be designed in Markdown. You can record your results from above in the tables provided.
 Classifer 1 - ?
| Training Set Size | Training Time | Prediction Time (test) | F1 Score (train) | F1 Score (test) |
| :---------------: | :---------------------: | :--------------------: | :--------------: | :-------------: |
| 100               |                         |                        |                  |                 |
| 200               |        EXAMPLE          |                        |                  |                 |
| 300               |                         |                        |                  |    EXAMPLE      |
 Classifer 2 - ?
| Training Set Size | Training Time | Prediction Time (test) | F1 Score (train) | F1 Score (test) |
| :---------------: | :---------------------: | :--------------------: | :--------------: | :-------------: |
| 100               |                         |                        |                  |                 |
| 200               |     EXAMPLE             |                        |                  |                 |
| 300               |                         |                        |                  |     EXAMPLE     |
 Classifer 3 - ?
| Training Set Size | Training Time | Prediction Time (test) | F1 Score (train) | F1 Score (test) |
| :---------------: | :---------------------: | :--------------------: | :--------------: | :-------------: |
| 100               |                         |                        |                  |                 |
| 200               |                         |                        |                  |                 |
| 300               |                         |                        |                  |                 |
Choosing the Best Model
In this final section, you will choose from the three supervised learning models the best model to use on the student data. You will then perform a grid search optimization for the model over the...
Can you write Python code for it?

# TODO: Import 'GridSearchCV' and 'make_scorer'

# TODO: Create the parameters list you wish to tune
parameters = None

# TODO: Initialize the classifier
clf = None

# TODO: Make an f1 scoring function using 'make_scorer'
f1_scorer = None

# TODO: Perform grid search on the classifier using the f1_scorer as the scoring method
grid_obj = None

# TODO: Fit the grid search object to the training data and find the optimal parameters
grid_obj = None

# Get the estimator
clf = grid_obj.best_estimator_

# Report the final F1 score for training and testing after parameter tuning
print "Tuned model has a training F1 score of {:.4f}.".format(predict_labels(clf, X_train, y_train))
print "Tuned model has a testing F1 score of {:.4f}.".format(predict_labels(clf, X_test, y_test))


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Aside. You may get a warning to the effect of "Terminated due to numerical difficulties --- this model may not be ideal". It means that the quality metric (to be covered in Module 3) failed to improve in the last iteration of the run. The difficulty arises as the sentiment model puts too much weight on extremely rare words. A way to rectify this is to apply regularization, to be covered in Module 4. Regularization lessens the effect of extremely rare words. For the purpose of this assignment, however, please proceed with the model above.
Now that we have fitted the model, we can extract the weights (coefficients) as an SFrame as follows:
Can you write Python code for it?

weights = sentiment_model.coefficients
weights.column_names()


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Checkpoint: Make sure your probability predictions match the ones obtained from GraphLab Create.
Can you write Python code for it?

print "Class predictions according to GraphLab Create:"
print sentiment_model.predict(sample_test_data, output_type='probability')


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Comparing models
We will now compare the accuracy of the sentiment_model and the simple_model using the get_classification_accuracy method you implemented above.
First, compute the classification accuracy of the sentiment_model on the train_data:
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Data preparation
We will use a dataset consisting of baby product reviews on Amazon.com.
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Extract sentiments
We will ignore all reviews with rating = 3, since they tend to have a neutral sentiment.
Can you write Python code for it?

products = products[products['rating'] != 3]
len(products)


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
For each review, we will use the word_count column and trim out all words that are not in the significant_words list above. We will use the SArray dictionary trim by keys functionality. Note that we are performing this on both the training and test set.
Can you write Python code for it?

train_data['word_count_subset'] = train_data['word_count'].dict_trim_by_keys(significant_words, exclude=False)
test_data['word_count_subset'] = test_data['word_count'].dict_trim_by_keys(significant_words, exclude=False)


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Let's dig deeper into the first row of the sample_test_data. Here's the full review:
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Let's see what the first example of the dataset looks like:
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Let's sort the coefficients (in descending order) by the value to obtain the coefficients with the most positive effect on the sentiment.
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Next, we will compute the classification accuracy of the simple_model on the test_data:
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Now, let us see a preview of what the dataset looks like.
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Now, let's compute the classification accuracy of the sentiment_model on the test_data.
Can you write Python code for it?

acc = get_classification_accuracy(sentiment_model, test_data, test_data['sentiment'])
print acc


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Now, we can see that the dataset contains an extra column called sentiment which is either positive (+1) or negative (-1).
Split data into training and test sets
Let's perform a train/test split with 80% of the data in the training set and 20% of the data in the test set. We use seed=1 so that everyone gets the same result.
Can you write Python code for it?

train_data, test_data = products.random_split(.8, seed=1)
print len(train_data)
print len(test_data)


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Now, we will inspect the weights (coefficients) of the simple_model:
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Quiz Question: Are the positive words in the simple_model (let us call them positive_significant_words) also positive words in the sentiment_model?
Can you write Python code for it?

different_values = []
for x in sentiment_model.coefficients:
    index = x['index']
    if index is not None and index in pv_words_simple_model and x['value'] < 0:
        print '' + index + ' --> ' + str(x['value'])
        different_values.append(index)
print different_values


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Quiz Question: How many weights are >= 0?
Making predictions with logistic regression
Now that a model is trained, we can make predictions on the test data. In this section, we will explore this in the context of 3 examples in the test dataset.  We refer to this set of 3 examples as the sample_test_data.
Can you write Python code for it?

sample_test_data = test_data[10:13]
print sample_test_data['rating']
sample_test_data


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Quiz Question: Of the three data points in sample_test_data, which one (first, second, or third) has the lowest probability of being classified as a positive review?
Find the most positive (and negative) review
We now turn to examining the full test dataset, test_data, and use GraphLab Create to form predictions on all of the test data points for faster performance.
Using the sentiment_model, find the 20 reviews in the entire test_data with the highest probability of being classified as a positive review. We refer to these as the "most positive reviews."
To calculate these top-20 reviews, use the following steps:
1.  Make probability predictions on test_data using the sentiment_model. (Hint: When you call .predict to make predictions on the test data, use option output_type='probability' to output the probability rather than just the most likely class.)
2.  Sort the data according to those predictions and pick the top 20. (Hint: You can use the .topk method on an SFrame to find the top k rows sorted according to the value of a specified column.)
Can you write Python code for it?

test_data['prob'] = sentiment_model.predict(test_data, output_type='probability')

for k in test_data.topk('prob', k=20):
    print k['name']


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Quiz Question: What is the accuracy of the sentiment_model on the test_data? Round your answer to 2 decimal places (e.g. 0.76).
Quiz Question: Does a higher accuracy value on the training_data always imply that the classifier is better?
Learn another classifier with fewer words
There were a lot of words in the model we trained above. We will now train a simpler logistic regression model using only a subset of words that occur in the reviews. For this assignment, we selected a 20 words to work with. These are:
Can you write Python code for it?

significant_words = ['love', 'great', 'easy', 'old', 'little', 'perfect', 'loves',
      'well', 'able', 'car', 'broke', 'less', 'even', 'waste', 'disappointed',
      'work', 'product', 'money', 'would', 'return']

len(significant_words)


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Quiz Question: Which model (sentiment_model or simple_model) has higher accuracy on the TEST set?
Baseline: Majority class prediction
It is quite common to use the majority class classifier as the a baseline (or reference) model for comparison with your classifier model. The majority classifier model predicts the majority class for all data points. At the very least, you should healthily beat the majority class classifier, otherwise, the model is (usually) pointless.
What is the majority class in the train_data?
Can you write Python code for it?

num_positive  = (train_data['sentiment'] == +1).sum()
num_negative = (train_data['sentiment'] == -1).sum()
print num_positive
print num_negative
print num_positive/len(train_data)


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Quiz Question: Which model (sentiment_model or simple_model) has higher accuracy on the TRAINING set?
Now, we will repeat this exercise on the test_data. Start by computing the classification accuracy of the sentiment_model on the test_data:
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Quiz Question: Which of the following products are represented in the 20 most negative reviews?  [multiple choice]
Compute accuracy of the classifier
We will now evaluate the accuracy of the trained classifier. Recall that the accuracy is given by
$$
\mbox{accuracy} = \frac{\mbox{# correctly classified examples}}{\mbox{# total examples}}
$$
This can be computed as follows:

Step 1: Use the trained model to compute class predictions (Hint: Use the predict method)
Step 2: Count the number of data points when the predicted class labels match the ground truth labels (called true_labels below).
Step 3: Divide the total number of correct predictions by the total number of data points in the dataset.

Complete the function below to compute the classification accuracy:
Can you write Python code for it?

def get_classification_accuracy(model, data, true_labels):
    # First get the predictions
    ## YOUR CODE HERE
    data['sentiment_predict_1'] = model.predict(data)

    # Compute the number of correctly classified examples
    ## YOUR CODE HERE
    count = 0
    for d in data:
        if d['sentiment'] == d['sentiment_predict_1']:
            count = count + 1
    # Then compute accuracy by dividing num_correct by total number of examples
    ## YOUR CODE HERE
    accuracy = count/len(data)

    return accuracy


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Quiz Question: Which of the following products are represented in the 20 most positive reviews? [multiple choice]
Now, let us repeat this exercise to find the "most negative reviews." Use the prediction probabilities to find the  20 reviews in the test_data with the lowest probability of being classified as a positive review. Repeat the same steps above but make sure you sort in the opposite order.
Can you write Python code for it?

for k in test_data.topk('prob', k=20, reverse=True):
    print k['name']


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Since we are only working with a subset of these words, the column word_count_subset is a subset of the above dictionary. In this example, only 2 significant words are present in this review.
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
The word_count column had been working with before looks like the following:
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
There are a total of 121713 coefficients in the model. Recall from the lecture that positive weights $w_j$ correspond to weights that cause positive sentiment, while negative weights correspond to negative sentiment.
Fill in the following block of code to calculate how many weights are positive ( >= 0). (Hint: The 'value' column in SFrame weights must be positive ( >= 0)).
Can you write Python code for it?

num_positive_weights = len([x for x in weights if x['value'] >= 0])
num_negative_weights = len([x for x in weights if x['value'] < 0 ])

print "Number of positive weights: %s " % num_positive_weights
print "Number of negative weights: %s " % num_negative_weights


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
Train a logistic regression model on a subset of data
We will now build a classifier with word_count_subset as the feature and sentiment as the target.
Can you write Python code for it?

simple_model = graphlab.logistic_classifier.create(train_data,
                                                   target = 'sentiment',
                                                   features=['word_count_subset'],
                                                   validation_set=None)
simple_model


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
We can compute the classification accuracy using the get_classification_accuracy function you implemented earlier.
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_classification/week1/module-2-linear-classifier-assignment.ipynb".
The first task is:
We will now make a class prediction for the sample_test_data. The sentiment_model should predict +1 if the sentiment is positive and -1 if the sentiment is negative. Recall from the lecture that the score (sometimes called margin) for the logistic regression model  is defined as:
$$
\mbox{score}_i = \mathbf{w}^T h(\mathbf{x}_i)
$$
where $h(\mathbf{x}_i)$ represents the features for example $i$.  We will write some code to obtain the scores using GraphLab Create. For each row, the score (or margin) is a number in the range [-inf, inf].
Can you write Python code for it?

scores = sentiment_model.predict(sample_test_data, output_type='margin')
print scores


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/PhillyCrime.ipynb".
The first task is:
Above: We see that for the "no Center City" version, per unit increase in crime, the predicted decrease in house prices is 2,287.  In contrast, for the original dataset, the drop is only 576 per unit increase in crime.  This is significantly different!
High leverage points:
Center City is said to be a "high leverage" point because it is at an extreme x value where there are not other observations.  As a result, recalling the closed-form solution for simple regression, this point has the potential to dramatically change the least squares line since the center of x mass is heavily influenced by this one point and the least squares line will try to fit close to that outlying (in x) point.  If a high leverage point follows the trend of the other data, this might not have much effect.  On the other hand, if this point somehow differs, it can be strongly influential in the resulting fit.
Influential observations:
An influential observation is one where the removal of the point significantly changes the fit.  As discussed above, high leverage points are good candidates for being influential observations, but need not be.  Other observations that are not leverage points can also be influential observations (e.g., strongly outlying in y even if x is a typical value).
Remove high-value outlier neighborhoods and redo analysis
Based on the discussion above, a question is whether the outlying high-value towns are strongly influencing the fit.  Let's remove them and see what happens.
Can you write Python code for it?

sales_nohighend = sales_noCC[sales_noCC['HousePrice'] < 350000]
crime_model_nohighend = graphlab.linear_regression.create(sales_nohighend, target='HousePrice', features=['CrimeRate'],validation_set=None, verbose=False)


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/PhillyCrime.ipynb".
The first task is:
Above: blue dots are original data, green line is the fit from the simple regression.
Remove Center City and redo the analysis
Center City is the one observation with an extremely high crime rate, yet house prices are not very low.  This point does not follow the trend of the rest of the data very well.  A question is how much including Center City is influencing our fit on the other datapoints.  Let's remove this datapoint and see what happens.
Can you write Python code for it?

sales_noCC = sales[sales['MilesPhila'] != 0.0]

sales_noCC.show(view="Scatter Plot", x="CrimeRate", y="HousePrice")


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/PhillyCrime.ipynb".
The first task is:
Compare coefficients for full-data fit versus no-Center-City fit
Visually, the fit seems different, but let's quantify this by examining the estimated coefficients of our original fit and that of the modified dataset with Center City removed.
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/PhillyCrime.ipynb".
The first task is:
Do the coefficients change much?
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/PhillyCrime.ipynb".
The first task is:
Exploring the data
The house price in a town is correlated with the crime rate of that town. Low crime towns tend to be associated with higher house prices and vice versa.
Can you write Python code for it?

graphlab.canvas.set_target('ipynb')
sales.show(view="Scatter Plot", x="CrimeRate", y="HousePrice")


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/PhillyCrime.ipynb".
The first task is:
Let's see what our fit looks like
Matplotlib is a Python plotting library that is also useful for plotting.  You can install it with:
'pip install matplotlib'
Can you write Python code for it?

import matplotlib.pyplot as plt

plt.plot(sales['CrimeRate'],sales['HousePrice'],'.',
        sales['CrimeRate'],crime_model.predict(sales),'-')


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/PhillyCrime.ipynb".
The first task is:
Look at the fit:
Can you write Python code for it?

plt.plot(sales_noCC['CrimeRate'],sales_noCC['HousePrice'],'.',
        sales_noCC['CrimeRate'],crime_model.predict(sales_noCC),'-')


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/PhillyCrime.ipynb".
The first task is:
Refit our simple regression model on this modified dataset:
Can you write Python code for it?

crime_model_noCC = graphlab.linear_regression.create(sales_noCC, target='HousePrice', features=['CrimeRate'],validation_set=None, verbose=False)


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/simple-regression-assignment.ipynb".
The first task is:
As we see we get the same answer both ways
Can you write Python code for it?

# if we want to multiply every price by 0.5 it's a simple as:
half_prices = 0.5*prices
# Let's compute the sum of squares of price. We can multiply two SArrays of the same length elementwise also with *
prices_squared = prices*prices
sum_prices_squared = prices_squared.sum() # price_squared is an SArray of the squares and we want to add them up.
print "the sum of price squared is: " + str(sum_prices_squared)


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/simple-regression-assignment.ipynb".
The first task is:
Aside: The python notation x.xxe+yy means x.xx * 10^(yy). e.g 100 = 10^2 = 1*10^2 = 1e2
Build a generic simple linear regression function
Armed with these SArray functions we can use the closed form solution found from lecture to compute the slope and intercept for a simple linear regression on observations stored as SArrays: input_feature, output.
Complete the following function (or write your own) to compute the simple linear regression slope and intercept:
Can you write Python code for it?

def simple_linear_regression(input_feature, output):
    N = len(input_feature)
    # compute the sum of input_feature and output
    SXi = input_feature.sum() # Sigma Xi
    print 'Sigma Xi:',SXi
    SYi = output.sum() # Sigma Yi
    print 'Sigma Yi:',SYi
    # compute the product of the output and the input_feature and its sum
    SXiYi = (input_feature * output).sum() # Sigma XiYi
    print 'Sigma XiYi:',SXiYi
    # compute the squared value of the input_feature and its sum
    SXi2 = (input_feature * input_feature).sum() # Sigma Xi2
    print 'Sigma Xi2:',SXi2
    # use the formula for the slope
    slope = (SXiYi - ((SXi * SYi)/N))/(SXi2 - ((SXi * SXi)/N))
    # use the formula for the intercept
    intercept = (SYi/N) - (slope * (SXi/N))
    return (intercept, slope)


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/simple-regression-assignment.ipynb".
The first task is:
Let's test our get_residual_sum_of_squares function by applying it to the test model where the data lie exactly on a line. Since they lie exactly on a line the residual sum of squares should be zero!
Can you write Python code for it?

print get_residual_sum_of_squares(test_feature, test_output, test_intercept, test_slope) # should be 0.0


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/simple-regression-assignment.ipynb".
The first task is:
Load house sales data
Dataset is from house sales in King County, the region where the city of Seattle, WA is located.
Can you write Python code for it?

sales = graphlab.SFrame('kc_house_data.gl/')
sales.head()


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/simple-regression-assignment.ipynb".
The first task is:
New Model: estimate prices from bedrooms
We have made one model for predicting house prices using squarefeet, but there are many other features in the sales SFrame.
Use your simple linear regression function to estimate the regression parameters from predicting Prices based on number of bedrooms. Use the training data!
Can you write Python code for it?

# Estimate the slope and intercept for predicting 'price' based on 'bedrooms'

br_intercept, br_slope = simple_linear_regression(train_data['bedrooms'], train_data['price'])
print "Intercept: " + str(br_intercept)
print "Slope: " + str(br_slope)


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/simple-regression-assignment.ipynb".
The first task is:
Now that we can calculate a prediction given the slope and intercept let's make a prediction. Use (or alter) the following to find out the estimated price for a house with 2650 squarefeet according to the squarefeet model we estiamted above.
Quiz Question: Using your Slope and Intercept from (4), What is the predicted price for a house with 2650 sqft?
Can you write Python code for it?

my_house_sqft = 2650
estimated_price = get_regression_predictions(my_house_sqft, sqft_intercept, sqft_slope)
print "The estimated price for a house with %d squarefeet is $%.2f" % (my_house_sqft, estimated_price)


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/simple-regression-assignment.ipynb".
The first task is:
Now that we have a function to compute the squarefeet given the price from our simple regression model let's see how big we might expect a house that costs $800,000 to be.
Quiz Question: According to this function and the regression slope and intercept from (3) what is the estimated square-feet for a house costing $800,000?
Can you write Python code for it?

my_house_price = 800000
estimated_squarefeet = inverse_regression_predictions(my_house_price, sqft_intercept, sqft_slope)
print "The estimated squarefeet for a house worth $%.2f is %d" % (my_house_price, estimated_squarefeet)


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/simple-regression-assignment.ipynb".
The first task is:
Now that we know it works let's build a regression model for predicting price based on sqft_living. Rembember that we train on train_data!
Can you write Python code for it?

sqft_intercept, sqft_slope = simple_linear_regression(train_data['sqft_living'], train_data['price'])

print "Intercept: " + str(sqft_intercept)
print "Slope: " + str(sqft_slope)


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/simple-regression-assignment.ipynb".
The first task is:
Predict the squarefeet given price
What if we want to predict the squarefoot given the price? Since we have an equation y = a + b*x we can solve the function for x. So that if we have the intercept (a) and the slope (b) and the price (y) we can solve for the estimated squarefeet (x).
Comlplete the following function to compute the inverse regression estimate, i.e. predict the input_feature given the output!
Can you write Python code for it?

def inverse_regression_predictions(output, intercept, slope):
    # solve output = intercept + slope*input_feature for input_feature.
    # Use this equation to compute the inverse predictions:
    # estimated_feature = (output - intercept) / slope
    estimated_feature = (output - intercept) / slope
    return estimated_feature


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/simple-regression-assignment.ipynb".
The first task is:
Residual Sum of Squares
Now that we have a model and can make predictions let's evaluate our model using Residual Sum of Squares (RSS). Recall that RSS is the sum of the squares of the residuals and the residuals is just a fancy word for the difference between the predicted output and the true output.
Complete the following (or write your own) function to compute the RSS of a simple linear regression model given the input_feature, output, intercept and slope:
Can you write Python code for it?

def get_residual_sum_of_squares(input_feature, output, intercept, slope):
    # First get the predictions
    predictions = []
    for f in input_feature:
        estimated_output = get_regression_predictions(f, intercept, slope)
        predictions.append(estimated_output)
    # then compute the residuals (since we are squaring it doesn't matter which order you subtract)
    errors = [x - y for x, y in zip(predictions, output)]
    # square the residuals and add them up
    RSS  = sum([x * y for x, y in zip(errors, errors)])
    return(RSS)


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/simple-regression-assignment.ipynb".
The first task is:
Split data into training and testing
We use seed=0 so that everyone running this notebook gets the same results.  In practice, you may set a random seed (or let GraphLab Create pick a random seed for you).
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/simple-regression-assignment.ipynb".
The first task is:
Test your Linear Regression Algorithm
Now we have two models for predicting the price of a house. How do we know which one is better? Calculate the RSS on the TEST data (remember this data wasn't involved in learning the model). Compute the RSS from predicting prices using bedrooms and from predicting prices using squarefeet.
Quiz Question: Which model (square feet or bedrooms) has lowest RSS on TEST data? Think about why this might be the case.
Can you write Python code for it?

# Compute RSS when using bedrooms on TEST data:
rss_prices_on_br = get_residual_sum_of_squares(train_data['bedrooms'], train_data['price'], br_intercept, br_slope)
print 'The RSS of predicting Prices based on Bed Room is : ' + str(rss_prices_on_br)

# Compute RSS when using squarefeet on TEST data:
rss_prices_on_sqft = get_residual_sum_of_squares(train_data['sqft_living'], train_data['price'], sqft_intercept, sqft_slope)
print 'The RSS of predicting Prices based on Square Feet is : ' + str(rss_prices_on_sqft)


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week1/simple-regression-assignment.ipynb".
The first task is:
We can test that our function works by passing it something where we know the answer. In particular we can generate a feature and then put the output exactly on a line: output = 1 + 1*input_feature then we know both our slope and intercept should be 1
Can you write Python code for it?

test_feature = graphlab.SArray(range(5))
print test_feature
test_output = graphlab.SArray(1 + 1*test_feature)
print test_output
(test_intercept, test_slope) =  simple_linear_regression(test_feature, test_output)
print "Intercept: " + str(test_intercept)
print "Slope: " + str(test_slope)


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-1.ipynb".
The first task is:
Compute RSS
Now that we can make predictions given the model, let's write a function to compute the RSS of the model. Complete the function below to calculate RSS given the model, data, and the outcome.
Can you write Python code for it?

def get_residual_sum_of_squares(model, data, outcome):
    # First get the predictions
    predictions = model.predict(data)
    # Then compute the residuals/errors
    errors = outcome - predictions
    # Then square and add them up
    RSS = (errors * errors).sum()
    return(RSS)


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-1.ipynb".
The first task is:
Learning Multiple Models
Now we will learn the weights for three (nested) models for predicting house prices. The first model will have the fewest features the second model will add one more feature and the third will add a few more:
* Model 1: squarefeet, # bedrooms, # bathrooms, latitude & longitude
* Model 2: add bedrooms*bathrooms
* Model 3: Add log squarefeet, bedrooms squared, and the (nonsensical) latitude + longitude
Can you write Python code for it?

model_1_features = ['sqft_living', 'bedrooms', 'bathrooms', 'lat', 'long']
model_2_features = model_1_features + ['bed_bath_rooms']
model_3_features = model_2_features + ['bedrooms_squared', 'log_sqft_living', 'lat_plus_long']


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-1.ipynb".
The first task is:
Learning a multiple regression model
Recall we can use the following code to learn a multiple regression model predicting 'price' based on the following features:
example_features = ['sqft_living', 'bedrooms', 'bathrooms'] on training data with the following code:
(Aside: We set validation_set = None to ensure that the results are always the same)
Can you write Python code for it?

example_features = ['sqft_living', 'bedrooms', 'bathrooms']
example_model = graphlab.linear_regression.create(train_data, target = 'price', features = example_features,
                                                  validation_set = None)


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-1.ipynb".
The first task is:
Load in house sales data
Dataset is from house sales in King County, the region where the city of Seattle, WA is located.
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-1.ipynb".
The first task is:
Making Predictions
In the gradient descent notebook we use numpy to do our regression. In this book we will use existing graphlab create functions to analyze multiple regressions.
Recall that once a model is built we can use the .predict() function to find the predicted values for data we pass. For example using the example model above:
Can you write Python code for it?

example_predictions = example_model.predict(train_data)
print example_predictions[0] # should be 271789.505878


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-1.ipynb".
The first task is:
Next create the following 4 new features as column in both TEST and TRAIN data:
* bedrooms_squared = bedrooms*bedrooms
* bed_bath_rooms = bedrooms*bathrooms
* log_sqft_living = log(sqft_living)
* lat_plus_long = lat + long
As an example here's the first one:
Can you write Python code for it?

train_data['bedrooms_squared'] = train_data['bedrooms'].apply(lambda x: x**2)
test_data['bedrooms_squared'] = test_data['bedrooms'].apply(lambda x: x**2)

# create the remaining 3 features in both TEST and TRAIN data
train_data['bed_bath_rooms'] = (train_data['bedrooms'] * train_data['bathrooms']).apply(lambda x: x)
test_data['bed_bath_rooms'] = (test_data['bedrooms'] * test_data['bathrooms']).apply(lambda x: x)

train_data['log_sqft_living'] = train_data['sqft_living'].apply(lambda x: log(x))
test_data['log_sqft_living'] = test_data['sqft_living'].apply(lambda x: log(x))

train_data['lat_plus_long'] = (train_data['lat'] + train_data['long']).apply(lambda x: x)
test_data['lat_plus_long'] = (test_data['lat'] + test_data['long']).apply(lambda x: x)


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-1.ipynb".
The first task is:
Now that you have the features, learn the weights for the three different models for predicting target = 'price' using graphlab.linear_regression.create() and look at the value of the weights/coefficients:
Can you write Python code for it?

# Learn the three models: (don't forget to set validation_set = None)
model_1 = graphlab.linear_regression.create(train_data, target = 'price',
                                            features = model_1_features, validation_set = None)
model_2 = graphlab.linear_regression.create(train_data, target = 'price',
                                            features = model_2_features, validation_set = None)
model_3 = graphlab.linear_regression.create(train_data, target = 'price',
                                            features = model_3_features, validation_set = None)

# Examine/extract each model's coefficients:
model_1_summary = model_1.get("coefficients")
model_2_summary = model_2.get("coefficients")
model_3_summary = model_3.get("coefficients")
print model_1_summary
print model_2_summary
print model_3_summary


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-1.ipynb".
The first task is:
Quiz Question: What is the sign (positive or negative) for the coefficient/weight for 'bathrooms' in model 1?
Quiz Question: What is the sign (positive or negative) for the coefficient/weight for 'bathrooms' in model 2?
Think about what this means.
Comparing multiple models
Now that you've learned three models and extracted the model weights we want to evaluate which model is best.
First use your functions from earlier to compute the RSS on TRAINING Data for each of the three models.
Can you write Python code for it?

# Compute the RSS on TRAINING data for each of the three models and record the values:
rss_model_1 = get_residual_sum_of_squares(model_1, train_data, train_data['price'])
rss_model_2 = get_residual_sum_of_squares(model_2, train_data, train_data['price'])
rss_model_3 = get_residual_sum_of_squares(model_3, train_data, train_data['price'])
print 'rss_model_1: ', rss_model_1
print 'rss_model_2: ', rss_model_2
print 'rss_model_3: ', rss_model_3


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-1.ipynb".
The first task is:
Quiz Question: Which model (1, 2 or 3) has lowest RSS on TRAINING Data? Is this what you expected?
Now compute the RSS on on TEST data for each of the three models.
Can you write Python code for it?

# Compute the RSS on TESTING data for each of the three models and record the values:
rss_model_1 = get_residual_sum_of_squares(model_1, test_data, test_data['price'])
rss_model_2 = get_residual_sum_of_squares(model_2, test_data, test_data['price'])
rss_model_3 = get_residual_sum_of_squares(model_3, test_data, test_data['price'])
print 'rss_model_1: ', rss_model_1
print 'rss_model_2: ', rss_model_2
print 'rss_model_3: ', rss_model_3


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-1.ipynb".
The first task is:
Split data into training and testing.
We use seed=0 so that everyone running this notebook gets the same results.  In practice, you may set a random seed (or let GraphLab Create pick a random seed for you).
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-1.ipynb".
The first task is:
Squaring bedrooms will increase the separation between not many bedrooms (e.g. 1) and lots of bedrooms (e.g. 4) since 1^2 = 1 but 4^2 = 16. Consequently this feature will mostly affect houses with many bedrooms.
bedrooms times bathrooms gives what's called an "interaction" feature. It is large when both of them are large.
Taking the log of squarefeet has the effect of bringing large values closer together and spreading out small values.
Adding latitude to longitude is totally non-sensical but we will do it anyway (you'll see why)

Quiz Question: What is the mean (arithmetic average) value of your 4 new features on TEST data? (round to 2 digits)
Can you write Python code for it?

print 'bedrooms_squared:', test_data['bedrooms_squared'].mean()
print 'bed_bath_rooms', test_data['bed_bath_rooms'].mean()
print 'lat_plus_long', test_data['lat_plus_long'].mean()
print 'log_sqft_living', test_data['log_sqft_living'].mean()


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-2.ipynb".
The first task is:
Although the gradient descent is designed for multiple regression since the constant is now a feature we can use the gradient descent function to estimat the parameters in the simple regression on squarefeet. The folowing cell sets up the feature_matrix, output, initial weights and step size for the first model:
Can you write Python code for it?

# let's test out the gradient descent
simple_features = ['sqft_living']
my_output = 'price'
(simple_feature_matrix, output) = get_numpy_data(train_data, simple_features, my_output)
initial_weights = np.array([-47000., 1.])
step_size = 7e-12
tolerance = 2.5e7


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-2.ipynb".
The first task is:
For testing let's use the 'sqft_living' feature and a constant as our features and price as our output:
Can you write Python code for it?

(example_features, example_output) = get_numpy_data(sales, ['sqft_living'], 'price') # the [] around 'sqft_living' makes it a list
print example_features[0,:] # this accesses the first row of the data the ':' indicates 'all columns'
print example_output[0] # and the corresponding output


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-2.ipynb".
The first task is:
Gradient Descent
Now we will write a function that performs a gradient descent. The basic premise is simple. Given a starting point we update the current weights by moving in the negative gradient direction. Recall that the gradient is the direction of increase and therefore the negative gradient is the direction of decrease and we're trying to minimize a cost function.
The amount by which we move in the negative gradient direction  is called the 'step size'. We stop when we are 'sufficiently close' to the optimum. We define this by requiring that the magnitude (length) of the gradient vector to be smaller than a fixed 'tolerance'.
With this in mind, complete the following gradient descent function below using your derivative function above. For each step in the gradient descent we update the weight for each feature befofe computing our stopping criteria
Can you write Python code for it?

from math import sqrt # recall that the magnitude/length of a vector [g[0], g[1], g[2]] is sqrt(g[0]^2 + g[1]^2 + g[2]^2)

def regression_gradient_descent(feature_matrix, output, initial_weights, step_size, tolerance):
    converged = False
    weights = np.array(initial_weights) # make sure it's a numpy array
    count = 0
    while not converged:
        print 'weights in ',count ,'iteration is: ', weights
        # compute the predictions based on feature_matrix and weights using your predict_output() function
        predictions = np.dot(feature_matrix, weights)
        # compute the errors as predictions - output
        errors = predictions - output
        gradient_sum_squares = 0 # initialize the gradient sum of squares
        # while we haven't reached the tolerance yet, update each feature's weight
        for i in range(len(weights)): # loop over each weight
            # Recall that feature_matrix[:, i] is the feature column associated with weights[i]
            # compute the derivative for weight[i]:
            derivative = feature_derivative(errors, feature_matrix[:, i])
            # add the squared value of the derivative to the gradient sum of squares (for assessing convergence)
            gradient_sum_squares = gradient_sum_squares + (derivative * derivative)
            # subtract the step size times the derivative from the current weight
            weights[i] = weights[i] - (step_size * derivative)
        # compute the square-root of the gradient sum of squares to get the gradient matnigude:
        gradient_magnitude = sqrt(gradient_sum_squares)
#         print 'gradient_magnitude: ', gradient_magnitude , 'and tolerance: ', tolerance
        if gradient_magnitude < tolerance:
            converged = True
        count  = count + 1
    return(weights)


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-2.ipynb".
The first task is:
How do your weights compare to those achieved in week 1 (don't expect them to be exactly the same)?
Quiz Question: What is the value of the weight for sqft_living -- the second element of ‘simple_weights’ (rounded to 1 decimal place)?
Use your newly estimated weights and your predict_output() function to compute the predictions on all the TEST data (you will need to create a numpy array of the test feature_matrix and test output first:
Can you write Python code for it?

(test_simple_feature_matrix, test_output) = get_numpy_data(test_data, simple_features, my_output)


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-2.ipynb".
The first task is:
If we want to do any "feature engineering" like creating new features or adjusting existing ones we should do this directly using the SFrames as seen in the other Week 2 notebook. For this notebook, however, we will work with the existing features.
Convert to Numpy Array
Although SFrames offer a number of benefits to users (especially when using Big Data and built-in graphlab functions) in order to understand the details of the implementation of algorithms it's important to work with a library that allows for direct (and optimized) matrix operations. Numpy is a Python solution to work with matrices (or any multi-dimensional "array").
Recall that the predicted value given the weights and the features is just the dot product between the feature and weight vector. Similarly, if we put all of the features row-by-row in a matrix then the predicted value for all the observations can be computed by right multiplying the "feature matrix" by the "weight vector".
First we need to take the SFrame of our data and convert it into a 2D numpy array (also called a matrix). To do this we use graphlab's built in .to_dataframe() which converts the SFrame into a Pandas (another python library) dataframe. We can then use Panda's .as_matrix() to convert the dataframe into a numpy matrix.
Can you write Python code for it?

print sales.head()
import numpy as np # note this allows us to refer to numpy as np instead


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-2.ipynb".
The first task is:
Load in house sales data
Dataset is from house sales in King County, the region where the city of Seattle, WA is located.
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-2.ipynb".
The first task is:
Next run your gradient descent with the above parameters.
Can you write Python code for it?

simple_weights = regression_gradient_descent(simple_feature_matrix, output, initial_weights, step_size, tolerance)
print simple_weights


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-2.ipynb".
The first task is:
Now that you have the predictions on test data, compute the RSS on the test data set. Save this value for comparison later. Recall that RSS is the sum of the squared errors (difference between prediction and output).
Can you write Python code for it?

rss = ((predictions - test_output) ** 2).sum()
print rss


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-2.ipynb".
The first task is:
Now we will write a function that will accept an SFrame, a list of feature names (e.g. ['sqft_living', 'bedrooms']) and an target feature e.g. ('price') and will return two things:
* A numpy matrix whose columns are the desired features plus a constant column (this is how we create an 'intercept')
* A numpy array containing the values of the output
With this in mind, complete the following function (where there's an empty line you should write a line of code that does what the comment above indicates)
Please note you will need GraphLab Create version at least 1.7.1 in order for .to_numpy() to work!
Can you write Python code for it?

def get_numpy_data(data_sframe, features, output):
    data_sframe['constant'] = 1 # this is how you add a constant column to an SFrame
    # add the column 'constant' to the front of the features list so that we can extract it along with the others:
    features = ['constant'] + features # this is how you combine two lists
    # select the columns of data_SFrame given by the features list into the SFrame features_sframe (now including constant):
    features_sframe = data_sframe[features]
    # the following line will convert the features_SFrame into a numpy matrix:
    feature_matrix = features_sframe.to_numpy()
    # assign the column of data_sframe associated with the output to the SArray output_sarray
    output_sarray = data_sframe[output]
    # the following will convert the SArray into a numpy array by first converting it to a list
    output_array = output_sarray.to_numpy()
    return(feature_matrix, output_array)


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-2.ipynb".
The first task is:
Quiz Question: What is the predicted price for the 1st house in the TEST data set for model 1 (round to nearest dollar)?
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-2.ipynb".
The first task is:
Quiz Question: What is the predicted price for the 1st house in the TEST data set for model 2 (round to nearest dollar)?
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-2.ipynb".
The first task is:
Quiz Question: Which model (1 or 2) has lowest RSS on all of the TEST data?
Can you write Python code for it?

rss = ((multi_predictions - multi_output) ** 2).sum()
print rss

print 'RSS from first model is 2.75400047593e+14 and RSS from 2nd model is 2.70263446465e+14'


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-2.ipynb".
The first task is:
Running a multiple regression
Now we will use more than one actual feature. Use the following code to produce the weights for a second model with the following parameters:
Can you write Python code for it?

model_features = ['sqft_living', 'sqft_living15'] # sqft_living15 is the average squarefeet for the nearest 15 neighbors.
my_output = 'price'
(feature_matrix, multi_output) = get_numpy_data(train_data, model_features, my_output)
initial_weights = np.array([-100000., 1., 1.])
step_size = 4e-12
tolerance = 1e9


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-2.ipynb".
The first task is:
To test your feature derivartive run the following:
Can you write Python code for it?

(example_features, example_output) = get_numpy_data(sales, ['sqft_living'], 'price')
my_weights = np.array([0., 0.]) # this makes all the predictions 0
test_predictions = predict_output(example_features, my_weights)
# just like SFrames 2 numpy arrays can be elementwise subtracted with '-':
errors = test_predictions - example_output # prediction errors in this case is just the -example_output
feature = example_features[:,0] # let's compute the derivative with respect to 'constant', the ":" indicates "all rows"
derivative = feature_derivative(errors, feature)
print derivative
print -np.sum(example_output)*2 # should be the same as derivative


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/multiple-regression-assignment-2.ipynb".
The first task is:
What is the actual price for the 1st house in the test data set?
Can you write Python code for it?


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/numpy-tutorial.ipynb".
The first task is:
2D arrays are accessed similarly by referring to the row and column index separated by a comma:
Can you write Python code for it?

my_matrix = np.array([[1, 2, 3], [4, 5, 6]])
print my_matrix

print my_matrix[1, 2]


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/numpy-tutorial.ipynb".
The first task is:
Creating Numpy Arrays
New arrays can be made in several ways. We can take an existing list and convert it to a numpy array:
Can you write Python code for it?

mylist = [1., 2., 3., 4.]
mynparray = np.array(mylist)
mynparray


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/numpy-tutorial.ipynb".
The first task is:
For 2D arrays you can select specific columns and specific rows. Passing ':' selects all rows/columns
Can you write Python code for it?

select_cols = np.array([True, False, True]) # 1st and 3rd column
select_rows = np.array([False, True]) # 2nd row

print my_matrix[select_rows, :] # just 2nd row but all columns

print my_matrix[:, select_cols] # all rows and just the 1st and 3rd column


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/numpy-tutorial.ipynb".
The first task is:
Operations on Arrays
You can use the operations '*', '**', '\', '+' and '-' on numpy arrays and they operate elementwise.
Can you write Python code for it?

my_array = np.array([1., 2., 3., 4.])
print my_array*my_array

print my_array**2

print my_array - np.ones(4)

print my_array + np.ones(4)

print my_array / 3

print my_array / np.array([2., 3., 4., 5.]) # = [1.0/2.0, 2.0/3.0, 3.0/4.0, 4.0/5.0]


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/numpy-tutorial.ipynb".
The first task is:
Recall that the Euclidean length (or magnitude) of a vector is the squareroot of the sum of the squares of the components. This is just the squareroot of the dot product of the vector with itself:
Can you write Python code for it?

array1_mag = np.sqrt(np.dot(array1, array1))
print array1_mag

print np.sqrt(np.sum(array1*array1))


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/numpy-tutorial.ipynb".
The first task is:
Sequences of indices can be accessed using ':' for example
Can you write Python code for it?

print my_matrix[0:2, 2] # recall 0:2 = [0, 1]

print my_matrix[0, 0:3]


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/numpy-tutorial.ipynb".
The first task is:
Similarly if you have a vector with the same number of elements as the matrix has rows you can left multiply them.
Can you write Python code for it?

my_matrix = my_features
my_array = np.array([0.3, 0.4, 0.5, 0.6])

print np.dot(my_array, my_matrix) # which has 2 elements because my_matrix has 2 columns


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/numpy-tutorial.ipynb".
The first task is:
The dot product
An important mathematical operation in linear algebra is the dot product.
When we compute the dot product between two vectors we are simply multiplying them elementwise and adding them up. In numpy you can do this with np.dot()
Can you write Python code for it?

array1 = np.array([1., 2., 3., 4.])
array2 = np.array([2., 3., 4., 5.])
print np.dot(array1, array2)

print np.sum(array1*array2)


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/numpy-tutorial.ipynb".
The first task is:
We can also use the dot product when we have a 2D array (or matrix). When you have an vector with the same number of elements as the matrix (2D array) has columns you can right-multiply the matrix by the vector to get another vector with the same number of elements as the matrix has rows. For example this is how you compute the predicted values given a matrix of features and an array of weights.
Can you write Python code for it?

my_features = np.array([[1., 2.], [3., 4.], [5., 6.], [7., 8.]])
print my_features

my_weights = np.array([0.4, 0.5])
print my_weights

my_predictions = np.dot(my_features, my_weights) # note that the weights are on the right
print my_predictions # which has 4 elements since my_features has 4 rows


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/numpy-tutorial.ipynb".
The first task is:
You can also initialize an empty array which will be filled with values. This is the fastest way to initialize a fixed-size numpy array however you must ensure that you replace all of the values.
Can you write Python code for it?

empty_vector = np.empty(5)
print empty_vector


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/numpy-tutorial.ipynb".
The first task is:
You can also pass a list of indices.
Can you write Python code for it?

fib_indices = np.array([1, 1, 2, 3])
random_vector = np.random.random(10) # 10 random numbers between 0 and 1
print random_vector

print random_vector[fib_indices]


I am working on the file "Machine-Learning-Specialization/machine_learning_regression/week2/numpy-tutorial.ipynb".
The first task is:
You can compute the sum with np.sum() and the average with np.average()
Can you write Python code for it?

print np.sum(my_array)

print np.average(my_array)

print np.sum(my_array)/len(my_array)


I am working on the file "MachineL/notes/ML13-监督学习-基本分类模型.ipynb".
The first task is:
使用默认参数，创建一个高斯朴素贝叶斯分类器，并将该分类器赋给变量clf。
Can you write Python code for it?

clf = GaussianNB(priors=None)

clf.fit(X,y)
print(clf.predict([[-0.8,-1]]))


I am working on the file "MachineL/notes/ML13-监督学习-基本分类模型.ipynb".
The first task is:
决策树本质上是一种寻找对特征空间上的划分，旨在构建一个训练数据拟合的好，并且复杂度小的决策树
朴素贝叶斯分类器
朴素贝叶斯分类器介绍
在sklearn库中，包含三个朴素贝叶斯分类器：
naive_bayes.GussianNB：高斯朴素贝叶斯
naive_bayes.MultinomialNB：针对多项式模型的朴素贝叶斯分类器
naive_bayes.BernoulliNB：针对多元伯努利模型的朴素贝叶斯分类器
在sklearn库中，可以使用sklearn.naive_bayes.GaussianNB创建一个高斯朴素贝叶斯分类器
朴素贝叶斯分类器的使用
导入numpy库，并构造训练数据X和y
Can you write Python code for it?


I am working on the file "MachineL/notes/ML13-监督学习-基本分类模型.ipynb".
The first task is:
决策树的使用
导入sklearn内嵌的鸢尾花数据集
Can you write Python code for it?

from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import cross_val_score


I am working on the file "MachineL/notes/ML13-监督学习-基本分类模型.ipynb".
The first task is:
利用决策树fit()函数训练模型，并使用predict()函数进行预测
Can you write Python code for it?


I am working on the file "MachineL/notes/ML13-监督学习-基本分类模型.ipynb".
The first task is:
导入高斯贝叶斯分类器
Can you write Python code for it?

from sklearn.naive_bayes import GaussianNB


I am working on the file "MachineL/notes/ML13-监督学习-基本分类模型.ipynb".
The first task is:
调用predict()函数，对未知样本[1.1]进行分类
Can you write Python code for it?


I am working on the file "MachineLearning(Advanced)/p0_titanic_survival_exploration/titanic_survival_exploration.ipynb".
The first task is:
2. Female的图形特性
SibSp（乘客在船上的兄弟姐妹和配偶的数量）特性（这个特性区分度挺好）。
Can you write Python code for it?


I am working on the file "MachineLearning(Advanced)/p0_titanic_survival_exploration/titanic_survival_exploration.ipynb".
The first task is:
Review20170619结果
预测结果准确率为82.27%，比之前80.36%，有进一步的提升。
其他发现
前面，”男性小于10岁“这个问题的编程时，发现我原来的
if passenger['Sex'] == 'male':
    if passenger['Age'] > 10:
        predictions.append(0)
    else:
        predictions.append(1)
else:
    predictions.append(1)
的准确率68.91%与新的这次改的
if passenger['Sex'] == 'male':
    if passenger['Age'] < 10:
        predictions.append(1)
    else:
        predictions.append(0)
else:
    predictions.append(1)
的准确率79.35%不同，其实就是一个取反的结果集，但是输出确实有差别的。原因是，对于年龄为空的时候，的取值是不一样的。以乘客Moran, Mr. James为例，它的PassengerId=6，年龄为空。那么，他的预测结果会是怎么样的呢？
Can you write Python code for it?

james = full_data[full_data['PassengerId'] == 6]
display(james)
print james['Age'] < 10
print james['Age'] == 10
print james['Age'] > 10


I am working on the file "MachineLearning(Advanced)/p0_titanic_survival_exploration/titanic_survival_exploration.ipynb".
The first task is:
从图中可以看出，SibSp > 4的全部female都遇难了。
另外，从数据中，发现Fare==31.275的那几个人都幸存了，也取出来（这个有点投机取巧）。
Can you write Python code for it?

def predictions_female(data):
    """ Model with multiple features. Makes a prediction with an accuracy of at least 80%. """

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

        if passenger['Sex'] == 'male':
            predictions.append(0)
        else:
            if passenger['SibSp'] > 4:
                predictions.append(0)
            else:
                if passenger['Fare'] == 31.275:
                    predictions.append(0)
                else:
                    predictions.append(1)

    # Return our predictions
    return pd.Series(predictions)

# Make the predictions
predictions = predictions_female(data)

print accuracy_score(outcomes, predictions)


I am working on the file "MachineLearning(Advanced)/p0_titanic_survival_exploration/titanic_survival_exploration.ipynb".
The first task is:
从图中看出Male在区间passenger['Fare'] < 120 or passenger['Fare'] > 170有遇难者
另外，从数据中，发现passenger['Fare'] >= 26.2875 and passenger['Fare'] <= 26.3875那几个人都幸存了，也取出来（这个有点投机取巧）。
Can you write Python code for it?

def predictions_male(data):
    """ Model with multiple features. Makes a prediction with an accuracy of at least 80%. """

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

        #if passenger['Sex'] == 'male' and (passenger['Fare'] < 80 or (passenger['Fare'] > 85 and passenger['Fare'] < 100) or passenger['Fare'] > 170):
        if passenger['Sex'] == 'male':
            if (passenger['Fare'] < 120 and not(passenger['Fare'] >= 26.2875 and passenger['Fare'] <= 26.3875)) or passenger['Fare'] > 170:
                predictions.append(0)
            else:
                predictions.append(1)
        else:
            if passenger['SibSp'] > 4:
                predictions.append(0)
            else:
                if passenger['Fare'] == 31.275:
                    predictions.append(0)
                else:
                    predictions.append(1)

    # Return our predictions
    return pd.Series(predictions)

# Make the predictions
predictions = predictions_male(data)

print accuracy_score(outcomes, predictions)


I am working on the file "MachineLearning(Advanced)/p0_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())
display(outcomes.head())
display(data.loc[0])
display(outcomes.loc[0])


I am working on the file "MachineLearning(Advanced)/p0_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 "MachineLearning(Advanced)/p0_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, 'Sex', ["Age < 10"])


I am working on the file "MachineLearning(Advanced)/p0_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?

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


I am working on the file "MachineLearning(Advanced)/p0_titanic_survival_exploration/titanic_survival_exploration.ipynb".
The first task is:
回答: Predictions have an accuracy of 82.27%.
处理过程
性别是个非常好的维度，结果78.68%，接近80%。但是，在其上，直接简单的添加一些特性，效果并不好。比如特性10岁以下男性幸免，反而拉低了准确率。凭借猜想，其他的特性也试了一下，结果都不好。靠人为对事物的理解，进行处理，是一种方法。但是，发现新知识的工程比较费劲，又很局限。但是，当前只会这种方法。先把性别分开，然后，在性别基础上，找出进一步的特性，只要有一个比较好的，应该就可以达到80%了。
主要方法是：从female中，找出遇难者。从male中，找出幸存者。
1. 预处理数据：先将数据按性别分开
Can you write Python code for it?

# 加载数据集
female_full_data = full_data[full_data['Sex'] == 'female']
female_outcomes = female_full_data['Survived']
female_data = female_full_data.drop('Survived', axis = 1)
print 'Female passenger: %d rows' %len(female_data)
display(female_data.head())
display(female_outcomes.head())

male_full_data = full_data[full_data['Sex'] == 'male']
male_outcomes = male_full_data['Survived']
male_data = male_full_data.drop('Survived', axis = 1)
print 'Male passenger: %d rows' %len(male_data)
display(male_data.head())
display(male_outcomes.head())


I am working on the file "MachineLearning(Advanced)/p0_titanic_survival_exploration/titanic_survival_exploration.ipynb".
The first task is:
结论
经过了数次对数据的探索和分类，你创建了一个预测泰坦尼克号乘客存活率的有用的算法。在这个项目中你手动地实现了一个简单的机器学习模型——决策树（decision tree）。决策树每次按照一个特征把数据分割成越来越小的群组（被称为 nodes）。每次数据的一个子集被分出来，如果分割结果的子集中的数据比之前更同质（包含近似的标签），我们的预测也就更加准确。电脑来帮助我们做这件事会比手动做更彻底，更精确。这个链接提供了另一个使用决策树做机器学习入门的例子。
决策树是许多监督学习算法中的一种。在监督学习中，我们关心的是使用数据的特征并根据数据的结果标签进行预测或建模。也就是说，每一组数据都有一个真正的结果值，不论是像泰坦尼克号生存数据集一样的标签，或者是连续的房价预测。
Review20170619，根据评审反馈，重新处理数据，并验证评审员给出的代码。
if passenger['SibSp'] >= 3 or passenger['Parch'] > 3:
    predictions.append(0)
elif passenger['Sex'] == 'female':
    predictions.append(1)
elif passenger['Sex'] == 'male' and passenger['Age'] < 10:
    predictions.append(1)
else:
    predictions.append(0)
使用函数survival_stats()，进一步验证以上给出的结论。
Can you write Python code for it?

survival_stats(data, outcomes, 'SibSp')

survival_stats(data, outcomes, 'Parch')

survival_stats(data, outcomes, 'Sex', ['Age < 10'])

def predictions_review20170619(data):
    """ Model with multiple features. Makes a prediction with an accuracy of at least 80%. """

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

        if passenger['SibSp'] >= 3 or passenger['Parch'] > 3:
            predictions.append(0)
        elif passenger['Sex'] == 'female':
            predictions.append(1)
        elif passenger['Sex'] == 'male' and passenger['Age'] < 10:
            predictions.append(1)
        else:
            predictions.append(0)

    # Return our predictions
    return pd.Series(predictions)

# Make the predictions
predictions = predictions_3(data)

print accuracy_score(outcomes, predictions)


I am working on the file "MachineLearning(Advanced)/p0_titanic_survival_exploration/titanic_survival_exploration.ipynb".
The first task is:
结论
请描述你实现80%准确度的预测模型所经历的步骤。您观察过哪些特征？某些特性是否比其他特征更有帮助？你用了什么条件来预测生还结果？你最终的预测的准确率是多少？
提示:运行下面的代码来查看你的预测准确度。
Can you write Python code for it?


I am working on the file "MachineLearning(Advanced)/p0_titanic_survival_exploration/titanic_survival_exploration.ipynb".
The first task is:
这个例子展示了如何将泰坦尼克号的 Survived 数据从 DataFrame 移除。注意到 data（乘客数据）和 outcomes （是否存活）现在已经匹配好。这意味着对于任何乘客的 data.loc[i] 都有对应的存活的结果 outcome[i]。
为了验证我们预测的结果，我们需要一个标准来给我们的预测打分。因为我们最感兴趣的是我们预测的准确率，既正确预测乘客存活的比例。运行下面的代码来创建我们的 accuracy_score 函数以对前五名乘客的预测来做测试。
思考题：从第六个乘客算起，如果我们预测他们全部都存活，你觉得我们预测的准确率是多少？
Can you write Python code for it?

def accuracy_score(truth, pred):
    """ Returns accuracy score for input truth and predictions. """

    # Ensure that the number of predictions matches number of outcomes
    # 确保预测的数量与结果的数量一致
    if len(truth) == len(pred):

        # Calculate and return the accuracy as a percent
        # 计算预测准确率（百分比）
        return "Predictions have an accuracy of {:.2f}%.".format((truth == pred).mean()*100)

    else:
        return "Number of predictions does not match number of outcomes!"

# Test the 'accuracy_score' function
# 测试 'accuracy_score' 函数
predictions = pd.Series(np.ones(5, dtype = int))
print 'Outcomes:'
display(outcomes[:5].head())
print 'Predictions:'
display(predictions.head())
print accuracy_score(outcomes[:5], predictions)


I am working on the file "MachineLearning(Advanced)/p0_titanic_survival_exploration/titanic_survival_exploration.ipynb".
The first task is:
问题1
对比真实的泰坦尼克号的数据，如果我们做一个所有乘客都没有存活的预测，你认为这个预测的准确率能达到多少？
提示：运行下面的代码来查看预测的准确率。
Can you write Python code for it?


I am working on the file "MachineLearning(Advanced)/p0_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 "MachineLearning(Advanced)/p0_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 "MachineLearning(Advanced)/p1_boston_housing/boston_housing.ipynb".
The first task is:
做出预测
当我们用数据训练出一个模型，它现在就可用于对新的数据进行预测。在决策树回归函数中，模型已经学会对新输入的数据提问，并返回对目标变量的预测值。你可以用这个预测来获取数据未知目标变量的信息，这些数据必须是不包含在训练数据之内的。
问题 9- 最优模型
最优模型的最大深度（maximum depth）是多少？此答案与你在问题 6所做的猜测是否相同？
运行下方区域内的代码，将决策树回归函数代入训练数据的集合，以得到最优化的模型。
Can you write Python code for it?

# Fit the training data to the model using grid search
reg = fit_model(features, prices)

# Produce the value for 'max_depth'
print "Parameter 'max_depth' is {} for the optimal model.".format(reg.get_params()['max_depth'])


I am working on the file "MachineLearning(Advanced)/p1_boston_housing/boston_housing.ipynb".
The first task is:
分析数据
在项目的第一个部分，你会对波士顿房地产数据进行初步的观察并给出你的分析。通过对数据的探索来熟悉数据可以让你更好地理解和解释你的结果。
由于这个项目的最终目标是建立一个预测房屋价值的模型，我们需要将数据集分为特征(features)和目标变量(target variable)。特征 'RM'， 'LSTAT'，和 'PTRATIO'，给我们提供了每个数据点的数量相关的信息。目标变量：'MEDV'，是我们希望预测的变量。他们分别被存在features和prices两个变量名中。
练习：基础统计运算
你的第一个编程练习是计算有关波士顿房价的描述统计数据。我们已为你导入了numpy，你需要使用这个库来执行必要的计算。这些统计数据对于分析模型的预测结果非常重要的。
在下面的代码中，你要做的是：
- 计算prices中的'MEDV'的最小值、最大值、均值、中值和标准差；
- 将运算结果储存在相应的变量中。
Can you write Python code for it?

# TODO: Minimum price of the data
#目标：计算价值的最小值
minimum_price = prices.as_matrix().min()

# TODO: Maximum price of the data
#目标：计算价值的最大值
maximum_price = prices.values.max()

# TODO: Mean price of the data
#目标：计算价值的平均值
mean_price = np.array(prices).mean()

# TODO: Median price of the data
#目标：计算价值的中值
median_price = np.median(np.array(prices))

# TODO: Standard deviation of prices of the data
#目标：计算价值的标准差
std_price = np.std(prices)

# Show the calculated statistics
#目标：输出计算的结果
print "Statistics for Boston housing dataset:\n"
print "Minimum price:\t ${:,.2f}".format(minimum_price)
print "Maximum price:\t ${:,.2f}".format(maximum_price)
print "Mean price:\t ${:,.2f}".format(mean_price)
print "Median price:\t ${:,.2f}".format(median_price)
print "Standard deviation of prices:\t ${:,.2f}\n".format(std_price)

print "Note:"
print "Panda.std:\t\t ${:,.2f}".format(prices.std())
print "Panda.std(ddof=0):\t ${:,.2f}".format(prices.std(ddof=0))
print "Numpy.std:\t\t ${:,.2f}".format(np.std(prices))
print "Summary: prices.std(ddof=0) equals np.std(prices)"


I am working on the file "MachineLearning(Advanced)/p1_boston_housing/boston_housing.ipynb".
The first task is:
回答: 成功，因为R^2等于0.923，非常接近1，而且，算是预测得比较好。
练习: 数据分割与重排
接下来，你需要把波士顿房屋数据集分成训练和测试两个子集。通常在这个过程中，数据也会被重新排序，以消除数据集中由于排序而产生的偏差。
在下面的代码中，你需要：
- 使用 sklearn.model_selection 中的 train_test_split， 将features和prices的数据都分成用于训练的数据子集和用于测试的数据子集。
  - 分割比例为：80%的数据用于训练，20%用于测试；
  - 选定一个数值以设定 train_test_split 中的 random_state ，这会确保结果的一致性；
- 最终分离出的子集为X_train,X_test,y_train,和y_test。
Can you write Python code for it?

# TODO: Import 'train_test_split'
from sklearn.model_selection import train_test_split

print type(prices)
print type(features)
print "prices.shape:\t{0}".format(prices.shape)
print "features.shape:\t{0}".format(features.shape)

# TODO: Shuffle and split the data into training and testing subsets
X_train, X_test, y_train, y_test = train_test_split(prices, features, test_size=0.2, random_state=31)

print "X_train.shape:\t{0}".format(X_train.shape)
print "X_test.shape:\t{0}".format(X_test.shape)
print "y_train.shape:\t{0}".format(y_train.shape)
print "y_test.shape:\t{0}".format(y_test.shape)

# Success
print "Training and testing split was successful."


I am working on the file "MachineLearning(Advanced)/p1_boston_housing/boston_housing.ipynb".
The first task is:
答案: 建议价格：
| 特征 | 客戶 1 | 客戶 2 | 客戶 3 |
| :---: | :---: | :---: | :---: |
| 建议售价 | \$408,800.00 | \$231,253.45 | \$938,053.85 |
价格比较合理。因为，从数据趋势来看，社区越富有，社区贫困指数（％被认为是贫困阶层）越低，房屋内房间总数越多，邻近学校的学生-老师比例越低，销售总价也就越高。该地区，房屋平均价值\$454,342.94，中位数\$438,900.00，最低房价\$105,000.00，最高房价$1,024,800.00。可以看出，客户2是低收入的社区，客户1是中等收入的社区，客户3是高收入的社区。
敏感度
一个最优的模型不一定是一个健壮模型。有的时候模型会过于复杂或者过于简单，以致于难以泛化新增添的数据；有的时候模型采用的学习算法并不适用于特定的数据结构；有的时候样本本身可能有太多噪点或样本过少，使得模型无法准确地预测目标变量。这些情况下我们会说模型是欠拟合的。执行下方区域中的代码，采用不同的训练和测试集执行 fit_model 函数10次。注意观察对一个特定的客户来说，预测是如何随训练数据的变化而变化的。
Can you write Python code for it?

# 如果你在 fit_model 函数里使用了 print pd.DataFrame(grid.cv_results_) 请注释掉这行之后再运行本代码
vs.PredictTrials(features, prices, fit_model, client_data)


I am working on the file "MachineLearning(Advanced)/p1_boston_housing/boston_housing.ipynb".
The first task is:
答案: 最优模型的最大深度（maximum depth）是4，与之前的猜测4，相同。
问题 10 - 预测销售价格
想像你是一个在波士顿地区的房屋经纪人，并期待使用此模型以帮助你的客户评估他们想出售的房屋。你已经从你的三个客户收集到以下的资讯:
| 特征 | 客戶 1 | 客戶 2 | 客戶 3 |
| :---: | :---: | :---: | :---: |
| 房屋内房间总数 | 5 间房间 | 4 间房间 | 8 间房间 |
| 社区贫困指数（％被认为是贫困阶层） | 17% | 32% | 3% |
| 邻近学校的学生-老师比例 | 15：1 | 22：1 | 12：1 |
你会建议每位客户的房屋销售的价格为多少？从房屋特征的数值判断，这样的价格合理吗？为什么？
提示：用你在分析数据部分计算出来的统计信息来帮助你证明你的答案。
运行下列的代码区域，使用你优化的模型来为每位客户的房屋价值做出预测。
Can you write Python code for it?

# Produce a matrix for client data
client_data = [[5, 17, 15], # Client 1
               [4, 32, 22], # Client 2
               [8, 3, 12]]  # Client 3

# Show predictions
for i, price in enumerate(reg.predict(client_data)):
    print "Predicted selling price for Client {}'s home: ${:,.2f}".format(i+1, price)

import matplotlib.pyplot as plt
plt.hist(prices, bins = 100)
for price in reg.predict(client_data):
    plt.axvline(price, lw = 5, c = 'r')


I am working on the file "MachineLearning(Advanced)/p1_boston_housing/boston_housing.ipynb".
The first task is:
问题 3- 训练及测试
将数据集按一定比例分为训练用的数据集和测试用的数据集对学习算法有什么好处？如果用模型已经见过的数据，例如部分训练集数据进行测试，又有什么坏处？
提示： 如果没有数据来对模型进行测试，会出现什么问题？
答案: 前提是，我们希望预测结果尽量准确。那么，
1. 如果全部数据都用于训练，没有数据来测试，我们就没法知道预测的效果。
2. 如果全部数据都用于训练，并用其中部分的数据用于测试，预测的结果，将是没法进行泛化的。因为，这些数据都已经出现过，或者说，反复多次训练过，正常的话，没法做出预期才奇怪。我们要的预测是对于未知情况的猜测。

分析模型的表现
在项目的第三部分，我们来看一下几个模型针对不同的数据集在学习和测试上的表现。另外，你需要专注于一个特定的算法，用全部训练集训练时，提高它的'max_depth' 参数，观察这一参数的变化如何影响模型的表现。把你模型的表现画出来对于分析过程十分有益。可视化可以让我们看到一些单看结果看不到的行为。
学习曲线
下方区域内的代码会输出四幅图像，它们是一个决策树模型在不同最大深度下的表现。每一条曲线都直观的显示了随着训练数据量的增加，模型学习曲线的训练评分和测试评分的变化。注意，曲线的阴影区域代表的是该曲线的不确定性（用标准差衡量）。这个模型的训练和测试部分都使用决定系数R2来评分。
运行下方区域中的代码，并利用输出的图形回答下面的问题。
Can you write Python code for it?

# Produce learning curves for varying training set sizes and maximum depths
vs.ModelLearning(features, prices)


I am working on the file "MachineLearning(Advanced)/p1_boston_housing/boston_housing.ipynb".
The first task is:
问题 4 - 学习数据
选择上述图像中的其中一个，并给出其最大深度。随着训练数据量的增加，训练曲线的评分有怎样的变化？测试曲线呢？如果有更多的训练数据，是否能有效提升模型的表现呢？
提示：学习曲线的评分是否最终会收敛到特定的值？
答案: max_depth=6时，随着训练数据量的增加，训练曲线的逐渐下降，并趋于平缓，测试曲线先快速上升，然后快速趋于平缓。 如果有更多的训练数据，将不能提高模型的表现，因为，训练曲线和测试曲线都已经收敛，并趋近于一个特定的值。
复杂度曲线
下列代码内的区域会输出一幅图像，它展示了一个已经经过训练和验证的决策树模型在不同最大深度条件下的表现。这个图形将包含两条曲线，一个是训练的变化，一个是测试的变化。跟学习曲线相似，阴影区域代表该曲线的不确定性，模型训练和测试部分的评分都用的 performance_metric 函数。
运行下方区域中的代码，并利用输出的图形并回答下面的两个问题。
Can you write Python code for it?


I am working on the file "MachineLearning(Advanced)/p1_boston_housing/boston_housing.ipynb".
The first task is:
问题1 - 特征观察
如前文所述，本项目中我们关注的是其中三个值:'RM'、'LSTAT' 和'PTRATIO'，对每一个数据点:
- 'RM' 是该地区中每个房屋的平均房间数量；
- 'LSTAT' 是指该地区有多少百分比的房东属于是低收入阶层（有工作但收入微薄）；
- 'PTRATIO' 是该地区的中学和小学里，学生和老师的数目比（学生/老师）。
凭直觉，上述三个特征中对每一个来说，你认为增大该特征的数值，'MEDV'的值会是增大还是减小呢？每一个答案都需要你给出理由。
提示：你预期一个'RM' 值是6的房屋跟'RM' 值是7的房屋相比，价值更高还是更低呢？
Can you write Python code for it?

print data.loc[1]
print data.loc[1]['RM']
# for index, row in data.iterrows():
#     print index
#     print row

import matplotlib.pyplot as plt

plt.figure(figsize=(20, 5))
for i, col in enumerate(features.columns):
    plt.subplot(1, 3, (i+1))
    plt.plot(data[col], prices, 'o')
    plt.title(col)
    plt.xlabel(col)
    plt.ylabel('prices')

plt.figure(figsize=(10,10))
ax = plt.subplot(111, projection='3d')
# ax.scatter(data['RM'], data['LSTAT'], data['PTRATIO'], marker='o')

for index, row in data.iterrows():
    ax.scatter(row['RM'], row['LSTAT'], row['PTRATIO'], c=('#000080'), marker='o')

ax.set_xlabel('RM: Room number')
ax.set_ylabel('LSTAT: Low imcome')
ax.set_zlabel('PTRATIO: Student/Teather')

plt.show()


I am working on the file "MachineLearning(Advanced)/p2_finding_donors/.ipynb_checkpoints/finding_donors-checkpoint.ipynb".
The first task is:
准备数据
在数据能够被作为输入提供给机器学习算法之前，它经常需要被清洗，格式化，和重新组织 - 这通常被叫做预处理。幸运的是，对于这个数据集，没有我们必须处理的无效或丢失的条目，然而，由于某一些特征存在的特性我们必须进行一定的调整。这个预处理都可以极大地帮助我们提升几乎所有的学习算法的结果和预测能力。
转换倾斜的连续特征
一个数据集有时可能包含至少一个靠近某个数字的特征，但有时也会有一些相对来说存在极大值或者极小值的不平凡分布的的特征。算法对这种分布的数据会十分敏感，并且如果这种数据没有能够很好地规一化处理会使得算法表现不佳。在人口普查数据集的两个特征符合这个描述：'capital-gain'和'capital-loss'。
运行下面的代码单元以创建一个关于这两个特征的条形图。请注意当前的值的范围和它们是如何分布的。
Can you write Python code for it?

# 将数据切分成特征和对应的标签
income_raw = data['income']
features_raw = data.drop('income', axis = 1)

# 可视化原来数据的倾斜的连续特征
vs.distribution(data)


I am working on the file "MachineLearning(Advanced)/p2_finding_donors/.ipynb_checkpoints/finding_donors-checkpoint.ipynb".
The first task is:
对于高度倾斜分布的特征如'capital-gain'和'capital-loss'，常见的做法是对数据施加一个对数转换，将数据转换成对数，这样非常大和非常小的值不会对学习算法产生负面的影响。并且使用对数变换显著降低了由于异常值所造成的数据范围异常。但是在应用这个变换时必须小心：因为0的对数是没有定义的，所以我们必须先将数据处理成一个比0稍微大一点的数以成功完成对数转换。
运行下面的代码单元来执行数据的转换和可视化结果。再次，注意值的范围和它们是如何分布的。
Can you write Python code for it?

# 对于倾斜的数据使用Log转换
skewed = ['capital-gain', 'capital-loss']
features_raw[skewed] = data[skewed].apply(lambda x: np.log(x + 1))

# 可视化经过log之后的数据分布
vs.distribution(features_raw, transformed = True)


I am working on the file "MachineLearning(Advanced)/p2_finding_donors/.ipynb_checkpoints/finding_donors-checkpoint.ipynb".
The first task is:
混洗和切分数据
现在所有的 类别变量 已被转换成数值特征，而且所有的数值特征已被规一化。和我们一般情况下做的一样，我们现在将数据（包括特征和它们的标签）切分成训练和测试集。其中80%的数据将用于训练和20%的数据用于测试。
运行下面的代码单元来完成切分。
Can you write Python code for it?

# 导入 train_test_split
from sklearn.model_selection import train_test_split

# 将'features'和'income'数据切分成训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(features, income, test_size = 0.2, random_state = 0)

# 显示切分的结果
print "Training set has {} samples.".format(X_train.shape[0])
print "Testing set has {} samples.".format(X_test.shape[0])