Machine Learning and Python Code

+1 vote

So, I recently started with Machine Learning and coding in Python. I've been trying to figure out the partition method used in the Amazon fine food review data from kaggle and its code. What i also can't understand, is the purpose of the last 3 lines of code.

    %matplotlib inline
    import sqlite3
    import pandas as pd
    import numpy as np
    import nltk
    import string
    import matplotlib.pyplot as plt
    import seaborn as sns
    from sklearn.feature_extraction.text import TfidfTransformer
    from sklearn.feature_extraction.text import TfidfVectorizer

    from sklearn.feature_extraction.text import CountVectorizer
    from sklearn.metrics import confusion_matrix
    from sklearn import metrics
    from sklearn.metrics import roc_curve, auc
    from nltk.stem.porter import PorterStemmer



    # using the SQLite Table to read data.
    con = sqlite3.connect('./amazon-fine-food-reviews/database.sqlite') 




    #filtering only positive and negative reviews i.e. 
    # not taking into consideration those reviews with Score=3
    filtered_data = pd.read_sql_query("""
    SELECT *
    FROM Reviews
    WHERE Score != 3
    """, con) 




    # Give reviews with Score>3 a positive rating, and reviews with a 
    score<3 a negative rating.
    def partition(x):
    if x < 3:
        return 'negative'
    return 'positive'

    #changing reviews with score less than 3 to be positive vice-versa
    actualScore = filtered_data['Score']
    positiveNegative = actualScore.map(partition) 
    filtered_data['Score'] = positiveNegative

Any help would be greatly appreciated. Thanks.

Dec 13, 2018 in Data Analytics by Upasana
• 8,620 points
932 views

1 answer to this question.

0 votes

You can create an array called actualScore using the column Score from filtered_data

actualScore = filtered_data['Score']

Then create an array positiveNegative coding negative for values less than 3 and positive for values greater than 3.

positiveNegative = actualScore.map(partition)

Then you can overwrite the old column score with the new coded values

filtered_data['Score'] = positiveNegative

Hope this helps!

If you wanna know more about Machine Learning, It's recommended to go for Python Machine Learning course today.

Thank you!

answered Dec 13, 2018 by Shubham
• 13,490 points

Related Questions In Data Analytics

0 votes
2 answers

Why should anyone learn Python instead of R for machine learning?

Machine learning is the latest technology everyone ...READ MORE

answered Apr 13, 2019 in Data Analytics by SA
• 1,090 points
1,038 views
0 votes
3 answers

R vs MATLAB, which is better with respect to machine learning?

Hello, Both are a good programming language you ...READ MORE

answered Apr 12, 2019 in Data Analytics by SA
• 1,090 points
1,498 views
0 votes
1 answer

What is the Difference in Size and Count in pandas (python)?

The major difference is "size" includes NaN values, ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 8, 2020 by Gitika 2,868 views
0 votes
1 answer

Which package is used to do data import in R and Python and How do you import SAS data?

We can do data import using multiple ...READ MORE

answered Aug 24, 2018 in Data Analytics by Abhi
• 3,720 points
927 views
0 votes
1 answer

Python Machine Learning/Data Science Project Structure

In response to your question regarding reusing ...READ MORE

answered Mar 30, 2022 in Machine Learning by Dev
• 6,000 points
1,551 views
0 votes
2 answers
+1 vote
1 answer

Jupyter Notebook : superscripts and subscripts

You can use the markdown cell to do this. ...READ MORE

answered Dec 6, 2018 in Data Analytics by Shubham
• 13,490 points

edited Dec 12, 2018 by Shubham 12,736 views
+1 vote
1 answer

R Code : Combine two lists with different structures

You seem to be having an empty ...READ MORE

answered Dec 13, 2018 in Data Analytics by Shubham
• 13,490 points
1,499 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP