Showing posts with label coding. Show all posts
Showing posts with label coding. Show all posts

Monday, February 6, 2017

The ever-changing landscape

Update on 2017.2.16: TensorFlow 1.0
I started to learn machine learning in September 2016, and recently found that some python functions from the machine learning library was already deprived. I realize now machine learning is growing so fast that the API is being updated non-stop to better suit the real-world requirements.
Here are some notes to track the grammar changes that affect my projects.

Tensorflow

tf.initialize_all_variables() # 0.11
tf.global_variables_initializer() # 0.12

scikit-learn

from sklearn.model_selection import validation_curve, train_test_split, GridSearchCV, KFold, cross_val_score  # 0.18, 
from sklearn.cross_validation import train_test_split # 0.17

Note:

model_selection is a new module, which groups several functionalities together:
  • cross_val_score(svc, X, y, cv=KFold(N_splits=3, n_jobs=-1) is very convenient. You get 3 sets of data, fit, prediction and score in one line of code. So you can easily see the variation caused by data fluctuation.
  • A more fancy way is to use validation_curve, in which you get both training score and test score for a set of hyperparameters. e.g. train_scores, test_scores = validation_curve(SVC(), X, y, param_name="gamma", param_range=np.logspace(-6,-1,5), cv=10, scoring="accuracy", n_jobs=1)
  • An even more fancy way is to use learning_curve, in which you see the score change with data size. e.g. train_sizes, train_scores, valid_scores = learning_curve (SVC(kernel='linear'), X, y, train_sizes=[50, 80, 110], cv=5)
  • Don’t forget the previous GridSearchCV is still powerful.

Sunday, November 13, 2016

7 programming languages to say "Hello World"

Java

public class Hello
{
  public static void main(String args[])
  {
    String s = "Hello world!";
    System.out.println(s);
  }
}

C++

#include <string>
#include <iostream>
using namespace std;
int main()
{
  string s("Hello world!");  // use library string
  char s1[]="Hello world!";  // use base library
  cout<<s<<endl;
  return 0;
}

Python

s = "Hello world!"
print s  # python 2.x
print(s) # python 3.x

HTML

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        "Hello world!"
    </body>
</html>

PHP

<?php  
  $s= "Hello wolrd!"; 
  echo $s;
  ?>

JavaScript

var s=“Hello World!”;
console.log(s);

swift

var s="Hello world" // inferred typing
var s1: Character = "Hello world" //explicit typing
print(s)

Monday, September 26, 2016

Hanker Rank: order of word

Given a string, find the lexical sequence:
import math
def num(word):
    length=len(word)
    ans=math.factorial(length)
    wordset=set(word)
    for letter in wordset:
        fre=word.count(letter)
        ans=ans/fre
    return ans
def  get_ranks( words):
    if len(words)==1:
        return 0
    setwords=set(words)
    listwords=list(setwords)
    listwords.sort()
    prenumber=0
    head=words[0]
    seq=listwords.index(head)
    for i in range(seq):
        absent=listwords[i]
        k=words.index(absent)
        prenumber+=num(words[0:k]+words[k+1:])

    return prenumber+get_ranks(words[1:])

Wednesday, September 21, 2016

A failed phone interview

On the second day of my previous post, I kind of rushed into a phone interview by G. I didn’t do a good job because I wasted quite a bunch of time on the format of inputs. I should better communicate with the interviewer.
Question: given 2 rectangles, output the intersection rectangle.
def getIntersection(r1,r2):
    '''
    :type r1:list[float]
    :type r2:list[float]
    # only need 2 x-coordiantes and 2 y-coordinates to represent a rectangle. e.g.[x1,x2,y1,y2] #re max,min of x; max,min of y
    :rtype list[float]
    '''
    if r1[0]<=r2[1] or r1[1]>=r2[0] or\
        r1[2]<=r2[3] or r1[3]>=r2[2]:
        return []
    listx=r1[0:2]+r2[0:2]
    ans_x=[x for x in listx if x<max(listx) and x>min(listx)]
    listy=r1[2:]+r2[2:]
    ans_y=[x for x in listy if x<max(listy) and x>min(listy)]
    return ans_x+ans_y