A support vector machine constructs a hyper-plane or set of
hyper-planes in a high or infinite dimensional space, which can be used
for classification, regression or other tasks. Intuitively, a good
separation is achieved by the hyper-plane that has the largest distance
to the nearest training data points of any class (so-called functional
margin), since in general the larger the margin the lower the
generalization error of the classifier.
upload successful
Mathematical formulation
Given training vectors \(x_{i}\in
\mathbb{R}^{p}\), i=1,..., n, and a vector \(y\in {1,-1}^{n}\),SVC solves the following
primal problem:
where 'e' is the vector of all ones,'C' > 0 is the upper bound,'Q'
is an n by n positive semidefinite matrix,\(Q_{ij}\equiv
y_{i}y_{j}K(x_{i},x_{j})\),where \(K(x_{i},x_{j})=\phi (x_{i})^{T}\phi
(x_{j})\) is the kernel. Here training vectors are implicitly
mapped into a higher (maybe infinite) dimensional space by the function
\(\phi\).
The decision function is:
\(sgn(\sum_{i=1}^{n}y_{i}a_{i}K(x_{i},x)+\rho
)\)
These parameters can be accessed through the members dual_coef_ which
holds the difference , support_vectors_ which holds the support vectors,
and intercept_ which holds the independent term
reduce() accept 2 arguments:reduce(callback,initialValue),this
callback function will get 4 arguments:
callback(accumulator,currentValue,currentIndex,array) >
accumulator > > The accumulated value previously returned in the
last invocation of the callback, or initialValue, if supplied. (See
below.) > > currentValue > > The current element being
processed in the array. > > currentIndex > > The index of
the current element being processed in the array. Starts at index 0, if
an initialValue is provided, and at index 1 otherwise. > > array
> > The array reduce was called upon.
For example,use
1 2 3
[1,2,3,4,5].reduce(function(a,b){ return a+b })
It returned 15.
First callback function got (1,2) returned 3,then the 2nd callback
function got (3,3) returned 6.The 3rd callback got (6,4) returned 10,the
4st callback got (10,5) returned 15,the finally result.
Math.pow() will calculate power value.It accept 2 arguments,like
Math.pow(base,exponent)
It's equal to baseexponent
In this problem,[1,2,3].reduce(Math.pow) means that:
step1: Math.pow got (1,2) returned 1
step2: Math.pow got (1,3) returned 1
Finally it got 1.
But the 2nd array's item is []
reduce() method need an intial value at least,so it will throw an
error:
TypeError: Reduce of empty array with no initial value
So this problem's correct answer is A
4.What
is the result of this expression? (or multiple ones)
1 2
var val = 'smtg'; console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing');
A Value is Something
B Value is Nothing
C NaN
D Other
This is a trick question.The correct is D
The operator '+' will calculate first because it has higher
precedence.
That expression equal to
1
console.log('Value is true'? 'Something':'Nothing');
An string expression like 'true' in condition,So it will print
'Something'
5.What
is the result of this expression? (or multiple ones)
1 2 3 4 5 6 7 8 9
var name = 'World!'; (function () { if (typeof name === 'undefined') { var name = 'Jack'; console.log('Goodbye ' + name); } else { console.log('Hello ' + name); } })();
A Goodbye Jack
B Hello Jack
C Hello undefined
D Hello World
This a question about "Hoisting".The 'var' will hoisting in the
function scope.
So the code will be this:
1 2 3 4 5 6 7 8 9 10
var name = 'World!'; (function () { var name if (typeof name === 'undefined') { name = 'Jack'; console.log('Goodbye ' + name); } else { console.log('Hello ' + name); } })();
For if (typeof name === 'undefined'),it will find name from scope
chain,so this 'name' is declared in anonymous function: var name
'var name' means that name is undefined,so condition of 'if' is
true,then name is declared 'Jack'.
I got the data structure of stack and binary tree in last article,and
finally I got a stack what can describe algebraic expression clearly.Now
I can create a expression tree with this stack.
Create the expression tree
This expression tree is a binary tree because each function has 2
arguments commonly.So there are 3 things to describe an
algebraic:operator,1st operand(orginal) and 2nd operand.I set binary
tree node's left child is the 1st operand,right child is the 2nd operand
and its value is the operator.
For example,1+2 is writen
1 2 3
+ / \ 1 2
Before create the expression tree,I have to deal with the stack I got
before.Because that stack is a reverse array to describe the algebraic
expression.
Reverse stack
1 2
stack = Stack() while origin.length() isnot0: stack.push(origin.pop())
Um..that's all.
And create an empty stack to store the operands temporarily while pop
this stack.
Create expression
tree and assign elements
1 2 3 4 5 6 7 8 9
while stack.length() isnot0: if stack.getTop() in operators: node = BiTree(stack.pop()) node.right = temp.pop() node.left = temp.pop() else: node = BiTree(stack.pop()) temp.push(node) return temp.pop()
By this way,I get the correct expression tree.
For example,I input "A+b-x*(12.13+51y)1.4121" before,now I
get a binary tree:
1 2 3 4 5 6 7 8 9 10 11
+ / \ A - / \ b * / \ + ^ / \ + 1.4121 / \ 51 y
Traverse
the binary tree and create functional expression
This is an iterative process.I convert each child node into a
functional expression,such as
1 2 3
+ / \ 1 2
will change to add(1,2)
And put this functional expression as its parent node's child,and
loop again and again,until only one node left.
1 2 3 4 5 6
operators_name = {'+':'add','-':'mius','*':'muilt','/':'divide','^':'power'} #create_function_expression defcfe(Tree): if Tree.value in operators_name: Tree.value = operators_name[Tree.value] +'('+ cfe(Tree.left) +','+ cfe(Tree.right) +')' return Tree.value
Test it,input "A+b-x*(12.13+51y)1.4121" and this script
ouput add(A,mius(b,muilt(x,power(add(12.13,power(51,y)),1.4121))))
My colleague,Tu is writing a programme for calculation of money in
bank.Those algebraic expression are complicated.And he write program
with Java.So when he is writing functional expression in Java,he is
confusion because there are too much functions and brackets.
To save him in brackets' sea,I plan to make an tool to convert
algebraic expression to functional expression.
I will use Python to do that.
First,I consider commonly operators are functions have 2 arguments,so
I can construct binary tree to store algebraic expression.
Second,it is likely that many brackets in expression.So I consider
using stack to deal those brackets.
Ps:I find that it is difficult to consider the situation when
expression has negative item....
TODO:consider the negative item!
(2)Consider those brackets
Brackets will change the step of calculate,so I have to cope them
before constructing expression tree.
I set an empty stack,when I meet "(" in expression,I will push this
bracket and elems behind it into stack.When i meet ")" in expression,I
will pop stack's elems until the next "(" is out from stack.
By this way,I will get an correct order list of calculation whitout
bracket.
defclear_brackets(exp): temp = Stack() result = Stack() for elem in exp: if elem notin operators: result.push(elem) else: if temp.length() is0or elem is'(': temp.push(elem) else: if elem is')': while temp.getTop() isnot'(': result.push(temp.pop()) temp.pop() elif operators[elem] < operators[temp.getTop()]: while temp.length() isnot0: if temp.getTop() is'(': break result.push(temp.pop()) temp.push(elem) else: temp.push(elem) while temp.length() isnot0: result.push(temp.pop()) return result
Ps:Must import stack class before:
1 2
from data import Stack from data import BiTree
test it!
input:
1 2 3
a = clear_brackets(Exp) while a.length() isnot0: print a.pop()
+ - * ^ 1.4121 + ^ y 51 12.13 x b A =========== + - * ^ 1.4121 + 51^y 12.13 x b A ========== + - * ^ 1.4121 12.13+51^y x b A ========== + - * (12.13+51^y)^1.4121 x b A ========== + - x*(12.13+51^y)^1.4121 b A ========== + b-x*(12.13+51^y)^1.4121 A ========== A+b-x*(12.13+51^y)^1.4121
Perfect!I got the original expression,it says this function is work
normally.
summary
Now,the original expression is converted to a ordered stack.I will
convert this stack to expression tree later.
You are playing the following Nim Game with your friend: There is a
heap of stones on the table, each time one of you take turns to remove 1
to 3 stones. The one who removes the last stone will be the winner. You
will take the first turn to remove the stones.
Both of you are very clever and have optimal strategies for the game.
Write a function to determine whether you can win the game given the
number of stones in the heap.
For example, if there are 4 stones in the heap, then you will never
win the game: no matter 1, 2, or 3 stones you remove, the last stone
will always be removed by your friend.
idea of Nim game
There are some situations in this game.
Let's think about what will happend while stones is few:
If there are 4 stones in the heap,you will lose the game in every
matters.
If there are less than 3 stones,you will win the game in one
step.
So,when there are more than 4 stones,you should remove some stones
and let your friend getting 4 stones.In this situation,we can simplify
this problem to that how to leaving 4 stones.And you will find it is the
same as initially problem.
Now, what you should do is leaving multiple of 4 stones in every
matters.So,if the number of stones is not the muiltiple of 4,you will
win this game.
Finally,we can use a formula to express this problem: