-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessing.py
137 lines (102 loc) · 4.66 KB
/
preprocessing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""This notebook is for helper functions for exploration"""
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import prepare
import nltk
import re
from wordcloud import WordCloud
df = prepare.wrangle_data()
def make_word_list(df):
""" creates a list of every not unique word in dataframe"""
all_words = re.sub(r'[^\w\s]', '', (' '.join(df.lemmatized))).split()
all_freq = pd.Series(all_words).value_counts()
mask = all_freq > 1
all_not_unique = list(all_freq[mask].index)
return all_not_unique
def finding_non_single_words(x):
"""finds all words in column that appear in df more than one time
will be used to make a column that counts words that appear more than once"""
all_not_unique = make_word_list(df)
l = []
for w in x:
if w in all_not_unique:
l.append(w)
return l
def feature_engineering(df):
"""creates calculated columns for df subsetted by type of column"""
#list making features
df['word_list'] = df.lemmatized.apply(lambda x: re.sub(r'[^\w\s]', '', x).split())
df['unique_words'] = df.word_list.apply(lambda x: pd.Series(x).unique())
df['non_single_words'] = df.word_list.apply(lambda x: finding_non_single_words(x))
# counting
df['word_count_simple'] = df.lemmatized.str.count(" ") + 1
df['word_count'] = df.word_list.apply(lambda x: len(x))
df['unique_count'] = df.unique_words.apply(lambda x: len(x))
df['non_single_count'] = df.non_single_words.apply(lambda x: len(x))
# calculating
df['percent_unique'] = (df.unique_count / df.word_count)
df['percent_repeat'] = (1 - df.unique_count / df.word_count)
df['percent_one_word'] = df.word_list.apply(lambda x: (pd.Series(x).value_counts() == 1).mean())
df['percent_non_single'] = (df.non_single_count / df.word_count)
return df
# new dataframes
def language_series(df):
"""makes df with words and frequency of words for each programming language"""
all_words = (' '.join(df.lemmatized))
all_words = re.sub(r'[^\w\s]', '', all_words).split()
all_freq = pd.Series(all_words).value_counts()
php_words = (' '.join(df[df.language == 'PHP'].lemmatized))
php_words = re.sub(r'[^\w\s]', '', php_words).split()
php_freq = pd.Series(php_words).value_counts()
python_words = (' '.join(df[df.language == 'Python'].lemmatized))
python_words = re.sub(r'[^\w\s]', '', python_words).split()
python_freq = pd.Series(python_words).value_counts()
java_words = (' '.join(df[df.language == 'Java'].lemmatized))
java_words = re.sub(r'[^\w\s]', '', java_words).split()
java_freq = pd.Series(java_words).value_counts()
js_words = (' '.join(df[df.language == 'JavaScript'].lemmatized))
js_words = re.sub(r'[^\w\s]', '', js_words).split()
js_freq = pd.Series(js_words).value_counts()
freq_df = pd.DataFrame({'all': all_freq,
'php': php_freq,
'python': python_freq,
'java': java_freq,
'java_script': js_freq
})
freq_df = freq_df.fillna(0)
freq_df = freq_df.astype(int)
return freq_df
def make_language_df(df=df):
"""creates a data frame of words in rows"""
freq_df = language_series(df)
freq_list = freq_df['all'].sort_values(ascending = False).head(15).index
df_js = df[0:200]
df_python = df[200:400]
df_java = df[400:600]
df_php= df[600:800]
all_list = []
js_list = []
python_list = []
php_list = []
java_list = []
for i in freq_list:
v = len(pd.DataFrame({'loc_index' :df[df.lemmatized.apply(lambda x: len(re.findall(i, x))) > 0].index}))
all_list.append(v)
js = len(pd.DataFrame({'loc_index' :df_js[df_js.lemmatized.apply(lambda x: len(re.findall(i, x))) > 0].index}))
js_list.append(js)
py = len(pd.DataFrame({'loc_index' :df_python[df_python.lemmatized.apply(lambda x: len(re.findall(i, x))) > 0].index}))
python_list.append(py)
php = len(pd.DataFrame({'loc_index' :df_php[df_php.lemmatized.apply(lambda x: len(re.findall(i, x))) > 0].index}))
php_list.append(php)
jv = len(pd.DataFrame({'loc_index' :df_java[df_java.lemmatized.apply(lambda x: len(re.findall(i, x))) > 0].index}))
java_list.append(jv)
language_df = pd.DataFrame({'word': freq_list,
'all_languages': all_list,
'javascript': js_list,
'python': python_list,
'php': php_list,
'java': java_list
})
return language_df