Posts

Showing posts from 2020

NLP in action: train a Naive Bayes model on movie reviews

Image
In the book Natural Language Processing in Action , section 2.3.2 Naive Bayes , we train a multinomial Naive Bayes classifier on movie reviews using scikit-learn 's  MultinomialNB .   NOTE:  I am using numpy 1.19.1, pandas 1.1.3, and scikit-learn 0.23.2 However, we get a ValueError when transforming the predicted probabilities in the [-4, 4] range as done in the book: nb = MultinomialNB () nb = nb . fit ( df_bows , movies . sentiment > 0 ) movies [ 'predicted_sentiment' ] = nb . predict_proba ( df_bows ) * 8 - 4 ValueError: Wrong number of items passed 2, placement implies 1 The reason is that nb.predict_proba() returns a numpy array with two columns and we are trying to assign it to a single column from the Pandas table  movies  (which I believe you could do in previous Pandas versions; I am using Pandas version 1.1.3) : array ([[ 1.86060657e-01 , 8.13939343e-01 ], [ 1.19745717e-05 , 9.99988025e-01 ]...

Tips on publishing your Python package to PyPI

Image
  If you want to publish your first Python package to PyPI, here are some tips that might help you in avoiding some pitfalls when releasing your code to the world. Table of contents   1. Unsupported reST directives and other limitations        1.1 PyPI limitations        1.2 GitHub limitations        1.3 Limitations from all   2. Versioning your project   3.  Validate your documentation before uploading   4. Make a release   5. Publishing first to TestPyPI   6. Mistakes found in a published README   7. Remove a release from PyPI   8. Conclusion   9. Resources 10. Notes 1. Unsupported reST directives and other limitations The README.rst [ 1 ] that you painstakingly wrote for readthedocs.org [ 2 ]  might not render correctly on PyPI, nor on GitHub [ 3 ] . Here are some of the limitations that you need to look carefully for each website on which you might ...

Using pynput on macOS

Image
While working on my personal project Darth-Vader-RPi , I used the Python package pynput for monitoring the keyboard as to simulate push buttons on a Raspberry Pi. However, some keyboard keys were not detected on macOS without running the script with sudo (after adding PYTHONPATH to etc/sudoers ).  The keyboard keys that didn't need sudo were the following: alt keys cmd keys ctrl keys media buttons for play, pause, volume up/down/mute shift keys The other keyboard keys (all the alphanumeric and some special keys such as backspace and right) required the Python script to run with sudo . The pynput documentation explains the modifications you should apply to your application if you want to make it run on Linux, macOS, or Windows. Image credit:   "Apple Key"  by  Antijingoist  is licensed under  CC BY-NC-ND 2.0

A Raspberry Pi project about activating Darth Vader with LEDs and sounds

Image
Darth-Vader-RPi is a Python-based Raspberry Pi (RPi) project about activating a Darth Vader action figure by turning on LEDs on his suit and lightsaber, and by playing sounds such as some of his famous quotes. Click on the gif to play the video The Darth Vader action figure is 11.5 inches tall (which is this one from Hasbro ) and was modified to make it more lifelike by illuminating the lightsaber, chest control box, and belt. 3 push buttons control the following sounds and LEDs: Some of his famous quotes The Imperial march theme song The lightsaber opening and retraction sounds The lightbsaber illumination (3 LEDs) His iconic breathing sound plays in the background indefinitely as soon as the RPi is run with the start_dv   script. Connection diagram Here’s how the various LEDs and push buttons are connected to the Raspberry Pi: The lightsaber is illuminated by 3 LEDs connected in parallel. The Top , Middle , and Bottom LEDs illuminate the slots in Darth Vader's chest control...

Introducing the Python library SimulRPi

Image
SimulRPi is a Python library that I wrote as part of my Darth-Vader-RPi  project. It partly fakes RPi.GPIO and simulates some I/O devices connected to a Raspberry Pi (RPi): push buttons by listening to pressed keyboard keys and LEDs by displaying blinking red dots in the terminal along with their GPIO pin numbers. The Python package pynput is used to monitor the keyboard for any pressed key. Thus, the SimulRPi library can be useful in the case that you want to try your  RPi.GPIO -based script by running it on your computer when no RPi is available at the moment. Example: terminal output Simulating LEDs on an RPi via a terminal Each dot represents a blinking LED connected to an RPi and the number between brackets is the associated GPIO channel number. Here the LED on channel 22 toggles between on and off when a key is pressed. Dependencies Platforms:  Linux, macOS Python:  3.5, 3.6, 3.7, 3.8  pynput >=1.6.8: for monitoring the keyboard for any pressed key...