Map

1.Outlets and Actions
2.Presenting View Controllers
3.The Delegate Pattern
4.Tables
5.Navigation
6.MemeMe Techniques

Clike counter
ViewController -> UIViewm UILabel, UIButton

import UIKit

class ViewController: UIViewController {
	override func viewDidLoad(){
		super.viewDidLoad()
		var label = UILabel()
		label.frame = CGRectMake(150, 150, 60, 60)
		label.text = "0"

		self.view.addSubview(label)

		var button = UIButton()
		button.frame = CGRectMake(150, 250, 60, 60)
		button.setTitle("click", forState: .Normal)
		button.setTitleColor(UIColor.blueColor(), forState: .Normal)
		self.view.addSubview(button)
	}
}
import UIKit

class ViewController: UIViewController {

	var count = 0
	var label:UILabel!

	override func viewDidLoad(){
		super.viewDidLoad()
		var label = UILabel()
		label.frame = CGRectMake(150, 150, 60, 60)
		label.text = "0"

		self.view.addSubview(label)
		self.label = label

		var button = UIButton()
		button.frame = CGRectMake(150, 250, 60, 60)
		button.setTitle("click", forState: .Normal)
		button.setTitleColor(UIColor.blueColor(), forState: .Normal)
		self.view.addSubview(button)

		button.addTarget(self, action: "incrementCount", forControlEvents:
			UIControlEvents.TouchUpInside)
	}
	func incrementCount(){
		self.count++
		self.label.text = "\(self.count)"
	}
}

Implementation Steps

Name, Service, Conditions, Event Handler
Name:makeUppercase, Realtime Database, Trigger on specific path, makeUppercase handler

const functions = require('firebase-functions');

exports.emojify = functions.database.ref('/messages/{pushId}/text')
	.onWrite(event => {
		if(!event.data.val()|| event.data.previous.val()){
			console.log("not a new write event");
			return;
		}

		console.log("emojifying");

		const originalText = event.data.val();
		const emojifiedText = emojifyText(originalText);

		return event.data.ref.set(emojifiedText);
	});

	function emojifyText(text){
		var emojifiedText = text;
		emojifiedText = emojifiedText.replace(/\blob\b/id, "");
		emojifiedText = emojifiedText.replace(/\blob\b/id, "");
		return emojifiedText;
	}

FirebaseUI Authentication

Adding Sign In
-Email/Password
-Google
-Facebook
-Twitter
-GitHub

mPhotoPickerButton.setOnClickListener(new View.OnClickListener(){
	@Override
	public void onClick(View view){
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setType("image/jpeg");
		intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
		startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER);
	}
});
service firebase.storage {
	match /b/friendlychat-12987.appspot.com/o {
		match /{allPaths=**} {
			allow read, write: if request.auth != null;
		}
	}
}

match /funGifs/cats/{catPics=**}

resource: existing storage data
request: incoming request data

allow read: if request.auth != null && imageId.matches(“.*.gif”)

String name = "Firebase":
name = 3;

var name = "Firebase";
name = 3;

final x = 42;
x = 1024;

const x = 42;
x = 1024

var x = 42;
var y = 1024;

System.out.println("Message");
Log.d(TAG, "Message");

public int multiply(int a, int b){
	return a * b;
}

String[] list = {"apple", "pear", "orange"};

for (int i = 0; i < list.length; i++){
	System.out.println(list[i])
}

Databse implementation

mSendButton.setOnClickListener(new View.OnClickListener(){
	@Override
	public void onClick(View view){
		mMessageEditText.setText("");
	}
});
{
	"rules": {
		".read": true,
		".write": ture
	}
}

Database Security
-Database security rules
-Security rule configuration examples
-Authentication with Firebase

