<jupyter_start><jupyter_text>Build word count vector for each view<jupyter_code>from sklearn.feature_extraction.text import CountVectorizer
df.dropna(inplace=True)
vectorizer = CountVectorizer()
def word_count(s):
    try:
        x = vectorizer.fit_transform([s])
        res = {}
        voc = vectorizer.get_feature_names()
        for index, count in enumerate(x.toarray()[0]):
            if count > 0:
                res[voc[index]] = count
    except:
        return ''
    return res
df['word_count'] = df['review'].apply(word_count)
df.head()
giraffe_reviews = df[df['name']=='<NAME> the G<NAME>ther']
len(giraffe_reviews)
import matplotlib.pyplot as plt
%matplotlib notebook
plt.figure()
giraffe_reviews['rating'].value_counts(ascending=True).plot(kind='bar')
plt.xticks(rotation=0)
df.rating.value_counts()
df = df[df.rating != 3]

df['sentiment'] = df['rating'] >= 4
df.head()
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

X_train, X_test, y_train, y_test = train_test_split(df['review'], df['sentiment'], random_state=0, train_size=0.8)
sentiment_model = LogisticRegression()
X_train = vectorizer.fit_transform(X_train)
sentiment_model.fit(X_train, y_train)
sentiment_model.score(X_train, y_train)
X_test = vectorizer.transform(X_test)
sentiment_model.score(X_test, y_test)
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
%matplotlib notebook
def metric_roc_curve(fpr,tpr,roc_auc):
    plt.figure()
    lw = 2
    plt.plot(fpr, tpr, color='darkorange',
             lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)
    plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Sentiment Analyzing')
    plt.legend(loc="lower right")
    plt.show()
y_pred = sentiment_model.predict_proba(X_test)
fpr, tpr, _ = roc_curve(y_test, y_pred[:, 1])
roc_auc = auc(fpr, tpr)
metric_roc_curve(fpr, tpr, roc_auc)<jupyter_output><empty_output><jupyter_text>Question 1<jupyter_code>selected_words = ['awesome', 'great', 'fantastic', 'amazing', 'love', 'horrible', 'bad', 'terrible', 'awful', 'wow', 'hate']
index = {}
for word in selected_words:
    index[word] = 0
def f(s):
    for word in selected_words:
        if word in s:
            index[word] += s[word]
_ = df.word_count.apply(f)
index
max(index.items(), key=lambda x: x[1])<jupyter_output><empty_output><jupyter_text>Question two<jupyter_code>min(index.items(), key=lambda x: x[1])
def selected_count(dic, word):
    if word in dic:
        return dic[word]
    return 0
for word in selected_words:
    df[word] = df.word_count.apply(selected_count, args=(word,))
X_train, X_test, y_train, y_test = train_test_split(df[selected_words], df['sentiment'], random_state=0, train_size=0.8)
model = LogisticRegression()
model.fit(X_train, y_train)
coeff = pd.DataFrame({'name': selected_words, 'value': model.coef_[0]})
coeff.sort_values('value')
model.score(X_test, y_test)
np.sum(y_test)/len(list(y_test))
df[df['name'] == 'Baby Trend Diaper Champ']
Baby_df = df[df['name']=='Baby Trend Diaper Champ']
X_train = Baby_df['review']
y_train = Baby_df['sentiment']

vectorize = CountVectorizer()
X_train = vectorize.fit_transform(X_train)
baby_model = LogisticRegression()
baby_model.fit(X_train, y_train)
Baby_df['pred'] = baby_model.predict_proba(X_train)[:, 1]
Baby_df['pred1'] = model.predict_proba(Baby_df[selected_words])[:, 1]
Baby_df.sort_values('pred', ascending=False)
Baby_df.sort_values('pred', ascending=False).iloc[0, 1]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15247741582261154, 'fraction_numerical': 0.010676156583629894, 'mean_word_length': 5.525, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '26327', 'license': 'MIT', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1375, 'n_words': 261, 'path': 'Machine_Learning_WashingTon/Machine Learning Foundations (A Case Study Approach)/week3_analyzing_product_sentiment_assignment/Analyzing Product Review Sentiment.ipynb', 'repo_name': 'PerpetualSmile/-Coursera', 'size': '3663'}