The Science of Machine Learning
/I’ve just completed and published a new website - The Science of Machine Learning
It addresses the mathematics, data science, and computer science of Machine Learning.
Main topic categories include:
I’ve just completed and published a new website - The Science of Machine Learning
It addresses the mathematics, data science, and computer science of Machine Learning.
Main topic categories include:
Machine Learning and Artificial Intelligence are rapidly moving up a growth S curve similar to previous major information technologies. Twenty years ago, the Internet/Mobil technology curve was in the same S curve position as ML/AI is today. The internet was growing in use, but nowhere near the penetration we see today. Mobil devices existed, but with small, black and white displays and no internet access ... primitive compared to today's large, touch screen, internet connected computers in your hand. ML/AI today is likely as primitive compared to what it will be in 2030 as the 1998 Internet/Mobil technology was compared to its present state. By one estimate, AI will add 16 trillion dollars to the world's economy by 2030. And the benefits of ML/AI discussed below with have a networked, multiplicative impact as they reinforce one another.
It's impossible to predict exactly what ML/AI will look like over a decade from now, but it is possible to form a rough idea of what shape key developments will take. Here are a few...
ML/AI is already used extensively for information search. Steady progress is being made in understanding the contextual nuances of search queries. This trend will continue, and by 2030 we should be able to find just the right information for almost any search with ever increasing precision.
Autonomous vehicle progress has been steady and has achieved significant milestones. The technology has been demonstrated to work successfully at a fundamental level. There remain a number of barriers to widespread adoption. However, the benefits and incentives to overcome these barriers are significant. By 2030 we should see a significant number of autonomous vehicles on the road.
Many countries, including the United States, have aging populations. This will put an significant stress on healthcare over the coming decade. ML/AI holds the possibility of filling the gap between available resources and healthcare demand in 2030. According to one set of experts, we'll trust AI more than doctors to diagnose disease. This will free doctors to spends more time on the things technology can't yet do.
McKinsey predicts that by 2030, "60 percent of occupations have at least 30 percent of constituent work activities that could be automated." New jobs will also be created, but robotics and automation will continue to create significant shifts is how work is performed.
ML/AI is disrupting the traditional model for successful education. A top futurist predicts the largest internet company of 2030 will be an online school and students will learn from robot teachers over the internet. ML/AI in education has the potential to create individually customized experiences that will enhance and speed the learning process.
Retail shopping is already being dramatically effected by ML/AI. The e-commerce share of total retail sales in the U.S. is rising rapidly. Customized on-line shopping experiences are growing in sophistication. Retail in 2030 may look very different than it does today and include interactive dressing room mirrors and a more on-demand at-home shopping experience.
ML/AI can digest information at a speed and scope that already exceeds human capabilities. Professional service providers in 2030 will use ML/AI to provide analyzed and summarized information needed for decision making. This will dramatically speed services delivery and lower services costs.
Financial services rely on collecting, storing and analyzing vast amounts of data. ML/AI is already replacing workers who perform many of the tasks related to these activities. One estimate is that up to 230,000 employees in capital markets will be replaced by ML/AI as we approach 2030. This will lower costs and improve delivery of services, but will also require significant staffing shifts in the industry.
Advances in robotics and sensing technologies are radically modifying agricultural practices. New ML/AI approaches include: automated harvesting, pest control, animal tracking, and soil conservation. By 2030, we should see significant increases in crop yields at lowered costs.
ML/AI does pose challenges as it progresses spreads further into our lives and businesses. Here are a couple of examples...
As mentioned above, McKinsey predicts that by 2030, "60 percent of occupations have at least 30 percent of constituent work activities that could be automated." Overall, this would mean that about 20% of all jobs would be automated. New jobs will be created, with many of those requiring higher levels of education, training or skill.
As more work is performed by ML/AI, legal questions of responsibility and liability will arise. One example is autonomous car liability. Autonomous vehicles are expected to lower deaths caused by accidents. The Atlantic reports that automated cars could save up to 30,000 lives per year in the United States. But how responsibility for the deaths that do occur remains an open question.
It can be difficult to imagine a total picture of what ML/AI will mean for our lives in 2030. One way to grasp this is to imagine use cases that demonstrate their impact. Here's one example...
John is sitting in his study at home when he receives a notification on his smartphone. It tells him that the biosensor imbedded in his arm has detected a slight irregularity in his heartbeat. John hasn't noticed any physical pain or abnormality, but he clicks the notice and sees the display of a heart rhythm pattern with annotations showing him where there might be an issue. It assures him that it's nothing immediately life threatening, but that he should consult a doctor. He's shown the location of the nearest clinic and asked if he'd like an appointment be made along with arrangements for a ride. He clicks yes and immediately sees that his ride is on the way and will arrive in 5 minutes. John grabs his jacket and goes outside to wait for the car. In a few minutes, an autonomous vehicle pulls up, he gets in and a friendly voice asks him if he's John and going to the AbleWay clinic. John says yes, sits back and turns on his phone to read more information he's been sent about his symptoms. He arrives shortly at the clinic, is welcomed by name and escorted immediately into an examination room. A couple of minutes later, Dr. Able enters carrying a tablet which he uses to show John a real-time display of his heart rhythm. Dr. Able explains what could be the cause and that the condition is something they should watch carefully to see if it continues. He prescribes a medication that should help correct the arrhythmia and tells John that the medication will be delivered to his home by the end of the day. John shakes Dr. Able's hand and walks out to the reception desk where he's told that the clinic has his insurance information and the car to take him home is waiting outside. John enters the car and starts his trip home feeling relieved that he knows more about his condition and taking steps to deal with it.
In developing examples for my book on HTML5 Canvas (HTML5 Canvas for Dummies) I experimented with using stochastic processes to improve the realism of an animated displays. Early versions of the displays appeared rigid and artificial. Adding stochastic (random) variations brought the displays closer to real life and made them much more fun to watch.
Stochastic processes are already important to AI. Stochastic Gradient Descent combined with Backpropagation is used to iteratively modify data values passed between neural network nodes in order to minimize the error between the output of the neural network and the correct/true result.
Recently, I've explored whether stochastic techniques can be used to help humanize AI. Humans are not robots. We don't mindlessly pursue objectives without variation from a given path. A more human-like AI would certainly make the man-machine interface more pleasant to deal with than a purely robotic one. There are certainly AI applications where we don't want these kind of random variations taking place ... in autonomous vehicles for example.
You can experiment with one of my displays that employs stochastic processes ... click here to see it in action - once it's started, click control to see the variables you can modify using your keyboard.
Digital Computing has been with us for over 70 years. It's a deterministic technology using stored program software designed to produce accurate, precise results. For example, software for calculating your pay check will give you results that are correct down to the penny, just what you want!
Machine Learning technology is different ... it works in the domain of probabilities. For example, a machine learning based autonomous self-driving car makes many probability calculations every second ... such as the probability that a person approaching an intersection will stop and not cross in front of the car.
Digital Computing is able to perform some probabilistic calculations, but these are limited compared to those that can be performed by Machine Learning. Machine Learning is, conversely, limited in the deterministic calculations it can perform compared to the capabilities of Digital Computing.
So it's pretty obvious, the combination of Digital Computing and Machine Learning yields the perfect combination for interacting with the world we live in, as illustrated in the graphic below:
The combination of these two technologies will give us a huge boost in overall computing accuracy and cost effectiveness. Much of what we deal with on a daily basis is probabilistic in nature, and this dimension can now be effectively addressed with Machine Learning, such as is used in virtual assistants that are able to hear us speak, understand our words and give us answers to our questions.
And these two technologies will remain wedded to each other, as Machine Learning runs on a Digital Computing platform and needs Digital Computing to perform most practical tasks. Take our self-driving car example. That system needs access to precise road maps and feedback from car systems such as the engine and brakes. It's the combination of deterministic and probabilistic computing that creates the complete self-driving system that we'll end up trusting to get us safely home.
One of the hot technology topics of discussion lately surrounds the question of when the Machine Learning/Artificial Intelligence (ML/AI) 'singularity' will occur ... that is, when machine intelligence will evolve to equal human intelligence. Opinions run over a long time frame ... from as soon as 2029 (Ray Kurzweil) to around 2040-2050 (average of experts) to many decades from now.
Answering this question is linked to how rapidly one believes the ML/AI technology lifecycle S curve will rise. We do seem to have ML/AI S curve liftoff, as recent fundamental breakthrough developments in artificial neural networks, graphics processing units and other technologies have moved ML/AI from the laboratory to the field.
One perspective on the growth of ML/AI can be had by comparing ML/AI to the growth curves of previous major transformational information technology developments:
The chart below shows how the Machine Learning & Artificial Intelligence curve would look if it grew at the same rate as these previous developments:
The result is an S curve that grows at a rate that would place the singularity at the earlier of the estimates. These S curves seem to share some characteristics:
Is it possible there's a hidden law of major transformational technology lifecycle growth? That is, once liftoff is achieved, do market forces pour into the technology and push it rapidly up the S curve over a period of two decades. Personally, I think this is likely the case, and that the ML/AI curve will be no different than its predecessors. Time will tell the tale.
Machine Learning (ML) and Artificial Intelligence (AI) are changing the landscape of computing around the world, allowing us to do things with computers that were previously thought to be nearly impossible. ML/AI systems are now able to hear, speak, see, touch and interact with the environment and people around them.
In this article, I'll treat ML and AI together as a single topic. ML is the computer learning subset of AI, but for this discussion, they can be thought of as one.
A high level understanding of the layers of technology in ML/AI can help sort through the many options for developing and implementing an ML/AI system. ML/AI technology can be viewed as a three level hierarchy:
Mathematics > Models > Applications
Each of these layers provides an essential set of elements needed for a successful ML/AI system. Let's start with the mathematics base and build from there...
Mathematics, which dates back to 3000 BC and basic arithmetic, is a field of study that uses formulas (sequences of symbols) to represent ideas and the real world. Sounds a bit abstract, right? It is, but think about it ... ML/AI by its nature is doing just that inside a computing device - representing ideas and the real world. So mathematics is naturally and ideally suited to the pursuits of ML/AI.
ML/AI derives its tremendous power from the use of mathematics to, among other things, analyze probabilistic situations and outcomes. For example, an object recognition model might return the probability of .73 that a given photo contains the image of a cat. When we humans see a cat, we're usually pretty sure it's a cat. However, the combination of our senses and brains have done the complex mathematical-like analysis that produces that conclusion. Mathematics represents the calculations, estimations and processes needed to develop successful ML/AI models.
It's not necessary to understand all the math involved in an ML/AI system if you're using commercially supplied APIs or building on existing open source code. However, having some level of understanding of the underlying math can often be very useful and sometimes essential. One example, Stochastic Gradient Descent (SGD), is a mathematical function used to find a minima or maxima by iteration. It's used in a number of ML/AI models to iteratively improve the accuracy of output functions such as identifying objects in an input image. At a high level, the formula for SGD looks like this:
The components of this equation are:
Below is a list of some of typical mathematical concepts and functions used in ML/AI. Wikipedia is a good source of articles to start delving into these topics:
Bayesian Probability and Statistics - Calculus - Classification - Cluster Analysis - Convolution - Deviation Analysis - Dimensional Analysis - Eigenvalues, Eigenvectors - Error Analysis, Accuracy, Precision, Sensitivity, Specificity - Functional Analysis, Activation Functions, Sigmoid Function, Rectified Linear Unit - Geometry, Geometric Transformations - Gradients, Stochastic Gradient Descent, Gradient Boosting - Graph Theory - Hyperparameter Optimization - Information Theory, Entropy, Cross Entropy - K-means Clustering - Linear Algebra - Logistic Regression - Loss/Cost Functions - Markov Chains - Mathematical Constants - Matrix Mathematics - Model Fitting, Underfitting, Overfitting, Regularization - Monte Carlo Algorithms - Pattern Recognition - Probability Theory - Regression Analysis, Linear, Non-Linear, Softmax - Sampling - Statistical Analysis, Bias, Correlation, Hypothesis Testing, Inference, Validation, Cross Validation - Time Series Analysis - Variation Analysis, Coefficient of Determination - Vector Spaces, Vector, Algebra, Scalars - Weights, Synaptic Weights
Models are the embodiment in computer code of the mathematical representations used to perform ML/AI functions. In our example of a computer recognizing a cat in a photo, the ML/AI model represents the layers of processing needed to differentiate the image of a cat from all other possibilities.
Below is a conceptual model of an Artificial Neural Network (ANN), one of the types of ML/AI mathematical models. Data (shown as lines) is passed in a forward left to right direction between processing nodes (shown as circles). Numerical weights (w) are applied to individual data flows and biases (b) are applied to nodes in order to shape the output, such as the identification of a cat in an input image. Mathematical methods such as Stochastic Gradient Descent (discussed above) are used to adjust the weights and biases as data is repeatedly passed through the ANN.
In this diagram, the shapes and letters represent:
Below is a list of some of the mathematical models used in ML/AI. Wikipedia is a good source of basic information about these models:
Artificial Neural Networks - Association Rule Learning - Bayesian Networks - Decision Tree Learning - Deep Learning - Ensemble Learning - Hierarchical Clustering - Learning Classifier Systems - Learning to Rank - Long Short-Term Memory Neural Networks - Nearest Neighbors Algorithms - Recurrent Neural Networks - Reinforcement Learning - Sequence-to-Sequence Neural Networks - Similarity Learning - Sparse Dictionary Learning - Stochastic Neural Networks - Support Vector Machines - Unsupervised Learning
ML/AI applications use mathematical models, such as those discussed above, to perform meaningful tasks and produce meaningful results. Raw ML/AI results from mathematical models can be very interesting, but by themselves offer little utility. ML/AI applications provide that utility.
As an example, let's say we wanted to use our cat detecting Artificial Neural Network to let users of our smartphone app take a photo of a cat and determine what breed it belongs to. Our development team would need to:
ML/AI applications are appearing in every imaginable area of computing. If you do an internet search on almost any topic and include the term 'machine learning' you'll likely find results. Below are just a few examples of areas of active ML/AI applications:
Biometrics - DNA Classification - Computer Vision - Fraud Detection - Marketing - Medical Diagnosis - Economics - Natural Language Processing - Language Translation - Online Advertising - Search Engines - Handwriting Recognition - Speaker Recognition - Speech Recognition - Financial Market Analysis and Trading - Customer Service - Systems Monitoring - Recommender Systems - Self-driving Vehicles - Robotics - Cybersecurity - Legal Research - Criminal Investigations - Security Screening - Mapping - Healthcare - Face Detection and Recognition - Object Recognition - Weather Forecasting - Image Processing
Java and Python are two popular programming languages that represent two very different approaches to structure and syntax. Java has a more formal, strict syntax. Python syntax is more forgiving and provides many shortcuts and abbreviations. Java is compiled and Python is interpreted.
The table below shows a sampling of Java and Python programming language elements that highlight these differences.
Java | Python | |
---|---|---|
Block | { . . . } |
def my_block( ): . . . CR |
Class Definition |
public class Hello { . . . } |
class Hello: . . . CR |
Class Constructor |
public Hello(Strng msg) { defaultMsg=msg; } |
def __init__(self, param1, param2, ...) self.p1=param1 self.p2=param2 CR |
Class Instantiation |
Hello mHello=new Hello(); |
mHello=Hello( ) |
Class Variable |
static String myVariable; | class MyClass: my_class_variable = 0 |
Comment | // Comment text. | # Comment text. |
Constant | final static MY_CONSTANT; | MY_CONSTANT |
Conditional | if (a>b) { . . . } else { . . . } |
if (a>b): . . . else: . . . |
Exception Handling |
try { . . . } catch { . . . } |
try: . . . except Error, e: . . . |
Global Variable |
public class MyGlobals { public static String mG1 } |
global my_global_variable |
Importing | import java.sql.*; | import apackage as ap from apackage import amodule |
Inheritance | class Child extends Parent{...} | class Child(Parent): |
Instance Variable |
// Declared outside methods String mVariable; |
class Hello: def __init__(self, param1) self.param1=param1 |
Iteration | for (i=0; i<5; i++) { . . . } for (myElement : myCollection){ . . . } |
for x in range(0, 5): . . . for my_element in my_collection: . . . for key, value in my_dictionary: . . . |
Local Variable |
String myVariable; | my_variable |
Method Definition |
public void myIdea(String param) { . . . } |
def my_idea(my_parameter): . . . CR |
Method Use |
myIdea(mParameter); | my_idea(my_parameter) |
Namespace Reference |
packageName; |
my_namespace={'a':a_object, ...} |
Null Value | null |
None |
Statement Separator |
; |
new line |
Variable | String mString="Hello Java"; |
my_string="Hello Python" |
Below is a list of links to and brief descriptions of common software components. It's useful to keep these in mind and reference them during software design as a shorthand for code that will be written to implement the design. Implementing a component could involve just a few or many lines of code.
A suggested way to use these components in solving/coding a computing problem is to include them using a Separation of Concerns design pattern folded into what I call Layered Logic Problem Solving that includes these steps:
A type system is a collection of rules that assign a property called type to constructs such as variables, functions, expressions and modules. Here are the two main dimensions of typing and some programming language examples:
There are a number of commonly used Java data structures. Here are links to additional documentation and highlights of structural aspects:
The Internet Protocol Suite is the networking model and protocols used on the Internet. The diagram below shows the flow of control and data for the HTTP-TCP-IP-ARP most common protocol sequence. Here are links for referenced technologies:
As shown in the diagram below, there are three major Java class groups used for working with lists: Lists (such as List, ArrayList, LinkedList, AbstractList, Stack and Vector), Iterators (Iterator and ListIterator) and Collections. Together, these classes and their methods provide a wide spectrum options for list construction, examination and manipulation. For an example of using these classes, look here.
ArrayList data structures (also called Dynamic Arrays) are a frequently used programming function. ArrayLists have an advantage over arrays - ArrayLists are not fixed in size and can be dynamically changed by adding and deleting elements. Below are some common Java array manipulation constructs. For an overview of working with Java lists, look here.
Creating a new ArrayList is coded as shown below using the String type with an initial capacity of 20 entries:
ArrayList mArrayList = new ArrayList<String>(int 20);
The example below shows how to construct an ArrayList from the individual words in a String using an intermediary List object. Notice that the sequence of objects created is: String -> String array -> List -> ArrayList
String wordString = "this is a string of words"; String[] wordsArray = wordString.split(" "); List<String> wordsList = Arrays.asList(wordsArray); ArrayList wordsArrayList = new ArrayList<String>(wordsList);
The example below shows how to use the ArrayList add() method to add a String to wordsArrayList::
wordsArrayList.add("Example2");
The example below shows how to use the indexOf() ArrayList method find the index of an entry:
int index = wordsArrayList.indexOf("Example2");
The example below shows how to use the ArrayList remove() method using the entry index or the entry itself:
wordsArrayList.remove(index); wordsArrayList.remove("Example2");
The Collections sort() methods provide a variety of options for sorting ArrayList entries. Note that the ArrayList is sorted in place without returning a new ArrayList. This can be done because the array parameter is a value pointing to the ArrayList of objects. For example, the following statement would sort the words ArrayList from the example above using a modified MergeSort algorithm in an average O(n log(n)) time performance:
Collections.sort(wordsArrayList);
The Collections binarySearch() methods provide an efficient way to find an entry in a sorted ArrayList. For example, the following statement would find the index of the wordsArrayList entry for the String Example1 in an average of O(log(n)) time performance:
int entryIndex = Collections.binarySearch(wordsArrayList, "Example1");
The Iterator Class and ArrayList interator() method can be used to iterate over the entries of an ArrayList as shown in the example below :
Iterator mIterator = wordsArrayList.iterator(); while(mIterator.hasNext()){ System.out.println(mIterator.next()); };
Working with array data structures is one of the most frequently used programming functions. Arrays are implemented in a number of Java classes, such as String, Integer, and Boolean. Those arrays can be manipulated using the Java Arrays class (note that the Arrays class name is a plural). Below are some common Java array manipulation constructs.
The String split() method separates string characters into array entries using a supplied regular expression. For example, the following statement would create entries in the String array wordsArray using a blank character as a regular expression separator for processing the String wordString:
String wordString = "this is a string of words"; String[] wordsArray = wordString.split(" ");
The Arrays sort() methods provide a variety of options for sorting array entries. Note that the array is sorted in place without returning a new array. This can be done because the array parameter is a value pointing to the array of objects. For example, the following statement would sort the words array from the split() example above using a Quicksort process in an average O(n log(n)) time performance:
Arrays.sort(wordsArray);
The Arrays binarySearch() methods provide an efficient way to find an entry in a sorted array. For example, the following statement would find the index of the array wordsArray entry for the String entryValue in an average of O(log(n)) time performance:
int entryIndex = Arrays.binarySearch(wordsArray, entryValue);
The for() construct can be used to iterate over the entries in an array. For example, the following statement would iterate over and print every entry in wordsArray:
for (String word : wordsArray) {System.out.print(word);}
If you want to know and use the index of each entry, then this construct can be used:
for (int i=0; i<wordsArray.length; i++) {System.out.print(wordsArray(i);}
Android APIs provide classes and methods to facilitate displaying data in various forms, such as lists, arrays and custom layouts. The graphic below shows how the various classes and methods interact to produce the desired result.
Some examples of coding and use are:
The data to be displayed can be in such forms as an Array, List, File or SQLite database. If a database is used, a Cursor object is also employed. A simple array of String values might be created and converted to an ArrayList like this:
String[] mStringArray = new String[] { "blue", "green", "yellow", "red", "orange", "black", "white", "purple"}; List<String> mList = Arrays.asList(mStringArray); ArrayList<String> mArrayList = new ArrayList<String>(mList);
Cursor is an interface that provides random read-write access to the result set returned by a database query. Setting up a Cursor for an SQLite database might look like this:
MySQLiteOpenHelper mDatabaseHelper = new MySQLiteOpenHelper(this); Cursor mCursor = mDatabaseHelper.getDataIntoCursor(); ListView mListView = (ListView) findViewById(R.id.mListView); MyCursorAdapter mCursorAdapter = new MyCursorAdapter(this, mCursor); mListView.setAdapter(mCursorAdapter); class MyCursorAdapter extends CursorAdapter { public MyCursorAdapter(Context context, Cursor c) { super(context, c); } // Implement CursorAdapter bindView method. public void bindView(View view, Context context, Cursor cursor) { String name = cursor.getString(1); TextView textView = (TextView) view; textView.setText(name); } // Implement CursorAdapter newView method. public View newView(Context context, Cursor cursor, ViewGroup parent) { TextView view = new TextView(context); return view; } } private final class MySQLiteOpenHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "myDatabase"; private static final int DATABASE_VERSION = 1; private static final String CREATE_TABLE_TIMELINE = "CREATE TABLE IF NOT EXISTS table_name (_id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar);"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE_TIMELINE); db.execSQL("INSERT INTO ddd (name) VALUES ('Name1')"); db.execSQL("INSERT INTO ddd (name) VALUES ('Name2')"); db.execSQL("INSERT INTO ddd (name) VALUES ('Name3')"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } public Cursor getDataIntoCursor() { String selectQuery = "SELECT * FROM table_name; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); return cursor; } }
An Adapter acts as a bridge between an AdapterView and the underlying data for that view. A custom array adapter defined in Java might look like this:
class MyArrayAdapter extends ArrayAdapter<String> { HashMap<String, Integer> mHashMap = new HashMap<String, Integer>(); public MyArrayAdapter(Context context, int textViewResourceId, List<String> objects) { super(context, textViewResourceId, objects); for (int i = 0; i < objects.size(); ++i) { mHashMap.put(objects.get(i), i); } } @Override public long getItemId(int position) { String item = getItem(position); return mHashMap.get(item); } @Override public boolean hasStableIds() { return true; } }
The custom array adapter mArrayAdapter would be instantiated and linked to a ListView mListView with Java code like this:
setContentView(R.layout.mLayout); final ListView mListView = (ListView) findViewById(R.id.listview); final MyArrayAdapter mArrayAdapter = new MyArrayAdapter(this, android.R.layout.simple_list_item_1, mArrayList); mListView.setAdapter(mArrayAdapter);
An AdapterView is a view whose children are determined by an Adapter. You might use an AdapterView like this:
private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { // Do something in response to the click } }; listView.setOnItemClickListener(mMessageClickedHandler);
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/mLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/mListView" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
ViewGroup is a special view that can contain other views called children. A ViewGroup defined in XML might look like this:
<ViewGroup xmlns:android="http://schemas.android.com/apk/res/android" android:id="@id/mViewGroup" android:layout_height="wrap_content" android:layout_width="fill_parent" > <TextView android:id="@id/mTextView" android:layout_height="wrap_content" android:layout_width="fill_parent" </TextView> </ViewGroup>
A layout defines the visual structure for a user interface. A layout might look like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/mLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/mListView" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
An Activity provides a screen with which users can interact.
public class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); . . . } }
public class ArticleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.article_view, container, false); } }
Because Android contains an extremely large number of Classes and Methods, it's impossible to hold all of the syntax and details in your head. For this reason, I've found that Android interview questions tend to be more conceptual in nature. For example, you might be asked how you'd approach a given problem using Android and Java constructs.
As you study the commonly used interview topics below, try to retain the main ideas and some relevant details.
Android Fragments provide a mechanism to create modular sections of an Activity. You can see how Fragments fit into the overall Android structure in the blog post Android App Major Elements. You can see an example of Fragment use in the blog post Android Fragments.
Fragments provide a couple of significant benefits for Apps:
Below is an overview of Fragments and the Android elements they interact with for Fragment control.
The main Activity of an app uses the setContentView() method to establish a View.
Activities can start and be started by Fragments.
The View class represents the basic building block for user interface components. A View occupies a rectangular area on the screen and is responsible for drawing and event handling. View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). Views can be accessed via their ids or from the Root View.
The Root View is the base of the tree for all the Views for an app. It can be referenced using android.R.id.content.
Layout XML can be used to define Views. When an app is compiled, the XML definitions are used to create View details. Note that this can include Fragment properties. If a Layout that includes a Fragment specification is used by the Activity setContentView() method, that Fragment will be automatically started by the Android operating system.
A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity. Sub classes of Fragment include: PreferenceFragment, DialogFragment and ListFragment.
The FragmentManager class is an Interface for interacting with Fragment objects.
The FragmentTransaction class is used for performing Fragment operations.
There are many elements that can be included in an Android app. It's helpful to see how these are clustered and interact, as shown in the chart below. Only major elements are shown. The Android Developers Website provides details on the full set of app elements.
The minimum required elements are shown with a red dot. An app with just these elements wouldn't be very useful, but you can see that there are many optional elements that can be added for a variety of functions.
Intents are used to request an action from an app component. Intents are detected and acted upon by Broadcast Receivers.
Activities manage the Displays that app users see. Fragments are modular sections of an Activity that, among other uses, facilitate app adaptation to various screen sizes and dimensions.
The User Interface includes a variety of pre-built elements that facilitate constructing screen displays, including: View, Control, Dialog, Notification, Setting, Action Bar, Menu and Toast.
Services perform long-running operations in the background and do not provide a User Interface.
These elements facilitate asynchronous operations that protect the UI thread from undesirable delays. Loaders make it easy to asynchronously load data in an Activity or Fragment. AsyncTask allows performing background operations and publishing results on the UI thread without having to manipulate threads and/or handlers.
These elements facilitate the access to Data, Devices and Sensors. Connectivity provides access to devices and the Internet. Content Providers can optionally be used to access databases and internal/external files. Sensors include: GPS, network location, accelerometer, gyroscope and more.
These elements are essential for app operation. Libraries provide access to compiled code from Google and other sources. Access to the user Display is built into the operating system. The Manifest include essential information about the app and its elements. Resources include: Layouts, Values and Drawables. Shared Preferences are use to store key-value pairs that act as global variables for the app.
Java and Ruby are two popular programming languages that represent two very different approaches to structure and syntax. Java has a more formal, strict syntax. Ruby syntax is more forgiving and provides many shortcuts and abbreviations. Java is compiled and Ruby is interpreted.
The table below shows a sampling of Java and Ruby programming language elements that highlight these differences.
Java | Ruby | |
---|---|---|
Block | { . . . } |
def/class/do/if... . . . end |
Class Definition |
public class Hello { . . . } |
class Hello . . . end |
Class Constructor |
public Hello(Strng msg) { defaultMsg=msg; } |
def initialize(msg) . . . end |
Class Instantiation |
Hello mHello=new Hello(); |
mHello=Hello.new("Hello Ruby") |
Class Variable |
static String myVariable; | @@myVariable |
Comment | // Comment text. | # Comment text. |
Constant | final static MY_CONSTANT; | MY_CONSTANT |
Conditional | if (a>b) { . . . } else { . . . } |
if a>b . . . else . . . end |
Exception Handling |
try { . . . } catch { . . . } |
raise . . . |
Global Variable |
public class MyGlobals { public static String mG1 } |
$mG1 |
Importing | import java.sql.*; | require 'extensions' |
Inheritance | extends MyClass | < MyClass |
Instance Variable |
// Declared outside methods String mVariable; |
@mVariable |
Iteration | for (i=0; i<5; i++) { . . . } for (myElement : myCollection){ . . . } |
5.times do | i | . . . end |
Local Variable |
String myVariable; | my_variable |
Method Definition |
public void myIdea(String param) { . . . } |
def myIdea(param="Starter") . . . end |
Method Use |
myIdea(mParameter) | myIdea m_parameter |
Namespace Reference |
packageName; |
: : |
Null Value | null |
nill |
Statement Separator |
; |
new line |
Variable | String mString="Hello Java"; |
mString="Hello Ruby" |
There are hundreds of programming languages. Although most aren't commonly used, a software developer might use a number of them during their career. It's useful to understand the concepts that underlie these languages. Click here for a comparison of programming languages.
Below are brief descriptions of some common and important programming constructs with links to more details.
Separating ideas from specific instances of those ideas. Click here for more details.
Define characteristics of constructs such as classes and methods. Examples of modifiers are: public, private, package and protected: Click here for more details.
A data structure consisting of a collection of elements, each identified by at least one array index or key. Click here for more details.
Defines a property of an object, element, or file. Click here for more details.
Section of code grouped together. Click here for more information.
Sequence of statements triggered by a conditional. Click here for more information.
Pointer to a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some appropriate time. Click here for more details.
Code template for creating objects. Think of architect's drawings. Click here for more details.
Performs different actions depending on whether a condition is true or false. Click here for more details.
In Object Oriented Programming, a block of code that's executed when an object is created. Click here for more details.
An object made up of other objects. Click here for more details.
Convention for representation and interaction between objects in HTML documents. Click here for more details.
Variable sized data structure that allows adding and deleting elements. In Java, an ArrayList is one type of Dynamic Array. Click here for more information.
Packing of data and functions into a single component. Click here for more details.
Anomalous or failure event that may require special handling. Click here for more details.
Combination of elements that produces a result. Click here for more details.
Reclaiming of unused memory. In some operating systems, such as Android, Garbage Collection is automatic. Click here for more details.
Used to provide direct access to data based on a calculated key. Click here for more details.
A set of elements that can read by web browsers for displaying web pages. Click here for more details.
Individual component of an HTML document. Click here for more details.
Indicates the desired action to be performed on an identified resource. Actions include: GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE, PATCH and CONNECT. Click here for more details.
Names for objects and entities. Click here for more details.
Obtaining the capabilities on an object. Click here for more details.
A Class that is declared entirely within the body of another class or interface. An inner class cannot be instantiated without being bound to a top-level class. Click here for more details.
Specific realization of an object. Think of a house vs. the plans for a house. Click here for more details.
An object that enables traversing a container, such as lists. Click here for more details.
Human-readable text used to transmit data objects consisting of attribute–value pairs. Often used to transmit data between a server and web application. Click here for more details.
Word with a special meaning. Click here for more details.
A sequence of values, where the same value may occur more than once. Click here for more details.
A data structure consisting of a group of nodes which together represent a sequence. Click here for more details.
Notation representing a fixed value. Click here for more details.
A sequence of statements which is specified once but can be carried out multiple times in succession. Click here for more details.
A class whose instances are classes. Click here for more details.
Code that defines the run time behavior of objects. Click here for more details.
A class which contains a combination of methods from other classes without using inheritance. Click here for more details. Not available in pure Java.
In Object Oriented Programming, an object is an instance of a class. Click here for more details.
Perform actions on elements such as variables. Click here for more details.
Ability to create multiple methods of the same name with different implementations. Click here for more details.
In object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. Click here for more details.
A Java package is a mechanism for organizing Java classes into namespace groups. Click here for more information.
A reference or value passed to a function, procedure, subroutine, command, or program. Click here for more details.
Provision of a single interface to entities of different types. Click here for more details.
Smallest 'unit of processing' available to a programmer of a particular machine, or can be an atomic element of an expression in a language. Click here for more details. For example, in Java, primitive data types are: byte, short, int, long, float, double, boolean and char.
Ability to modify an object's own properties at runtime. Click here for more details.
Sequence of characters that can be used as a search pattern. Sometimes referred to as Regex or Regexen. Click here for more details.
A word that cannot be used as an identifier, such as the name of a variable or function. Click here for more details.
A value that is passed from a calculating function to an invoking function. Click here for more details.
Functions that put elements in a certain order. Sorting is used so often that many programming languages have built-in sort functions. Click here for more information.
Expresses some action to be carried out. Click here for more details.
Control mechanism used to allow the value of a variable or expression to change the control flow of program execution. Click here for more details.
A collection of related data held in a structured format within a database. It consists of fields (columns), and rows. Click here for more details.
Refers to the object, class, or other entity that the currently running code is part of. Click here for more details.
An object which represents the right to perform an operation. Click here for more details.
Collection of rules for constructs such as variables and functions. Untyped languages allow any operation to be performed on any data type. Click here for more details.
A storage location and associated name which contains a modifiable value. Click here for more details.
Copyright © 2024 Don Cowan All Rights Reserved
About The Science of Machine Learning Publications. Speaking Blog Contact