Friday, July 7, 2017

How to Analyze Tweet Sentiments with PHP Machine Learning

As of late, it seems everyone and their proverbial grandma is talking about Machine Learning. Your social media feeds are inundated with posts about ML, Python, TensorFlow, Spark, Scala, Go and so on; and if you are anything like me, you might be wondering, what about PHP?

Yes, what about Machine Learning and PHP? Fortunately, someone was crazy enough not only to ask that question, but to also develop a generic machine learning library that we can use in our next project. In this post we are going take a look at PHP-ML - a machine learning library for PHP - and we'll write a sentiment analysis class that we can later reuse for our own chat or tweet bot. The main goals of this post are:

  • Explore the general concepts around Machine learning and Sentiment Analysis
  • Review the capabilities and shortcomings of PHP-ML
  • Define the problem we are going to work on
  • Prove that trying to do Machine learning in PHP isn't a completely crazy goal (optional)

A robot elephpant

What is Machine Learning?

Machine learning is a subset of Artificial Intelligence that focuses on giving "computers the ability to learn without being explicitly programmed". This is achieved by using generic algorithms that can "learn" from a particular set of data.

For example, one common usage of machine learning is classification. Classification algorithms are used to put data into different groups or categories. Some examples of classification applications are:

  • Email spam filters
  • Market segmentation
  • Fraud detection

Machine learning is something of an umbrella term that covers many generic algorithms for different tasks, and there are two main algorithm types classified on how they learn – supervised learning and unsupervised learning.

Supervised Learning

In supervised learning, we train our algorithm using labelled data in the form of an input object (vector) and a desired output value; the algorithm analyzes the training data and produces what is referred to as an inferred function which we can apply to a new, unlabelled dataset.

For the remainder of this post we will focus on supervised learning, just because its easier to see and validate the relationship; keep in mind that both algorithms are equally important and interesting; one could argue that unsupervised is more useful because it precludes the labelled data requirements.

Unsupervised Learning

This type of learning on the other hand works with unlabelled data from the get-go. We don't know the desired output values of the dataset and we are letting the algorithm draw inferences from datasets; unsupervised learning is especially handy when doing exploratory data analysis to find hidden patterns in the data.

PHP-ML

Meet PHP-ML, a library that claims to be a fresh approach to Machine Learning in PHP. The library implements algorithms, neural networks, and tools to do data pre-processing, cross validation, and feature extraction.

I'll be the first to admit PHP is an unusual choice for machine learning, as the language's strengths are not that well suited for Machine Learning applications. That said, not every machine learning application needs to process petabytes of data and do massive calculations - for simple applications, we should be able to get away with using PHP and PHP-ML.

The best use case that I can see for this library right now is the implementation of a classifier, be it something like a spam filter or even sentiment analysis. We are going to define a classification problem and build a solution step by step to see how we can use PHP-ML in our projects.

The Problem

To exemplify the process of implementing PHP-ML and adding some machine learning to our applications, I wanted to find a fun problem to tackle and what better way to showcase a classifier than building a tweet sentiment analysis class.

One of the key requirements needed to build successful machine learning projects is a decent starting dataset. Datasets are critical since they will allow us to train our classifier against already classified examples. As there has recently been significant noise in the media around airlines, what better dataset to use than tweets from customers to airlines?

Fortunately, a dataset of tweets is already available to us thanks to Kaggle.io. The Twitter US Airline Sentiment database can be downloaded from their site using this link

The Solution

Let's begin by taking a look at the dataset we will be working on. The raw dataset has the following columns:

Continue reading %How to Analyze Tweet Sentiments with PHP Machine Learning%


by Allan MacGregor via SitePoint

#342: Why Reddit Chose TypeScript

This week's JavaScript newsRead this e-mail on the Web
JavaScript Weekly
Issue 342 — July 7, 2017
British design studio Hive has collected together guidelines for working on JS projects.
Hive

Reddit felt that TypeScript was the best option for typed front-end development.
Upvoted

Component IO
Building with reusable, isolated components will make your work clean & maintainable. Component IO offers ready-to-use website components that work everywhere. Use the live online content & code editor to improve your workflow.
Component IO   Sponsor

A powerful React-based static site generator. v1 can integrate with CMSs like Wordpress, and Drupal, and supports route-based code splitting, service workers, offline support, etc.
Kyle Mathews

Provides React’s component-based UI building experience, but for CLI apps. An intriguing idea.
Vadim Demedes

