forked from justmarkham/DAT5
-
Notifications
You must be signed in to change notification settings - Fork 1
/
17_ensembling_exercise.py
50 lines (36 loc) · 1.68 KB
/
17_ensembling_exercise.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
# Helper code for class 17 exercise
# define the function
def make_features(filename):
df = pd.read_csv(filename, index_col=0)
df.rename(columns={'OwnerUndeletedAnswerCountAtPostTime':'Answers'}, inplace=True)
df['TitleLength'] = df.Title.apply(len)
df['BodyLength'] = df.BodyMarkdown.apply(len)
df['NumTags'] = df.loc[:, 'Tag1':'Tag5'].notnull().sum(axis=1)
return df
# apply function to both training and testing files
train = make_features('train.csv')
test = make_features('test.csv')
# define X and y
feature_cols = ['ReputationAtPostCreation', 'Answers', 'TitleLength', 'BodyLength', 'NumTags']
X = train[feature_cols]
y = train.OpenStatus
###############################################################################
##### Create some models with the derived features
###############################################################################
###############################################################################
##### Count vectorizer
###############################################################################
# define X and y
X = train.Title
y = train.OpenStatus
# split into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)
# use CountVectorizer with the default settings
from sklearn.feature_extraction.text import CountVectorizer
vect = CountVectorizer()
# fit and transform on X_train, but only transform on X_test
train_dtm = vect.fit_transform(X_train)
test_dtm = vect.transform(X_test)
###############################################################################
##### Create a model with the text features
###############################################################################