├── .gitignore ├── Appendix A ├── auto_coloring.ipynb └── map.png ├── Chapter 1 └── Chapter1.ipynb ├── Chapter 10 ├── bag_of_words.ipynb ├── category_predictor.ipynb ├── chunker.ipynb ├── data.txt ├── gender_identifier.ipynb ├── lemmatizer.ipynb ├── sentiment_analyzer.ipynb ├── stemmer.ipynb ├── text_chunker.ipynb ├── tokenizer.ipynb └── topic_modeler.ipynb ├── Chapter 11 ├── crf.ipynb ├── data_1D.txt ├── data_2D.txt ├── hmm.ipynb ├── operator.ipynb ├── read_data.ipynb ├── slicer.ipynb ├── stats_extractor.ipynb ├── stock_market.ipynb └── timeseries.ipynb ├── Chapter 12 ├── LICENSE.txt ├── audio_generator.ipynb ├── audio_plotter.ipynb ├── data │ ├── apple │ │ ├── apple01.wav │ │ ├── apple02.wav │ │ ├── apple03.wav │ │ ├── apple04.wav │ │ ├── apple05.wav │ │ ├── apple06.wav │ │ ├── apple07.wav │ │ ├── apple08.wav │ │ ├── apple09.wav │ │ ├── apple10.wav │ │ ├── apple11.wav │ │ ├── apple12.wav │ │ ├── apple13.wav │ │ ├── apple14.wav │ │ └── apple15.wav │ ├── banana │ │ ├── banana01.wav │ │ ├── banana02.wav │ │ ├── banana03.wav │ │ ├── banana04.wav │ │ ├── banana05.wav │ │ ├── banana06.wav │ │ ├── banana07.wav │ │ ├── banana08.wav │ │ ├── banana09.wav │ │ ├── banana10.wav │ │ ├── banana11.wav │ │ ├── banana12.wav │ │ ├── banana13.wav │ │ ├── banana14.wav │ │ └── banana15.wav │ ├── kiwi │ │ ├── kiwi01.wav │ │ ├── kiwi02.wav │ │ ├── kiwi03.wav │ │ ├── kiwi04.wav │ │ ├── kiwi05.wav │ │ ├── kiwi06.wav │ │ ├── kiwi07.wav │ │ ├── kiwi08.wav │ │ ├── kiwi09.wav │ │ ├── kiwi10.wav │ │ ├── kiwi11.wav │ │ ├── kiwi12.wav │ │ ├── kiwi13.wav │ │ ├── kiwi14.wav │ │ └── kiwi15.wav │ ├── lime │ │ ├── lime01.wav │ │ ├── lime02.wav │ │ ├── lime03.wav │ │ ├── lime04.wav │ │ ├── lime05.wav │ │ ├── lime06.wav │ │ ├── lime07.wav │ │ ├── lime08.wav │ │ ├── lime09.wav │ │ ├── lime10.wav │ │ ├── lime11.wav │ │ ├── lime12.wav │ │ ├── lime13.wav │ │ ├── lime14.wav │ │ └── lime15.wav │ ├── orange │ │ ├── orange01.wav │ │ ├── orange02.wav │ │ ├── orange03.wav │ │ ├── orange04.wav │ │ ├── orange05.wav │ │ ├── orange06.wav │ │ ├── orange07.wav │ │ ├── orange08.wav │ │ ├── orange09.wav │ │ ├── orange10.wav │ │ ├── orange11.wav │ │ ├── orange12.wav │ │ ├── orange13.wav │ │ ├── orange14.wav │ │ └── orange15.wav │ ├── peach │ │ ├── peach01.wav │ │ ├── peach02.wav │ │ ├── peach03.wav │ │ ├── peach04.wav │ │ ├── peach05.wav │ │ ├── peach06.wav │ │ ├── peach07.wav │ │ ├── peach08.wav │ │ ├── peach09.wav │ │ ├── peach10.wav │ │ ├── peach11.wav │ │ ├── peach12.wav │ │ ├── peach13.wav │ │ ├── peach14.wav │ │ └── peach15.wav │ └── pineapple │ │ ├── pineapple01.wav │ │ ├── pineapple02.wav │ │ ├── pineapple03.wav │ │ ├── pineapple04.wav │ │ ├── pineapple05.wav │ │ ├── pineapple06.wav │ │ ├── pineapple07.wav │ │ ├── pineapple08.wav │ │ ├── pineapple09.wav │ │ ├── pineapple10.wav │ │ ├── pineapple11.wav │ │ ├── pineapple12.wav │ │ ├── pineapple13.wav │ │ ├── pineapple14.wav │ │ └── pineapple15.wav ├── feature_extractor.ipynb ├── frequency_transformer.ipynb ├── python_speech_features │ ├── LICENSE │ ├── __init__.py │ ├── base.py │ └── sigproc.py ├── random_sound.wav ├── speech_recognizer.ipynb ├── spoken_word.wav └── synthesizer.ipynb ├── Chapter 13 ├── LICENSE.txt ├── background_subtraction.ipynb ├── bbb.mp4 ├── camshift.ipynb ├── colorspaces.ipynb ├── ed.mp4 ├── eye_detector.ipynb ├── face_detector.ipynb ├── frame_diff.ipynb ├── haar_cascade_files │ ├── haarcascade_eye.xml │ └── haarcascade_frontalface_default.xml ├── ipython_show_image.ipynb ├── ipython_video_play.ipynb └── optical_flow.ipynb ├── Chapter 14 ├── LICENSE ├── character_visualizer.ipynb ├── data_perceptron.txt ├── data_simple_nn.txt ├── data_vector_quantization.txt ├── letter.data ├── multilayer_neural_network.ipynb ├── ocr.ipynb ├── perceptron_classifier.ipynb ├── recurrent_neural_network.ipynb ├── simple_neural_network.ipynb └── vector_quantizer.ipynb ├── Chapter 15 ├── agent1.ipynb ├── agent2.ipynb ├── balancer.ipynb └── run_environment.ipynb ├── Chapter 16 ├── cnn.ipynb ├── cnn_keras.ipynb ├── linear_regression.ipynb ├── linear_regression_keras.ipynb ├── single_layer.ipynb └── single_layer_keras.ipynb ├── Chapter 2 ├── LICENSE.txt ├── confusion_matrix.ipynb ├── data_multivar_nb.txt ├── data_multivar_regr.txt ├── data_singlevar_regr.txt ├── house_prices.ipynb ├── income_classifier.ipynb ├── income_data.txt ├── label_encoder.ipynb ├── logistic_regression.ipynb ├── naive_bayes.ipynb ├── preprocessing.ipynb ├── regressor_multivar.ipynb ├── regressor_polynomial.ipynb ├── regressor_singlevar.ipynb └── utilities.ipynb ├── Chapter 3 ├── LICENSE.txt ├── class_imbalance.ipynb ├── data_decision_trees.txt ├── data_imbalance.txt ├── data_random_forests.txt ├── decision_trees.ipynb ├── feature_importance.ipynb ├── random_forests.ipynb ├── random_forests.py ├── run_grid_search.ipynb ├── traffic_data.txt ├── traffic_prediction.ipynb └── utilities.ipynb ├── Chapter 4 ├── clustering_quality.ipynb ├── company_symbol_mapping.json ├── data_clustering.txt ├── data_quality.txt ├── gmm_classifier.ipynb ├── kmeans.ipynb ├── market_segmentation.ipynb ├── mean_shift.ipynb ├── sales.csv └── stocks.ipynb ├── Chapter 5 ├── data.txt ├── k_nearest_neighbors.ipynb ├── movie_recommender.ipynb ├── nearest_neighbors_classifier.ipynb ├── pipeline_trainer.ipynb └── ratings.json ├── Chapter 6 ├── adjacent_states.txt ├── coastal_states.txt ├── expression_matcher.ipynb ├── family.ipynb ├── prime.ipynb ├── puzzle.ipynb ├── relationships.json └── states.ipynb ├── Chapter 7 ├── coloring.ipynb ├── constrained_problem.ipynb ├── greedy_search.ipynb ├── maze.ipynb └── puzzle.ipynb ├── Chapter 8 ├── bit_counter.ipynb ├── robot.ipynb ├── symbol_regression.ipynb ├── target_map.txt └── visualization.ipynb ├── Chapter 9 ├── coins.ipynb ├── connect_four.ipynb ├── hexapawn.ipynb └── tic_tac_toe.ipynb ├── README.md └── artificial-intelligence-with-python-ja.png /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *~ 3 | .ipynb_checkpoints 4 | __pycache__ 5 | -------------------------------------------------------------------------------- /Appendix A/map.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Appendix A/map.png -------------------------------------------------------------------------------- /Chapter 1/Chapter1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from sklearn import datasets\n", 10 | "house_prices = datasets.load_boston()\n", 11 | "print(house_prices.data)" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": null, 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "print(house_prices.target)" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": null, 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "digits = datasets.load_digits()\n", 30 | "print(digits.images[4])" 31 | ] 32 | } 33 | ], 34 | "metadata": { 35 | "kernelspec": { 36 | "display_name": "Python 3", 37 | "language": "python", 38 | "name": "python3" 39 | }, 40 | "language_info": { 41 | "codemirror_mode": { 42 | "name": "ipython", 43 | "version": 3 44 | }, 45 | "file_extension": ".py", 46 | "mimetype": "text/x-python", 47 | "name": "python", 48 | "nbconvert_exporter": "python", 49 | "pygments_lexer": "ipython3", 50 | "version": "3.6.7" 51 | } 52 | }, 53 | "nbformat": 4, 54 | "nbformat_minor": 2 55 | } 56 | -------------------------------------------------------------------------------- /Chapter 10/bag_of_words.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "scrolled": false 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "%run chunker.ipynb\n", 12 | "from sklearn.feature_extraction.text import CountVectorizer \n", 13 | "from nltk.corpus import brown \n", 14 | "\n", 15 | "input_data = ' '.join(brown.words()[:5400]) \n", 16 | "\n", 17 | "chunk_size = 800 \n", 18 | "text_chunks = chunker(input_data, chunk_size) \n", 19 | "\n", 20 | "count_vectorizer = CountVectorizer(min_df=7, max_df=20) \n", 21 | "document_term_matrix = count_vectorizer.fit_transform(text_chunks) \n", 22 | "\n", 23 | "vocabulary = count_vectorizer.get_feature_names() \n", 24 | "print(\"Vocabulary:\\n\", vocabulary) " 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": null, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "print(\"Document term matrix:\") \n", 34 | "fmt = '{:>8} '\n", 35 | "for v in vocabulary:\n", 36 | " fmt += '{{:>{}}} '.format(len(v))\n", 37 | "print(fmt.format('Document', *vocabulary))\n", 38 | "for i, item in enumerate(document_term_matrix.toarray()): \n", 39 | " print(fmt.format('Chunk-' + str(i+1), *item.data)) " 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": null, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [] 48 | } 49 | ], 50 | "metadata": { 51 | "kernelspec": { 52 | "display_name": "Python 3", 53 | "language": "python", 54 | "name": "python3" 55 | }, 56 | "language_info": { 57 | "codemirror_mode": { 58 | "name": "ipython", 59 | "version": 3 60 | }, 61 | "file_extension": ".py", 62 | "mimetype": "text/x-python", 63 | "name": "python", 64 | "nbconvert_exporter": "python", 65 | "pygments_lexer": "ipython3", 66 | "version": "3.6.7" 67 | } 68 | }, 69 | "nbformat": 4, 70 | "nbformat_minor": 2 71 | } 72 | -------------------------------------------------------------------------------- /Chapter 10/category_predictor.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from sklearn.datasets import fetch_20newsgroups \n", 10 | "from sklearn.naive_bayes import MultinomialNB \n", 11 | "from sklearn.feature_extraction.text import TfidfTransformer \n", 12 | "from sklearn.feature_extraction.text import CountVectorizer \n", 13 | "\n", 14 | "category_map = {'talk.politics.misc': 'Politics',\n", 15 | " 'rec.autos': 'Autos', 'rec.sport.hockey': 'Hockey',\n", 16 | " 'sci.electronics': 'Electronics', 'sci.med': 'Medicine'} \n", 17 | "\n", 18 | "training_data = fetch_20newsgroups(subset='train', \n", 19 | " categories=category_map.keys(), shuffle=True, random_state=5) \n", 20 | "\n", 21 | "count_vectorizer = CountVectorizer() \n", 22 | "train_tc = count_vectorizer.fit_transform(training_data.data) \n", 23 | "print(\"Dimensions of training data:\", train_tc.shape) \n" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": null, 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "tfidf = TfidfTransformer() \n", 33 | "train_tfidf = tfidf.fit_transform(train_tc) \n", 34 | "classifier = MultinomialNB().fit(train_tfidf, training_data.target) \n", 35 | "\n", 36 | "input_data = [ \n", 37 | " 'You need to be careful with cars when you are driving on slippery roads', \n", 38 | " 'A lot of devices can be operated wirelessly', \n", 39 | " 'Players need to be careful when they are close to goal posts', \n", 40 | " 'Political debates help us understand the perspectives of both sides' \n", 41 | "] \n", 42 | "input_tc = count_vectorizer.transform(input_data) \n", 43 | "input_tfidf = tfidf.transform(input_tc) \n", 44 | "\n", 45 | "predictions = classifier.predict(input_tfidf) \n", 46 | "for sent, category in zip(input_data, predictions): \n", 47 | " print('Input:', sent, '\\nPredicted category:',\n", 48 | " category_map[training_data.target_names[category]]) " 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [] 57 | } 58 | ], 59 | "metadata": { 60 | "kernelspec": { 61 | "display_name": "Python 3", 62 | "language": "python", 63 | "name": "python3" 64 | }, 65 | "language_info": { 66 | "codemirror_mode": { 67 | "name": "ipython", 68 | "version": 3 69 | }, 70 | "file_extension": ".py", 71 | "mimetype": "text/x-python", 72 | "name": "python", 73 | "nbconvert_exporter": "python", 74 | "pygments_lexer": "ipython3", 75 | "version": "3.6.7" 76 | } 77 | }, 78 | "nbformat": 4, 79 | "nbformat_minor": 2 80 | } 81 | -------------------------------------------------------------------------------- /Chapter 10/chunker.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def chunker(input_data, N): \n", 10 | " input_words = input_data.split(' ') \n", 11 | " output = [] \n", 12 | " while len(input_words) > N:\n", 13 | " output.append(' '.join(input_words[:N]))\n", 14 | " input_words = input_words[N:]\n", 15 | " output.append(' '.join(input_words))\n", 16 | " return output " 17 | ] 18 | } 19 | ], 20 | "metadata": { 21 | "kernelspec": { 22 | "display_name": "Python 3", 23 | "language": "python", 24 | "name": "python3" 25 | }, 26 | "language_info": { 27 | "codemirror_mode": { 28 | "name": "ipython", 29 | "version": 3 30 | }, 31 | "file_extension": ".py", 32 | "mimetype": "text/x-python", 33 | "name": "python", 34 | "nbconvert_exporter": "python", 35 | "pygments_lexer": "ipython3", 36 | "version": "3.6.7" 37 | } 38 | }, 39 | "nbformat": 4, 40 | "nbformat_minor": 2 41 | } 42 | -------------------------------------------------------------------------------- /Chapter 10/data.txt: -------------------------------------------------------------------------------- 1 | The Roman empire expanded very rapidly and it was the biggest empire in the world for a long time. 2 | An algebraic structure is a set with one or more finitary operations defined on it that satisfies a list of axioms. 3 | Renaissance started as a cultural movement in Italy in the Late Medieval period and later spread to the rest of Europe. 4 | The line of demarcation between prehistoric and historical times is crossed when people cease to live only in the present. 5 | Mathematicians seek out patterns and use them to formulate new conjectures. 6 | A notational symbol that represents a number is called a numeral in mathematics. 7 | The process of extracting the underlying essence of a mathematical concept is called abstraction. 8 | Historically, people have frequently waged wars against each other in order to expand their empires. 9 | Ancient history indicates that various outside influences have helped formulate the culture and traditions of Eastern Europe. 10 | Mappings between sets which preserve structures are of special interest in many fields of mathematics. -------------------------------------------------------------------------------- /Chapter 10/gender_identifier.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import random \n", 10 | "\n", 11 | "from nltk import NaiveBayesClassifier \n", 12 | "from nltk.classify import accuracy as nltk_accuracy \n", 13 | "from nltk.corpus import names \n", 14 | "\n", 15 | "def extract_features(word, N=2): \n", 16 | " last_n_letters = word[-N:] \n", 17 | " return {'feature': last_n_letters.lower()} \n", 18 | "\n", 19 | "male_list = [(name, 'male') for name in names.words('male.txt')] \n", 20 | "female_list = [(name, 'female') for name in names.words('female.txt')] \n", 21 | "data = (male_list + female_list) \n", 22 | "num_train = int(0.8 * len(data))\n", 23 | "\n", 24 | "random.seed(5) \n", 25 | "random.shuffle(data) \n", 26 | "\n", 27 | "for i in range(1, 6): \n", 28 | " print('\\nNumber of end letters:', i) \n", 29 | " features = [(extract_features(n, i), gender) for (n, gender) in data] \n", 30 | " train_data, test_data = features[:num_train], features[num_train:] \n", 31 | " classifier = NaiveBayesClassifier.train(train_data) \n", 32 | "\n", 33 | " accuracy = round(100 * nltk_accuracy(classifier, test_data), 2) \n", 34 | " print('Accuracy = ' + str(accuracy) + '%') \n", 35 | "\n", 36 | " input_names = ['Alexander', 'Danielle', 'David', 'Cheryl'] \n", 37 | " for name in input_names: \n", 38 | " print(name, '==>', classifier.classify(extract_features(name, i))) \n", 39 | " " 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": null, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [] 48 | } 49 | ], 50 | "metadata": { 51 | "kernelspec": { 52 | "display_name": "Python 3", 53 | "language": "python", 54 | "name": "python3" 55 | }, 56 | "language_info": { 57 | "codemirror_mode": { 58 | "name": "ipython", 59 | "version": 3 60 | }, 61 | "file_extension": ".py", 62 | "mimetype": "text/x-python", 63 | "name": "python", 64 | "nbconvert_exporter": "python", 65 | "pygments_lexer": "ipython3", 66 | "version": "3.6.7" 67 | } 68 | }, 69 | "nbformat": 4, 70 | "nbformat_minor": 2 71 | } 72 | -------------------------------------------------------------------------------- /Chapter 10/lemmatizer.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "scrolled": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "from nltk.stem import WordNetLemmatizer \n", 12 | "\n", 13 | "input_words = ['writing', 'calves', 'be', 'branded', 'horse', 'randomize', \n", 14 | " 'possibly', 'provision', 'hospital', 'kept', 'scratchy', 'code'] \n", 15 | "\n", 16 | "lemmatizer = WordNetLemmatizer() \n", 17 | "\n", 18 | "lemmatizer_names = ['INPUT WORD', 'NOUN LEMMATIZER', 'VERB LEMMATIZER'] \n", 19 | "fmt = '{:>24}' * len(lemmatizer_names)\n", 20 | "print(fmt.format(*lemmatizer_names))\n", 21 | "print('=' * 75) \n", 22 | "\n", 23 | "for word in input_words: \n", 24 | " output = [word, lemmatizer.lemmatize(word, pos='n'), \n", 25 | " lemmatizer.lemmatize(word, pos='v')] \n", 26 | " print(fmt.format(*output)) " 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [] 35 | } 36 | ], 37 | "metadata": { 38 | "kernelspec": { 39 | "display_name": "Python 3", 40 | "language": "python", 41 | "name": "python3" 42 | }, 43 | "language_info": { 44 | "codemirror_mode": { 45 | "name": "ipython", 46 | "version": 3 47 | }, 48 | "file_extension": ".py", 49 | "mimetype": "text/x-python", 50 | "name": "python", 51 | "nbconvert_exporter": "python", 52 | "pygments_lexer": "ipython3", 53 | "version": "3.6.7" 54 | } 55 | }, 56 | "nbformat": 4, 57 | "nbformat_minor": 2 58 | } 59 | -------------------------------------------------------------------------------- /Chapter 10/stemmer.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from nltk.stem.porter import PorterStemmer \n", 10 | "from nltk.stem.lancaster import LancasterStemmer \n", 11 | "from nltk.stem.snowball import SnowballStemmer \n", 12 | "\n", 13 | "input_words = ['writing', 'calves', 'be', 'branded', 'horse', 'randomize', \n", 14 | " 'possibly', 'provision', 'hospital', 'kept', 'scratchy', 'code'] \n", 15 | "\n", 16 | "porter = PorterStemmer() \n", 17 | "lancaster = LancasterStemmer() \n", 18 | "snowball = SnowballStemmer('english') \n", 19 | "\n", 20 | "stemmer_names = ['INPUT WORD', 'PORTER', 'LANCASTER', 'SNOWBALL'] \n", 21 | "fmt = '{:>16}' * len(stemmer_names)\n", 22 | "print(fmt.format(*stemmer_names))\n", 23 | "print('=' * 68)\n", 24 | "\n", 25 | "for word in input_words: \n", 26 | " output = [word, porter.stem(word), lancaster.stem(word), \n", 27 | " snowball.stem(word)] \n", 28 | " print(fmt.format(*output)) " 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": null, 34 | "metadata": {}, 35 | "outputs": [], 36 | "source": [] 37 | } 38 | ], 39 | "metadata": { 40 | "kernelspec": { 41 | "display_name": "Python 3", 42 | "language": "python", 43 | "name": "python3" 44 | }, 45 | "language_info": { 46 | "codemirror_mode": { 47 | "name": "ipython", 48 | "version": 3 49 | }, 50 | "file_extension": ".py", 51 | "mimetype": "text/x-python", 52 | "name": "python", 53 | "nbconvert_exporter": "python", 54 | "pygments_lexer": "ipython3", 55 | "version": "3.6.7" 56 | } 57 | }, 58 | "nbformat": 4, 59 | "nbformat_minor": 2 60 | } 61 | -------------------------------------------------------------------------------- /Chapter 10/text_chunker.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "%run chunker.ipynb\n", 10 | "from nltk.corpus import brown \n", 11 | "\n", 12 | "input_data = ' '.join(brown.words()[:12000]) \n", 13 | "\n", 14 | "chunk_size = 700 \n", 15 | "chunks = chunker(input_data, chunk_size) \n", 16 | "print('Number of text chunks =', len(chunks), '\\n') \n", 17 | "for i, chunk in enumerate(chunks): \n", 18 | " print('Chunk', i+1, '==>', chunk[:50]) " 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [] 27 | } 28 | ], 29 | "metadata": { 30 | "kernelspec": { 31 | "display_name": "Python 3", 32 | "language": "python", 33 | "name": "python3" 34 | }, 35 | "language_info": { 36 | "codemirror_mode": { 37 | "name": "ipython", 38 | "version": 3 39 | }, 40 | "file_extension": ".py", 41 | "mimetype": "text/x-python", 42 | "name": "python", 43 | "nbconvert_exporter": "python", 44 | "pygments_lexer": "ipython3", 45 | "version": "3.6.7" 46 | } 47 | }, 48 | "nbformat": 4, 49 | "nbformat_minor": 2 50 | } 51 | -------------------------------------------------------------------------------- /Chapter 10/tokenizer.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from nltk.tokenize import sent_tokenize, word_tokenize, WordPunctTokenizer \n", 10 | "input_text = \"Do you know how tokenization works? It's actually quite interesting! Let's analyze a couple of sentences and figure it out.\"\n", 11 | "print(\"Sentence tokenizer:\") \n", 12 | "print(sent_tokenize(input_text)) " 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": null, 18 | "metadata": {}, 19 | "outputs": [], 20 | "source": [ 21 | "print(\"Word tokenizer:\") \n", 22 | "print(word_tokenize(input_text)) " 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "print(\"Word punct tokenizer:\") \n", 32 | "print(WordPunctTokenizer().tokenize(input_text)) " 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": null, 38 | "metadata": {}, 39 | "outputs": [], 40 | "source": [] 41 | } 42 | ], 43 | "metadata": { 44 | "kernelspec": { 45 | "display_name": "Python 3", 46 | "language": "python", 47 | "name": "python3" 48 | }, 49 | "language_info": { 50 | "codemirror_mode": { 51 | "name": "ipython", 52 | "version": 3 53 | }, 54 | "file_extension": ".py", 55 | "mimetype": "text/x-python", 56 | "name": "python", 57 | "nbconvert_exporter": "python", 58 | "pygments_lexer": "ipython3", 59 | "version": "3.6.7" 60 | } 61 | }, 62 | "nbformat": 4, 63 | "nbformat_minor": 2 64 | } 65 | -------------------------------------------------------------------------------- /Chapter 10/topic_modeler.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from nltk.tokenize import RegexpTokenizer \n", 10 | "from nltk.corpus import stopwords\n", 11 | "from nltk.stem.snowball import SnowballStemmer\n", 12 | "from gensim import models, corpora\n", 13 | "\n", 14 | "def load_data(input_file):\n", 15 | " data = []\n", 16 | " with open(input_file, 'r') as f:\n", 17 | " for line in f.readlines():\n", 18 | " data.append(line.strip())\n", 19 | " return data\n", 20 | "\n", 21 | "def process(input_text): \n", 22 | " tokenizer = RegexpTokenizer(r'\\w+') \n", 23 | " tokens = tokenizer.tokenize(input_text.lower()) \n", 24 | " \n", 25 | " stop_words = stopwords.words('english') \n", 26 | " tokens = [x for x in tokens if not x in stop_words] \n", 27 | "\n", 28 | " stemmer = SnowballStemmer('english') \n", 29 | " tokens_stemmed = [stemmer.stem(x) for x in tokens] \n", 30 | " return tokens_stemmed \n", 31 | "\n", 32 | "data = load_data('data.txt') \n", 33 | "tokens = [process(x) for x in data] \n", 34 | "\n", 35 | "dict_tokens = corpora.Dictionary(tokens) \n", 36 | "doc_term_mat = [dict_tokens.doc2bow(token) for token in tokens] \n", 37 | "\n", 38 | "num_topics = 2 \n", 39 | "ldamodel = models.ldamodel.LdaModel(doc_term_mat, \n", 40 | " num_topics=num_topics, id2word=dict_tokens, passes=25) \n", 41 | "\n", 42 | "num_words = 5\n", 43 | "print('Top ' + str(num_words) + ' contributing words to each topic:') \n", 44 | "for n,values in ldamodel.show_topics(num_topics=num_topics, num_words=num_words, \n", 45 | " formatted=False): \n", 46 | " print('\\nTopic', n)\n", 47 | " for word,weight in values: \n", 48 | " print(word, '==>', str(round(float(weight) * 100, 2)) + '%') " 49 | ] 50 | } 51 | ], 52 | "metadata": { 53 | "kernelspec": { 54 | "display_name": "Python 3", 55 | "language": "python", 56 | "name": "python3" 57 | }, 58 | "language_info": { 59 | "codemirror_mode": { 60 | "name": "ipython", 61 | "version": 3 62 | }, 63 | "file_extension": ".py", 64 | "mimetype": "text/x-python", 65 | "name": "python", 66 | "nbconvert_exporter": "python", 67 | "pygments_lexer": "ipython3", 68 | "version": "3.6.7" 69 | } 70 | }, 71 | "nbformat": 4, 72 | "nbformat_minor": 2 73 | } 74 | -------------------------------------------------------------------------------- /Chapter 11/crf.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import string\n", 10 | "import numpy as np \n", 11 | "from pystruct.datasets import load_letters \n", 12 | "from pystruct.models import ChainCRF \n", 13 | "from pystruct.learners import FrankWolfeSSVM \n", 14 | "\n", 15 | "class CRFModel(object): \n", 16 | " def __init__(self, c_val=1.0): \n", 17 | " self.clf = FrankWolfeSSVM(model=ChainCRF(),\n", 18 | " C=c_val, max_iter=50) \n", 19 | " def load_data(self): \n", 20 | " alphabets = load_letters() \n", 21 | " X = np.array(alphabets['data']) \n", 22 | " y = np.array(alphabets['labels']) \n", 23 | " folds = alphabets['folds'] \n", 24 | " return X, y, folds \n", 25 | " \n", 26 | " def train(self, X_train, y_train): \n", 27 | " self.clf.fit(X_train, y_train) \n", 28 | "\n", 29 | " def evaluate(self, X_test, y_test): \n", 30 | " return self.clf.score(X_test, y_test) \n", 31 | " \n", 32 | " def classify(self, input_data): \n", 33 | " return self.clf.predict(input_data)[0] \n", 34 | "\n", 35 | " \n", 36 | "def convert_to_letters(indices): \n", 37 | " # 全アルファベットのnumpy配列を作る\n", 38 | " alphabets = np.array(list(string.ascii_lowercase)) \n", 39 | "\n", 40 | " # 入力インデックスに対応した文字を抽出する\n", 41 | " output = np.take(alphabets, indices) \n", 42 | " output = ''.join(output) \n", 43 | "\n", 44 | " return output \n", 45 | "\n", 46 | "crf = CRFModel(1.0) \n", 47 | "X, y, folds = crf.load_data() \n", 48 | "X_train, X_test = X[folds == 1], X[folds != 1] \n", 49 | "y_train, y_test = y[folds == 1], y[folds != 1] \n", 50 | "\n", 51 | "print('Training the CRF model...') \n", 52 | "crf.train(X_train, y_train) \n", 53 | "\n", 54 | "score = crf.evaluate(X_test, y_test) \n", 55 | "print('Accuracy score =', str(round(score*100, 2)) + '%') \n" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": null, 61 | "metadata": { 62 | "scrolled": false 63 | }, 64 | "outputs": [], 65 | "source": [ 66 | "indices = range(3000, len(y_test), 200) \n", 67 | "for index in indices: \n", 68 | " print(\"\\nOriginal =\", convert_to_letters(y_test[index])) \n", 69 | " predicted = crf.classify([X_test[index]]) \n", 70 | " print(\"Predicted =\", convert_to_letters(predicted)) " 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [] 79 | } 80 | ], 81 | "metadata": { 82 | "kernelspec": { 83 | "display_name": "Python 3", 84 | "language": "python", 85 | "name": "python3" 86 | }, 87 | "language_info": { 88 | "codemirror_mode": { 89 | "name": "ipython", 90 | "version": 3 91 | }, 92 | "file_extension": ".py", 93 | "mimetype": "text/x-python", 94 | "name": "python", 95 | "nbconvert_exporter": "python", 96 | "pygments_lexer": "ipython3", 97 | "version": "3.6.7" 98 | } 99 | }, 100 | "nbformat": 4, 101 | "nbformat_minor": 2 102 | } 103 | -------------------------------------------------------------------------------- /Chapter 11/hmm.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "%matplotlib inline\n", 11 | "import matplotlib.pyplot as plt \n", 12 | "\n", 13 | "data = np.loadtxt('data_1D.txt', delimiter=',') \n", 14 | "X = np.column_stack([data[:, 2]]) \n", 15 | "\n", 16 | "plt.plot(np.arange(X.shape[0]), X[:,0], c='black') \n", 17 | "plt.title('Training data') \n", 18 | "plt.show()" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "metadata": { 25 | "scrolled": true 26 | }, 27 | "outputs": [], 28 | "source": [ 29 | "from hmmlearn.hmm import GaussianHMM \n", 30 | "\n", 31 | "num_components = 10\n", 32 | "hmm = GaussianHMM(n_components=num_components, \n", 33 | " covariance_type='diag', n_iter=1000) \n", 34 | "print('Training the Hidden Markov Model...') \n", 35 | "hmm.fit(X) \n", 36 | "\n", 37 | "print('Means and variances:') \n", 38 | "for i in range(hmm.n_components): \n", 39 | " print('\\nHidden state', i+1) \n", 40 | " print('Mean =', round(hmm.means_[i][0], 2)) \n", 41 | " print('Variance =', round(np.diag(hmm.covars_[i])[0], 2)) \n" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "metadata": { 48 | "scrolled": true 49 | }, 50 | "outputs": [], 51 | "source": [ 52 | "num_samples = 1200\n", 53 | "generated_data, _ = hmm.sample(num_samples)\n", 54 | "plt.plot(np.arange(num_samples), generated_data[:, 0], c='black')\n", 55 | "plt.title('Generated data')\n", 56 | "plt.show()" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [] 65 | } 66 | ], 67 | "metadata": { 68 | "kernelspec": { 69 | "display_name": "Python 3", 70 | "language": "python", 71 | "name": "python3" 72 | }, 73 | "language_info": { 74 | "codemirror_mode": { 75 | "name": "ipython", 76 | "version": 3 77 | }, 78 | "file_extension": ".py", 79 | "mimetype": "text/x-python", 80 | "name": "python", 81 | "nbconvert_exporter": "python", 82 | "pygments_lexer": "ipython3", 83 | "version": "3.6.7" 84 | } 85 | }, 86 | "nbformat": 4, 87 | "nbformat_minor": 2 88 | } 89 | -------------------------------------------------------------------------------- /Chapter 11/operator.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "%matplotlib inline\n", 10 | "import matplotlib.pyplot as plt \n", 11 | "import pandas as pd \n", 12 | "%run read_data.ipynb\n", 13 | "\n", 14 | "input_file = 'data_2D.txt' \n", 15 | "x1 = read_data(input_file, 2) \n", 16 | "x2 = read_data(input_file, 3) \n", 17 | "\n", 18 | "data = pd.DataFrame({'dim1': x1, 'dim2': x2}) \n", 19 | "\n", 20 | "start = '1968' \n", 21 | "end = '1975' \n", 22 | "data[start:end].plot(style=['-', '--']) \n", 23 | "plt.title('Data overlapped on top of each other') \n", 24 | "plt.show()" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": null, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "data[(data['dim1'] < 45) & (data['dim2'] > 30)][start:end].plot(style=['-', '--']) \n", 34 | "plt.title('dim1 < 45 and dim2 > 30') \n", 35 | "plt.show()" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": null, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [ 44 | "sum = data[start:end]['dim1'] + data[start:end]['dim2'] \n", 45 | "sum.plot() \n", 46 | "plt.title('Summation (dim1 + dim2)') \n", 47 | "plt.show() " 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [] 56 | } 57 | ], 58 | "metadata": { 59 | "kernelspec": { 60 | "display_name": "Python 3", 61 | "language": "python", 62 | "name": "python3" 63 | }, 64 | "language_info": { 65 | "codemirror_mode": { 66 | "name": "ipython", 67 | "version": 3 68 | }, 69 | "file_extension": ".py", 70 | "mimetype": "text/x-python", 71 | "name": "python", 72 | "nbconvert_exporter": "python", 73 | "pygments_lexer": "ipython3", 74 | "version": "3.6.7" 75 | } 76 | }, 77 | "nbformat": 4, 78 | "nbformat_minor": 2 79 | } 80 | -------------------------------------------------------------------------------- /Chapter 11/read_data.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "import pandas as pd \n", 11 | "\n", 12 | "def read_data(input_file, i): \n", 13 | " def to_date(x, y):\n", 14 | " return str(int(x)) + '-' + str(int(y))\n", 15 | "\n", 16 | " input_data = np.loadtxt(input_file, delimiter=',') \n", 17 | " start = to_date(input_data[0, 0], input_data[0, 1]) \n", 18 | "\n", 19 | " if input_data[-1, 1] == 12: \n", 20 | " year = input_data[-1, 0] + 1 \n", 21 | " month = 1 \n", 22 | " else: \n", 23 | " year = input_data[-1, 0] \n", 24 | " month = input_data[-1, 1] + 1 \n", 25 | " end = to_date(year, month) \n", 26 | " \n", 27 | " date_indices = pd.date_range(start, end, freq='M') \n", 28 | " output = pd.Series(input_data[:, i], index=date_indices) \n", 29 | " return output " 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": null, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [] 38 | } 39 | ], 40 | "metadata": { 41 | "kernelspec": { 42 | "display_name": "Python 3", 43 | "language": "python", 44 | "name": "python3" 45 | }, 46 | "language_info": { 47 | "codemirror_mode": { 48 | "name": "ipython", 49 | "version": 3 50 | }, 51 | "file_extension": ".py", 52 | "mimetype": "text/x-python", 53 | "name": "python", 54 | "nbconvert_exporter": "python", 55 | "pygments_lexer": "ipython3", 56 | "version": "3.6.7" 57 | } 58 | }, 59 | "nbformat": 4, 60 | "nbformat_minor": 2 61 | } 62 | -------------------------------------------------------------------------------- /Chapter 11/slicer.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "%matplotlib inline\n", 10 | "import matplotlib.pyplot as plt \n", 11 | "%run read_data.ipynb\n", 12 | "\n", 13 | "index = 2 \n", 14 | "data = read_data('data_2D.txt', index) \n", 15 | "\n", 16 | "start = '2003' \n", 17 | "end = '2011' \n", 18 | "plt.figure() \n", 19 | "data[start:end].plot() \n", 20 | "plt.title('Input data from ' + start + ' to ' + end) \n", 21 | "plt.show()" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": null, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "start = '1998-2' \n", 31 | "end = '2006-7' \n", 32 | "plt.figure() \n", 33 | "data[start:end].plot() \n", 34 | "plt.title('Input data from ' + start + ' to ' + end) \n", 35 | "plt.show() \n" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": null, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [ 44 | "d = pd.Series([0,1,2])\n", 45 | "print(d[0:2])" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "d = pd.Series([0,1,2], index=['foo','bar','zot'])\n", 55 | "print(d['foo':'zot'])" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": null, 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [] 64 | } 65 | ], 66 | "metadata": { 67 | "kernelspec": { 68 | "display_name": "Python 3", 69 | "language": "python", 70 | "name": "python3" 71 | }, 72 | "language_info": { 73 | "codemirror_mode": { 74 | "name": "ipython", 75 | "version": 3 76 | }, 77 | "file_extension": ".py", 78 | "mimetype": "text/x-python", 79 | "name": "python", 80 | "nbconvert_exporter": "python", 81 | "pygments_lexer": "ipython3", 82 | "version": "3.6.7" 83 | } 84 | }, 85 | "nbformat": 4, 86 | "nbformat_minor": 2 87 | } 88 | -------------------------------------------------------------------------------- /Chapter 11/stats_extractor.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "%matplotlib inline\n", 10 | "import matplotlib.pyplot as plt \n", 11 | "import pandas as pd \n", 12 | "%run read_data.ipynb\n", 13 | "\n", 14 | "input_file = 'data_2D.txt' \n", 15 | "x1 = read_data(input_file, 2) \n", 16 | "x2 = read_data(input_file, 3) \n", 17 | "data = pd.DataFrame({'dim1': x1, 'dim2': x2}) \n", 18 | "\n", 19 | "print('Maximum values for each dimension:') \n", 20 | "print(data.max()) \n", 21 | "print('\\nMinimum values for each dimension:') \n", 22 | "print(data.min()) " 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "print('Overall mean:') \n", 32 | "print(data.mean()) \n", 33 | "print('\\nRow-wise mean:') \n", 34 | "print(data.mean(axis=1)[:12]) " 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": null, 40 | "metadata": { 41 | "scrolled": false 42 | }, 43 | "outputs": [], 44 | "source": [ 45 | "start = '1968' \n", 46 | "end = '1975' \n", 47 | "data[start:end].plot(style=['-','--']) \n", 48 | "plt.title('Original') \n", 49 | "plt.show()\n", 50 | "data[start:end].rolling(window=24).mean().plot(style=['-','--']) \n", 51 | "plt.title('Rolling mean') \n", 52 | "plt.show()\n" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "print('Correlation coefficients:')\n", 62 | "print(data.corr()) \n" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "data['dim1'].rolling(window=60).corr(other=data['dim2']).plot() \n", 72 | "plt.title('Rolling correlation') \n", 73 | "plt.show() \n" 74 | ] 75 | } 76 | ], 77 | "metadata": { 78 | "kernelspec": { 79 | "display_name": "Python 3", 80 | "language": "python", 81 | "name": "python3" 82 | }, 83 | "language_info": { 84 | "codemirror_mode": { 85 | "name": "ipython", 86 | "version": 3 87 | }, 88 | "file_extension": ".py", 89 | "mimetype": "text/x-python", 90 | "name": "python", 91 | "nbconvert_exporter": "python", 92 | "pygments_lexer": "ipython3", 93 | "version": "3.6.7" 94 | } 95 | }, 96 | "nbformat": 4, 97 | "nbformat_minor": 2 98 | } 99 | -------------------------------------------------------------------------------- /Chapter 11/stock_market.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import datetime \n", 10 | "import numpy as np \n", 11 | "import matplotlib.pyplot as plt \n", 12 | "from hmmlearn.hmm import GaussianHMM \n", 13 | "import pandas as pd\n", 14 | "import pandas_datareader.data as pdd\n", 15 | "%matplotlib inline\n", 16 | "\n", 17 | "QUANDL_API_KEY = 'xxxxxxxxxxxxxxxxxxxx'\n", 18 | "assert QUANDL_API_KEY != 'xxxxxxxxxxxxxxxxxxxx', 'You must set a valid API KEY.'\n", 19 | "\n", 20 | "start = datetime.date(1970, 9, 4) \n", 21 | "end = datetime.date(2016, 5, 17) \n", 22 | "stock_quotes = pdd.DataReader('WIKI/INTC', 'quandl', start, end,\n", 23 | " access_key=QUANDL_API_KEY)\n", 24 | "\n", 25 | "closing_quotes = np.array(stock_quotes['Close'])\n", 26 | "volumes = np.array(stock_quotes['Volume'])[1:]\n", 27 | "\n", 28 | "diff_percentages = 100.0 * np.diff(closing_quotes) / closing_quotes[:-1] \n", 29 | "training_data = np.column_stack([diff_percentages, volumes]) \n", 30 | "\n", 31 | "hmm = GaussianHMM(n_components=7, covariance_type='diag', n_iter=1000) \n", 32 | "hmm.fit(training_data) \n", 33 | "\n", 34 | "num_samples = 300 \n", 35 | "samples, _ = hmm.sample(num_samples) \n", 36 | "\n", 37 | "\n", 38 | "plt.figure() \n", 39 | "plt.title('Difference percentages') \n", 40 | "plt.plot(np.arange(num_samples), samples[:, 0], c='black') \n", 41 | "plt.show() " 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [ 50 | "plt.figure() \n", 51 | "plt.title('Volume of shares') \n", 52 | "plt.plot(np.arange(num_samples), samples[:, 1], c='black') \n", 53 | "plt.ylim(ymin=0) \n", 54 | "plt.show() " 55 | ] 56 | } 57 | ], 58 | "metadata": { 59 | "kernelspec": { 60 | "display_name": "Python 3", 61 | "language": "python", 62 | "name": "python3" 63 | }, 64 | "language_info": { 65 | "codemirror_mode": { 66 | "name": "ipython", 67 | "version": 3 68 | }, 69 | "file_extension": ".py", 70 | "mimetype": "text/x-python", 71 | "name": "python", 72 | "nbconvert_exporter": "python", 73 | "pygments_lexer": "ipython3", 74 | "version": "3.6.7" 75 | } 76 | }, 77 | "nbformat": 4, 78 | "nbformat_minor": 2 79 | } 80 | -------------------------------------------------------------------------------- /Chapter 11/timeseries.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "scrolled": false 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "%matplotlib inline\n", 12 | "import matplotlib.pyplot as plt \n", 13 | "%run read_data.ipynb\n", 14 | "\n", 15 | "input_file = 'data_2D.txt' \n", 16 | "indices = [2, 3] \n", 17 | "for index in indices: \n", 18 | " timeseries = read_data(input_file, index) \n", 19 | "\n", 20 | " plt.figure() \n", 21 | " timeseries.plot() \n", 22 | " plt.title('Dimension ' + str(index - 1)) \n", 23 | "\n", 24 | "plt.show() " 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": null, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [] 33 | } 34 | ], 35 | "metadata": { 36 | "kernelspec": { 37 | "display_name": "Python 3", 38 | "language": "python", 39 | "name": "python3" 40 | }, 41 | "language_info": { 42 | "codemirror_mode": { 43 | "name": "ipython", 44 | "version": 3 45 | }, 46 | "file_extension": ".py", 47 | "mimetype": "text/x-python", 48 | "name": "python", 49 | "nbconvert_exporter": "python", 50 | "pygments_lexer": "ipython3", 51 | "version": "3.6.7" 52 | } 53 | }, 54 | "nbformat": 4, 55 | "nbformat_minor": 2 56 | } 57 | -------------------------------------------------------------------------------- /Chapter 12/LICENSE.txt: -------------------------------------------------------------------------------- 1 | * spoken_word.wav and data/ 2 | https://code.google.com/archive/p/hmm-speech-recognition/ 3 | Apache License 2.0 4 | Created by Hakon Sandsmark. 5 | 6 | * python_speech_features/ 7 | http://python-speech-features.readthedocs.org/ 8 | The MIT License (MIT) 9 | Copyright (c) 2013 James Lyons 10 | See python_speech_features/LICENSE 11 | -------------------------------------------------------------------------------- /Chapter 12/audio_generator.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "scrolled": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import numpy as np \n", 12 | "import matplotlib.pyplot as plt \n", 13 | "from scipy.io.wavfile import write \n", 14 | "%matplotlib inline\n", 15 | "\n", 16 | "duration = 4 # 秒\n", 17 | "sampling_freq = 44100 # Hz \n", 18 | "tone_freq = 784 # Hz \n", 19 | "\n", 20 | "t = np.linspace(0, duration, duration * sampling_freq) \n", 21 | "signal = np.sin(2 * np.pi * tone_freq * t) \n", 22 | "\n", 23 | "noise = 0.5 * np.random.rand(duration * sampling_freq) \n", 24 | "signal += noise \n", 25 | "\n", 26 | "scaling_factor = 2 ** 15 - 1\n", 27 | "signal_normalized = signal / np.max(np.abs(signal)) \n", 28 | "signal_scaled = np.int16(signal_normalized * scaling_factor) \n", 29 | "\n", 30 | "output_file = 'generated_audio.wav'\n", 31 | "write(output_file, sampling_freq, signal_scaled)" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": null, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "size = 200\n", 41 | "signal = signal[:size] \n", 42 | "time_axis = np.linspace(0, 1000 * size / sampling_freq, size)\n", 43 | "\n", 44 | "plt.plot(time_axis, signal, color='black') \n", 45 | "plt.xlabel('Time (milliseconds)') \n", 46 | "plt.ylabel('Amplitude') \n", 47 | "plt.title('Generated audio signal') \n", 48 | "plt.show()" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "metadata": {}, 55 | "outputs": [], 56 | "source": [] 57 | } 58 | ], 59 | "metadata": { 60 | "kernelspec": { 61 | "display_name": "Python 3", 62 | "language": "python", 63 | "name": "python3" 64 | }, 65 | "language_info": { 66 | "codemirror_mode": { 67 | "name": "ipython", 68 | "version": 3 69 | }, 70 | "file_extension": ".py", 71 | "mimetype": "text/x-python", 72 | "name": "python", 73 | "nbconvert_exporter": "python", 74 | "pygments_lexer": "ipython3", 75 | "version": "3.6.7" 76 | } 77 | }, 78 | "nbformat": 4, 79 | "nbformat_minor": 2 80 | } 81 | -------------------------------------------------------------------------------- /Chapter 12/audio_plotter.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "import matplotlib.pyplot as plt \n", 11 | "from scipy.io import wavfile \n", 12 | "%matplotlib inline\n", 13 | "\n", 14 | "sampling_freq, signal = wavfile.read('random_sound.wav') \n", 15 | "\n", 16 | "print('Signal shape:', signal.shape) \n", 17 | "print('Datatype:', signal.dtype) \n", 18 | "print('Signal duration:', round(signal.shape[0] / float(sampling_freq), 2), 'seconds') " 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "signal = signal / (2 ** 15)\n", 28 | "size = 50\n", 29 | "signal = signal[:size] \n", 30 | "time_axis = np.linspace(0, 1000 * size / sampling_freq, size)\n", 31 | "\n", 32 | "plt.plot(time_axis, signal, color='black') \n", 33 | "plt.xlabel('Time (milliseconds)') \n", 34 | "plt.ylabel('Amplitude') \n", 35 | "plt.title('Input audio signal') \n", 36 | "plt.show() " 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [] 45 | } 46 | ], 47 | "metadata": { 48 | "kernelspec": { 49 | "display_name": "Python 3", 50 | "language": "python", 51 | "name": "python3" 52 | }, 53 | "language_info": { 54 | "codemirror_mode": { 55 | "name": "ipython", 56 | "version": 3 57 | }, 58 | "file_extension": ".py", 59 | "mimetype": "text/x-python", 60 | "name": "python", 61 | "nbconvert_exporter": "python", 62 | "pygments_lexer": "ipython3", 63 | "version": "3.6.7" 64 | } 65 | }, 66 | "nbformat": 4, 67 | "nbformat_minor": 2 68 | } 69 | -------------------------------------------------------------------------------- /Chapter 12/data/apple/apple01.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/apple/apple01.wav -------------------------------------------------------------------------------- /Chapter 12/data/apple/apple02.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/apple/apple02.wav -------------------------------------------------------------------------------- /Chapter 12/data/apple/apple03.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/apple/apple03.wav -------------------------------------------------------------------------------- /Chapter 12/data/apple/apple04.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/apple/apple04.wav -------------------------------------------------------------------------------- /Chapter 12/data/apple/apple05.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/apple/apple05.wav -------------------------------------------------------------------------------- /Chapter 12/data/apple/apple06.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/apple/apple06.wav -------------------------------------------------------------------------------- /Chapter 12/data/apple/apple07.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/apple/apple07.wav -------------------------------------------------------------------------------- /Chapter 12/data/apple/apple08.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/apple/apple08.wav -------------------------------------------------------------------------------- /Chapter 12/data/apple/apple09.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/apple/apple09.wav -------------------------------------------------------------------------------- /Chapter 12/data/apple/apple10.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/apple/apple10.wav -------------------------------------------------------------------------------- /Chapter 12/data/apple/apple11.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/apple/apple11.wav -------------------------------------------------------------------------------- /Chapter 12/data/apple/apple12.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/apple/apple12.wav -------------------------------------------------------------------------------- /Chapter 12/data/apple/apple13.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/apple/apple13.wav -------------------------------------------------------------------------------- /Chapter 12/data/apple/apple14.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/apple/apple14.wav -------------------------------------------------------------------------------- /Chapter 12/data/apple/apple15.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/apple/apple15.wav -------------------------------------------------------------------------------- /Chapter 12/data/banana/banana01.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/banana/banana01.wav -------------------------------------------------------------------------------- /Chapter 12/data/banana/banana02.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/banana/banana02.wav -------------------------------------------------------------------------------- /Chapter 12/data/banana/banana03.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/banana/banana03.wav -------------------------------------------------------------------------------- /Chapter 12/data/banana/banana04.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/banana/banana04.wav -------------------------------------------------------------------------------- /Chapter 12/data/banana/banana05.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/banana/banana05.wav -------------------------------------------------------------------------------- /Chapter 12/data/banana/banana06.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/banana/banana06.wav -------------------------------------------------------------------------------- /Chapter 12/data/banana/banana07.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/banana/banana07.wav -------------------------------------------------------------------------------- /Chapter 12/data/banana/banana08.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/banana/banana08.wav -------------------------------------------------------------------------------- /Chapter 12/data/banana/banana09.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/banana/banana09.wav -------------------------------------------------------------------------------- /Chapter 12/data/banana/banana10.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/banana/banana10.wav -------------------------------------------------------------------------------- /Chapter 12/data/banana/banana11.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/banana/banana11.wav -------------------------------------------------------------------------------- /Chapter 12/data/banana/banana12.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/banana/banana12.wav -------------------------------------------------------------------------------- /Chapter 12/data/banana/banana13.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/banana/banana13.wav -------------------------------------------------------------------------------- /Chapter 12/data/banana/banana14.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/banana/banana14.wav -------------------------------------------------------------------------------- /Chapter 12/data/banana/banana15.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/banana/banana15.wav -------------------------------------------------------------------------------- /Chapter 12/data/kiwi/kiwi01.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/kiwi/kiwi01.wav -------------------------------------------------------------------------------- /Chapter 12/data/kiwi/kiwi02.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/kiwi/kiwi02.wav -------------------------------------------------------------------------------- /Chapter 12/data/kiwi/kiwi03.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/kiwi/kiwi03.wav -------------------------------------------------------------------------------- /Chapter 12/data/kiwi/kiwi04.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/kiwi/kiwi04.wav -------------------------------------------------------------------------------- /Chapter 12/data/kiwi/kiwi05.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/kiwi/kiwi05.wav -------------------------------------------------------------------------------- /Chapter 12/data/kiwi/kiwi06.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/kiwi/kiwi06.wav -------------------------------------------------------------------------------- /Chapter 12/data/kiwi/kiwi07.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/kiwi/kiwi07.wav -------------------------------------------------------------------------------- /Chapter 12/data/kiwi/kiwi08.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/kiwi/kiwi08.wav -------------------------------------------------------------------------------- /Chapter 12/data/kiwi/kiwi09.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/kiwi/kiwi09.wav -------------------------------------------------------------------------------- /Chapter 12/data/kiwi/kiwi10.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/kiwi/kiwi10.wav -------------------------------------------------------------------------------- /Chapter 12/data/kiwi/kiwi11.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/kiwi/kiwi11.wav -------------------------------------------------------------------------------- /Chapter 12/data/kiwi/kiwi12.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/kiwi/kiwi12.wav -------------------------------------------------------------------------------- /Chapter 12/data/kiwi/kiwi13.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/kiwi/kiwi13.wav -------------------------------------------------------------------------------- /Chapter 12/data/kiwi/kiwi14.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/kiwi/kiwi14.wav -------------------------------------------------------------------------------- /Chapter 12/data/kiwi/kiwi15.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/kiwi/kiwi15.wav -------------------------------------------------------------------------------- /Chapter 12/data/lime/lime01.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/lime/lime01.wav -------------------------------------------------------------------------------- /Chapter 12/data/lime/lime02.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/lime/lime02.wav -------------------------------------------------------------------------------- /Chapter 12/data/lime/lime03.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/lime/lime03.wav -------------------------------------------------------------------------------- /Chapter 12/data/lime/lime04.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/lime/lime04.wav -------------------------------------------------------------------------------- /Chapter 12/data/lime/lime05.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/lime/lime05.wav -------------------------------------------------------------------------------- /Chapter 12/data/lime/lime06.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/lime/lime06.wav -------------------------------------------------------------------------------- /Chapter 12/data/lime/lime07.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/lime/lime07.wav -------------------------------------------------------------------------------- /Chapter 12/data/lime/lime08.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/lime/lime08.wav -------------------------------------------------------------------------------- /Chapter 12/data/lime/lime09.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/lime/lime09.wav -------------------------------------------------------------------------------- /Chapter 12/data/lime/lime10.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/lime/lime10.wav -------------------------------------------------------------------------------- /Chapter 12/data/lime/lime11.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/lime/lime11.wav -------------------------------------------------------------------------------- /Chapter 12/data/lime/lime12.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/lime/lime12.wav -------------------------------------------------------------------------------- /Chapter 12/data/lime/lime13.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/lime/lime13.wav -------------------------------------------------------------------------------- /Chapter 12/data/lime/lime14.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/lime/lime14.wav -------------------------------------------------------------------------------- /Chapter 12/data/lime/lime15.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/lime/lime15.wav -------------------------------------------------------------------------------- /Chapter 12/data/orange/orange01.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/orange/orange01.wav -------------------------------------------------------------------------------- /Chapter 12/data/orange/orange02.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/orange/orange02.wav -------------------------------------------------------------------------------- /Chapter 12/data/orange/orange03.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/orange/orange03.wav -------------------------------------------------------------------------------- /Chapter 12/data/orange/orange04.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/orange/orange04.wav -------------------------------------------------------------------------------- /Chapter 12/data/orange/orange05.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/orange/orange05.wav -------------------------------------------------------------------------------- /Chapter 12/data/orange/orange06.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/orange/orange06.wav -------------------------------------------------------------------------------- /Chapter 12/data/orange/orange07.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/orange/orange07.wav -------------------------------------------------------------------------------- /Chapter 12/data/orange/orange08.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/orange/orange08.wav -------------------------------------------------------------------------------- /Chapter 12/data/orange/orange09.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/orange/orange09.wav -------------------------------------------------------------------------------- /Chapter 12/data/orange/orange10.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/orange/orange10.wav -------------------------------------------------------------------------------- /Chapter 12/data/orange/orange11.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/orange/orange11.wav -------------------------------------------------------------------------------- /Chapter 12/data/orange/orange12.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/orange/orange12.wav -------------------------------------------------------------------------------- /Chapter 12/data/orange/orange13.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/orange/orange13.wav -------------------------------------------------------------------------------- /Chapter 12/data/orange/orange14.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/orange/orange14.wav -------------------------------------------------------------------------------- /Chapter 12/data/orange/orange15.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/orange/orange15.wav -------------------------------------------------------------------------------- /Chapter 12/data/peach/peach01.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/peach/peach01.wav -------------------------------------------------------------------------------- /Chapter 12/data/peach/peach02.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/peach/peach02.wav -------------------------------------------------------------------------------- /Chapter 12/data/peach/peach03.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/peach/peach03.wav -------------------------------------------------------------------------------- /Chapter 12/data/peach/peach04.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/peach/peach04.wav -------------------------------------------------------------------------------- /Chapter 12/data/peach/peach05.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/peach/peach05.wav -------------------------------------------------------------------------------- /Chapter 12/data/peach/peach06.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/peach/peach06.wav -------------------------------------------------------------------------------- /Chapter 12/data/peach/peach07.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/peach/peach07.wav -------------------------------------------------------------------------------- /Chapter 12/data/peach/peach08.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/peach/peach08.wav -------------------------------------------------------------------------------- /Chapter 12/data/peach/peach09.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/peach/peach09.wav -------------------------------------------------------------------------------- /Chapter 12/data/peach/peach10.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/peach/peach10.wav -------------------------------------------------------------------------------- /Chapter 12/data/peach/peach11.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/peach/peach11.wav -------------------------------------------------------------------------------- /Chapter 12/data/peach/peach12.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/peach/peach12.wav -------------------------------------------------------------------------------- /Chapter 12/data/peach/peach13.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/peach/peach13.wav -------------------------------------------------------------------------------- /Chapter 12/data/peach/peach14.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/peach/peach14.wav -------------------------------------------------------------------------------- /Chapter 12/data/peach/peach15.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/peach/peach15.wav -------------------------------------------------------------------------------- /Chapter 12/data/pineapple/pineapple01.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/pineapple/pineapple01.wav -------------------------------------------------------------------------------- /Chapter 12/data/pineapple/pineapple02.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/pineapple/pineapple02.wav -------------------------------------------------------------------------------- /Chapter 12/data/pineapple/pineapple03.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/pineapple/pineapple03.wav -------------------------------------------------------------------------------- /Chapter 12/data/pineapple/pineapple04.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/pineapple/pineapple04.wav -------------------------------------------------------------------------------- /Chapter 12/data/pineapple/pineapple05.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/pineapple/pineapple05.wav -------------------------------------------------------------------------------- /Chapter 12/data/pineapple/pineapple06.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/pineapple/pineapple06.wav -------------------------------------------------------------------------------- /Chapter 12/data/pineapple/pineapple07.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/pineapple/pineapple07.wav -------------------------------------------------------------------------------- /Chapter 12/data/pineapple/pineapple08.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/pineapple/pineapple08.wav -------------------------------------------------------------------------------- /Chapter 12/data/pineapple/pineapple09.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/pineapple/pineapple09.wav -------------------------------------------------------------------------------- /Chapter 12/data/pineapple/pineapple10.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/pineapple/pineapple10.wav -------------------------------------------------------------------------------- /Chapter 12/data/pineapple/pineapple11.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/pineapple/pineapple11.wav -------------------------------------------------------------------------------- /Chapter 12/data/pineapple/pineapple12.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/pineapple/pineapple12.wav -------------------------------------------------------------------------------- /Chapter 12/data/pineapple/pineapple13.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/pineapple/pineapple13.wav -------------------------------------------------------------------------------- /Chapter 12/data/pineapple/pineapple14.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/pineapple/pineapple14.wav -------------------------------------------------------------------------------- /Chapter 12/data/pineapple/pineapple15.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/data/pineapple/pineapple15.wav -------------------------------------------------------------------------------- /Chapter 12/feature_extractor.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "import matplotlib.pyplot as plt \n", 11 | "from scipy.io import wavfile \n", 12 | "from python_speech_features import mfcc, logfbank \n", 13 | "%matplotlib inline\n", 14 | "\n", 15 | "sampling_freq, signal = wavfile.read('random_sound.wav') \n", 16 | "signal = signal[:10000] \n", 17 | "\n", 18 | "features_mfcc = mfcc(signal, sampling_freq) \n", 19 | "\n", 20 | "print('MFCC:\\nNumber of windows =', features_mfcc.shape[0]) \n", 21 | "print('Length of each feature =', features_mfcc.shape[1]) " 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": null, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "features_mfcc = features_mfcc.T \n", 31 | "plt.matshow(features_mfcc) \n", 32 | "plt.title('MFCC') \n", 33 | "plt.show()" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "features_fb = logfbank(signal, sampling_freq) \n", 43 | "\n", 44 | "print('Filter bank:\\nNumber of windows =', features_fb.shape[0]) \n", 45 | "print('Length of each feature =', features_fb.shape[1]) " 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "features_fb = features_fb.T \n", 55 | "plt.matshow(features_fb) \n", 56 | "plt.title('Filter bank') \n", 57 | "plt.show() " 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": null, 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [] 66 | } 67 | ], 68 | "metadata": { 69 | "kernelspec": { 70 | "display_name": "Python 3", 71 | "language": "python", 72 | "name": "python3" 73 | }, 74 | "language_info": { 75 | "codemirror_mode": { 76 | "name": "ipython", 77 | "version": 3 78 | }, 79 | "file_extension": ".py", 80 | "mimetype": "text/x-python", 81 | "name": "python", 82 | "nbconvert_exporter": "python", 83 | "pygments_lexer": "ipython3", 84 | "version": "3.6.7" 85 | } 86 | }, 87 | "nbformat": 4, 88 | "nbformat_minor": 2 89 | } 90 | -------------------------------------------------------------------------------- /Chapter 12/frequency_transformer.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "scrolled": false 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import numpy as np \n", 12 | "import matplotlib.pyplot as plt \n", 13 | "from scipy.io import wavfile \n", 14 | "%matplotlib inline\n", 15 | "\n", 16 | "sampling_freq, signal = wavfile.read('spoken_word.wav') \n", 17 | "signal = signal / (2 ** 15)\n", 18 | "\n", 19 | "len_signal = len(signal) \n", 20 | "len_half = (len_signal + 1) // 2\n", 21 | "\n", 22 | "freq_signal = np.fft.fft(signal) \n", 23 | "\n", 24 | "freq_signal = np.abs(freq_signal[0:len_half]) / len_half\n", 25 | "signal_power = 20 * np.log10(freq_signal)\n", 26 | "x_axis = np.linspace(0, sampling_freq / 2 / 1000.0, len(signal_power))\n", 27 | "\n", 28 | "plt.figure() \n", 29 | "plt.plot(x_axis, signal_power, color='black') \n", 30 | "plt.xlabel('Frequency (kHz)') \n", 31 | "plt.ylabel('Signal power (dB)') \n", 32 | "plt.show() " 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": null, 38 | "metadata": {}, 39 | "outputs": [], 40 | "source": [ 41 | "plt.figure() \n", 42 | "plt.xscale('log')\n", 43 | "plt.plot(x_axis, signal_power, color='black') \n", 44 | "plt.xlabel('Frequency (kHz)') \n", 45 | "plt.ylabel('Signal power (dB)') \n", 46 | "plt.show() " 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [] 55 | } 56 | ], 57 | "metadata": { 58 | "kernelspec": { 59 | "display_name": "Python 3", 60 | "language": "python", 61 | "name": "python3" 62 | }, 63 | "language_info": { 64 | "codemirror_mode": { 65 | "name": "ipython", 66 | "version": 3 67 | }, 68 | "file_extension": ".py", 69 | "mimetype": "text/x-python", 70 | "name": "python", 71 | "nbconvert_exporter": "python", 72 | "pygments_lexer": "ipython3", 73 | "version": "3.6.7" 74 | } 75 | }, 76 | "nbformat": 4, 77 | "nbformat_minor": 2 78 | } 79 | -------------------------------------------------------------------------------- /Chapter 12/python_speech_features/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 James Lyons 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /Chapter 12/python_speech_features/__init__.py: -------------------------------------------------------------------------------- 1 | from .base import * 2 | -------------------------------------------------------------------------------- /Chapter 12/random_sound.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/random_sound.wav -------------------------------------------------------------------------------- /Chapter 12/spoken_word.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 12/spoken_word.wav -------------------------------------------------------------------------------- /Chapter 12/synthesizer.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "import matplotlib.pyplot as plt \n", 11 | "from scipy.io.wavfile import write \n", 12 | "\n", 13 | "sampling_freq = 44100\n", 14 | "\n", 15 | "def tone_synthesizer(freq, duration, amplitude=2**15-1): \n", 16 | " time_axis = np.linspace(0, duration, int(duration * sampling_freq)) \n", 17 | " signal = amplitude * np.sin(2 * np.pi * freq * time_axis) \n", 18 | " return signal.astype(np.int16) \n", 19 | "\n", 20 | "tone_map = {\n", 21 | " \"A\": 440,\n", 22 | " \"A#\": 466,\n", 23 | " \"B\": 494,\n", 24 | " \"C\": 523,\n", 25 | " \"C#\": 554,\n", 26 | " \"D\": 587,\n", 27 | " \"D#\": 622,\n", 28 | " \"E\": 659,\n", 29 | " \"F\": 698,\n", 30 | " \"F#\": 740,\n", 31 | " \"G\": 784,\n", 32 | " \"G#\": 831\n", 33 | "}\n", 34 | "\n", 35 | "file_tone_single = 'generated_tone_single.wav' \n", 36 | "synthesized_tone = tone_synthesizer(tone_map['F'], 3) \n", 37 | "write(file_tone_single, sampling_freq, synthesized_tone) " 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [ 46 | "tone_sequence = [('G', 0.4), ('D', 0.5), ('F', 0.3), ('C', 0.6), ('A', 0.4)] \n", 47 | "\n", 48 | "signal = np.array([], dtype=np.int16)\n", 49 | "\n", 50 | "for tone_name, duration in tone_sequence: \n", 51 | " freq = tone_map[tone_name] \n", 52 | " synthesized_tone = tone_synthesizer(freq, duration)\n", 53 | " signal = np.append(signal, synthesized_tone, axis=0) \n", 54 | "\n", 55 | "file_tone_sequence = 'generated_tone_sequence.wav' \n", 56 | "write(file_tone_sequence, sampling_freq, signal)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [] 65 | } 66 | ], 67 | "metadata": { 68 | "kernelspec": { 69 | "display_name": "Python 3", 70 | "language": "python", 71 | "name": "python3" 72 | }, 73 | "language_info": { 74 | "codemirror_mode": { 75 | "name": "ipython", 76 | "version": 3 77 | }, 78 | "file_extension": ".py", 79 | "mimetype": "text/x-python", 80 | "name": "python", 81 | "nbconvert_exporter": "python", 82 | "pygments_lexer": "ipython3", 83 | "version": "3.6.7" 84 | } 85 | }, 86 | "nbformat": 4, 87 | "nbformat_minor": 2 88 | } 89 | -------------------------------------------------------------------------------- /Chapter 13/LICENSE.txt: -------------------------------------------------------------------------------- 1 | * `haar_cascade_files/*.xml` 2 | Copyright (C) 2000, Intel Corporation, all rights reserved. 3 | See each xml file 4 | 5 | * `bbb.mp4` 6 | "Big Buck Bunny" 1:25--1:29 CC by 3.0 7 | https://peach.blender.org/ 8 | 9 | * `ed.mp4` 10 | "Elephants Dream" 3:10--3:14 CC by 2.5 11 | https://orange.blender.org/ 12 | -------------------------------------------------------------------------------- /Chapter 13/background_subtraction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "%run ipython_show_image.ipynb\n", 10 | "import cv2 \n", 11 | "import numpy as np\n", 12 | "\n", 13 | "cap = cv2.VideoCapture('bbb.mp4') \n", 14 | "scaling_factor = 1 \n", 15 | "\n", 16 | "bg_subtractor = cv2.createBackgroundSubtractorMOG2() \n", 17 | "\n", 18 | "history = 100 \n", 19 | "learning_rate = 1.0 / history \n", 20 | "\n", 21 | "try:\n", 22 | " while True:\n", 23 | " frame = get_frame(cap, scaling_factor)\n", 24 | " if frame is None: break\n", 25 | "\n", 26 | " mask = bg_subtractor.apply(frame, learningRate=learning_rate) \n", 27 | " mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) \n", 28 | " img2 = cv2.hconcat([frame, frame & mask])\n", 29 | " show_image(img2)\n", 30 | "\n", 31 | "except KeyboardInterrupt:\n", 32 | " print('Interrupted')\n", 33 | "\n", 34 | "cap.release()" 35 | ] 36 | } 37 | ], 38 | "metadata": { 39 | "kernelspec": { 40 | "display_name": "Python 3", 41 | "language": "python", 42 | "name": "python3" 43 | }, 44 | "language_info": { 45 | "codemirror_mode": { 46 | "name": "ipython", 47 | "version": 3 48 | }, 49 | "file_extension": ".py", 50 | "mimetype": "text/x-python", 51 | "name": "python", 52 | "nbconvert_exporter": "python", 53 | "pygments_lexer": "ipython3", 54 | "version": "3.6.7" 55 | } 56 | }, 57 | "nbformat": 4, 58 | "nbformat_minor": 2 59 | } 60 | -------------------------------------------------------------------------------- /Chapter 13/bbb.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 13/bbb.mp4 -------------------------------------------------------------------------------- /Chapter 13/colorspaces.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "scrolled": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "%run ipython_show_image.ipynb\n", 12 | "import cv2 \n", 13 | "import numpy as np\n", 14 | "\n", 15 | "cap = cv2.VideoCapture('ed.mp4') \n", 16 | "scaling_factor = 1 \n", 17 | "\n", 18 | "lower = np.array([0, 30, 60]) \n", 19 | "upper = np.array([50, 255, 255]) \n", 20 | "\n", 21 | "try:\n", 22 | " while True:\n", 23 | " frame = get_frame(cap, scaling_factor)\n", 24 | " if frame is None: break\n", 25 | " \n", 26 | " hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)\n", 27 | " mask = cv2.inRange(hsv, lower, upper) \n", 28 | " img_bitwise_and = cv2.bitwise_and(frame, frame, mask=mask)\n", 29 | " img_median_blurred = cv2.medianBlur(img_bitwise_and, 5)\n", 30 | " img2 = cv2.hconcat([frame, img_median_blurred])\n", 31 | " show_image(img2)\n", 32 | "\n", 33 | "except KeyboardInterrupt:\n", 34 | " print('Interrupted')\n", 35 | "\n", 36 | "cap.release()" 37 | ] 38 | } 39 | ], 40 | "metadata": { 41 | "kernelspec": { 42 | "display_name": "Python 3", 43 | "language": "python", 44 | "name": "python3" 45 | }, 46 | "language_info": { 47 | "codemirror_mode": { 48 | "name": "ipython", 49 | "version": 3 50 | }, 51 | "file_extension": ".py", 52 | "mimetype": "text/x-python", 53 | "name": "python", 54 | "nbconvert_exporter": "python", 55 | "pygments_lexer": "ipython3", 56 | "version": "3.6.7" 57 | } 58 | }, 59 | "nbformat": 4, 60 | "nbformat_minor": 2 61 | } 62 | -------------------------------------------------------------------------------- /Chapter 13/ed.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/Chapter 13/ed.mp4 -------------------------------------------------------------------------------- /Chapter 13/eye_detector.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "%run ipython_show_image.ipynb\n", 10 | "import cv2 \n", 11 | "import numpy as np \n", 12 | "\n", 13 | "face_cascade = cv2.CascadeClassifier( \n", 14 | " 'haar_cascade_files/haarcascade_frontalface_default.xml') \n", 15 | "eye_cascade = cv2.CascadeClassifier(\n", 16 | " 'haar_cascade_files/haarcascade_eye.xml') \n", 17 | "if face_cascade.empty(): \n", 18 | " raise IOError('Unable to load the face cascade classifier xml file') \n", 19 | "if eye_cascade.empty(): \n", 20 | " raise IOError('Unable to load the eye cascade classifier xml file') \n", 21 | "\n", 22 | "cap = cv2.VideoCapture('ed.mp4') \n", 23 | "scaling_factor = 1\n", 24 | "\n", 25 | "try:\n", 26 | " while True:\n", 27 | " frame = get_frame(cap, scaling_factor)\n", 28 | " if frame is None: break\n", 29 | " gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) \n", 30 | " \n", 31 | " face_rects = face_cascade.detectMultiScale(gray, 1.1, 3) \n", 32 | " for x,y,w,h in face_rects:\n", 33 | " roi_gray = gray[y:y+h, x:x+w] \n", 34 | " roi_color = frame[y:y+h, x:x+w] \n", 35 | " \n", 36 | " eyes = eye_cascade.detectMultiScale(roi_gray) \n", 37 | " for xe,ye,we,he in eyes: \n", 38 | " center = (int(xe + we/2), int(ye + he/2))\n", 39 | " radius = int(0.3 * (we + he)) \n", 40 | " color = (0,255,0) \n", 41 | " thickness = 3 \n", 42 | " cv2.circle(roi_color, center, radius, color, thickness) \n", 43 | " \n", 44 | " show_image(frame)\n", 45 | "\n", 46 | "except KeyboardInterrupt:\n", 47 | " print('Interrupted')\n", 48 | "\n", 49 | "cap.release() " 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [] 58 | } 59 | ], 60 | "metadata": { 61 | "kernelspec": { 62 | "display_name": "Python 3", 63 | "language": "python", 64 | "name": "python3" 65 | }, 66 | "language_info": { 67 | "codemirror_mode": { 68 | "name": "ipython", 69 | "version": 3 70 | }, 71 | "file_extension": ".py", 72 | "mimetype": "text/x-python", 73 | "name": "python", 74 | "nbconvert_exporter": "python", 75 | "pygments_lexer": "ipython3", 76 | "version": "3.6.7" 77 | } 78 | }, 79 | "nbformat": 4, 80 | "nbformat_minor": 2 81 | } 82 | -------------------------------------------------------------------------------- /Chapter 13/face_detector.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "%run ipython_show_image.ipynb\n", 10 | "import cv2 \n", 11 | "import numpy as np \n", 12 | "\n", 13 | "face_cascade = cv2.CascadeClassifier( \n", 14 | " 'haar_cascade_files/haarcascade_frontalface_default.xml') \n", 15 | "if face_cascade.empty(): \n", 16 | " raise IOError('Unable to load the face cascade classifier xml file') \n", 17 | "\n", 18 | "cap = cv2.VideoCapture('ed.mp4') \n", 19 | "scaling_factor = 1\n", 20 | "\n", 21 | "try:\n", 22 | " while True:\n", 23 | " frame = get_frame(cap, scaling_factor)\n", 24 | " if frame is None: break\n", 25 | " gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) \n", 26 | " \n", 27 | " face_rects = face_cascade.detectMultiScale(gray, 1.1, 3) \n", 28 | " for x,y,w,h in face_rects: \n", 29 | " cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3) \n", 30 | " \n", 31 | " show_image(frame)\n", 32 | " \n", 33 | "except KeyboardInterrupt:\n", 34 | " print('Interrupted')\n", 35 | "\n", 36 | "cap.release() " 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [] 45 | } 46 | ], 47 | "metadata": { 48 | "kernelspec": { 49 | "display_name": "Python 3", 50 | "language": "python", 51 | "name": "python3" 52 | }, 53 | "language_info": { 54 | "codemirror_mode": { 55 | "name": "ipython", 56 | "version": 3 57 | }, 58 | "file_extension": ".py", 59 | "mimetype": "text/x-python", 60 | "name": "python", 61 | "nbconvert_exporter": "python", 62 | "pygments_lexer": "ipython3", 63 | "version": "3.6.7" 64 | } 65 | }, 66 | "nbformat": 4, 67 | "nbformat_minor": 2 68 | } 69 | -------------------------------------------------------------------------------- /Chapter 13/frame_diff.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "%run ipython_show_image.ipynb\n", 10 | "import cv2\n", 11 | "\n", 12 | "def get_gray_frame(cap, scaling_factor):\n", 13 | " frame = get_frame(cap, scaling_factor)\n", 14 | " if frame is None: return None\n", 15 | " gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) \n", 16 | " return gray\n", 17 | " \n", 18 | "def frame_diff(prev_frame, cur_frame, next_frame): \n", 19 | " diff_frames_1 = cv2.absdiff(next_frame, cur_frame) \n", 20 | " diff_frames_2 = cv2.absdiff(cur_frame, prev_frame) \n", 21 | " return cv2.bitwise_and(diff_frames_1, diff_frames_2) \n", 22 | " \n", 23 | "cap = cv2.VideoCapture('bbb.mp4') \n", 24 | "scaling_factor = 1 \n", 25 | "\n", 26 | "prev_frame = get_gray_frame(cap, scaling_factor) \n", 27 | "cur_frame = get_gray_frame(cap, scaling_factor) \n", 28 | "next_frame = get_gray_frame(cap, scaling_factor) \n", 29 | "\n", 30 | "try:\n", 31 | " while True:\n", 32 | " diff = frame_diff(prev_frame, cur_frame, next_frame)\n", 33 | " show_image(diff)\n", 34 | "\n", 35 | " prev_frame = cur_frame \n", 36 | " cur_frame = next_frame \n", 37 | " next_frame = get_gray_frame(cap, scaling_factor) \n", 38 | " if next_frame is None: break\n", 39 | " \n", 40 | "except KeyboardInterrupt:\n", 41 | " print('Interrupted')\n", 42 | "\n", 43 | "cap.release()" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [] 52 | } 53 | ], 54 | "metadata": { 55 | "kernelspec": { 56 | "display_name": "Python 3", 57 | "language": "python", 58 | "name": "python3" 59 | }, 60 | "language_info": { 61 | "codemirror_mode": { 62 | "name": "ipython", 63 | "version": 3 64 | }, 65 | "file_extension": ".py", 66 | "mimetype": "text/x-python", 67 | "name": "python", 68 | "nbconvert_exporter": "python", 69 | "pygments_lexer": "ipython3", 70 | "version": "3.6.7" 71 | }, 72 | "widgets": { 73 | "state": {}, 74 | "version": "1.1.2" 75 | } 76 | }, 77 | "nbformat": 4, 78 | "nbformat_minor": 2 79 | } 80 | -------------------------------------------------------------------------------- /Chapter 13/ipython_show_image.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import cv2\n", 10 | "import IPython.display\n", 11 | "\n", 12 | "def show_image(image):\n", 13 | " _, png = cv2.imencode('.png', image) \n", 14 | " i = IPython.display.Image(data=png)\n", 15 | " IPython.display.clear_output(wait=True)\n", 16 | " IPython.display.display(i)\n", 17 | " \n", 18 | "def get_frame(cap, scaling_factor): \n", 19 | " r, frame = cap.read() \n", 20 | " if not r: return None\n", 21 | " frame = cv2.resize(frame, None,\n", 22 | " fx=scaling_factor, fy=scaling_factor, \n", 23 | " interpolation=cv2.INTER_AREA) \n", 24 | " return frame " 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": null, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [] 33 | } 34 | ], 35 | "metadata": { 36 | "kernelspec": { 37 | "display_name": "Python 3", 38 | "language": "python", 39 | "name": "python3" 40 | }, 41 | "language_info": { 42 | "codemirror_mode": { 43 | "name": "ipython", 44 | "version": 3 45 | }, 46 | "file_extension": ".py", 47 | "mimetype": "text/x-python", 48 | "name": "python", 49 | "nbconvert_exporter": "python", 50 | "pygments_lexer": "ipython3", 51 | "version": "3.6.7" 52 | } 53 | }, 54 | "nbformat": 4, 55 | "nbformat_minor": 2 56 | } 57 | -------------------------------------------------------------------------------- /Chapter 13/ipython_video_play.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "%run ipython_show_image.ipynb\n", 10 | "import cv2\n", 11 | "\n", 12 | "# ファイルを読み込む場合\n", 13 | "cap = cv2.VideoCapture('bbb.mp4') \n", 14 | "# カメラからキャプチャする場合\n", 15 | "#cap = cv2.VideoCapture(0)\n", 16 | "\n", 17 | "try:\n", 18 | " while True:\n", 19 | " frame = get_frame(cap, 1.0)\n", 20 | " if frame is None: break\n", 21 | " show_image(frame)\n", 22 | " print('Finished.')\n", 23 | "\n", 24 | "except KeyboardInterrupt:\n", 25 | " print('Interrupted.')\n", 26 | "\n", 27 | "cap.release()" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [] 36 | } 37 | ], 38 | "metadata": { 39 | "kernelspec": { 40 | "display_name": "Python 3", 41 | "language": "python", 42 | "name": "python3" 43 | }, 44 | "language_info": { 45 | "codemirror_mode": { 46 | "name": "ipython", 47 | "version": 3 48 | }, 49 | "file_extension": ".py", 50 | "mimetype": "text/x-python", 51 | "name": "python", 52 | "nbconvert_exporter": "python", 53 | "pygments_lexer": "ipython3", 54 | "version": "3.6.7" 55 | } 56 | }, 57 | "nbformat": 4, 58 | "nbformat_minor": 2 59 | } 60 | -------------------------------------------------------------------------------- /Chapter 14/LICENSE: -------------------------------------------------------------------------------- 1 | * letter.data 2 | http://ai.stanford.edu/~btaskar/ocr 3 | 4 | -------------------------------------------------------------------------------- /Chapter 14/character_visualizer.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import cv2 \n", 10 | "import numpy as np\n", 11 | "import matplotlib.pyplot as plt\n", 12 | "%matplotlib inline\n", 13 | "\n", 14 | "input_file = 'letter.data' \n", 15 | "\n", 16 | "W, H = 8, 16 \n", 17 | "W1, H1 = W + 1, H + 1\n", 18 | "N_COLS, N_ROWS = 9, 5\n", 19 | "simg = np.zeros((H1 * N_ROWS, W1 * N_COLS), dtype=np.uint8)\n", 20 | "\n", 21 | "col, row = 0, 0\n", 22 | "with open(input_file, 'r') as f: \n", 23 | " for line in f.readlines():\n", 24 | " list_vals = line.split('\\t')\n", 25 | " c = list_vals[1]\n", 26 | " print(c, end='')\n", 27 | " data = np.array([255 * int(x) for x in list_vals[6:-1]]) \n", 28 | " img = np.reshape(data, (H, W)).astype(np.uint8)\n", 29 | " simg[row*H1:row*H1+H,col*W1:col*W1+W] = img\n", 30 | " col += 1\n", 31 | " if col == N_COLS:\n", 32 | " col = 0\n", 33 | " row += 1\n", 34 | " print('')\n", 35 | " if row == N_ROWS:\n", 36 | " break\n", 37 | " \n", 38 | "simg = cv2.cvtColor(simg, cv2.COLOR_GRAY2RGB)\n", 39 | "plt.figure()\n", 40 | "plt.imshow(simg)\n", 41 | "plt.show()" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [] 50 | } 51 | ], 52 | "metadata": { 53 | "kernelspec": { 54 | "display_name": "Python 3", 55 | "language": "python", 56 | "name": "python3" 57 | }, 58 | "language_info": { 59 | "codemirror_mode": { 60 | "name": "ipython", 61 | "version": 3 62 | }, 63 | "file_extension": ".py", 64 | "mimetype": "text/x-python", 65 | "name": "python", 66 | "nbconvert_exporter": "python", 67 | "pygments_lexer": "ipython3", 68 | "version": "3.6.7" 69 | } 70 | }, 71 | "nbformat": 4, 72 | "nbformat_minor": 2 73 | } 74 | -------------------------------------------------------------------------------- /Chapter 14/data_perceptron.txt: -------------------------------------------------------------------------------- 1 | 0.38 0.19 0 2 | 0.17 0.31 0 3 | 0.29 0.54 0 4 | 0.89 0.55 1 5 | 0.78 0.36 1 -------------------------------------------------------------------------------- /Chapter 14/data_simple_nn.txt: -------------------------------------------------------------------------------- 1 | 1.0 4.0 0 0 2 | 1.1 3.9 0 0 3 | 1.2 4.1 0 0 4 | 0.9 3.7 0 0 5 | 7.0 4.0 0 1 6 | 7.2 4.1 0 1 7 | 6.9 3.9 0 1 8 | 7.1 4.2 0 1 9 | 4.0 1.0 1 0 10 | 4.1 0.9 1 0 11 | 4.2 1.1 1 0 12 | 3.9 0.8 1 0 13 | 4.0 7.0 1 1 14 | 4.2 7.2 1 1 15 | 3.9 7.1 1 1 16 | 4.1 6.8 1 1 17 | -------------------------------------------------------------------------------- /Chapter 14/data_vector_quantization.txt: -------------------------------------------------------------------------------- 1 | 0.9 5.1 1 0 0 0 2 | 1.2 4.8 1 0 0 0 3 | 1.0 4.9 1 0 0 0 4 | 0.8 5.2 1 0 0 0 5 | 8.0 4.1 0 1 0 0 6 | 8.2 4.3 0 1 0 0 7 | 7.9 3.8 0 1 0 0 8 | 8.3 4.3 0 1 0 0 9 | 5.0 1.1 0 0 1 0 10 | 5.1 0.8 0 0 1 0 11 | 5.3 1.2 0 0 1 0 12 | 4.9 0.9 0 0 1 0 13 | 5.0 7.0 0 0 0 1 14 | 5.2 7.2 0 0 0 1 15 | 4.9 7.1 0 0 0 1 16 | 5.1 6.8 0 0 0 1 17 | -------------------------------------------------------------------------------- /Chapter 14/multilayer_neural_network.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "import matplotlib.pyplot as plt \n", 11 | "import neurolab as nl \n", 12 | "%matplotlib inline\n", 13 | "\n", 14 | "min_val = -15 \n", 15 | "max_val = 15 \n", 16 | "num_points = 130 \n", 17 | "x = np.linspace(min_val, max_val, num_points) \n", 18 | "y = 3 * np.square(x) + 5 \n", 19 | "y /= np.linalg.norm(y) \n", 20 | "\n", 21 | "data = x.reshape(-1, 1) \n", 22 | "labels = y.reshape(-1, 1) \n", 23 | "\n", 24 | "plt.figure() \n", 25 | "plt.scatter(data, labels) \n", 26 | "plt.xlabel('Dimension 1') \n", 27 | "plt.ylabel('Dimension 2') \n", 28 | "plt.title('Input data') \n", 29 | "plt.show()" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": null, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "nn = nl.net.newff([[min_val, max_val]], [10, 6, 1]) \n", 39 | "nn.trainf = nl.train.train_gd \n", 40 | "error_progress = nn.train(data, labels, epochs=2000, show=100, goal=0.01) \n", 41 | "\n", 42 | "plt.figure() \n", 43 | "plt.plot(error_progress) \n", 44 | "plt.xlabel('Number of epochs') \n", 45 | "plt.ylabel('Error') \n", 46 | "plt.title('Training error progress') \n", 47 | "plt.show()" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "output = nn.sim(data) \n", 57 | "\n", 58 | "plt.figure() \n", 59 | "plt.scatter(data, labels, marker='.') \n", 60 | "plt.scatter(data, output) \n", 61 | "plt.title('Actual vs predicted') \n", 62 | "plt.show() " 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [] 71 | } 72 | ], 73 | "metadata": { 74 | "kernelspec": { 75 | "display_name": "Python 3", 76 | "language": "python", 77 | "name": "python3" 78 | }, 79 | "language_info": { 80 | "codemirror_mode": { 81 | "name": "ipython", 82 | "version": 3 83 | }, 84 | "file_extension": ".py", 85 | "mimetype": "text/x-python", 86 | "name": "python", 87 | "nbconvert_exporter": "python", 88 | "pygments_lexer": "ipython3", 89 | "version": "3.6.7" 90 | } 91 | }, 92 | "nbformat": 4, 93 | "nbformat_minor": 2 94 | } 95 | -------------------------------------------------------------------------------- /Chapter 14/ocr.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "import neurolab as nl\n", 11 | "\n", 12 | "input_file = 'letter.data'\n", 13 | "\n", 14 | "num_datapoints = 50\n", 15 | "num_train = int(0.9 * num_datapoints)\n", 16 | "num_test = num_datapoints - num_train\n", 17 | "\n", 18 | "orig_labels = 'omandig'\n", 19 | "num_orig_labels = len(orig_labels)\n", 20 | "\n", 21 | "data = []\n", 22 | "labels = []\n", 23 | "\n", 24 | "with open(input_file, 'r') as f:\n", 25 | " for line in f.readlines():\n", 26 | " list_vals = line.split('\\t')\n", 27 | "\n", 28 | " if list_vals[1] not in orig_labels:\n", 29 | " continue\n", 30 | "\n", 31 | " label = np.zeros((num_orig_labels, 1))\n", 32 | " label[orig_labels.index(list_vals[1])] = 1\n", 33 | " labels.append(label)\n", 34 | " \n", 35 | " cur_char = np.array([float(x) for x in list_vals[6:-1]])\n", 36 | " data.append(cur_char)\n", 37 | "\n", 38 | " if len(data) >= num_datapoints:\n", 39 | " break\n", 40 | "\n", 41 | "labels = np.array(labels).reshape(-1, num_orig_labels)\n", 42 | "num_dims = 8 * 16\n", 43 | "data = np.array(data).reshape(-1, num_dims)\n", 44 | "\n", 45 | "nn = nl.net.newff([[0, 1]] * num_dims, [128, 16, num_orig_labels])\n", 46 | "nn.trainf = nl.train.train_gd\n", 47 | "\n", 48 | "error_progress = nn.train(data[:num_train,:], labels[:num_train,:], \n", 49 | " epochs=10000, show=100, goal=0.01)" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "print('Testing on unknown data:') \n", 59 | "predicted = nn.sim(data[num_train:, :]) \n", 60 | "for i in range(num_test): \n", 61 | " print('\\nOriginal:', orig_labels[np.argmax(labels[num_train+i])]) \n", 62 | " print('Predicted:', orig_labels[np.argmax(predicted[i])]) " 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [] 71 | } 72 | ], 73 | "metadata": { 74 | "kernelspec": { 75 | "display_name": "Python 3", 76 | "language": "python", 77 | "name": "python3" 78 | }, 79 | "language_info": { 80 | "codemirror_mode": { 81 | "name": "ipython", 82 | "version": 3 83 | }, 84 | "file_extension": ".py", 85 | "mimetype": "text/x-python", 86 | "name": "python", 87 | "nbconvert_exporter": "python", 88 | "pygments_lexer": "ipython3", 89 | "version": "3.6.7" 90 | } 91 | }, 92 | "nbformat": 4, 93 | "nbformat_minor": 2 94 | } 95 | -------------------------------------------------------------------------------- /Chapter 14/perceptron_classifier.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "import matplotlib.pyplot as plt \n", 11 | "import neurolab as nl \n", 12 | "%matplotlib inline\n", 13 | "\n", 14 | "text = np.loadtxt('data_perceptron.txt') \n", 15 | "data = text[:, :2] \n", 16 | "labels = text[:, 2]\n", 17 | "\n", 18 | "plt.figure() \n", 19 | "plt.scatter(data[labels==0, 0], data[labels==0,1], marker='o')\n", 20 | "plt.scatter(data[labels==1, 0], data[labels==1,1], marker='x')\n", 21 | "plt.xlabel('Dimension 1') \n", 22 | "plt.ylabel('Dimension 2') \n", 23 | "plt.title('Input data') \n", 24 | "plt.show()" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": null, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "dim1_min, dim1_max, dim2_min, dim2_max = 0, 1, 0, 1 \n", 34 | "dim1 = [dim1_min, dim1_max] \n", 35 | "dim2 = [dim2_min, dim2_max] \n", 36 | "num_output = 1\n", 37 | "\n", 38 | "perceptron = nl.net.newp([dim1, dim2], num_output) \n", 39 | "error_progress = perceptron.train(data, labels.reshape(-1,1), epochs=100, show=20, lr=0.03) \n", 40 | "\n", 41 | "plt.figure() \n", 42 | "plt.plot(error_progress) \n", 43 | "plt.xlabel('Number of epochs') \n", 44 | "plt.ylabel('Training error') \n", 45 | "plt.title('Training error progress') \n", 46 | "plt.grid() \n", 47 | "plt.show() " 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "xy = np.random.rand(100, 2)\n", 57 | "out = perceptron.sim(xy).ravel()\n", 58 | "\n", 59 | "plt.figure()\n", 60 | "plt.scatter(xy[out==0,0], xy[out==0,1], marker='o')\n", 61 | "plt.scatter(xy[out==1,0], xy[out==1,1], marker='x')\n", 62 | "plt.xlabel('Dimension 1') \n", 63 | "plt.ylabel('Dimension 2') \n", 64 | "plt.show()" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": null, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [] 73 | } 74 | ], 75 | "metadata": { 76 | "kernelspec": { 77 | "display_name": "Python 3", 78 | "language": "python", 79 | "name": "python3" 80 | }, 81 | "language_info": { 82 | "codemirror_mode": { 83 | "name": "ipython", 84 | "version": 3 85 | }, 86 | "file_extension": ".py", 87 | "mimetype": "text/x-python", 88 | "name": "python", 89 | "nbconvert_exporter": "python", 90 | "pygments_lexer": "ipython3", 91 | "version": "3.6.7" 92 | } 93 | }, 94 | "nbformat": 4, 95 | "nbformat_minor": 2 96 | } 97 | -------------------------------------------------------------------------------- /Chapter 14/vector_quantizer.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "import matplotlib.pyplot as plt \n", 11 | "import neurolab as nl \n", 12 | "%matplotlib inline\n", 13 | "\n", 14 | "text = np.loadtxt('data_vector_quantization.txt') \n", 15 | "data = text[:, 0:2] \n", 16 | "labels = text[:, 2:] \n", 17 | "\n", 18 | "num_input_neurons = 10 \n", 19 | "num_output_neurons = 4 \n", 20 | "weights = [1/num_output_neurons] * num_output_neurons \n", 21 | "nn = nl.net.newlvq(nl.tool.minmax(data), num_input_neurons, weights) \n", 22 | "\n", 23 | "nn.train(data, labels, epochs=500, goal=-1) \n", 24 | "\n", 25 | "xx, yy = np.meshgrid(np.arange(0, 10, 0.2), np.arange(0, 10, 0.2)) \n", 26 | "xx = xx.reshape(-1, 1) \n", 27 | "yy = yy.reshape(-1, 1) \n", 28 | "grid_xy = np.hstack([xx, yy])\n", 29 | "\n", 30 | "grid_eval = nn.sim(grid_xy) \n", 31 | "\n", 32 | "grid_1 = grid_xy[grid_eval[:,0] == 1] \n", 33 | "grid_2 = grid_xy[grid_eval[:,1] == 1] \n", 34 | "grid_3 = grid_xy[grid_eval[:,2] == 1] \n", 35 | "grid_4 = grid_xy[grid_eval[:,3] == 1] \n", 36 | "\n", 37 | "plt.plot(grid_1[:,0], grid_1[:,1], 'm.', \n", 38 | " grid_2[:,0], grid_2[:,1], 'bx', \n", 39 | " grid_3[:,0], grid_3[:,1], 'c^', \n", 40 | " grid_4[:,0], grid_4[:,1], 'y+') \n", 41 | "\n", 42 | "class_1 = data[labels[:,0] == 1] \n", 43 | "class_2 = data[labels[:,1] == 1] \n", 44 | "class_3 = data[labels[:,2] == 1] \n", 45 | "class_4 = data[labels[:,3] == 1] \n", 46 | "\n", 47 | "plt.plot(class_1[:,0], class_1[:,1], 'ko', \n", 48 | " class_2[:,0], class_2[:,1], 'ko', \n", 49 | " class_3[:,0], class_3[:,1], 'ko', \n", 50 | " class_4[:,0], class_4[:,1], 'ko') \n", 51 | "\n", 52 | "plt.axis([0, 10, 0, 10]) \n", 53 | "plt.xlabel('Dimension 1') \n", 54 | "plt.ylabel('Dimension 2') \n", 55 | "plt.title('Vector quantization') \n", 56 | "\n", 57 | "plt.show() " 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": null, 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [] 66 | } 67 | ], 68 | "metadata": { 69 | "kernelspec": { 70 | "display_name": "Python 3", 71 | "language": "python", 72 | "name": "python3" 73 | }, 74 | "language_info": { 75 | "codemirror_mode": { 76 | "name": "ipython", 77 | "version": 3 78 | }, 79 | "file_extension": ".py", 80 | "mimetype": "text/x-python", 81 | "name": "python", 82 | "nbconvert_exporter": "python", 83 | "pygments_lexer": "ipython3", 84 | "version": "3.6.7" 85 | } 86 | }, 87 | "nbformat": 4, 88 | "nbformat_minor": 2 89 | } 90 | -------------------------------------------------------------------------------- /Chapter 15/agent1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "scrolled": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import gym\n", 12 | "\n", 13 | "env = gym.make('CartPole-v1')\n", 14 | "\n", 15 | "steps = []\n", 16 | "for episode in range(100):\n", 17 | " observation = env.reset()\n", 18 | "\n", 19 | " for step in range(200):\n", 20 | " env.render()\n", 21 | " _,_,th,_ = observation\n", 22 | " if th < 0:\n", 23 | " action = 0\n", 24 | " else:\n", 25 | " action = 1\n", 26 | "\n", 27 | " observation, reward, done, info = env.step(action)\n", 28 | " if done: break\n", 29 | " \n", 30 | " print('Episode {} finished after {} timesteps'.format(episode+1, step+1))\n", 31 | " steps.append(step+1)\n", 32 | " \n", 33 | "env.close()" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [ 42 | "import matplotlib.pyplot as plt\n", 43 | "%matplotlib inline\n", 44 | "\n", 45 | "plt.figure()\n", 46 | "plt.plot(steps)\n", 47 | "plt.xlabel('Episode')\n", 48 | "plt.ylabel('Step')\n", 49 | "plt.show()" 50 | ] 51 | } 52 | ], 53 | "metadata": { 54 | "kernelspec": { 55 | "display_name": "Python 3", 56 | "language": "python", 57 | "name": "python3" 58 | }, 59 | "language_info": { 60 | "codemirror_mode": { 61 | "name": "ipython", 62 | "version": 3 63 | }, 64 | "file_extension": ".py", 65 | "mimetype": "text/x-python", 66 | "name": "python", 67 | "nbconvert_exporter": "python", 68 | "pygments_lexer": "ipython3", 69 | "version": "3.6.7" 70 | } 71 | }, 72 | "nbformat": 4, 73 | "nbformat_minor": 2 74 | } 75 | -------------------------------------------------------------------------------- /Chapter 15/balancer.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import gym\n", 10 | "\n", 11 | "env = gym.make('CartPole-v1')\n", 12 | "\n", 13 | "for episode in range(20):\n", 14 | " observation = env.reset()\n", 15 | "\n", 16 | " for step in range(100):\n", 17 | " env.render()\n", 18 | " print(observation)\n", 19 | " action = env.action_space.sample()\n", 20 | "\n", 21 | " observation, reward, done, info = env.step(action)\n", 22 | " if done: break\n", 23 | " \n", 24 | " print('Episode {} finished after {} timesteps'.format(episode+1, step+1))\n", 25 | "env.close()" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "metadata": {}, 32 | "outputs": [], 33 | "source": [] 34 | } 35 | ], 36 | "metadata": { 37 | "kernelspec": { 38 | "display_name": "Python 3", 39 | "language": "python", 40 | "name": "python3" 41 | }, 42 | "language_info": { 43 | "codemirror_mode": { 44 | "name": "ipython", 45 | "version": 3 46 | }, 47 | "file_extension": ".py", 48 | "mimetype": "text/x-python", 49 | "name": "python", 50 | "nbconvert_exporter": "python", 51 | "pygments_lexer": "ipython3", 52 | "version": "3.6.7" 53 | } 54 | }, 55 | "nbformat": 4, 56 | "nbformat_minor": 2 57 | } 58 | -------------------------------------------------------------------------------- /Chapter 15/run_environment.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import gym\n", 10 | " \n", 11 | "name_map = ['CartPole-v1', \n", 12 | " 'MountainCar-v0',\n", 13 | " 'Pendulum-v0']\n", 14 | "\n", 15 | "example = 0\n", 16 | "env = gym.make(name_map[example])\n", 17 | "env.reset()\n", 18 | "\n", 19 | "for _ in range(1000):\n", 20 | " env.render()\n", 21 | " action = env.action_space.sample()\n", 22 | " env.step(action) \n", 23 | "\n", 24 | "env.close()" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": null, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [] 33 | } 34 | ], 35 | "metadata": { 36 | "kernelspec": { 37 | "display_name": "Python 3", 38 | "language": "python", 39 | "name": "python3" 40 | }, 41 | "language_info": { 42 | "codemirror_mode": { 43 | "name": "ipython", 44 | "version": 3 45 | }, 46 | "file_extension": ".py", 47 | "mimetype": "text/x-python", 48 | "name": "python", 49 | "nbconvert_exporter": "python", 50 | "pygments_lexer": "ipython3", 51 | "version": "3.6.7" 52 | } 53 | }, 54 | "nbformat": 4, 55 | "nbformat_minor": 2 56 | } 57 | -------------------------------------------------------------------------------- /Chapter 16/cnn_keras.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "scrolled": false 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import numpy as np\n", 12 | "from tensorflow.keras.models import Sequential\n", 13 | "from tensorflow.keras.layers import Dense, Flatten, \\\n", 14 | " MaxPooling2D, Conv2D, Dropout\n", 15 | "from tensorflow.keras.optimizers import SGD\n", 16 | "from tensorflow.keras.datasets import mnist\n", 17 | "\n", 18 | "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", 19 | "x_train = x_train / 255.0\n", 20 | "x_test = x_test / 255.0\n", 21 | "y_train = np.eye(10)[y_train]\n", 22 | "y_test = np.eye(10)[y_test]\n", 23 | "\n", 24 | "x_train = x_train.reshape(-1, 28, 28, 1)\n", 25 | "x_test = x_test.reshape(-1, 28, 28, 1)\n", 26 | "\n", 27 | "model = Sequential([\n", 28 | " Conv2D(32, (5, 5), input_shape=(28, 28, 1), activation='relu'),\n", 29 | " MaxPooling2D(pool_size=(2,2)),\n", 30 | " Conv2D(64, (5, 5), activation='relu'),\n", 31 | " MaxPooling2D(pool_size=(2,2)),\n", 32 | " Flatten(),\n", 33 | " Dense(1024, activation='relu'),\n", 34 | " Dropout(0.5),\n", 35 | " Dense(10, activation='softmax')\n", 36 | "])\n", 37 | "model.compile(loss='categorical_crossentropy',\n", 38 | " optimizer='adam',\n", 39 | " metrics=['accuracy'])\n", 40 | "\n", 41 | "history = model.fit(x_train, y_train, epochs=5, batch_size=75,\n", 42 | " verbose=1)" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "score = model.evaluate(x_test, y_test, verbose=1)\n", 52 | "print('Accuracy =', score[1])" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "%matplotlib inline\n", 62 | "import matplotlib.pyplot as plt\n", 63 | "\n", 64 | "for i in range(10):\n", 65 | " plt.figure(figsize=(1,1))\n", 66 | "\n", 67 | " score = model.predict(x_test[i].reshape(1, 28, 28, 1))\n", 68 | " predicted = np.argmax(score) \n", 69 | " answer = np.argmax(y_test[i])\n", 70 | " plt.title('Answer:' + str(answer) + ' Predicted:' + str(predicted))\n", 71 | " \n", 72 | " plt.imshow(x_test[i].reshape(28, 28), cmap='gray')\n", 73 | " plt.xticks(())\n", 74 | " plt.yticks(())\n", 75 | " plt.show()" 76 | ] 77 | } 78 | ], 79 | "metadata": { 80 | "kernelspec": { 81 | "display_name": "Python 3", 82 | "language": "python", 83 | "name": "python3" 84 | }, 85 | "language_info": { 86 | "codemirror_mode": { 87 | "name": "ipython", 88 | "version": 3 89 | }, 90 | "file_extension": ".py", 91 | "mimetype": "text/x-python", 92 | "name": "python", 93 | "nbconvert_exporter": "python", 94 | "pygments_lexer": "ipython3", 95 | "version": "3.6.7" 96 | } 97 | }, 98 | "nbformat": 4, 99 | "nbformat_minor": 2 100 | } 101 | -------------------------------------------------------------------------------- /Chapter 16/linear_regression.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "import matplotlib.pyplot as plt\n", 11 | "import tensorflow as tf\n", 12 | "%matplotlib inline\n", 13 | "\n", 14 | "num_points = 1200\n", 15 | "m = 0.2\n", 16 | "c = 0.5\n", 17 | "x_data = np.random.normal(0.0, 0.8, num_points)\n", 18 | "noise = np.random.normal(0.0, 0.04, num_points)\n", 19 | "y_data = m * x_data + c + noise \n", 20 | "\n", 21 | "plt.figure()\n", 22 | "plt.plot(x_data, y_data, 'ro')\n", 23 | "plt.title('Input data')\n", 24 | "plt.show()" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": null, 30 | "metadata": { 31 | "scrolled": false 32 | }, 33 | "outputs": [], 34 | "source": [ 35 | "W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))\n", 36 | "b = tf.Variable(tf.zeros([1]))\n", 37 | "y = W * x_data + b\n", 38 | "\n", 39 | "loss = tf.reduce_mean(tf.square(y - y_data))\n", 40 | "\n", 41 | "optimizer = tf.train.GradientDescentOptimizer(0.5)\n", 42 | "train = optimizer.minimize(loss)\n", 43 | "\n", 44 | "sess = tf.Session()\n", 45 | "\n", 46 | "init = tf.global_variables_initializer()\n", 47 | "sess.run(init)\n", 48 | "\n", 49 | "num_iterations = 10\n", 50 | "for step in range(num_iterations):\n", 51 | " \n", 52 | " sess.run(train)\n", 53 | " print('ITERATION', step+1)\n", 54 | " print('W =', sess.run(W)[0])\n", 55 | " print('b =', sess.run(b)[0])\n", 56 | " print('loss =', sess.run(loss))\n", 57 | "\n", 58 | " plt.figure()\n", 59 | " plt.plot(x_data, y_data, 'ro')\n", 60 | "\n", 61 | " plt.plot(x_data, sess.run(W) * x_data + sess.run(b))\n", 62 | " plt.title('Iteration ' + str(step+1) + ' of ' + str(num_iterations))\n", 63 | " plt.show()" 64 | ] 65 | } 66 | ], 67 | "metadata": { 68 | "kernelspec": { 69 | "display_name": "Python 3", 70 | "language": "python", 71 | "name": "python3" 72 | }, 73 | "language_info": { 74 | "codemirror_mode": { 75 | "name": "ipython", 76 | "version": 3 77 | }, 78 | "file_extension": ".py", 79 | "mimetype": "text/x-python", 80 | "name": "python", 81 | "nbconvert_exporter": "python", 82 | "pygments_lexer": "ipython3", 83 | "version": "3.6.7" 84 | } 85 | }, 86 | "nbformat": 4, 87 | "nbformat_minor": 2 88 | } 89 | -------------------------------------------------------------------------------- /Chapter 16/linear_regression_keras.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "%matplotlib inline\n", 10 | "import numpy as np\n", 11 | "import matplotlib.pyplot as plt\n", 12 | "from tensorflow.keras.callbacks import Callback\n", 13 | "from tensorflow.keras.models import Sequential\n", 14 | "from tensorflow.keras.layers import Dense\n", 15 | "from tensorflow.keras.initializers import RandomUniform\n", 16 | "from tensorflow.keras.optimizers import SGD\n", 17 | "\n", 18 | "num_points = 1200\n", 19 | "m = 0.2\n", 20 | "c = 0.5\n", 21 | "x_data = np.random.normal(0.0, 0.8, num_points)\n", 22 | "noise = np.random.normal(0.0, 0.04, num_points)\n", 23 | "y_data = m * x_data + c + noise \n", 24 | "\n", 25 | "plt.figure()\n", 26 | "plt.plot(x_data, y_data, 'ro')\n", 27 | "plt.title('Input data')\n", 28 | "plt.show()" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": null, 34 | "metadata": { 35 | "scrolled": false 36 | }, 37 | "outputs": [], 38 | "source": [ 39 | "num_iterations = 10\n", 40 | "\n", 41 | "class MyCallback(Callback):\n", 42 | " def on_epoch_end(self, epoch, logs):\n", 43 | " W = self.model.get_weights()[0][0][0]\n", 44 | " b = self.model.get_weights()[1][0]\n", 45 | " print('ITERATION', epoch+1)\n", 46 | " print('W =', W)\n", 47 | " print('b =', b)\n", 48 | " print('loss =', logs.get('loss'))\n", 49 | " plt.figure()\n", 50 | " plt.plot(x_data, y_data, 'ro')\n", 51 | " plt.plot(x_data, W * x_data + b)\n", 52 | " plt.title('Iteration ' + str(epoch+1) + ' of ' + str(num_iterations))\n", 53 | " plt.show()\n", 54 | "\n", 55 | "model = Sequential([\n", 56 | " Dense(1, activation='linear', input_shape=(1,),\n", 57 | " kernel_initializer=RandomUniform(-1.0, 1.0))\n", 58 | "])\n", 59 | "model.compile(loss='mse', optimizer=SGD(0.001))\n", 60 | "\n", 61 | "history = model.fit(x_data, y_data, batch_size=1, epochs=num_iterations,\n", 62 | " verbose=0, callbacks=[MyCallback()])" 63 | ] 64 | } 65 | ], 66 | "metadata": { 67 | "kernelspec": { 68 | "display_name": "Python 3", 69 | "language": "python", 70 | "name": "python3" 71 | }, 72 | "language_info": { 73 | "codemirror_mode": { 74 | "name": "ipython", 75 | "version": 3 76 | }, 77 | "file_extension": ".py", 78 | "mimetype": "text/x-python", 79 | "name": "python", 80 | "nbconvert_exporter": "python", 81 | "pygments_lexer": "ipython3", 82 | "version": "3.6.7" 83 | } 84 | }, 85 | "nbformat": 4, 86 | "nbformat_minor": 2 87 | } 88 | -------------------------------------------------------------------------------- /Chapter 16/single_layer.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import tensorflow as tf\n", 10 | "from tensorflow.examples.tutorials.mnist import input_data\n", 11 | "\n", 12 | "input_dir = './mnist_data'\n", 13 | "mnist = input_data.read_data_sets(input_dir, one_hot=True)\n", 14 | "\n", 15 | "x = tf.placeholder(tf.float32, [None, 28*28])\n", 16 | "W = tf.Variable(tf.zeros([28*28, 10]))\n", 17 | "b = tf.Variable(tf.zeros([10]))\n", 18 | "y = tf.matmul(x, W) + b\n", 19 | "\n", 20 | "y_loss = tf.placeholder(tf.float32, [None, 10])\n", 21 | "loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(\n", 22 | " labels=y_loss, logits=y))\n", 23 | "optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)\n", 24 | "\n", 25 | "session = tf.Session()\n", 26 | "\n", 27 | "init = tf.global_variables_initializer()\n", 28 | "session.run(init)\n", 29 | "\n", 30 | "num_iterations = 2000\n", 31 | "batch_size = 75\n", 32 | "for _ in range(num_iterations):\n", 33 | " x_batch, y_batch = mnist.train.next_batch(batch_size)\n", 34 | " session.run(optimizer, feed_dict={x: x_batch, y_loss: y_batch})\n", 35 | "\n", 36 | "predicted = tf.equal(tf.argmax(y, 1), tf.argmax(y_loss, 1))\n", 37 | "accuracy = tf.reduce_mean(tf.cast(predicted, tf.float32))\n", 38 | "print('Accuracy =', session.run(accuracy, feed_dict={\n", 39 | " x: mnist.test.images, \n", 40 | " y_loss: mnist.test.labels}))" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": null, 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [] 49 | } 50 | ], 51 | "metadata": { 52 | "kernelspec": { 53 | "display_name": "Python 3", 54 | "language": "python", 55 | "name": "python3" 56 | }, 57 | "language_info": { 58 | "codemirror_mode": { 59 | "name": "ipython", 60 | "version": 3 61 | }, 62 | "file_extension": ".py", 63 | "mimetype": "text/x-python", 64 | "name": "python", 65 | "nbconvert_exporter": "python", 66 | "pygments_lexer": "ipython3", 67 | "version": "3.6.7" 68 | } 69 | }, 70 | "nbformat": 4, 71 | "nbformat_minor": 2 72 | } 73 | -------------------------------------------------------------------------------- /Chapter 16/single_layer_keras.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "scrolled": false 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import numpy as np\n", 12 | "from tensorflow.keras.models import Sequential\n", 13 | "from tensorflow.keras.layers import Dense, Flatten\n", 14 | "from tensorflow.keras.optimizers import SGD\n", 15 | "from tensorflow.keras.datasets import mnist\n", 16 | "\n", 17 | "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", 18 | "x_train = x_train / 255.0\n", 19 | "x_test = x_test / 255.0\n", 20 | "y_train = np.eye(10)[y_train]\n", 21 | "y_test = np.eye(10)[y_test]\n", 22 | "\n", 23 | "model = Sequential([\n", 24 | " Flatten(input_shape=(28, 28)),\n", 25 | " Dense(10, activation='softmax')\n", 26 | "])\n", 27 | "model.compile(loss='categorical_crossentropy',\n", 28 | " optimizer=SGD(0.5),\n", 29 | " metrics=['accuracy'])\n", 30 | "\n", 31 | "history = model.fit(x_train, y_train, epochs=100, batch_size=75,\n", 32 | " verbose=2)" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": null, 38 | "metadata": {}, 39 | "outputs": [], 40 | "source": [ 41 | "score = model.evaluate(x_test, y_test, verbose=0)\n", 42 | "print('Accuracy =', score[1])" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [ 51 | "%matplotlib inline\n", 52 | "import matplotlib.pyplot as plt\n", 53 | "\n", 54 | "for i in range(10):\n", 55 | " plt.figure(figsize=(1,1))\n", 56 | " \n", 57 | " score = model.predict(x_test[i].reshape(1, 28, 28))\n", 58 | " predicted = np.argmax(score) \n", 59 | " answer = np.argmax(y_test[i])\n", 60 | " plt.title('Answer:' + str(answer) + ' Predicted:' + str(predicted))\n", 61 | " \n", 62 | " plt.imshow(x_test[i].reshape(28, 28), cmap='gray')\n", 63 | " plt.xticks(())\n", 64 | " plt.yticks(())\n", 65 | " plt.show()" 66 | ] 67 | } 68 | ], 69 | "metadata": { 70 | "kernelspec": { 71 | "display_name": "Python 3", 72 | "language": "python", 73 | "name": "python3" 74 | }, 75 | "language_info": { 76 | "codemirror_mode": { 77 | "name": "ipython", 78 | "version": 3 79 | }, 80 | "file_extension": ".py", 81 | "mimetype": "text/x-python", 82 | "name": "python", 83 | "nbconvert_exporter": "python", 84 | "pygments_lexer": "ipython3", 85 | "version": "3.6.7" 86 | } 87 | }, 88 | "nbformat": 4, 89 | "nbformat_minor": 2 90 | } 91 | -------------------------------------------------------------------------------- /Chapter 2/LICENSE.txt: -------------------------------------------------------------------------------- 1 | * income_data.txt 2 | https://archive.ics.uci.edu/ml/datasets/Census+Income 3 | 4 | Dua, D. and Graff, C. (2019). UCI Machine Learning Repository [http://archive.ics.uci.edu/ml]. Irvine, CA: University of California, School of Information and Computer Science. 5 | -------------------------------------------------------------------------------- /Chapter 2/confusion_matrix.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "import matplotlib.pyplot as plt \n", 11 | "%matplotlib inline\n", 12 | "from sklearn.metrics import confusion_matrix \n", 13 | "from sklearn.metrics import classification_report\n", 14 | "\n", 15 | "true_labels = [2, 0, 0, 2, 4, 4, 1, 0, 3, 3, 3] \n", 16 | "pred_labels = [2, 1, 0, 2, 4, 3, 1, 0, 1, 3, 3] \n", 17 | "\n", 18 | "confusion_mat = confusion_matrix(true_labels, pred_labels) \n", 19 | "print(confusion_mat)\n", 20 | "\n", 21 | "plt.imshow(confusion_mat, interpolation='nearest', cmap=plt.cm.gray) \n", 22 | "plt.title('Confusion matrix') \n", 23 | "plt.colorbar() \n", 24 | "ticks = np.arange(5) \n", 25 | "plt.xticks(ticks, ticks) \n", 26 | "plt.yticks(ticks, ticks) \n", 27 | "plt.ylabel('True labels') \n", 28 | "plt.xlabel('Predicted labels') \n", 29 | "plt.show()" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": null, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "targets = ['Class-0', 'Class-1', 'Class-2', 'Class-3', 'Class-4'] \n", 39 | "print('\\n', classification_report(true_labels, pred_labels, target_names=targets)) " 40 | ] 41 | } 42 | ], 43 | "metadata": { 44 | "kernelspec": { 45 | "display_name": "Python 3", 46 | "language": "python", 47 | "name": "python3" 48 | }, 49 | "language_info": { 50 | "codemirror_mode": { 51 | "name": "ipython", 52 | "version": 3 53 | }, 54 | "file_extension": ".py", 55 | "mimetype": "text/x-python", 56 | "name": "python", 57 | "nbconvert_exporter": "python", 58 | "pygments_lexer": "ipython3", 59 | "version": "3.6.7" 60 | } 61 | }, 62 | "nbformat": 4, 63 | "nbformat_minor": 2 64 | } 65 | -------------------------------------------------------------------------------- /Chapter 2/data_singlevar_regr.txt: -------------------------------------------------------------------------------- 1 | -0.86,4.38 2 | 2.58,6.97 3 | 4.17,7.01 4 | 2.6,5.44 5 | 5.13,6.45 6 | 3.23,5.49 7 | -0.26,4.25 8 | 2.76,5.94 9 | 0.47,4.8 10 | -3.9,2.7 11 | 0.27,3.26 12 | 2.88,6.48 13 | -0.54,4.08 14 | -4.39,0.09 15 | -1.12,2.74 16 | 2.09,5.8 17 | -5.78,0.16 18 | 1.77,4.97 19 | -7.91,-2.26 20 | 4.86,5.75 21 | -2.17,3.33 22 | 1.38,5.26 23 | 0.54,4.43 24 | 3.12,6.6 25 | -2.19,3.77 26 | -0.33,2.4 27 | -1.21,2.98 28 | -4.52,0.29 29 | -0.46,2.47 30 | -1.13,4.08 31 | 4.61,8.97 32 | 0.31,3.94 33 | 0.25,3.46 34 | -2.67,2.46 35 | -4.66,1.14 36 | -0.2,4.31 37 | -0.52,1.97 38 | 1.24,4.83 39 | -2.53,3.12 40 | -0.34,4.97 41 | 5.74,8.65 42 | -0.34,3.59 43 | 0.99,3.66 44 | 5.01,7.54 45 | -2.38,1.52 46 | -0.56,4.55 47 | -1.01,3.23 48 | 0.47,4.39 49 | 4.81,7.04 50 | 2.38,4.46 51 | -3.32,2.41 52 | -3.86,1.11 53 | -1.41,3.23 54 | 6.04,7.46 55 | 4.18,5.71 56 | -0.78,3.59 57 | -2.2,2.93 58 | 0.76,4.16 59 | 2.02,6.43 60 | 0.42,4.92 61 | -------------------------------------------------------------------------------- /Chapter 2/house_prices.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "from sklearn import datasets \n", 11 | "from sklearn.svm import SVR \n", 12 | "from sklearn.metrics import mean_squared_error, explained_variance_score \n", 13 | "from sklearn.utils import shuffle \n", 14 | "\n", 15 | "data = datasets.load_boston() \n", 16 | "X, y = shuffle(data.data, data.target, random_state=7) \n", 17 | "\n", 18 | "num_training = int(0.8 * len(X)) \n", 19 | "X_train, y_train = X[:num_training], y[:num_training] \n", 20 | "X_test, y_test = X[num_training:], y[num_training:] \n", 21 | "\n", 22 | "sv_regressor = SVR(kernel='linear', C=1.0, epsilon=0.1) \n", 23 | "sv_regressor.fit(X_train, y_train) \n", 24 | "\n", 25 | "y_test_pred = sv_regressor.predict(X_test) \n", 26 | "mse = mean_squared_error(y_test, y_test_pred) \n", 27 | "evs = explained_variance_score(y_test, y_test_pred) \n", 28 | "print(\"#### Performance ####\") \n", 29 | "print(\"Mean squared error =\", round(mse, 2)) \n", 30 | "print(\"Explained variance score =\", round(evs, 2)) " 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "test_data = [3.7, 0, 18.4, 1, 0.87, 5.95, 91, 2.5052, 26, 666, 20.2, 351.34, 15.27] \n", 40 | "print(\"Predicted price:\", sv_regressor.predict([test_data])[0]) " 41 | ] 42 | } 43 | ], 44 | "metadata": { 45 | "kernelspec": { 46 | "display_name": "Python 3", 47 | "language": "python", 48 | "name": "python3" 49 | }, 50 | "language_info": { 51 | "codemirror_mode": { 52 | "name": "ipython", 53 | "version": 3 54 | }, 55 | "file_extension": ".py", 56 | "mimetype": "text/x-python", 57 | "name": "python", 58 | "nbconvert_exporter": "python", 59 | "pygments_lexer": "ipython3", 60 | "version": "3.6.7" 61 | } 62 | }, 63 | "nbformat": 4, 64 | "nbformat_minor": 2 65 | } 66 | -------------------------------------------------------------------------------- /Chapter 2/label_encoder.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "from sklearn import preprocessing\n", 11 | "\n", 12 | "input_labels = ['red', 'black', 'red', 'green', 'black', 'yellow', 'white'] \n", 13 | "\n", 14 | "encoder = preprocessing.LabelEncoder() \n", 15 | "encoder.fit(input_labels) \n", 16 | "\n", 17 | "print(\"Label mapping:\") \n", 18 | "for i, item in enumerate(encoder.classes_): \n", 19 | " print(item, '-->', i) \n", 20 | " \n", 21 | "test_labels = ['green', 'red', 'black'] \n", 22 | "encoded_values = encoder.transform(test_labels) \n", 23 | "print(\"Labels =\", test_labels) \n", 24 | "print(\"Encoded values =\", list(encoded_values)) " 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": null, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "encoded_values = [3, 0, 4, 1] \n", 34 | "decoded_list = encoder.inverse_transform(encoded_values) \n", 35 | "print(\"Encoded values =\", encoded_values) \n", 36 | "print(\"Decoded labels =\", list(decoded_list)) " 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [] 45 | } 46 | ], 47 | "metadata": { 48 | "kernelspec": { 49 | "display_name": "Python 3", 50 | "language": "python", 51 | "name": "python3" 52 | }, 53 | "language_info": { 54 | "codemirror_mode": { 55 | "name": "ipython", 56 | "version": 3 57 | }, 58 | "file_extension": ".py", 59 | "mimetype": "text/x-python", 60 | "name": "python", 61 | "nbconvert_exporter": "python", 62 | "pygments_lexer": "ipython3", 63 | "version": "3.6.7" 64 | } 65 | }, 66 | "nbformat": 4, 67 | "nbformat_minor": 2 68 | } 69 | -------------------------------------------------------------------------------- /Chapter 2/logistic_regression.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "from sklearn import linear_model \n", 11 | "\n", 12 | "X = np.array([[3.1, 7.2], [4, 6.7], [2.9, 8], [5.1, 4.5], [6, 5], [5.6, 5],\n", 13 | " [3.3, 0.4], [3.9, 0.9], [2.8, 1], [0.5, 3.4], [1, 4], [0.6, 4.9]]) \n", 14 | "y = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3]) \n", 15 | "\n", 16 | "classifier = linear_model.LogisticRegression(solver='liblinear', C=1, multi_class='auto') \n", 17 | "\n", 18 | "classifier.fit(X, y) \n", 19 | "\n", 20 | "%run utilities.ipynb\n", 21 | "\n", 22 | "visualize_classifier(classifier, X, y) " 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": null, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "classifier = linear_model.LogisticRegression(solver='liblinear', C=100, multi_class='auto') \n", 32 | "classifier.fit(X, y) \n", 33 | "visualize_classifier(classifier, X, y) " 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": {}, 40 | "outputs": [], 41 | "source": [] 42 | } 43 | ], 44 | "metadata": { 45 | "kernelspec": { 46 | "display_name": "Python 3", 47 | "language": "python", 48 | "name": "python3" 49 | }, 50 | "language_info": { 51 | "codemirror_mode": { 52 | "name": "ipython", 53 | "version": 3 54 | }, 55 | "file_extension": ".py", 56 | "mimetype": "text/x-python", 57 | "name": "python", 58 | "nbconvert_exporter": "python", 59 | "pygments_lexer": "ipython3", 60 | "version": "3.6.7" 61 | } 62 | }, 63 | "nbformat": 4, 64 | "nbformat_minor": 2 65 | } 66 | -------------------------------------------------------------------------------- /Chapter 2/naive_bayes.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "from sklearn.naive_bayes import GaussianNB\n", 11 | "%run utilities.ipynb\n", 12 | "\n", 13 | "input_file = 'data_multivar_nb.txt' \n", 14 | "\n", 15 | "data = np.loadtxt(input_file, delimiter=',') \n", 16 | "X, y = data[:, :-1], data[:, -1] \n", 17 | "\n", 18 | "classifier = GaussianNB()\n", 19 | "classifier.fit(X, y)\n", 20 | "\n", 21 | "y_pred = classifier.predict(X)\n", 22 | "\n", 23 | "accuracy = 100.0 * (y == y_pred).sum() / X.shape[0] \n", 24 | "print(\"Accuracy of Naive Bayes classifier =\", round(accuracy, 2), \"%\") \n", 25 | "\n", 26 | "visualize_classifier(classifier, X, y)" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "from sklearn import model_selection \n", 36 | "\n", 37 | "X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y,\n", 38 | " test_size=0.2, random_state=3) \n", 39 | "classifier_new = GaussianNB() \n", 40 | "classifier_new.fit(X_train, y_train) \n", 41 | "y_test_pred = classifier_new.predict(X_test) \n", 42 | "\n", 43 | "accuracy = 100.0 * (y_test == y_test_pred).sum() / X_test.shape[0] \n", 44 | "print(\"Accuracy of the new classifier =\", round(accuracy, 2), \"%\") \n", 45 | "\n", 46 | "visualize_classifier(classifier_new, X_test, y_test) \n", 47 | "\n", 48 | "num_folds = 3 \n", 49 | "accuracy_values = model_selection.cross_val_score(classifier, \n", 50 | " X, y, scoring='accuracy', cv=num_folds) \n", 51 | "print(\"Accuracy: \" + str(round(100*accuracy_values.mean(), 2)) + \"%\") \n", 52 | "\n", 53 | "precision_values = model_selection.cross_val_score(classifier, \n", 54 | " X, y, scoring='precision_weighted', cv=num_folds) \n", 55 | "print(\"Precision: \" + str(round(100*precision_values.mean(), 2)) + \"%\") \n", 56 | "\n", 57 | "recall_values = model_selection.cross_val_score(classifier, \n", 58 | " X, y, scoring='recall_weighted', cv=num_folds) \n", 59 | "print(\"Recall: \" + str(round(100*recall_values.mean(), 2)) + \"%\") \n", 60 | "\n", 61 | "f1_values = model_selection.cross_val_score(classifier, \n", 62 | " X, y, scoring='f1_weighted', cv=num_folds) \n", 63 | "print(\"F1: \" + str(round(100*f1_values.mean(), 2)) + \"%\") " 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [] 72 | } 73 | ], 74 | "metadata": { 75 | "kernelspec": { 76 | "display_name": "Python 3", 77 | "language": "python", 78 | "name": "python3" 79 | }, 80 | "language_info": { 81 | "codemirror_mode": { 82 | "name": "ipython", 83 | "version": 3 84 | }, 85 | "file_extension": ".py", 86 | "mimetype": "text/x-python", 87 | "name": "python", 88 | "nbconvert_exporter": "python", 89 | "pygments_lexer": "ipython3", 90 | "version": "3.6.7" 91 | } 92 | }, 93 | "nbformat": 4, 94 | "nbformat_minor": 2 95 | } 96 | -------------------------------------------------------------------------------- /Chapter 2/preprocessing.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "from sklearn import preprocessing\n", 11 | "\n", 12 | "input_data = np.array([[5.1, -2.9, 3.3], \n", 13 | " [-1.2, 7.8, -6.1], \n", 14 | " [3.9, 0.4, 2.1], \n", 15 | " [7.3, -9.9, -4.5]])\n", 16 | "\n", 17 | "data_binarized = preprocessing.Binarizer(threshold=2.1).transform(input_data) \n", 18 | "print(\"Binarized data:\\n\", data_binarized) " 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "print(\"BEFORE:\") \n", 28 | "print(\"Mean =\", input_data.mean(axis=0)) \n", 29 | "print(\"Std deviation =\", input_data.std(axis=0)) \n", 30 | "\n", 31 | "data_scaled = preprocessing.scale(input_data) \n", 32 | "print(\"AFTER:\") \n", 33 | "print(\"Mean =\", data_scaled.mean(axis=0)) \n", 34 | "print(\"Std deviation =\", data_scaled.std(axis=0)) " 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": null, 40 | "metadata": {}, 41 | "outputs": [], 42 | "source": [ 43 | "data_centered = preprocessing.scale(input_data, with_std=False) \n", 44 | "print(\"AFTER:\") \n", 45 | "print(\"Mean =\", data_centered.mean(axis=0)) \n", 46 | "print(\"Std deviation =\", data_centered.std(axis=0)) " 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [ 55 | "data_scaler_minmax = preprocessing.MinMaxScaler(feature_range=(0, 1)) \n", 56 | "data_scaled_minmax = data_scaler_minmax.fit_transform(input_data) \n", 57 | "print(\"Min max scaled data:\\n\", data_scaled_minmax) " 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": null, 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [ 66 | "data_normalized_l1 = preprocessing.normalize(input_data, norm='l1') \n", 67 | "data_normalized_l2 = preprocessing.normalize(input_data, norm='l2') \n", 68 | "print(\"L1 normalized data:\\n\", data_normalized_l1) \n", 69 | "print(\"L2 normalized data:\\n\", data_normalized_l2)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [] 78 | } 79 | ], 80 | "metadata": { 81 | "kernelspec": { 82 | "display_name": "Python 3", 83 | "language": "python", 84 | "name": "python3" 85 | }, 86 | "language_info": { 87 | "codemirror_mode": { 88 | "name": "ipython", 89 | "version": 3 90 | }, 91 | "file_extension": ".py", 92 | "mimetype": "text/x-python", 93 | "name": "python", 94 | "nbconvert_exporter": "python", 95 | "pygments_lexer": "ipython3", 96 | "version": "3.6.7" 97 | } 98 | }, 99 | "nbformat": 4, 100 | "nbformat_minor": 2 101 | } 102 | -------------------------------------------------------------------------------- /Chapter 2/regressor_multivar.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "from sklearn import linear_model \n", 11 | "import sklearn.metrics as sm \n", 12 | "\n", 13 | "input_file = 'data_multivar_regr.txt'\n", 14 | "data = np.loadtxt(input_file, delimiter=',') \n", 15 | "X, y = data[:, :-1], data[:, -1] \n", 16 | "\n", 17 | "linear_regressor = linear_model.LinearRegression() \n", 18 | "linear_regressor.fit(X, y) \n", 19 | "\n", 20 | "y_pred = linear_regressor.predict(X) \n", 21 | "\n", 22 | "print(\"Linear Regressor performance:\") \n", 23 | "print(\"Mean absolute error =\", round(sm.mean_absolute_error(y, y_pred), 2)) \n", 24 | "print(\"Mean squared error =\", round(sm.mean_squared_error(y, y_pred), 2)) \n", 25 | "print(\"Median absolute error =\", round(sm.median_absolute_error(y, y_pred), 2)) \n", 26 | "print(\"Explained variance score =\", round(sm.explained_variance_score(y, y_pred), 2)) \n", 27 | "print(\"R2 score =\", round(sm.r2_score(y, y_pred), 2)) " 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "print(\"Linear regression:\\n\", linear_regressor.predict(X[0:5])) \n", 37 | "print(\"True output values:\\n\", y[0:5])" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": null, 43 | "metadata": {}, 44 | "outputs": [], 45 | "source": [] 46 | } 47 | ], 48 | "metadata": { 49 | "kernelspec": { 50 | "display_name": "Python 3", 51 | "language": "python", 52 | "name": "python3" 53 | }, 54 | "language_info": { 55 | "codemirror_mode": { 56 | "name": "ipython", 57 | "version": 3 58 | }, 59 | "file_extension": ".py", 60 | "mimetype": "text/x-python", 61 | "name": "python", 62 | "nbconvert_exporter": "python", 63 | "pygments_lexer": "ipython3", 64 | "version": "3.6.7" 65 | } 66 | }, 67 | "nbformat": 4, 68 | "nbformat_minor": 2 69 | } 70 | -------------------------------------------------------------------------------- /Chapter 2/regressor_singlevar.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "from sklearn import linear_model \n", 11 | "import sklearn.metrics as sm \n", 12 | "import matplotlib.pyplot as plt\n", 13 | "%matplotlib inline\n", 14 | "\n", 15 | "input_file = 'data_singlevar_regr.txt'\n", 16 | "data = np.loadtxt(input_file, delimiter=',') \n", 17 | "X, y = data[:, :-1], data[:, -1] \n", 18 | "\n", 19 | "regressor = linear_model.LinearRegression() \n", 20 | "regressor.fit(X, y) \n", 21 | "\n", 22 | "y_pred = regressor.predict(X) \n", 23 | "\n", 24 | "plt.scatter(X, y, color='green') \n", 25 | "plt.plot(X, y_pred, color='black') \n", 26 | "plt.show() " 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "print(\"Linear regressor performance:\") \n", 36 | "print(\"Mean absolute error =\", round(sm.mean_absolute_error(y, y_pred), 2)) \n", 37 | "print(\"Mean squared error =\", round(sm.mean_squared_error(y, y_pred), 2)) \n", 38 | "print(\"Median absolute error =\", round(sm.median_absolute_error(y, y_pred), 2)) \n", 39 | "print(\"Explain variance score =\", round(sm.explained_variance_score(y, y_pred), 2)) \n", 40 | "print(\"R2 score =\", round(sm.r2_score(y, y_pred), 2)) " 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": null, 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "import pickle \n", 50 | "# モデルを保存\n", 51 | "output_model_file = 'model.pkl' \n", 52 | "with open(output_model_file, 'wb') as f: \n", 53 | " pickle.dump(regressor, f) " 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [ 62 | "# モデルを読み込む\n", 63 | "with open(output_model_file, 'rb') as f: \n", 64 | " regressor_model = pickle.load(f) \n", 65 | " \n", 66 | "# テストデータで推論する\n", 67 | "y_pred_new = regressor_model.predict(X) \n", 68 | "print(\"New mean absolute error =\", \n", 69 | " round(sm.mean_absolute_error(y, y_pred_new), 2)) " 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [] 78 | } 79 | ], 80 | "metadata": { 81 | "kernelspec": { 82 | "display_name": "Python 3", 83 | "language": "python", 84 | "name": "python3" 85 | }, 86 | "language_info": { 87 | "codemirror_mode": { 88 | "name": "ipython", 89 | "version": 3 90 | }, 91 | "file_extension": ".py", 92 | "mimetype": "text/x-python", 93 | "name": "python", 94 | "nbconvert_exporter": "python", 95 | "pygments_lexer": "ipython3", 96 | "version": "3.6.7" 97 | } 98 | }, 99 | "nbformat": 4, 100 | "nbformat_minor": 2 101 | } 102 | -------------------------------------------------------------------------------- /Chapter 2/utilities.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "import matplotlib.pyplot as plt\n", 11 | "%matplotlib inline\n", 12 | "\n", 13 | "def visualize_classifier(classifier, X, y, title=''): \n", 14 | " min_x, max_x = X[:, 0].min() - 1.0, X[:, 0].max() + 1.0 \n", 15 | " min_y, max_y = X[:, 1].min() - 1.0, X[:, 1].max() + 1.0 \n", 16 | " \n", 17 | " mesh_step_size = 0.01 \n", 18 | "\n", 19 | " x_vals, y_vals = np.meshgrid(np.arange(min_x, max_x, mesh_step_size),\n", 20 | " np.arange(min_y, max_y, mesh_step_size)) \n", 21 | " \n", 22 | " output = classifier.predict(np.c_[x_vals.ravel(), y_vals.ravel()]) \n", 23 | " output = output.reshape(x_vals.shape) \n", 24 | " \n", 25 | " plt.figure() \n", 26 | " plt.title(title)\n", 27 | " plt.pcolormesh(x_vals, y_vals, output, cmap=plt.cm.gray) \n", 28 | " plt.scatter(X[:, 0], X[:, 1], c=y, s=75, edgecolors='black', linewidth=1,\n", 29 | " cmap=plt.cm.Paired) \n", 30 | " \n", 31 | " plt.xlim(x_vals.min(), x_vals.max()) \n", 32 | " plt.ylim(y_vals.min(), y_vals.max()) \n", 33 | "\n", 34 | " plt.xticks((np.arange(int(min_x), int(max_x), 1.0))) \n", 35 | " plt.yticks((np.arange(int(min_y), int(max_y), 1.0))) \n", 36 | "\n", 37 | " plt.show() \n", 38 | " " 39 | ] 40 | } 41 | ], 42 | "metadata": { 43 | "kernelspec": { 44 | "display_name": "Python 3", 45 | "language": "python", 46 | "name": "python3" 47 | }, 48 | "language_info": { 49 | "codemirror_mode": { 50 | "name": "ipython", 51 | "version": 3 52 | }, 53 | "file_extension": ".py", 54 | "mimetype": "text/x-python", 55 | "name": "python", 56 | "nbconvert_exporter": "python", 57 | "pygments_lexer": "ipython3", 58 | "version": "3.6.7" 59 | } 60 | }, 61 | "nbformat": 4, 62 | "nbformat_minor": 2 63 | } 64 | -------------------------------------------------------------------------------- /Chapter 3/LICENSE.txt: -------------------------------------------------------------------------------- 1 | * traffic_data.txt 2 | https://archive.ics.uci.edu/ml/datasets/Dodgers+Loop+Sensor 3 | 4 | These loop sensor measurements were obtained from the Freeway Performance Measurement System (PeMS), http://pems.dot.ca.gov/ 5 | -------------------------------------------------------------------------------- /Chapter 3/class_imbalance.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "import matplotlib.pyplot as plt \n", 11 | "from sklearn.ensemble import ExtraTreesClassifier \n", 12 | "from sklearn import model_selection\n", 13 | "from sklearn.metrics import classification_report \n", 14 | "%matplotlib inline\n", 15 | "%run utilities.ipynb\n", 16 | "\n", 17 | "input_file = 'data_imbalance.txt' \n", 18 | "data = np.loadtxt(input_file, delimiter=',') \n", 19 | "X, y = data[:, :-1], data[:, -1] \n", 20 | "\n", 21 | "class_0 = np.array(X[y==0]) \n", 22 | "class_1 = np.array(X[y==1]) \n", 23 | "\n", 24 | "plt.figure() \n", 25 | "plt.scatter(class_0[:, 0], class_0[:, 1], s=75, facecolors='black', \n", 26 | " edgecolors='black', linewidth=1, marker='x') \n", 27 | "plt.scatter(class_1[:, 0], class_1[:, 1], s=75, facecolors='white', \n", 28 | " edgecolors='black', linewidth=1, marker='o') \n", 29 | "plt.title('Input data') \n", 30 | "plt.show()" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "X_train, X_test, y_train, y_test = model_selection.train_test_split( \n", 40 | " X, y, test_size=0.25, random_state=5) \n", 41 | "\n", 42 | "params = {'n_estimators': 100, 'max_depth': 4, 'random_state': 0,\n", 43 | "# 'class_weight': 'balanced'\n", 44 | " } \n", 45 | "\n", 46 | "classifier = ExtraTreesClassifier(**params) \n", 47 | "classifier.fit(X_train, y_train) \n", 48 | "#visualize_classifier(classifier, X_train, y_train, 'Training dataset')\n", 49 | "\n", 50 | "y_test_pred = classifier.predict(X_test) \n", 51 | "visualize_classifier(classifier, X_test, y_test, 'Test dataset') \n", 52 | "\n", 53 | "class_names = ['Class-0', 'Class-1'] \n", 54 | "#print(\"Classifier performance on training dataset\\n\") \n", 55 | "#print(classification_report(y_train, classifier.predict(X_train), \n", 56 | "# target_names=class_names)) \n", 57 | " \n", 58 | "print(\"\\nClassifier performance on test dataset\\n\") \n", 59 | "print(classification_report(y_test, y_test_pred, target_names=class_names)) " 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": null, 65 | "metadata": {}, 66 | "outputs": [], 67 | "source": [] 68 | } 69 | ], 70 | "metadata": { 71 | "kernelspec": { 72 | "display_name": "Python 3", 73 | "language": "python", 74 | "name": "python3" 75 | }, 76 | "language_info": { 77 | "codemirror_mode": { 78 | "name": "ipython", 79 | "version": 3 80 | }, 81 | "file_extension": ".py", 82 | "mimetype": "text/x-python", 83 | "name": "python", 84 | "nbconvert_exporter": "python", 85 | "pygments_lexer": "ipython3", 86 | "version": "3.6.7" 87 | } 88 | }, 89 | "nbformat": 4, 90 | "nbformat_minor": 2 91 | } 92 | -------------------------------------------------------------------------------- /Chapter 3/decision_trees.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "import matplotlib.pyplot as plt \n", 11 | "from sklearn.metrics import classification_report \n", 12 | "from sklearn import model_selection\n", 13 | "from sklearn.tree import DecisionTreeClassifier \n", 14 | "%matplotlib inline\n", 15 | "%run utilities.ipynb\n", 16 | "\n", 17 | "input_file = 'data_decision_trees.txt' \n", 18 | "data = np.loadtxt(input_file, delimiter=',') \n", 19 | "X, y = data[:, :-1], data[:, -1] \n", 20 | "\n", 21 | "class_0 = np.array(X[y==0]) \n", 22 | "class_1 = np.array(X[y==1]) \n", 23 | "\n", 24 | "plt.figure() \n", 25 | "plt.scatter(class_0[:, 0], class_0[:, 1], s=75, facecolors='black', \n", 26 | " edgecolors='black', linewidth=1, marker='x') \n", 27 | "plt.scatter(class_1[:, 0], class_1[:, 1], s=75, facecolors='white', \n", 28 | " edgecolors='black', linewidth=1, marker='o') \n", 29 | "plt.title('Input data') \n", 30 | "plt.show()" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "X_train, X_test, y_train, y_test = model_selection.train_test_split( \n", 40 | " X, y, test_size=0.25, random_state=5) \n", 41 | "\n", 42 | "params = {'random_state': 0, 'max_depth': 4} \n", 43 | "classifier = DecisionTreeClassifier(**params) \n", 44 | "classifier.fit(X_train, y_train) \n", 45 | "visualize_classifier(classifier, X_train, y_train, 'Training dataset') " 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "y_test_pred = classifier.predict(X_test) \n", 55 | "visualize_classifier(classifier, X_test, y_test, 'Test dataset') " 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": null, 61 | "metadata": {}, 62 | "outputs": [], 63 | "source": [ 64 | "class_names = ['Class-0', 'Class-1'] \n", 65 | "print(\"Classifier performance on training dataset\\n\") \n", 66 | "print(classification_report(y_train, classifier.predict(X_train), \n", 67 | " target_names=class_names)) \n", 68 | "\n", 69 | "print(\"\\nClassifier performance on test dataset\\n\") \n", 70 | "print(classification_report(y_test, y_test_pred, target_names=class_names)) " 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": {}, 77 | "outputs": [], 78 | "source": [] 79 | } 80 | ], 81 | "metadata": { 82 | "kernelspec": { 83 | "display_name": "Python 3", 84 | "language": "python", 85 | "name": "python3" 86 | }, 87 | "language_info": { 88 | "codemirror_mode": { 89 | "name": "ipython", 90 | "version": 3 91 | }, 92 | "file_extension": ".py", 93 | "mimetype": "text/x-python", 94 | "name": "python", 95 | "nbconvert_exporter": "python", 96 | "pygments_lexer": "ipython3", 97 | "version": "3.6.7" 98 | } 99 | }, 100 | "nbformat": 4, 101 | "nbformat_minor": 2 102 | } 103 | -------------------------------------------------------------------------------- /Chapter 3/feature_importance.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "import matplotlib.pyplot as plt\n", 11 | "%matplotlib inline\n", 12 | "from sklearn.tree import DecisionTreeRegressor \n", 13 | "from sklearn.ensemble import AdaBoostRegressor \n", 14 | "from sklearn import datasets \n", 15 | "from sklearn.metrics import mean_squared_error, explained_variance_score \n", 16 | "from sklearn import model_selection\n", 17 | "from sklearn.utils import shuffle \n", 18 | "\n", 19 | "housing_data = datasets.load_boston() \n", 20 | "\n", 21 | "X, y = shuffle(housing_data.data, housing_data.target, random_state=7) \n", 22 | "\n", 23 | "X_train, X_test, y_train, y_test = model_selection.train_test_split( \n", 24 | " X, y, test_size=0.2, random_state=7) \n", 25 | "\n", 26 | "regressor = AdaBoostRegressor(DecisionTreeRegressor(max_depth=4), \n", 27 | " n_estimators=400, random_state=7) \n", 28 | "regressor.fit(X_train, y_train) \n", 29 | "\n", 30 | "y_pred = regressor.predict(X_test) \n", 31 | "mse = mean_squared_error(y_test, y_pred) \n", 32 | "evs = explained_variance_score(y_test, y_pred ) \n", 33 | "print(\"ADABOOST REGRESSOR\") \n", 34 | "print(\"Mean squared error =\", round(mse, 2)) \n", 35 | "print(\"Explained variance score =\", round(evs, 2)) \n", 36 | "\n", 37 | "feature_importances = regressor.feature_importances_ \n", 38 | "feature_names = housing_data.feature_names \n", 39 | "\n", 40 | "feature_importances = 100.0 * (feature_importances / max(feature_importances))\n", 41 | "\n", 42 | "index_sorted = np.flipud(np.argsort(feature_importances)) \n", 43 | "\n", 44 | "pos = np.arange(index_sorted.shape[0]) + 0.5 \n", 45 | "\n", 46 | "plt.figure() \n", 47 | "plt.bar(pos, feature_importances[index_sorted], align='center') \n", 48 | "plt.xticks(pos, feature_names[index_sorted]) \n", 49 | "plt.ylabel('Relative Importance') \n", 50 | "plt.title('Feature importance using AdaBoost regressor') \n", 51 | "plt.show() " 52 | ] 53 | } 54 | ], 55 | "metadata": { 56 | "kernelspec": { 57 | "display_name": "Python 3", 58 | "language": "python", 59 | "name": "python3" 60 | }, 61 | "language_info": { 62 | "codemirror_mode": { 63 | "name": "ipython", 64 | "version": 3 65 | }, 66 | "file_extension": ".py", 67 | "mimetype": "text/x-python", 68 | "name": "python", 69 | "nbconvert_exporter": "python", 70 | "pygments_lexer": "ipython3", 71 | "version": "3.6.7" 72 | } 73 | }, 74 | "nbformat": 4, 75 | "nbformat_minor": 2 76 | } 77 | -------------------------------------------------------------------------------- /Chapter 3/random_forests.py: -------------------------------------------------------------------------------- 1 | 2 | # coding: utf-8 3 | 4 | # In[8]: 5 | 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | from sklearn.metrics import classification_report 9 | from sklearn import model_selection 10 | from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier 11 | from sklearn.metrics import classification_report 12 | 13 | from utilities import visualize_classifier 14 | 15 | classifier_type = 'erf' # 'rf'ならランダムフォレスト、それ以外ならERT 16 | 17 | input_file = 'data_random_forests.txt' 18 | data = np.loadtxt(input_file, delimiter=',') 19 | X, y = data[:, :-1], data[:, -1] 20 | 21 | class_0 = np.array(X[y==0]) 22 | class_1 = np.array(X[y==1]) 23 | class_2 = np.array(X[y==2]) 24 | 25 | plt.figure() 26 | plt.scatter(class_0[:, 0], class_0[:, 1], s=75, facecolors='white', 27 | edgecolors='black', linewidth=1, marker='s') 28 | plt.scatter(class_1[:, 0], class_1[:, 1], s=75, facecolors='white', 29 | edgecolors='black', linewidth=1, marker='o') 30 | plt.scatter(class_2[:, 0], class_2[:, 1], s=75, facecolors='white', 31 | edgecolors='black', linewidth=1, marker='^') 32 | plt.title('Input data') 33 | plt.show() 34 | 35 | 36 | # In[9]: 37 | 38 | X_train, X_test, y_train, y_test = model_selection.train_test_split( 39 | X, y, test_size=0.25, random_state=5) 40 | 41 | params = {'n_estimators': 100, 'max_depth': 4, 'random_state': 0} 42 | if classifier_type == 'rf': 43 | classifier = RandomForestClassifier(**params) 44 | else: 45 | classifier = ExtraTreesClassifier(**params) 46 | 47 | classifier.fit(X_train, y_train) 48 | visualize_classifier(classifier, X_train, y_train, 'Training dataset') 49 | 50 | 51 | # In[10]: 52 | 53 | y_test_pred = classifier.predict(X_test) 54 | visualize_classifier(classifier, X_test, y_test, 'Test dataset') 55 | class_names = ['Class-0', 'Class-1', 'Class-2'] 56 | print("\n" + "#"*40) 57 | print("\nClassifier performance on training dataset\n") 58 | print(classification_report(y_train, classifier.predict(X_train), 59 | target_names=class_names)) 60 | print("#"*40 + "\n") 61 | 62 | print("#"*40) 63 | print("\nClassifier performance on test dataset\n") 64 | print(classification_report(y_test, y_test_pred, target_names=class_names)) 65 | print("#"*40 + "\n") 66 | 67 | 68 | # In[ ]: 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /Chapter 3/run_grid_search.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "from sklearn.ensemble import ExtraTreesClassifier \n", 11 | "from sklearn import model_selection\n", 12 | "from sklearn.metrics import classification_report \n", 13 | "\n", 14 | "input_file = 'data_random_forests.txt' \n", 15 | "data = np.loadtxt(input_file, delimiter=',') \n", 16 | "X, y = data[:, :-1], data[:, -1] \n", 17 | "\n", 18 | "class_0 = np.array(X[y==0]) \n", 19 | "class_1 = np.array(X[y==1]) \n", 20 | "class_2 = np.array(X[y==2]) \n", 21 | "\n", 22 | "X_train, X_test, y_train, y_test = model_selection.train_test_split( \n", 23 | " X, y, test_size=0.25, random_state=5) \n", 24 | "\n", 25 | "parameter_grid = [ {'n_estimators': [100], 'max_depth': [2, 4, 7, 12, 16]}, \n", 26 | " {'max_depth': [4], 'n_estimators': [25, 50, 100, 250]} \n", 27 | " ] \n", 28 | "\n", 29 | "#parameter_grid = [ {'n_estimators': [25, 50, 100, 250], \n", 30 | "# 'max_depth': [2, 4, 7, 12, 16]} ] \n", 31 | "\n", 32 | "metrics = ['precision_weighted', 'recall_weighted'] \n", 33 | "\n", 34 | "for metric in metrics: \n", 35 | " print(\"\\n##### Searching optimal parameters for\", metric) \n", 36 | "\n", 37 | " classifier = model_selection.GridSearchCV( \n", 38 | " ExtraTreesClassifier(random_state=0), \n", 39 | " parameter_grid, cv=5, scoring=metric) \n", 40 | " classifier.fit(X_train, y_train) \n", 41 | " \n", 42 | " print(\"\\nGrid scores for the parameter grid:\") \n", 43 | " for params, avg_score in zip(classifier.cv_results_['params'],\n", 44 | " classifier.cv_results_['mean_test_score']): \n", 45 | " print(params, '-->', round(avg_score, 3)) \n", 46 | "\n", 47 | " print(\"\\nBest parameters:\", classifier.best_params_) \n", 48 | " \n", 49 | " y_pred = classifier.predict(X_test) \n", 50 | " print(\"\\nPerformance report:\\n\") \n", 51 | " print(classification_report(y_test, y_pred)) " 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": null, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [] 60 | } 61 | ], 62 | "metadata": { 63 | "kernelspec": { 64 | "display_name": "Python 3", 65 | "language": "python", 66 | "name": "python3" 67 | }, 68 | "language_info": { 69 | "codemirror_mode": { 70 | "name": "ipython", 71 | "version": 3 72 | }, 73 | "file_extension": ".py", 74 | "mimetype": "text/x-python", 75 | "name": "python", 76 | "nbconvert_exporter": "python", 77 | "pygments_lexer": "ipython3", 78 | "version": "3.6.7" 79 | } 80 | }, 81 | "nbformat": 4, 82 | "nbformat_minor": 2 83 | } 84 | -------------------------------------------------------------------------------- /Chapter 3/traffic_prediction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "import matplotlib.pyplot as plt \n", 11 | "from sklearn.metrics import classification_report, mean_absolute_error \n", 12 | "from sklearn import model_selection, preprocessing \n", 13 | "from sklearn.ensemble import ExtraTreesRegressor \n", 14 | "from sklearn.metrics import classification_report \n", 15 | "\n", 16 | "input_file = 'traffic_data.txt' \n", 17 | "data = [] \n", 18 | "with open(input_file, 'r') as f: \n", 19 | " for line in f.readlines(): \n", 20 | " items = line[:-1].split(',') \n", 21 | " data.append(items) \n", 22 | "\n", 23 | "data = np.array(data) \n", 24 | "\n", 25 | "label_encoder = [] \n", 26 | "X_encoded = np.empty(data.shape) \n", 27 | "for i, item in enumerate(data[0]): \n", 28 | " if item.isdigit(): \n", 29 | " X_encoded[:, i] = data[:, i] \n", 30 | " else: \n", 31 | " label_encoder.append(preprocessing.LabelEncoder()) \n", 32 | " X_encoded[:, i] = label_encoder[-1].fit_transform(data[:, i]) \n", 33 | "\n", 34 | "X = X_encoded[:, :-1].astype(int) \n", 35 | "y = X_encoded[:, -1].astype(int) \n", 36 | "\n", 37 | "X_train, X_test, y_train, y_test = model_selection.train_test_split( \n", 38 | " X, y, test_size=0.25, random_state=5) \n", 39 | "\n", 40 | "params = {'n_estimators': 100, 'max_depth': 4, 'random_state': 0} \n", 41 | "regressor = ExtraTreesRegressor(**params) \n", 42 | "regressor.fit(X_train, y_train)\n", 43 | "\n", 44 | "y_pred = regressor.predict(X_test) \n", 45 | "print(\"Mean absolute error:\", round(mean_absolute_error(y_test, y_pred), 2)) " 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": {}, 52 | "outputs": [], 53 | "source": [ 54 | "test_datapoint = ['Saturday', '10:20', 'Atlanta', 'no'] \n", 55 | "test_datapoint_encoded = [0] * len(test_datapoint) \n", 56 | "count = 0 \n", 57 | "for i, item in enumerate(test_datapoint): \n", 58 | " if item.isdigit(): \n", 59 | " test_datapoint_encoded[i] = int(test_datapoint[i]) \n", 60 | " else: \n", 61 | " test_datapoint_encoded[i] = int(label_encoder[count].transform(\n", 62 | " [test_datapoint[i]])[0]) \n", 63 | " count = count + 1 \n", 64 | "\n", 65 | "test_datapoint_encoded = np.array(test_datapoint_encoded) \n", 66 | "\n", 67 | "print(\"Predicted traffic:\", int(regressor.predict([test_datapoint_encoded])[0])) \n" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [] 76 | } 77 | ], 78 | "metadata": { 79 | "kernelspec": { 80 | "display_name": "Python 3", 81 | "language": "python", 82 | "name": "python3" 83 | }, 84 | "language_info": { 85 | "codemirror_mode": { 86 | "name": "ipython", 87 | "version": 3 88 | }, 89 | "file_extension": ".py", 90 | "mimetype": "text/x-python", 91 | "name": "python", 92 | "nbconvert_exporter": "python", 93 | "pygments_lexer": "ipython3", 94 | "version": "3.6.7" 95 | } 96 | }, 97 | "nbformat": 4, 98 | "nbformat_minor": 2 99 | } 100 | -------------------------------------------------------------------------------- /Chapter 3/utilities.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "import matplotlib.pyplot as plt\n", 11 | "%matplotlib inline\n", 12 | "\n", 13 | "def visualize_classifier(classifier, X, y, title=''): \n", 14 | " min_x, max_x = X[:, 0].min() - 1.0, X[:, 0].max() + 1.0 \n", 15 | " min_y, max_y = X[:, 1].min() - 1.0, X[:, 1].max() + 1.0 \n", 16 | " \n", 17 | " mesh_step_size = 0.01 \n", 18 | "\n", 19 | " x_vals, y_vals = np.meshgrid(np.arange(min_x, max_x, mesh_step_size),\n", 20 | " np.arange(min_y, max_y, mesh_step_size)) \n", 21 | " \n", 22 | " output = classifier.predict(np.c_[x_vals.ravel(), y_vals.ravel()]) \n", 23 | " output = output.reshape(x_vals.shape) \n", 24 | " \n", 25 | " plt.figure() \n", 26 | " plt.title(title)\n", 27 | " plt.pcolormesh(x_vals, y_vals, output, cmap=plt.cm.gray) \n", 28 | " plt.scatter(X[:, 0], X[:, 1], c=y, s=75, edgecolors='black', linewidth=1,\n", 29 | " cmap=plt.cm.Paired) \n", 30 | " \n", 31 | " plt.xlim(x_vals.min(), x_vals.max()) \n", 32 | " plt.ylim(y_vals.min(), y_vals.max()) \n", 33 | "\n", 34 | " plt.xticks((np.arange(int(min_x), int(max_x), 1.0))) \n", 35 | " plt.yticks((np.arange(int(min_y), int(max_y), 1.0))) \n", 36 | "\n", 37 | " plt.show() \n", 38 | " " 39 | ] 40 | } 41 | ], 42 | "metadata": { 43 | "kernelspec": { 44 | "display_name": "Python 3", 45 | "language": "python", 46 | "name": "python3" 47 | }, 48 | "language_info": { 49 | "codemirror_mode": { 50 | "name": "ipython", 51 | "version": 3 52 | }, 53 | "file_extension": ".py", 54 | "mimetype": "text/x-python", 55 | "name": "python", 56 | "nbconvert_exporter": "python", 57 | "pygments_lexer": "ipython3", 58 | "version": "3.6.7" 59 | } 60 | }, 61 | "nbformat": 4, 62 | "nbformat_minor": 2 63 | } 64 | -------------------------------------------------------------------------------- /Chapter 4/clustering_quality.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "import matplotlib.pyplot as plt\n", 11 | "%matplotlib inline\n", 12 | "from sklearn import metrics \n", 13 | "from sklearn.cluster import KMeans \n", 14 | "\n", 15 | "X = np.loadtxt('data_quality.txt', delimiter=',') \n", 16 | "\n", 17 | "plt.figure() \n", 18 | "plt.scatter(X[:,0], X[:,1], color='black', s=80, marker='o', facecolors='none') \n", 19 | "x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 \n", 20 | "y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 \n", 21 | "plt.title('Input data') \n", 22 | "plt.xlim(x_min, x_max) \n", 23 | "plt.ylim(y_min, y_max) \n", 24 | "plt.xticks(()) \n", 25 | "plt.yticks(()) \n", 26 | "\n", 27 | "plt.show() " 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "scores = [] \n", 37 | "values = np.arange(2, 10) \n", 38 | "\n", 39 | "for num_clusters in values: \n", 40 | " kmeans = KMeans(init='k-means++', n_clusters=num_clusters, n_init=10) \n", 41 | " kmeans.fit(X) \n", 42 | " score = metrics.silhouette_score(X, kmeans.labels_, \n", 43 | " metric='euclidean', sample_size=len(X)) \n", 44 | " print(\"\\nNumber of clusters =\", num_clusters) \n", 45 | " print(\"Silhouette score =\", score) \n", 46 | " \n", 47 | " scores.append(score) \n", 48 | " \n", 49 | "plt.figure() \n", 50 | "plt.bar(values, scores, width=0.7, color='black', align='center') \n", 51 | "plt.title('Silhouette score vs number of clusters') \n", 52 | "plt.show()\n", 53 | "\n", 54 | "num_clusters = np.argmax(scores) + values[0] \n", 55 | "print('Optimal number of clusters =', num_clusters) " 56 | ] 57 | } 58 | ], 59 | "metadata": { 60 | "kernelspec": { 61 | "display_name": "Python 3", 62 | "language": "python", 63 | "name": "python3" 64 | }, 65 | "language_info": { 66 | "codemirror_mode": { 67 | "name": "ipython", 68 | "version": 3 69 | }, 70 | "file_extension": ".py", 71 | "mimetype": "text/x-python", 72 | "name": "python", 73 | "nbconvert_exporter": "python", 74 | "pygments_lexer": "ipython3", 75 | "version": "3.6.7" 76 | } 77 | }, 78 | "nbformat": 4, 79 | "nbformat_minor": 2 80 | } 81 | -------------------------------------------------------------------------------- /Chapter 4/company_symbol_mapping.json: -------------------------------------------------------------------------------- 1 | { 2 | "TOT": "Total", 3 | "XOM": "Exxon", 4 | "CVX": "Chevron", 5 | "COP": "ConocoPhillips", 6 | "VLO": "Valero Energy", 7 | "MSFT": "Microsoft", 8 | "IBM": "IBM", 9 | "TWX": "Time Warner", 10 | "CMCSA": "Comcast", 11 | "YHOO": "Yahoo", 12 | "HPQ": "HP", 13 | "AMZN": "Amazon", 14 | "TM": "Toyota", 15 | "CAJ": "Canon", 16 | "MTU": "Mitsubishi", 17 | "SNE": "Sony", 18 | "F": "Ford", 19 | "HMC": "Honda", 20 | "NAV": "Navistar", 21 | "NOC": "Northrop Grumman", 22 | "BA": "Boeing", 23 | "KO": "Coca Cola", 24 | "MMM": "3M", 25 | "MCD": "Mc Donalds", 26 | "PEP": "Pepsi", 27 | "MDLZ": "Kraft Foods", 28 | "K": "Kellogg", 29 | "UN": "Unilever", 30 | "MAR": "Marriott", 31 | "PG": "Procter Gamble", 32 | "CL": "Colgate-Palmolive", 33 | "GE": "General Electrics", 34 | "WFC": "Wells Fargo", 35 | "JPM": "JPMorgan Chase", 36 | "AIG": "AIG", 37 | "AXP": "American express", 38 | "BAC": "Bank of America", 39 | "GS": "Goldman Sachs", 40 | "AAPL": "Apple", 41 | "SAP": "SAP", 42 | "CSCO": "Cisco", 43 | "TXN": "Texas instruments", 44 | "XRX": "Xerox", 45 | "WMT": "Wal-Mart", 46 | "WBA": "Walgreen", 47 | "HD": "Home Depot", 48 | "GSK": "GlaxoSmithKline", 49 | "PFE": "Pfizer", 50 | "SNY": "Sanofi-Aventis", 51 | "NVS": "Novartis", 52 | "KMB": "Kimberly-Clark", 53 | "R": "Ryder", 54 | "GD": "General Dynamics", 55 | "RTN": "Raytheon", 56 | "CVS": "CVS", 57 | "CAT": "Caterpillar" 58 | } 59 | -------------------------------------------------------------------------------- /Chapter 4/market_segmentation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import csv \n", 10 | "import numpy as np \n", 11 | "import matplotlib.pyplot as plt \n", 12 | "%matplotlib inline\n", 13 | "from sklearn.cluster import MeanShift, estimate_bandwidth\n", 14 | "\n", 15 | "# ファイルからデータを読み込む\n", 16 | "input_file = 'sales.csv' \n", 17 | "file_reader = csv.reader(open(input_file, 'r'), delimiter=',') \n", 18 | "\n", 19 | "X = [] \n", 20 | "for count, row in enumerate(file_reader): \n", 21 | " if not count: \n", 22 | " names = row[1:] \n", 23 | " continue \n", 24 | " X.append([float(x) for x in row[1:]]) \n", 25 | "\n", 26 | "# numpy 配列に変換する\n", 27 | "X = np.array(X) \n", 28 | "\n", 29 | "bandwidth = estimate_bandwidth(X, quantile=0.8, n_samples=len(X)) \n", 30 | "\n", 31 | "meanshift_model = MeanShift(bandwidth=bandwidth, bin_seeding=True) \n", 32 | "meanshift_model.fit(X) \n", 33 | "\n", 34 | "labels = meanshift_model.labels_ \n", 35 | "cluster_centers = meanshift_model.cluster_centers_ \n", 36 | "num_clusters = len(np.unique(labels)) \n", 37 | "\n", 38 | "print(\"Number of clusters in input data =\", num_clusters) \n", 39 | "\n", 40 | "print(\"\\nCenters of clusters:\") \n", 41 | "print('\\t'.join([name[:7] for name in names])) \n", 42 | "for cluster_center in cluster_centers: \n", 43 | " print('\\t'.join([str(int(x)) for x in cluster_center])) " 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [ 52 | "plt.figure() \n", 53 | "x = 1\n", 54 | "y = 2\n", 55 | "#plt.scatter(X[:,x], X[:,y], marker='.')\n", 56 | "plt.scatter(cluster_centers[:,x], cluster_centers[:,y], \n", 57 | " s=120, edgecolors='black', facecolors='none') \n", 58 | "\n", 59 | "plt.title('Centers of 2D clusters') \n", 60 | "plt.xlabel(names[x])\n", 61 | "plt.ylabel(names[y])\n", 62 | "plt.show() " 63 | ] 64 | } 65 | ], 66 | "metadata": { 67 | "kernelspec": { 68 | "display_name": "Python 3", 69 | "language": "python", 70 | "name": "python3" 71 | }, 72 | "language_info": { 73 | "codemirror_mode": { 74 | "name": "ipython", 75 | "version": 3 76 | }, 77 | "file_extension": ".py", 78 | "mimetype": "text/x-python", 79 | "name": "python", 80 | "nbconvert_exporter": "python", 81 | "pygments_lexer": "ipython3", 82 | "version": "3.6.7" 83 | } 84 | }, 85 | "nbformat": 4, 86 | "nbformat_minor": 2 87 | } 88 | -------------------------------------------------------------------------------- /Chapter 4/mean_shift.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "import matplotlib.pyplot as plt \n", 11 | "%matplotlib inline\n", 12 | "from sklearn.cluster import MeanShift, estimate_bandwidth \n", 13 | "from itertools import cycle \n", 14 | "\n", 15 | "X = np.loadtxt('data_clustering.txt', delimiter=',') \n", 16 | "bandwidth_X = estimate_bandwidth(X, quantile=0.1, n_samples=len(X)) \n", 17 | "\n", 18 | "meanshift_model = MeanShift(bandwidth=bandwidth_X, bin_seeding=True) \n", 19 | "meanshift_model.fit(X) \n", 20 | "\n", 21 | "cluster_centers = meanshift_model.cluster_centers_ \n", 22 | "print('Centers of clusters:\\n', cluster_centers) \n", 23 | "\n", 24 | "labels = meanshift_model.labels_ \n", 25 | "num_clusters = len(np.unique(labels)) \n", 26 | "print(\"\\nNumber of clusters in input data =\", num_clusters) \n", 27 | "\n", 28 | "plt.figure() \n", 29 | "markers = 'o*xvs' \n", 30 | "for i, marker in zip(range(num_clusters), markers): \n", 31 | " plt.scatter(X[labels==i, 0], X[labels==i, 1], marker=marker, color='black') \n", 32 | " cluster_center = cluster_centers[i] \n", 33 | " plt.plot(cluster_center[0], cluster_center[1], marker='o', \n", 34 | " markerfacecolor='black', markeredgecolor='black', \n", 35 | " markersize=15) \n", 36 | "\n", 37 | "plt.title('Clusters') \n", 38 | "plt.show() \n" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [] 47 | } 48 | ], 49 | "metadata": { 50 | "kernelspec": { 51 | "display_name": "Python 3", 52 | "language": "python", 53 | "name": "python3" 54 | }, 55 | "language_info": { 56 | "codemirror_mode": { 57 | "name": "ipython", 58 | "version": 3 59 | }, 60 | "file_extension": ".py", 61 | "mimetype": "text/x-python", 62 | "name": "python", 63 | "nbconvert_exporter": "python", 64 | "pygments_lexer": "ipython3", 65 | "version": "3.6.7" 66 | } 67 | }, 68 | "nbformat": 4, 69 | "nbformat_minor": 2 70 | } 71 | -------------------------------------------------------------------------------- /Chapter 4/stocks.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "scrolled": false 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import datetime \n", 12 | "import json \n", 13 | "import numpy as np \n", 14 | "from sklearn import covariance, cluster \n", 15 | "import pandas as pd\n", 16 | "import pandas_datareader.data as pdd\n", 17 | "\n", 18 | "input_file = 'company_symbol_mapping.json' \n", 19 | "with open(input_file, 'r') as f: \n", 20 | " company_symbols_map = json.loads(f.read()) \n", 21 | "symbols = list(company_symbols_map.keys())\n", 22 | " \n", 23 | "QUANDL_API_KEY = 'xxxxxxxxxxxxxxxxxxxx'\n", 24 | "assert QUANDL_API_KEY != 'xxxxxxxxxxxxxxxxxxxx', 'You must set a valid API KEY.'\n", 25 | "\n", 26 | "start_date = datetime.datetime(2003, 7, 3) \n", 27 | "end_date = datetime.datetime(2007, 5, 4) \n", 28 | "\n", 29 | "quotes = []\n", 30 | "names = []\n", 31 | "for symbol in symbols:\n", 32 | " try:\n", 33 | " print('Loading', symbol, company_symbols_map[symbol], end='...')\n", 34 | " d = pdd.DataReader('WIKI/' + symbol, 'quandl', start_date, end_date,\n", 35 | " access_key=QUANDL_API_KEY)\n", 36 | " print('done.')\n", 37 | " quotes.append(d)\n", 38 | " names.append(company_symbols_map[symbol])\n", 39 | " except:\n", 40 | " print('not found.')\n", 41 | "names = np.array(names)\n", 42 | "\n", 43 | "opening_quotes = np.array([quote['Open'] for quote in quotes]).astype(np.float) \n", 44 | "closing_quotes = np.array([quote['Close'] for quote in quotes]).astype(np.float) \n", 45 | "quotes_diff = closing_quotes - opening_quotes \n", 46 | "\n", 47 | "X = quotes_diff.copy().T \n", 48 | "X /= X.std(axis=0) \n", 49 | "\n", 50 | "edge_model = covariance.GraphicalLassoCV(cv=3) \n", 51 | "\n", 52 | "with np.errstate(invalid='ignore'): \n", 53 | " edge_model.fit(X) \n", 54 | "\n", 55 | "_, labels = cluster.affinity_propagation(edge_model.covariance_) \n", 56 | "num_labels = labels.max() \n", 57 | "\n", 58 | "for i in range(num_labels + 1): \n", 59 | " print(\"Cluster\", i+1, \"==>\", ', '.join(names[labels == i])) " 60 | ] 61 | } 62 | ], 63 | "metadata": { 64 | "kernelspec": { 65 | "display_name": "Python 3", 66 | "language": "python", 67 | "name": "python3" 68 | }, 69 | "language_info": { 70 | "codemirror_mode": { 71 | "name": "ipython", 72 | "version": 3 73 | }, 74 | "file_extension": ".py", 75 | "mimetype": "text/x-python", 76 | "name": "python", 77 | "nbconvert_exporter": "python", 78 | "pygments_lexer": "ipython3", 79 | "version": "3.6.7" 80 | } 81 | }, 82 | "nbformat": 4, 83 | "nbformat_minor": 2 84 | } 85 | -------------------------------------------------------------------------------- /Chapter 5/k_nearest_neighbors.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np \n", 10 | "import matplotlib.pyplot as plt\n", 11 | "%matplotlib inline\n", 12 | "from sklearn.neighbors import NearestNeighbors \n", 13 | "\n", 14 | "X = np.array([[2.1, 1.3], [1.3, 3.2], [2.9, 2.5], [2.7, 5.4], [3.8, 0.9], \n", 15 | " [7.3, 2.1], [4.2, 6.5], [3.8, 3.7], [2.5, 4.1], [3.4, 1.9], \n", 16 | " [5.7, 3.5], [6.1, 4.3], [5.1, 2.2], [6.2, 1.1]]) \n", 17 | "\n", 18 | "plt.figure() \n", 19 | "plt.title('Input data') \n", 20 | "plt.scatter(X[:,0], X[:,1], marker='o', s=75, color='black') \n", 21 | "plt.show()" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": null, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "k = 5 \n", 31 | "test_datapoint = [4.3, 2.7] \n", 32 | "\n", 33 | "knn_model = NearestNeighbors(n_neighbors=k, algorithm='ball_tree').fit(X) \n", 34 | "distances, indices = knn_model.kneighbors([test_datapoint]) \n", 35 | "\n", 36 | "print(\"K Nearest Neighbors:\") \n", 37 | "for rank, index in enumerate(indices[0][:k], start=1): \n", 38 | " print(str(rank) + \" ==>\", X[index]) " 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "plt.figure() \n", 48 | "plt.title('Nearest neighbors') \n", 49 | "plt.scatter(X[:, 0], X[:, 1], marker='o', s=75, color='k') \n", 50 | "plt.scatter(X[indices][0][:][:, 0], X[indices][0][:][:, 1], \n", 51 | " marker='o', s=250, color='k', facecolors='none') \n", 52 | "plt.scatter(test_datapoint[0], test_datapoint[1], \n", 53 | " marker='x', s=75, color='k') \n", 54 | "plt.show() " 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [] 63 | } 64 | ], 65 | "metadata": { 66 | "kernelspec": { 67 | "display_name": "Python 3", 68 | "language": "python", 69 | "name": "python3" 70 | }, 71 | "language_info": { 72 | "codemirror_mode": { 73 | "name": "ipython", 74 | "version": 3 75 | }, 76 | "file_extension": ".py", 77 | "mimetype": "text/x-python", 78 | "name": "python", 79 | "nbconvert_exporter": "python", 80 | "pygments_lexer": "ipython3", 81 | "version": "3.6.7" 82 | } 83 | }, 84 | "nbformat": 4, 85 | "nbformat_minor": 2 86 | } 87 | -------------------------------------------------------------------------------- /Chapter 5/pipeline_trainer.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from sklearn.datasets import samples_generator \n", 10 | "from sklearn.feature_selection import SelectKBest, f_regression \n", 11 | "from sklearn.pipeline import Pipeline \n", 12 | "from sklearn.ensemble import ExtraTreesClassifier \n", 13 | "\n", 14 | "X, y = samples_generator.make_classification(n_samples=150, \n", 15 | " n_features=25, n_classes=3, n_informative=6, \n", 16 | " n_redundant=0, random_state=7) \n", 17 | "\n", 18 | "k_best_selector = SelectKBest(f_regression, k=9) \n", 19 | "\n", 20 | "classifier = ExtraTreesClassifier(n_estimators=60, max_depth=4) \n", 21 | "\n", 22 | "processor_pipeline = Pipeline([('selector', k_best_selector), \n", 23 | " ('erf', classifier)]) \n", 24 | "\n", 25 | "processor_pipeline.set_params(selector__k=7, erf__n_estimators=30) \n", 26 | "\n", 27 | "processor_pipeline.fit(X, y) \n", 28 | "\n", 29 | "output = processor_pipeline.predict(X) \n", 30 | "print(\"Predicted output:\\n\", output) \n", 31 | "\n", 32 | "print(\"\\nScore:\", processor_pipeline.score(X, y)) \n", 33 | "\n", 34 | "# 特徴選択ブロックの状態を得る\n", 35 | "status = processor_pipeline.named_steps['selector'].get_support() \n", 36 | "\n", 37 | "# 選択された特徴の番号を取得して表示する\n", 38 | "selected = [i for i, x in enumerate(status) if x] \n", 39 | "print(\"\\nIndices of selected features:\", ', '.join([str(x) for x in selected])) \n" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": null, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [] 48 | } 49 | ], 50 | "metadata": { 51 | "kernelspec": { 52 | "display_name": "Python 3", 53 | "language": "python", 54 | "name": "python3" 55 | }, 56 | "language_info": { 57 | "codemirror_mode": { 58 | "name": "ipython", 59 | "version": 3 60 | }, 61 | "file_extension": ".py", 62 | "mimetype": "text/x-python", 63 | "name": "python", 64 | "nbconvert_exporter": "python", 65 | "pygments_lexer": "ipython3", 66 | "version": "3.6.7" 67 | } 68 | }, 69 | "nbformat": 4, 70 | "nbformat_minor": 2 71 | } 72 | -------------------------------------------------------------------------------- /Chapter 5/ratings.json: -------------------------------------------------------------------------------- 1 | { 2 | "David Smith": 3 | { 4 | "Vertigo": 4, 5 | "Scarface": 4.5, 6 | "Raging Bull": 3.0, 7 | "Goodfellas": 4.5, 8 | "The Apartment": 1.0 9 | }, 10 | "Brenda Peterson": 11 | { 12 | "Vertigo": 3.0, 13 | "Scarface": 1.5, 14 | "Raging Bull": 1.0, 15 | "Goodfellas": 2.0, 16 | "The Apartment": 5.0, 17 | "Roman Holiday": 4.5 18 | }, 19 | "Bill Duffy": 20 | { 21 | "Vertigo": 4.5, 22 | "Scarface": 5.0, 23 | "Goodfellas": 4.5, 24 | "The Apartment": 1.0 25 | }, 26 | "Samuel Miller": 27 | { 28 | "Scarface": 3.5, 29 | "Raging Bull": 5.0, 30 | "The Apartment": 1.0, 31 | "Goodfellas": 5.0, 32 | "Roman Holiday": 1.0 33 | }, 34 | "Julie Hammel": 35 | { 36 | "Scarface": 2.5, 37 | "Roman Holiday": 4.5, 38 | "Goodfellas": 3.0 39 | }, 40 | "Clarissa Jackson": 41 | { 42 | "Vertigo": 5.0, 43 | "Scarface": 4.5, 44 | "Raging Bull": 4.0, 45 | "Goodfellas": 2.5, 46 | "The Apartment": 1.0, 47 | "Roman Holiday": 1.5 48 | }, 49 | "Adam Cohen": 50 | { 51 | "Vertigo": 3.5, 52 | "Scarface": 3.0, 53 | "The Apartment": 1.0, 54 | "Goodfellas": 4.5, 55 | "Roman Holiday": 3.0 56 | }, 57 | "Chris Duncan": 58 | { 59 | "The Apartment": 1.5, 60 | "Raging Bull": 4.5 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Chapter 6/adjacent_states.txt: -------------------------------------------------------------------------------- 1 | Alaska 2 | Alabama,Mississippi,Tennessee,Georgia,Florida 3 | Arkansas,Missouri,Tennessee,Mississippi,Louisiana,Texas,Oklahoma 4 | Arizona,California,Nevada,Utah,Colorado,New Mexico 5 | California,Oregon,Nevada,Arizona 6 | Colorado,Wyoming,Nebraska,Kansas,Oklahoma,New Mexico,Arizona,Utah 7 | Connecticut,New York,Massachusetts,Rhode Island 8 | District of Columbia,Maryland,Virginia 9 | Delaware,Maryland,Pennsylvania,New Jersey 10 | Florida,Alabama,Georgia 11 | Georgia,Florida,Alabama,Tennessee,North Carolina,South Carolina 12 | Hawaii 13 | Iowa,Minnesota,Wisconsin,Illinois,Missouri,Nebraska,South Dakota 14 | Idaho,Montana,Wyoming,Utah,Nevada,Oregon,Washington 15 | Illinois,Indiana,Kentucky,Missouri,Iowa,Wisconsin 16 | Indiana,Michigan,Ohio,Kentucky,Illinois 17 | Kansas,Nebraska,Missouri,Oklahoma,Colorado 18 | Kentucky,Indiana,Ohio,West Virginia,Virginia,Tennessee,Missouri,Illinois 19 | Louisiana,Texas,Arkansas,Mississippi 20 | Massachusetts,Rhode Island,Connecticut,New York,New Hampshire,Vermont 21 | Maryland,Virginia,West Virginia,Pennsylvania,District of Columbia,Delaware 22 | Maine,New Hampshire 23 | Michigan,Wisconsin,Indiana,Ohio 24 | Minnesota,Wisconsin,Iowa,South Dakota,North Dakota 25 | Missouri,Iowa,Illinois,Kentucky,Tennessee,Arkansas,Oklahoma,Kansas,Nebraska 26 | Mississippi,Louisiana,Arkansas,Tennessee,Alabama 27 | Montana,North Dakota,South Dakota,Wyoming,Idaho 28 | North Carolina,Virginia,Tennessee,Georgia,South Carolina 29 | North Dakota,Minnesota,South Dakota,Montana 30 | Nebraska,South Dakota,Iowa,Missouri,Kansas,Colorado,Wyoming 31 | New Hampshire,Vermont,Maine,Massachusetts 32 | New Jersey,Delaware,Pennsylvania,New York 33 | New Mexico,Arizona,Utah,Colorado,Oklahoma,Texas 34 | Nevada,Idaho,Utah,Arizona,California,Oregon 35 | New York,New Jersey,Pennsylvania,Vermont,Massachusetts,Connecticut 36 | Ohio,Pennsylvania,West Virginia,Kentucky,Indiana,Michigan 37 | Oklahoma,Kansas,Missouri,Arkansas,Texas,New Mexico,Colorado 38 | Oregon,California,Nevada,Idaho,Washington 39 | Pennsylvania,New York,New Jersey,Delaware,Maryland,West Virginia,Ohio 40 | Rhode Island,Connecticut,Massachusetts 41 | South Carolina,Georgia,North Carolina 42 | South Dakota,North Dakota,Minnesota,Iowa,Nebraska,Wyoming,Montana 43 | Tennessee,Kentucky,Virginia,North Carolina,Georgia,Alabama,Mississippi,Arkansas,Missouri 44 | Texas,New Mexico,Oklahoma,Arkansas,Louisiana 45 | Utah,Idaho,Wyoming,Colorado,New Mexico,Arizona,Nevada 46 | Virginia,North Carolina,Tennessee,Kentucky,West Virginia,Maryland,District of Columbia 47 | Vermont,New York,New Hampshire,Massachusetts 48 | Washington,Idaho,Oregon 49 | Wisconsin,Michigan,Minnesota,Iowa,Illinois 50 | West Virginia,Ohio,Pennsylvania,Maryland,Virginia,Kentucky 51 | Wyoming,Montana,South Dakota,Nebraska,Colorado,Utah,Idaho 52 | -------------------------------------------------------------------------------- /Chapter 6/coastal_states.txt: -------------------------------------------------------------------------------- 1 | Washington,Oregon,California,Texas,Louisiana,Michigan,Alabama,Georgia,Florida,South Carolina,North Carolina,Virgin Islands,Maryland,Delaware,New Jersey,New York,Connecticut,Rhode Island,Massachusetts,Minnesota,New Hampshire -------------------------------------------------------------------------------- /Chapter 6/expression_matcher.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from kanren import run, var, fact \n", 10 | "import kanren.assoccomm as la \n", 11 | "\n", 12 | "add = 'addition' \n", 13 | "mul = 'multiplication' \n", 14 | "\n", 15 | "fact(la.commutative, mul) \n", 16 | "fact(la.commutative, add) \n", 17 | "fact(la.associative, mul) \n", 18 | "fact(la.associative, add) \n", 19 | "\n", 20 | "a, b, c = var('a'), var('b'), var('c') \n", 21 | "\n", 22 | "expression_orig = (add, (mul, 3, -2), (mul, (add, 1, (mul, 2, 3)), -1)) \n", 23 | "expression1 = (add, (mul, (add, 1, (mul, 2, a)), b), (mul, 3, c)) \n", 24 | "expression2 = (add, (mul, c, 3), (mul, b, (add, (mul, 2, a), 1))) \n", 25 | "expression3 = (add, (add, (mul, (mul, 2, a), b), b), (mul, 3, c)) \n", 26 | "\n", 27 | "print(run(0, (a, b, c), la.eq_assoccomm(expression1, expression_orig))) \n", 28 | "print(run(0, (a, b, c), la.eq_assoccomm(expression2, expression_orig))) \n", 29 | "print(run(0, (a, b, c), la.eq_assoccomm(expression3, expression_orig))) " 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": null, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [] 38 | } 39 | ], 40 | "metadata": { 41 | "kernelspec": { 42 | "display_name": "Python 3", 43 | "language": "python", 44 | "name": "python3" 45 | }, 46 | "language_info": { 47 | "codemirror_mode": { 48 | "name": "ipython", 49 | "version": 3 50 | }, 51 | "file_extension": ".py", 52 | "mimetype": "text/x-python", 53 | "name": "python", 54 | "nbconvert_exporter": "python", 55 | "pygments_lexer": "ipython3", 56 | "version": "3.6.7" 57 | } 58 | }, 59 | "nbformat": 4, 60 | "nbformat_minor": 2 61 | } 62 | -------------------------------------------------------------------------------- /Chapter 6/prime.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import itertools as it \n", 10 | "from kanren import isvar, membero, var, run, eq\n", 11 | "from kanren.core import success, fail, condeseq\n", 12 | "from sympy.ntheory.generate import prime, isprime \n", 13 | "\n", 14 | "# xが素数かどうかを判定する\n", 15 | "def check_prime(x): \n", 16 | " if isvar(x): \n", 17 | " return condeseq([eq(x, p)] for p in map(prime, it.count(1))) \n", 18 | " else: \n", 19 | " return success if isprime(x) else fail \n", 20 | "\n", 21 | "x = var() \n", 22 | "\n", 23 | "list_nums = (23, 4, 27, 17, 13, 10, 21, 29, 3, 32, 11, 19) \n", 24 | "print('List of primes in the list:') \n", 25 | "print(set(run(0, x, (membero, x, list_nums), (check_prime, x))))" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": null, 31 | "metadata": { 32 | "scrolled": true 33 | }, 34 | "outputs": [], 35 | "source": [ 36 | "print('List of first 7 prime numbers:') \n", 37 | "print(run(7, x, (check_prime, x))) " 38 | ] 39 | } 40 | ], 41 | "metadata": { 42 | "kernelspec": { 43 | "display_name": "Python 3", 44 | "language": "python", 45 | "name": "python3" 46 | }, 47 | "language_info": { 48 | "codemirror_mode": { 49 | "name": "ipython", 50 | "version": 3 51 | }, 52 | "file_extension": ".py", 53 | "mimetype": "text/x-python", 54 | "name": "python", 55 | "nbconvert_exporter": "python", 56 | "pygments_lexer": "ipython3", 57 | "version": "3.6.7" 58 | } 59 | }, 60 | "nbformat": 4, 61 | "nbformat_minor": 2 62 | } 63 | -------------------------------------------------------------------------------- /Chapter 6/puzzle.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from kanren import * \n", 10 | "\n", 11 | "people = var() \n", 12 | "\n", 13 | "rules = lall( \n", 14 | " (eq, (var(), var(), var(), var()), people), \n", 15 | " (membero, ('Steve', var(), 'blue', var()), people), \n", 16 | " (membero, (var(), 'cat', var(), 'Canada'), people),\n", 17 | " (membero, ('Matthew', var(), var(), 'USA'), people),\n", 18 | " (membero, (var(), var(), 'black', 'Australia'), people),\n", 19 | " (membero, ('Jack', 'cat', var(), var()), people),\n", 20 | " (membero, ('Alfred', var(), var(), 'Australia'), people),\n", 21 | " (membero, (var(), 'dog', var(), 'France'), people),\n", 22 | " (membero, (var(), 'rabbit', var(), var()), people) \n", 23 | ")\n", 24 | " \n", 25 | "solutions = run(0, people, rules) \n", 26 | "output = [house for house in solutions[0] if 'rabbit' in house][0][0] \n", 27 | "print(output + ' is the owner of the rabbit') " 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": null, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "print('Here are all the details:') \n", 37 | "attribs = ['Name', 'Pet', 'Color', 'Country'] \n", 38 | "print('\\n' + '\\t\\t'.join(attribs)) \n", 39 | "print('=' * 57) \n", 40 | "\n", 41 | "for item in solutions[0]: \n", 42 | " print('\\t\\t'.join([str(x) for x in item])) " 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": null, 48 | "metadata": {}, 49 | "outputs": [], 50 | "source": [] 51 | } 52 | ], 53 | "metadata": { 54 | "kernelspec": { 55 | "display_name": "Python 3", 56 | "language": "python", 57 | "name": "python3" 58 | }, 59 | "language_info": { 60 | "codemirror_mode": { 61 | "name": "ipython", 62 | "version": 3 63 | }, 64 | "file_extension": ".py", 65 | "mimetype": "text/x-python", 66 | "name": "python", 67 | "nbconvert_exporter": "python", 68 | "pygments_lexer": "ipython3", 69 | "version": "3.6.7" 70 | } 71 | }, 72 | "nbformat": 4, 73 | "nbformat_minor": 2 74 | } 75 | -------------------------------------------------------------------------------- /Chapter 6/relationships.json: -------------------------------------------------------------------------------- 1 | { 2 | "father": 3 | [ 4 | {"John": "William"}, 5 | {"John": "David"}, 6 | {"John": "Adam"}, 7 | {"William": "Chris"}, 8 | {"William": "Stephanie"}, 9 | {"David": "Wayne"}, 10 | {"David": "Tiffany"}, 11 | {"David": "Julie"}, 12 | {"David": "Neil"}, 13 | {"David": "Peter"}, 14 | {"Adam": "Sophia"} 15 | ], 16 | "mother": 17 | [ 18 | {"Megan": "William"}, 19 | {"Megan": "David"}, 20 | {"Megan": "Adam"}, 21 | {"Emma": "Stephanie"}, 22 | {"Emma": "Chris"}, 23 | {"Olivia": "Tiffany"}, 24 | {"Olivia": "Julie"}, 25 | {"Olivia": "Neil"}, 26 | {"Olivia": "Peter"}, 27 | {"Lily": "Sophia"} 28 | ] 29 | } 30 | -------------------------------------------------------------------------------- /Chapter 6/states.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from kanren import run, fact, eq, Relation, var \n", 10 | "\n", 11 | "coastal = Relation() \n", 12 | "adjacent = Relation() \n", 13 | "\n", 14 | "file_coastal = 'coastal_states.txt' \n", 15 | "with open(file_coastal, 'r') as f: \n", 16 | " line = f.read() \n", 17 | " coastal_states = line.split(',') \n", 18 | "\n", 19 | "for state in coastal_states: \n", 20 | " fact(coastal, state) \n", 21 | " \n", 22 | "file_adjacent = 'adjacent_states.txt' \n", 23 | "with open(file_adjacent, 'r') as f: \n", 24 | " adjlist = [line.strip().split(',') for line in f\n", 25 | " if line and line[0].isalpha()] \n", 26 | "\n", 27 | "for L in adjlist: \n", 28 | " head, tail = L[0], L[1:] \n", 29 | " for state in tail: \n", 30 | " fact(adjacent, head, state) " 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "x = var() \n", 40 | "output = run(0, x, adjacent('Nevada', 'Louisiana')) \n", 41 | "print('Yes' if len(output) else 'No') " 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [ 50 | "output = run(0, x, adjacent('Oregon', x)) \n", 51 | "for item in output: \n", 52 | " print(item) " 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": {}, 59 | "outputs": [], 60 | "source": [ 61 | "output = run(0, x, adjacent('Mississippi', x), coastal(x)) \n", 62 | "for item in output: \n", 63 | " print(item) " 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [ 72 | "y= var()\n", 73 | "output = run(7, x, coastal(y), adjacent(x, y)) \n", 74 | "for item in output: \n", 75 | " print(item) " 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": null, 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [ 84 | "output = run(0, x, adjacent('Arkansas', x), adjacent('Kentucky', x)) \n", 85 | "for item in output: \n", 86 | " print(item) " 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": {}, 93 | "outputs": [], 94 | "source": [] 95 | } 96 | ], 97 | "metadata": { 98 | "kernelspec": { 99 | "display_name": "Python 3", 100 | "language": "python", 101 | "name": "python3" 102 | }, 103 | "language_info": { 104 | "codemirror_mode": { 105 | "name": "ipython", 106 | "version": 3 107 | }, 108 | "file_extension": ".py", 109 | "mimetype": "text/x-python", 110 | "name": "python", 111 | "nbconvert_exporter": "python", 112 | "pygments_lexer": "ipython3", 113 | "version": "3.6.7" 114 | } 115 | }, 116 | "nbformat": 4, 117 | "nbformat_minor": 2 118 | } 119 | -------------------------------------------------------------------------------- /Chapter 7/coloring.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from simpleai.search import CspProblem, backtrack \n", 10 | "\n", 11 | "names = ('Mark', 'Julia', 'Steve', 'Amanda', 'Brian', \n", 12 | " 'Joanne', 'Derek', 'Allan', 'Michelle', 'Kelly', 'Chris')\n", 13 | "\n", 14 | "colors = dict((name, ['red', 'green', 'blue', 'gray']) for name in names)\n", 15 | "\n", 16 | "def constraint_func(names, values): \n", 17 | " return values[0] != values[1] \n", 18 | "\n", 19 | "constraints = [ \n", 20 | " (('Mark', 'Julia'), constraint_func), \n", 21 | " (('Mark', 'Steve'), constraint_func), \n", 22 | " (('Julia', 'Steve'), constraint_func), \n", 23 | " (('Julia', 'Amanda'), constraint_func), \n", 24 | " (('Julia', 'Derek'), constraint_func), \n", 25 | " (('Julia', 'Brian'), constraint_func), \n", 26 | " (('Steve', 'Amanda'), constraint_func), \n", 27 | " (('Steve', 'Allan'), constraint_func), \n", 28 | " (('Steve', 'Michelle'), constraint_func), \n", 29 | " (('Amanda', 'Michelle'), constraint_func), \n", 30 | " (('Amanda', 'Joanne'), constraint_func), \n", 31 | " (('Amanda', 'Derek'), constraint_func), \n", 32 | " (('Brian', 'Derek'), constraint_func), \n", 33 | " (('Brian', 'Kelly'), constraint_func), \n", 34 | " (('Joanne', 'Michelle'), constraint_func), \n", 35 | " (('Joanne', 'Derek'), constraint_func),\n", 36 | " (('Joanne', 'Chris'), constraint_func), \n", 37 | " (('Derek', 'Kelly'), constraint_func), \n", 38 | " (('Derek', 'Chris'), constraint_func), \n", 39 | " (('Kelly', 'Chris'), constraint_func), \n", 40 | " (('Allan', 'Michelle'), constraint_func), \n", 41 | "] \n", 42 | "\n", 43 | "problem = CspProblem(names, colors, constraints) \n", 44 | "output = backtrack(problem) \n", 45 | "print('Color mapping:') \n", 46 | "for k, v in output.items(): \n", 47 | " print(k, '==>', v) " 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [] 56 | } 57 | ], 58 | "metadata": { 59 | "kernelspec": { 60 | "display_name": "Python 3", 61 | "language": "python", 62 | "name": "python3" 63 | }, 64 | "language_info": { 65 | "codemirror_mode": { 66 | "name": "ipython", 67 | "version": 3 68 | }, 69 | "file_extension": ".py", 70 | "mimetype": "text/x-python", 71 | "name": "python", 72 | "nbconvert_exporter": "python", 73 | "pygments_lexer": "ipython3", 74 | "version": "3.6.7" 75 | } 76 | }, 77 | "nbformat": 4, 78 | "nbformat_minor": 2 79 | } 80 | -------------------------------------------------------------------------------- /Chapter 7/greedy_search.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import simpleai.search as ss\n", 10 | "\n", 11 | "class CustomProblem(ss.SearchProblem): \n", 12 | " def set_target(self, target_string): \n", 13 | " self.target_string = target_string \n", 14 | " \n", 15 | " def actions(self, cur_state):\n", 16 | " if len(cur_state) < len(self.target_string):\n", 17 | " alphabets = 'abcdefghijklmnopqrstuvwxyz'\n", 18 | " return list(alphabets + ' ' + alphabets.upper())\n", 19 | " else:\n", 20 | " return []\n", 21 | "\n", 22 | " def result(self, cur_state, action): \n", 23 | " return cur_state + action \n", 24 | "\n", 25 | " def is_goal(self, cur_state): \n", 26 | " return cur_state == self.target_string \n", 27 | "\n", 28 | " def heuristic(self, cur_state):\n", 29 | " # 現在の文字列を目標の文字列と比較する\n", 30 | " dist = sum([1 if cur_state[i] != self.target_string[i] else 0\n", 31 | " for i in range(len(cur_state))])\n", 32 | "\n", 33 | " # 文字列の長さの差を求める\n", 34 | " diff = len(self.target_string) - len(cur_state)\n", 35 | "\n", 36 | " return dist + diff\n", 37 | "\n", 38 | "problem = CustomProblem() \n", 39 | "\n", 40 | "input_string = 'Artificial Intelligence'\n", 41 | "initial_state = ''\n", 42 | "#input_string = 'Artificial Intelligence with Python'\n", 43 | "#initial_state = 'Artificial Inte'\n", 44 | "\n", 45 | "problem.set_target(input_string) \n", 46 | "problem.initial_state = initial_state \n", 47 | "\n", 48 | "output = ss.greedy(problem) \n", 49 | "\n", 50 | "print('Target string:', input_string) \n", 51 | "print('\\nPath to the solution:') \n", 52 | "for item in output.path(): \n", 53 | " print(item) " 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [] 62 | } 63 | ], 64 | "metadata": { 65 | "kernelspec": { 66 | "display_name": "Python 3", 67 | "language": "python", 68 | "name": "python3" 69 | }, 70 | "language_info": { 71 | "codemirror_mode": { 72 | "name": "ipython", 73 | "version": 3 74 | }, 75 | "file_extension": ".py", 76 | "mimetype": "text/x-python", 77 | "name": "python", 78 | "nbconvert_exporter": "python", 79 | "pygments_lexer": "ipython3", 80 | "version": "3.6.7" 81 | } 82 | }, 83 | "nbformat": 4, 84 | "nbformat_minor": 2 85 | } 86 | -------------------------------------------------------------------------------- /Chapter 8/target_map.txt: -------------------------------------------------------------------------------- 1 | S##............................. 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 | ................######.......... -------------------------------------------------------------------------------- /Chapter 9/coins.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from easyAI import TwoPlayersGame, id_solve, Human_Player, AI_Player \n", 10 | "from easyAI.AI import TT \n", 11 | "\n", 12 | "class LastCoinStanding(TwoPlayersGame): \n", 13 | " def __init__(self, players): \n", 14 | " self.players = players \n", 15 | " self.nplayer = 1 \n", 16 | " self.num_coins = 25 \n", 17 | " self.max_coins = 4 \n", 18 | "\n", 19 | " def possible_moves(self): \n", 20 | " return [str(x) for x in range(1, self.max_coins + 1)] \n", 21 | "\n", 22 | " def make_move(self, move): \n", 23 | " self.num_coins -= int(move) \n", 24 | "\n", 25 | " def win(self): \n", 26 | " return self.num_coins <= 0 \n", 27 | "\n", 28 | " def is_over(self): \n", 29 | " return self.win() \n", 30 | "\n", 31 | " def scoring(self): \n", 32 | " return 100 if self.win() else 0 \n", 33 | "\n", 34 | " def show(self): \n", 35 | " print(self.num_coins, 'coins left in the pile') \n", 36 | "\n", 37 | " def ttentry(self): \n", 38 | " return self.num_coins\n", 39 | "\n", 40 | "tt = TT() \n", 41 | "result, depth, move = id_solve(LastCoinStanding,\n", 42 | " range(2, 20), win_score=100, tt=tt) \n", 43 | "print(result, depth, move) \n", 44 | "\n", 45 | "game = LastCoinStanding([AI_Player(tt), Human_Player()]) \n", 46 | "game.play() " 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [] 55 | } 56 | ], 57 | "metadata": { 58 | "kernelspec": { 59 | "display_name": "Python 3", 60 | "language": "python", 61 | "name": "python3" 62 | }, 63 | "language_info": { 64 | "codemirror_mode": { 65 | "name": "ipython", 66 | "version": 3 67 | }, 68 | "file_extension": ".py", 69 | "mimetype": "text/x-python", 70 | "name": "python", 71 | "nbconvert_exporter": "python", 72 | "pygments_lexer": "ipython3", 73 | "version": "3.6.7" 74 | } 75 | }, 76 | "nbformat": 4, 77 | "nbformat_minor": 2 78 | } 79 | -------------------------------------------------------------------------------- /Chapter 9/tic_tac_toe.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from easyAI import TwoPlayersGame, AI_Player, Negamax \n", 10 | "from easyAI.Player import Human_Player \n", 11 | "\n", 12 | "class GameController(TwoPlayersGame): \n", 13 | " def __init__(self, players): \n", 14 | " self.players = players \n", 15 | " self.nplayer = 1 \n", 16 | " self.board = [0] * 9 \n", 17 | "\n", 18 | " def possible_moves(self): \n", 19 | " return [a + 1 for a, b in enumerate(self.board) if b == 0] \n", 20 | "\n", 21 | " def make_move(self, move): \n", 22 | " self.board[int(move) - 1] = self.nplayer \n", 23 | "\n", 24 | " def loss_condition(self): \n", 25 | " possible_combinations = [[1,2,3], [4,5,6], [7,8,9], [1,4,7], \n", 26 | " [2,5,8], [3,6,9], [1,5,9], [3,5,7]] \n", 27 | " return any([all([(self.board[i-1] == self.nopponent) \n", 28 | " for i in combination]) \n", 29 | " for combination in possible_combinations]) \n", 30 | "\n", 31 | " def is_over(self): \n", 32 | " return (self.possible_moves() == []) or self.loss_condition() \n", 33 | "\n", 34 | " def show(self): \n", 35 | " print('\\n'+'\\n'.join([' '.join([['.', 'O', 'X'][self.board[3*j + i]] \n", 36 | " for i in range(3)]) for j in range(3)])) \n", 37 | "\n", 38 | " def scoring(self): \n", 39 | " return -100 if self.loss_condition() else 0 \n", 40 | "\n", 41 | "algorithm = Negamax(7) \n", 42 | "game = GameController([Human_Player(), AI_Player(algorithm)])\n", 43 | "game.play() " 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "metadata": {}, 50 | "outputs": [], 51 | "source": [] 52 | } 53 | ], 54 | "metadata": { 55 | "kernelspec": { 56 | "display_name": "Python 3", 57 | "language": "python", 58 | "name": "python3" 59 | }, 60 | "language_info": { 61 | "codemirror_mode": { 62 | "name": "ipython", 63 | "version": 3 64 | }, 65 | "file_extension": ".py", 66 | "mimetype": "text/x-python", 67 | "name": "python", 68 | "nbconvert_exporter": "python", 69 | "pygments_lexer": "ipython3", 70 | "version": "3.6.7" 71 | } 72 | }, 73 | "nbformat": 4, 74 | "nbformat_minor": 2 75 | } 76 | -------------------------------------------------------------------------------- /artificial-intelligence-with-python-ja.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oreilly-japan/artificial-intelligence-with-python-ja/d1cf559fef53a1f617b06b77bc6805c65048221c/artificial-intelligence-with-python-ja.png --------------------------------------------------------------------------------