Bring the power of embedded videos to D3 visualizations.
Michals, Worrel and Nunns

Jobs Supported by Hired.com

Can't find the right job? Want companies to apply to you? Try Hired.com.

In Brief

'Prettier' Code Formatter Reaches Version 1.5 news
Introduces GraphQL, CSS-in-JS and JSON support.
Prettier

Integrate TypeScript in Your Vue Project tutorial
Alex Jover Morales

Build a Simple Realtime App with Vue.js 2.0 and Pusher tutorial
Olayinka Omole

Experimenting with the Background Fetch API tutorial
Phil Nash

Pattern Matching with TypeScript tutorial
Manuel Alabor

Get Started with Rust, WebAssembly, and Webpack tutorial
Ian J Sikes

Intro to 'date-fns', a Lightweight JS Date Library tutorial
A simple, functional alternative to Moment.js.
SitePoint

Building a Realtime Dashboard Powered by Laravel and Vue.js tutorial
Freek Van der Herten

Much faster way to debug code than with breakpoints or console.log 
This wallaby.js feature allows to inspect the result of any JavaScript expression execution in your editor.
Wallaby.js  Sponsor

Why Use SemVer / Semantic Versioning? opinion
And how it has ‘changed the way we write JavaScript’.
The npm Blog

Comparing and Contrasting React and Angular opinion
Dan Halverson

8 Habits of a Happy Node Hacker (2017 Edition) opinion
Just in case you’re not a Node Weekly reader :-)
Jeremy Morrell

Evan You on the State of Vue in 2017 video
VueConf

How to Get Started and Build Something with GraphQL video
Xavier Cazalot

Spend less time coding, more time shipping 🚀 
Build interactive features, like notifications, in minutes with just a few lines of code.
Pusher  Sponsor

14 'Live Search' jQuery Plugins tools
SitePoint

bundlesize: Keep Your Bundle Size in Check tools
Siddharth Kshetrapal

webpack-common-shake: A CommonJS Tree Shaker Plugin for WebPack tools
Fedor Indutny

math-random-polyfill: A 'More Random' Browser Polyfill for Math.random() code
David Anson

consolemock: A Small Dev Utility for Testing Console Logs code
Thomas Marek

share-this: Medium-like Text Selection Sharing Without Dependencies code
Massimo Artizzu

Try Codeship Basic: Simple hosted CI that works out of the box 
Codeship  Sponsor

Curated by Peter Cooper and published by Cooperpress.

Like this? You may also enjoy: FrontEnd Focus : Node Weekly : React Status

Stop getting JavaScript Weekly : Change email address : Read this issue on the Web

© Cooperpress Ltd. Office 30, Lincoln Way, Louth, LN11 0LS, UK


by via JavaScript Weekly

Python's Pickles

Pickles in Python are tasty in the sense that they represent a Python object as a string of bytes. Many things can actually be done with those bytes. For instance, you can store them in a file or database, or transfer them over a network. 

The pickled representation of a Python object is called a pickle file. The pickled file thus can be used for different purposes, like storing results to be used by another Python program or writing backups. To get the original Python object, you simply unpickle that string of bytes.

To pickle in Python, we will be using the pickle module. As stated in the documentation:

The pickle module implements binary protocols for serializing and de-serializing a Python object structure. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or bytes-like object) is converted back into an object hierarchy. Pickling (and unpickling) is alternatively known as “serialization”, “marshalling,” or “flattening”; however, to avoid confusion, the terms used here are “pickling” and “unpickling”.

The pickle module allows us to store almost any Python object directly to a file or string without the need to perform any conversions. What the pickle module actually performs is what's so called object serialization, that is, converting objects to and from strings of bytes. The object to be pickled will be serialized into a stream of bytes that can be written to a file, for instance, and restored at a later point.

Installing pickle

The pickle module actually comes already bundled with your Python installation. In order get a list of the installed modules, you can type the following command in the Python prompt: help('modules').

So all you need to do to work with the pickle module is to import pickle!

Creating a Pickle File

From this section onwards, we'll take a look at some examples of pickling to understand the concept better. Let's start by creating a pickle file from an object. Our object here will be the todo list we made in the Python's lists tutorial.

In order to pickle our list object (todo), we can do the following:

