Created
March 20, 2017 10:37
-
-
Save zymiboxpay/2e4782030f3c622f29d3c964c58b3590 to your computer and use it in GitHub Desktop.
machine_learning_with_python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Accuracy:87.0%.\n" | |
] | |
} | |
], | |
"source": [ | |
"from matplotlib import pyplot as plt\n", | |
"import numpy as np\n", | |
"from sklearn.datasets import load_iris\n", | |
"\n", | |
"data = load_iris()\n", | |
"features = data.data\n", | |
"feature_names = data.feature_names\n", | |
"target = data.target\n", | |
"target_names = data.target_names\n", | |
"\n", | |
"labels = target_names[target]\n", | |
"\n", | |
"plength = features[:, 2]\n", | |
"\n", | |
"is_setosa = (labels == 'setosa')\n", | |
"max_setosa = plength[is_setosa].max()\n", | |
"min_non_setosa = plength[~is_setosa].min()\n", | |
"# print('Maximum of setosa: {0}.'.format(max_setosa))\n", | |
"# print('Minimum of others: {0}.'.format(min_non_setosa))\n", | |
"\n", | |
"features = features[~is_setosa]\n", | |
"labels = labels[~is_setosa]\n", | |
"is_virginica = (labels == 'virginica')\n", | |
"\n", | |
"def fit_model(features, labels):\n", | |
" best_acc = -1.0\n", | |
" for fi in range(features.shape[1]):\n", | |
" \n", | |
" thresh = features[:, fi]\n", | |
" for t in thresh:\n", | |
" feature_i = features[:, fi]\n", | |
" pred = (feature_i > t)\n", | |
" acc = (pred == labels).mean()\n", | |
" rev_acc = (pred == ~labels).mean()\n", | |
" if rev_acc > acc:\n", | |
" reverse = True\n", | |
" acc = rev_acc\n", | |
" else:\n", | |
" reverse = False\n", | |
" if acc > best_acc:\n", | |
" best_acc = acc\n", | |
" best_fi = fi\n", | |
" best_t = t\n", | |
" best_reverse = reverse\n", | |
" return best_t, best_fi, best_reverse\n", | |
"\n", | |
"def predict(model, features):\n", | |
" t, fi, reverse = model\n", | |
" if reverse:\n", | |
" return features[:, fi] <= t\n", | |
" else:\n", | |
" return features[:, fi] > t\n", | |
" \n", | |
"correct = 0.0\n", | |
"for ei in range(len(features)):\n", | |
" training = np.ones(len(features),bool)\n", | |
" training[ei] = False\n", | |
" testing = ~training\n", | |
" model = fit_model(features[training], is_virginica[training])\n", | |
" predictions = predict(model, features[testing])\n", | |
" correct += np.sum(predictions == is_virginica[testing])\n", | |
"acc = correct/float(len(features))\n", | |
"print('Accuracy:{0:.1%}.'.format(acc))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Mean accuracy: 93.0%\n" | |
] | |
} | |
], | |
"source": [ | |
"from sklearn.neighbors import KNeighborsClassifier\n", | |
"from sklearn.cross_validation import KFold\n", | |
"\n", | |
"classifier = KNeighborsClassifier(n_neighbors = 1)\n", | |
"\n", | |
"kf = KFold(len(features), n_folds=5, shuffle=True)\n", | |
"\n", | |
"means = []\n", | |
"for training, testing in kf:\n", | |
" classifier.fit(features[training], labels[training])\n", | |
" prediction = classifier.predict(features[testing])\n", | |
" curmean = np.mean(prediction == labels[testing])\n", | |
" means.append(curmean)\n", | |
"\n", | |
"print('Mean accuracy: {:.1%}'.format(np.mean(means)))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Mean accuracy: 94.0%\n" | |
] | |
} | |
], | |
"source": [ | |
"from sklearn.pipeline import Pipeline\n", | |
"from sklearn.preprocessing import StandardScaler\n", | |
"\n", | |
"classifier = KNeighborsClassifier(n_neighbors = 1)\n", | |
"classifier = Pipeline([('norm', StandardScaler()), ('knn', classifier)])\n", | |
"\n", | |
"kf = KFold(len(features), n_folds=5, shuffle=True)\n", | |
"\n", | |
"means = []\n", | |
"for training, testing in kf:\n", | |
" classifier.fit(features[training], labels[training])\n", | |
" prediction = classifier.predict(features[testing])\n", | |
" curmean = np.mean(prediction == labels[testing])\n", | |
" means.append(curmean)\n", | |
"\n", | |
"print('Mean accuracy: {:.1%}'.format(np.mean(means)))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"ename": "SyntaxError", | |
"evalue": "invalid syntax (<ipython-input-6-3d88389e28e4>, line 10)", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-6-3d88389e28e4>\"\u001b[0;36m, line \u001b[0;32m10\u001b[0m\n\u001b[0;31m return lambda doc: (english_stemmer.stem(w)) for w in analyzer(doc)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" | |
] | |
} | |
], | |
"source": [ | |
"import os\n", | |
"import sys\n", | |
"import scipy as sp\n", | |
"from sklearn.feature_extraction.text import CountVectorizer\n", | |
"import nltk.stem\n", | |
"english_stemmer = nltk.stem.SnowballStemmer('english')\n", | |
"class StemmedCountVectorizer(CountVectorizer):\n", | |
" def build_analyzer(self):\n", | |
" analyzer = super(StemmedCountVectorizer, self).build_analyzer()\n", | |
" return lambda doc: (english_stemmer.stem(w) for w in analyzer(doc))\n", | |
" \n", | |
"\n", | |
"vectorizer = StemmedCountVectorizer(min_df=1, stop_words='english')\n", | |
"DIR = \"/home/zym/learn/machine_learning/BuildingMachineLearningSystemsWithPython/ch03/data/toy\"\n", | |
"posts = [open(os.path.join(DIR, f)).read() for f in os.listdir(DIR)]\n", | |
"X_train = vectorizer.fit_transform(posts)\n", | |
"num_samples, num_features = X_train.shape\n", | |
"\n", | |
"# print(\"#samples: %d, #features: %d\" % (num_samples, num_features))\n", | |
"# print(vectorizer.get_feature_names())\n", | |
"\n", | |
"new_post = \"imaging databases\"\n", | |
"new_post_vec = vectorizer.transform([new_post])\n", | |
"\n", | |
"def dist_raw(v1, v2):\n", | |
" delta = v1 - v2\n", | |
" return sp.linalg.norm(delta.toarray())\n", | |
"\n", | |
"def dist_norm(v1, v2):\n", | |
" v1_normalized = v1/sp.linalg.norm(v1.toarray())\n", | |
" v2_normalized = v2/sp.linalg.norm(v2.toarray())\n", | |
" delta = v1_normalized - v2_normalized\n", | |
" return sp.linalg.norm(delta.toarray())\n", | |
"\n", | |
"best_doc = None\n", | |
"best_dist = sys.maxsize\n", | |
"best_i = None\n", | |
"for i, post in enumerate(posts):\n", | |
" if post == new_post:\n", | |
" continue\n", | |
" post_vec = X_train.getrow(i)\n", | |
" d = dist_norm(post_vec, new_post_vec)\n", | |
" print('=== Post %i with dist=%.2f: %s' % (i, d, post))\n", | |
" if d < best_dist:\n", | |
" best_dist = d\n", | |
" best_i = i\n", | |
"\n", | |
"print(\"Best post is %i with dist=%.2f\" % (best_i, best_dist))\n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"#samples: 3529, #features: 4712\n", | |
"Initialization complete\n", | |
"Iteration 0, inertia 5899.560\n", | |
"Iteration 1, inertia 3218.298\n", | |
"Iteration 2, inertia 3184.333\n", | |
"Iteration 3, inertia 3164.867\n", | |
"Iteration 4, inertia 3152.004\n", | |
"Iteration 5, inertia 3143.111\n", | |
"Iteration 6, inertia 3136.256\n", | |
"Iteration 7, inertia 3129.325\n", | |
"Iteration 8, inertia 3124.567\n", | |
"Iteration 9, inertia 3121.900\n", | |
"Iteration 10, inertia 3120.210\n", | |
"Iteration 11, inertia 3118.627\n", | |
"Iteration 12, inertia 3117.363\n", | |
"Iteration 13, inertia 3116.811\n", | |
"Iteration 14, inertia 3116.588\n", | |
"Iteration 15, inertia 3116.417\n", | |
"Iteration 16, inertia 3115.760\n", | |
"Iteration 17, inertia 3115.374\n", | |
"Iteration 18, inertia 3115.155\n", | |
"Iteration 19, inertia 3114.949\n", | |
"Iteration 20, inertia 3114.515\n", | |
"Iteration 21, inertia 3113.937\n", | |
"Iteration 22, inertia 3113.720\n", | |
"Iteration 23, inertia 3113.548\n", | |
"Iteration 24, inertia 3113.475\n", | |
"Iteration 25, inertia 3113.447\n", | |
"Converged at iteration 25\n", | |
"['ac', 'birmingham', 'host', 'kingdom', 'nntp', 'sorri', 'test', 'uk', 'unit', 'univers']\n" | |
] | |
} | |
], | |
"source": [ | |
"import os\n", | |
"import sys\n", | |
"import scipy as sp\n", | |
"from sklearn.feature_extraction.text import TfidfVectorizer\n", | |
"import nltk.stem\n", | |
"english_stemmer = nltk.stem.SnowballStemmer('english')\n", | |
"class StemmedTfidfVectorizer(TfidfVectorizer):\n", | |
" def build_analyzer(self):\n", | |
" analyzer = super(StemmedTfidfVectorizer, self).build_analyzer()\n", | |
" return lambda doc: (english_stemmer.stem(w) for w in analyzer(doc))\n", | |
"\n", | |
"import sklearn.datasets\n", | |
"all_data = sklearn.datasets.fetch_20newsgroups(subset='all')\n", | |
"# print(len(all_data.filenames))\n", | |
"\n", | |
"groups = ['comp.graphics', 'comp.os.ms-windows.misc','comp.sys.ibm.pc.hardware', \n", | |
" 'comp.sys.mac.hardware', 'comp.windows.x','sci.space']\n", | |
"\n", | |
"train_data = sklearn.datasets.fetch_20newsgroups(subset='train', categories=groups)\n", | |
"test_data = sklearn.datasets.fetch_20newsgroups(subset='test', categories=groups)\n", | |
"\n", | |
"# print(len(train_data.filenames))\n", | |
"# print(len(test_data.filenames))\n", | |
"vectorizer = StemmedTfidfVectorizer(min_df=10, max_df=0.5, stop_words='english', decode_error='ignore')\n", | |
"vectorized = vectorizer.fit_transform(train_data.data)\n", | |
"num_samples, num_features = vectorized.shape\n", | |
"\n", | |
"print(\"#samples: %d, #features: %d\" % (num_samples, num_features))\n", | |
"\n", | |
"num_clusters = 50\n", | |
"from sklearn.cluster import KMeans\n", | |
"km = KMeans(n_clusters=num_clusters, init='random', n_init=1, verbose=1, random_state=3)\n", | |
"km.fit(vectorized)\n", | |
"\n", | |
"new_post = \"\"\"\n", | |
"Disk drive problems. Hi, I have a problem with my hard disk.\n", | |
"After 1 year it is working only sporadically now.\n", | |
"tried to format it, but now it doesn’t boot any more.\n", | |
"Any ideas? Thanks.\n", | |
"\"\"\"\n", | |
"\n", | |
"new_post_vec = vectorizer.transform([new_post])\n", | |
"new_post_label = km.predict(new_post_vec)[0]\n", | |
"\n", | |
"similar_indices = (km.labels_ == new_post_label).nonzero()[0]\n", | |
"\n", | |
"similar = []\n", | |
"for i in similar_indices:\n", | |
" dist = sp.linalg.norm((new_post_vec - vectorized[i]).toarray())\n", | |
" similar.append((dist, train_data.data[i]))\n", | |
" \n", | |
"similar = sorted(similar)\n", | |
"# print(len(similar))\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['ac', 'birmingham', 'host', 'kingdom', 'nntp', 'sorri', 'test', 'uk', 'unit', 'univers']\n" | |
] | |
} | |
], | |
"source": [ | |
"post_group = zip(train_data.data, train_data.target)\n", | |
"all = [(len(post[0]), post[0], train_data.target_names[post[1]]) for post in post_group]\n", | |
"\n", | |
"graphics = sorted([post for post in all if post[2] == 'comp.graphics'])\n", | |
"# print(graphics[5])\n", | |
"\n", | |
"noise_post = graphics[5][1]\n", | |
"analyzer = vectorizer.build_analyzer()\n", | |
"# print(list(analyzer(noise_post)))\n", | |
"useful = set(analyzer(noise_post)).intersection(vectorizer.get_feature_names())\n", | |
"\n", | |
"print(sorted(useful))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"met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klTF0CBJkqoYGiRJUhVDgyRJqvL/\nASbi2o2juOxoAAAAAElFTkSuQmCC\n", | |
"text/plain": [ | |
"<matplotlib.figure.Figure at 0x7f7d54fd8080>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"import os\n", | |
"from matplotlib import pyplot as plt\n", | |
"from gensim import corpora, models\n", | |
"DIR = '/home/zym/learn/machine_learning/BuildingMachineLearningSystemsWithPython/ch04/ap'\n", | |
"corpus = corpora.BleiCorpus(os.path.join(DIR, 'ap.dat'), os.path.join(DIR, 'vocab.txt'))\n", | |
"\n", | |
"model = models.ldamodel.LdaModel(corpus, num_topics=100,id2word=corpus.id2word)\n", | |
"doc = corpus.docbyoffset(0)\n", | |
"topics = model[doc]\n", | |
"# print(topics)\n", | |
"\n", | |
"num_topics_used = [len(model[doc]) for doc in corpus]\n", | |
"plt.hist(num_topics_used)\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Topic #0: \n", | |
"windows thanks file edu use drive software mail help does card pc know hi using need program problem looking window\n", | |
"Topic #1: \n", | |
"dog attack head drive talking human maybe disk printer drivers window problem mac advance thanks use hard work ii windows\n", | |
"Topic #2: \n", | |
"god accept read clock driving stuff nature port think error hard usually 60 computer faith know following limited peter control\n", | |
"Topic #3: \n", | |
"pp 22 18 19 11 23 26 55 van 10 31 44 27 21 38 34 48 24 armenian 37\n", | |
"Topic #4: \n", | |
"2nd math ground value said leafs display long try lunar launch monitor space version shuttle luck computer door probe moon\n", | |
"Topic #5: \n", | |
"cases 00 soon edu sale condition effective asking consider good general john stuff nhl card san cd deleted end hear\n", | |
"Topic #6: \n", | |
"mike love graphics hear heard try state time looking good performance windows marriage os crime company parts program comp men\n", | |
"Topic #7: \n", | |
"assume magi home order card right lot supposed hit better thing especially week record require use ground important course today\n", | |
"Topic #8: \n", | |
"just don people like think know good god time new does way say right make did really want year use\n", | |
"Topic #9: \n", | |
"wanted 1992 gm season 1993 wouldn vs john 30 price st want condition just excellent sale wrong file 10 really\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"from __future__ import print_function\n", | |
"from time import time\n", | |
"\n", | |
"from sklearn.feature_extraction.text import TfidfVectorizer,CountVectorizer\n", | |
"from sklearn.decomposition import LatentDirichletAllocation\n", | |
"from sklearn.datasets import fetch_20newsgroups\n", | |
"\n", | |
"n_samples = 2000\n", | |
"n_features = 1000\n", | |
"n_topics = 10\n", | |
"n_top_words = 20\n", | |
"\n", | |
"def print_top_words(model, feature_names, n_top_words):\n", | |
" for topic_idx, topic in enumerate(model.components_):\n", | |
" print(\"Topic #%d: \" % topic_idx)\n", | |
" print(\" \".join([feature_names[i]\n", | |
" for i in topic.argsort()[:-n_top_words - 1: -1]]))\n", | |
" print()\n", | |
" \n", | |
"dataset = fetch_20newsgroups(shuffle=True, random_state=1,\n", | |
" remove=('headers', 'footers', 'quotes'))\n", | |
"data_samples = dataset.data[:n_samples]\n", | |
"tfidf_vectorizer = TfidfVectorizer(max_df=0.95, min_df=2,\n", | |
" max_features=n_features,\n", | |
" stop_words='english')\n", | |
"tfidf = tfidf_vectorizer.fit_transform(data_samples)\n", | |
"\n", | |
"\n", | |
"lda = LatentDirichletAllocation(n_topics=n_topics, max_iter=5,\n", | |
" learning_method='online',\n", | |
" learning_offset=50.,\n", | |
" random_state=0)\n", | |
"\n", | |
"lda.fit(tfidf)\n", | |
"\n", | |
"tfidf_feature_names = tfidf_vectorizer.get_feature_names()\n", | |
"print_top_words(lda, tfidf_feature_names, n_top_words)\n" | |
] | |
} | |
], | |
"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.5.1+" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment