Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

done #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Created by https://www.gitignore.io

*.data
.DS_Store
.envrc
.direnv
### IPythonNotebook ###
# Temporary data
.ipynb_checkpoints/
Expand Down
125 changes: 125 additions & 0 deletions Spambase.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import pandas as pd\n",
"from sklearn import preprocessing, cross_validation\n",
"from sklearn.naive_bayes import MultinomialNB"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"spambase = pd.read_csv('./spambase.data', header=None)\n",
"spam2 = spambase.iloc[:, 0:54].join(spambase.iloc[:, 57]) # drop the integer values\n",
"spam3 = spambase.iloc[:, 0:47].join(spambase.iloc[:, 57]) # drop the single character values\n",
"\n",
"series = spambase.iloc[48:54].mean()\n",
"spam4 = spambase.iloc[:, 0:47]\n",
"spam4[47] = spambase.iloc[48:54].sum(1)\n",
"spam4 = spam4.join(spambase.iloc[:, 57])\n",
"#last is taking the sum over the single character value columns"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def spam_classify(spam, perc=.4, Ys=57):\n",
" X_train, X_test, Y_train, Y_test = cross_validation.train_test_split(spam.iloc[:, 0:-2], \\\n",
" spam.loc[:, 57], test_size=perc, \\\n",
" random_state = 144)\n",
" \n",
" nb = MultinomialNB()\n",
" nb.fit(X_train, Y_train)\n",
" set = nb.predict(spam.iloc[:, 0:-2])\n",
" final = sum(set)\n",
" score = nb.score(X_test, Y_test)\n",
" return round(score, 3), final, len(set) - final"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"test score, spam, non-spam\n",
"(0.81200000000000006, 2018, 2583)\n",
"(0.86699999999999999, 2239, 2362)\n",
"(0.86599999999999999, 2230, 2371)\n",
"(0.86599999999999999, 2230, 2371)\n"
]
}
],
"source": [
"print('test score, spam, non-spam')\n",
"print(spam_classify(spambase))\n",
"print(spam_classify(spam2))\n",
"print(spam_classify(spam3))\n",
"print(spam_classify(spam4))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"original values were\n",
" - spam: 1813\n",
" - non-spam: 2788\n",
" \n",
"# Overfitting achieved"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
ipython[notebook]
scikit-learn
scipy
pandas
numpy
matplotlib
seaborn