{
	"chat": {
		"messages": {
			"-KS3PV-iwUZp5wkNq70s":{
				"name": "person1",
				"text": "hey!"
			},
			"-KS3PXhIhs8J_inrExy4":{
				"name": "person2",
				"text": "what's up?"
			}
		}
	},
	"special_chat": {
		"messages": {
			"-KR-DwqtKzlWGxSn9P0y":{
				"name": "person1",
				"text":"want to go to the movies?"
			},
			"-KR4tIpWmNn-EYxquSrw": {
				"name": "person3",
				"text": "Yeah! let's meet at 7."
			}
		}
	},
	"users": {
		"uid1":{
			"paid":true
		},
		"uid2": {
			"paid": false
		},
		"uid3": {
			"paid": true
		}
	}

}

Firebase Realtime Database

what is firebase databse?
what can it be used for?
realtime database in action
database structure best practices

“:
“golden”: 1.618
“fog”: true
“palindrome”: “tacocat”
“messages”: “message1”: “name”:”person”, “text”:”Hello?”
“message2”: “name”:”Individual”, “text”:”Hi!”

node should be “child” and “parent

{
	"questions": {
		"ABCDakarandomkey": {
			"question": "Who was the 13th president of the United States?"
			"choice_1": "Millard Fillmore",
			"choice_2": "Zachary Taylor",
			"choice_3": "Franklin Pierce",
			"choice_4": "James K. Polk",
			"answer" :"choice_1"
		},
		"EFGHakarandomkey":{
			"question": "In what year was the first gasoline combustion engine invented?",
			"choice_1":"1769",
			"choice_2":"1886",
			"choice_3":"1807",
			"choice_4":"1864",
			"answer": "choice_4"
		}
	},
	"players":{
		"user_key_1":{
			"name":"Person",
			"opponents":{
				"IJKLakarandomkey":"user_key_2",
				"MNOPakarandomkey":"user_key_6"
			},
			"questions":{
				"ABCDakarandomkey": "Correct",
				"EFGHakarandomkey": "Incorrect"
			}
		},
		"user_key_2": {
			"name": "Mai",
			"opponents": {
				"QRAAakarandomkey": "user_key_1",
				"SQUEakarandomkey": "user_key_6"
			},
			"questions": {
				"ABCDakarandomkey":"Incorrect",
				"EFGHakarandomkey":"Incorrect"
			}
		}
	},
	"opponents": {
		"couple_Key_1": "user_key_1_user_key_2",
		"user_1": "user_key_1",
		"user_2": "user_key_2",
		"winner": "user_key_1"
	}
}

Firebase

Be comfortable with
– making a simple mobile application
– basic command line
– calling a web or cloud API

FriendlyChat
APP, DB, FILE

package com.google.firebase.xxxx.friendlychat;
public class FriendlyMessage {
	private String text;
	private String name;
	private String photoUrl;

	public FriendlyMessage(){
	}

	public FriendlyMessage(String text, String name, String photoUrl){
		this.text = text;
		this.name = name;
		this.photoUrl = photoUrl;
	}

	public String getText(){
		return text;
	}

	public void setText(string text){
		this.text = text;
	}

	public String getName(){
		return name;
	}

	public void setName(String name){
		this.name = name;
	}
	public String getPhotoUrl{
		return photoUrl;
	}

	public void setPhotoUrl(String photoUrl){
		this.photoUrl = photoUrl;
	}
}
package com.google.firebase.xxxx.friendlychat;

import android.os.Bundle;
import android.support.v7.app.AppCompactActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends(Bundle savedInstanceState){
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);

	mUsername = ANONYMOUS;

	mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
	mMessageListView = (ListView) findViewById(R.id.messageListView);
	mPhotoPickerButton = (ImageButton) findViewById(R.id.photoPickerButton);
	mMessageEditText = (EditText) findViewById(R.id.messageEditText);
	mSendButton = (Button) findViewById(R.id.sendButton);

	List<FriendlyMessage> friendlyMessages = new ArrayList<>();
	mMessageAdapter = new MessageAdapter(this, R.layout.item_message, friendlyMessages);
	mMessageListView.setAdapter(mMessageAdapter);

	mProgressBar.setVisibility(ProgressBar.INVISIBLE);

	mPhotoPickerButton.setOnClickListener(new View.OnClickListener(){
		@Override
		public void onClick(View view){

		}
	});

	mMessageEditText.addTextChangedListener(new TextWatcher(){
		@Override
		public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2){			
		}

		@Override
		public void onTestChanged(CharSequence charSequence, int i, int i1, int i2){
			if (charSequence.toString().trim().length() > 0){
				mSendButton.setEnable(true);
			} else {
				mSendButton.setEnabled(false);
			}
		}

		@Override
		public void afterTextChanged(Editable editable){
		}
	});
	mMessageEditText.setFilter(new InputFilter[]{new InputFilter.LengthFilter(DEFALT_MSG_LENGTH_LIMIT)});

	mSendButton.setOnClickListener(new View.OnClickListener(){
		@Override
		public void onClick(View view){
			mMessageEditText.setText("");
		}
	});

	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		return super.onOptionsItemSelected(item);
	}
}