Notice that we have made an import pickle to be able to use the pickle module. We have also created a pickle file to store the pickled object in, namely todo.pickle. The dump function writes a pickled representation of todo to the open file object pickle_file. In other words, the dump function here has two arguments: the object to pickle, which is the todo list, and a file object where we want to write the pickle, which is todo.pickle.

Unpickling (Restoring) the Pickled Data

Say that we would like to unpickle (restore) the pickled data; in our case, this is the todo list. To do that, we can write the following script:

The above script will output the todo list items:

As mentioned in the documentation, the load(file) function does the following:

Read a string from the open file object file and interpret it as a pickle data stream, reconstructing and returning the original object hierarchy. This is equivalent to Unpickler(file).load().

Pickles as Strings

In the above section, we saw how we can write/load pickles to/from a file. This is not necessary, however. I mean that if we want to write/load pickles, we don't always need to deal with files—we can instead work with pickles as strings. We can thus do the following:

Notice that we have used the dumps (with an "s" at the end) function, which, according to the documentation:

Returns the pickled representation of the object as a string, instead of writing it to a file.

In order to restore the pickled data above, we can use the loads(string) function, as follows:

According to the documentation, what the loads function does is that it:

Reads a pickled object hierarchy from a string. Characters in the string past the pickled object’s representation are ignored.

Pickling More Than One Object

In the above examples, we have dealt with pickling and restoring (loading) only one object at a time. In this section, I'm going to show you how we can do that for more than one object. Say that we have the following objects:

If you would like to learn more about Python dictionaries and tuples, check the following tutorials:

We can simply pickle the above objects by running a series of dump functions, as follows:

This will pickle all the four objects in the pickle file pickled_file.pickle.

There is another wonderful way to write the above script using the Pickler class in the pickle module, as follows:

To restore (load) the original data, we can simply use more than one load function, as follows:

The output of the above script is:

As with the Pickler class, we can rewrite the above script using the Unpickler class in the pickle module, as follows:

Note that the variables have to be written and read in the same order to get the desired output. To avoid any issues here, we can use a dictionary to administer the data, as follows:

To restore (load) the data pickled in the above script, we can do the following:

Pickles and Pandas

Well, this seems an interesting combination. If you are wondering what Pandas are, you can learn more about them from the Introducing Pandas tutorial. The basic data structure of pandas is called DataFrame, a tabular data structure composed of ordered columns and rows.

Let's take an example of DataFrame from the Pandas tutorial:

In order to pickle our DataFrame, we can use the to_pickle() function, as follows:

To restore (load) the pickled DataFrame, we can use the read_pickle() function, as follows:

Putting what we have mentioned in this section all together, this is what the script that pickles and loads a pandas object looks like:

Conclusion

In this tutorial, I have covered an interesting module called pickle. We have seen how easily this module enables us to store Python objects for different purposes, such as using the object with another Python program, transferring the object across a network, saving the object for later use, etc. We can simply pickle the Python object, and unpickle (load) it when we want to restore the original object.

Don’t hesitate to see what we have available for sale and for study in the marketplace, and don't hesitate to ask any questions and provide your valuable feedback using the feed below.


by Abder-Rahman Ali via Envato Tuts+ Code

The State of Social Video: Marketing in a Video-First World

It’s official – we are in the era of social video, and it’s coming up trumps. In fact, according to the Social Media Examiner’s 2016 Industry Report, 60% of marketers are currently using video and 73% plan to increase their use of video. It’s time to jump on the video bandwagon and establish your...

[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]

by DIW via Digital Information World

Facebook Ads: Creative Ways to Attract Prospects and Customers

Do you advertise on Facebook? Are your ads converting? To explore how to better use Facebook ads to reach leads and customers, I interview Zach Spuckler. More About This Show The Social Media Marketing podcast is an on-demand talk radio show from Social Media Examiner. It’s designed to help busy marketers and business owners discover [...]

This post Facebook Ads: Creative Ways to Attract Prospects and Customers first appeared on .
- Your Guide to the Social Media Jungle


by Michael Stelzner via

Matter Of Mind

Matter Of Mind is a creative agency, specialized in branding and advertising, based in France.
by via Awwwards - Sites of the day

CamanJS – Javascript Image Manipulation

CamanJS is manipulating images using the HTML5 canvas and Javascript. It's a combination of a simple-to-use interface with advanced and efficient image/canvas editing techniques.

It is also completely library independent and can be safely used next to NodeJS, jQuery, YUI, Scriptaculous, MooTools, etc.


by via jQuery-Plugins.net RSS Feed