-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathlogistic_regression_using_dataset.py
More file actions
38 lines (28 loc) · 1.14 KB
/
logistic_regression_using_dataset.py
File metadata and controls
38 lines (28 loc) · 1.14 KB
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
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import LabelEncoder
from sklearn.cross_validation import train_test_split
from sklearn.metrics import confusion_matrix
# Import the adult.txt file into Python
data = pd.read_csv('adults.txt', sep=',')
# DO NOT WORRY ABOUT THE FOLLOWING 2 LINES OF CODE
# Convert the string labels to numeric labels
for label in ['race', 'occupation']:
data[label] = LabelEncoder().fit_transform(data[label])
# Take the fields of interest and plug them into variable X
X = data[['race', 'hours_per_week', 'occupation']]
# Make sure to provide the corresponding truth value
Y = data['sex'].values.tolist()
# Split the data into test and training (30% for test)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3)
# Instantiate the classifier
clf = LogisticRegression()
# Train the classifier using the train data
clf = clf.fit(X_train, Y_train)
# Validate the classifier
accuracy = clf.score(X_test, Y_test)
print 'Accuracy: ' + str(accuracy)
# Make a confusion matrix
prediction = clf.predict(X_test)
cm = confusion_matrix(prediction, Y_test)
print cm