Evaluation Metrics

accuracy = no of items in a class labeled correctly / all items in that class

positive – negative
percision = true positive / true positive + false positive
recall = true positive / true positive + negative positive

predictions = [0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1]
true labels = [0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0]

data set, features, algorithms, evaluation

GridSearchCV

parameters = {'kerne':('linear','rbf'),'c':[1,10]}
svr = svm.SVC()
clf = grid_search.GridSearchCV(svr, parameters)
clf.fit(iris.data, iris.target)

parameters = {'kernel':('linear','rbf'),'C':[1,10]}

svr = svm.SVC()
clf = grid_search.GridSearchCV(svr,parameters)
clf.fit(iris.data, iris.target)
clf.best_params_

Validation

import numpy as np
from sklearn import cross_validation
from sklearn imort datasets
from sklearn import svm

iris = datasets.load_iris()
iris.data.shape, iris.target.shape((150, 4), (150,))

X_train, X_test, y_train, y_test = cross_validation.train_test_split(
	iris.data, iris.target, test_size=0.4, random_state=0)

X_train.shape, y_train.shape((90, 4), (90,))
X_test.shape, y_test.shape((60, 4), (60,))

clf = svm.SVC(kernel='linear',C=1).fit(X_train, y_train)
clf.score(X_test, y_test)0.96

Training, Transforms, Predicting
Train/test split -> pca -> svm

clf = GaussianNB()
t0 = time()
kf = KFold(len(authors), 2)
for train_indices, test_indicies in kf:
	features_train = [word_data[ii] for ii in train_indices]
	features_test = [word_data[ii] for ii in test_indices]
	authors_train = [authors[ii] for ii in train_indices]
	authors_test = [authors[ii] for ii in test_indices]

	vectorizer = TfidfVectorizer(sublinear_tf=True, max_df=0.5,
			stop_words='english')
	features_train_transformed = vectorizer.fit_transform(features_train)
	features_test_transformed = vectorizer.transform(features_test)
	selector = SelectPercentile(f_classif, percentile=10)
	selector.fit(features_train_transformed, authors_train)
	features_train_transformed = selector.transform(features_train_transformed).toarray()
	features_test_transformed = selector.transform(features_test_transformed).toarray()

	clf.fit(features_train_transformed, authors_train)
	print "training time:", round(time()-t0, 3), "s"
	t0 = time()
	pred = clf.predict( features_test_transformed )

When to use PCA

When to use PCA
-> latent features driving the patterns in data
-> dimensional reduction
-> visualize high-dimensional data, reduce noise
-> make other algorithms(regression, classification) work better fewer inputs

PCA for facial recognition

X_train, X_test, y_train, y_test = train_split(X, y, test_size=0.25)

n_components = 150

print "Extracting the top %d eigenfaces from %d faces" % (n_components, X_train.shape[0])
t0 = time()
pca = RandomizePCA(n_components=n_components, whiten=True).fit(X_train)
print "done in %0.3fs" % (time() - t0)

eigenfaces = pca.components_.reshape((n_component, h, w))

print "Projecting the input data on the eigenfaces orthnormal basis"
t0 = time()
X_train_pca = pca.tranform(X_train)
X_test_pca = pca.transform(X_test)
print "done in %0.3fs" % (time() - t0)

print "Fitting the classifier to the training set"

http://scikit-learn.org/stable/auto_examples/applications/face_recognition.html