|
2 | 2 | "cells": [
|
3 | 3 | {
|
4 | 4 | "cell_type": "code",
|
5 |
| - "execution_count": 50, |
| 5 | + "execution_count": 6, |
6 | 6 | "metadata": {
|
7 | 7 | "collapsed": true
|
8 | 8 | },
|
|
11 | 11 | "name": "stdout",
|
12 | 12 | "output_type": "stream",
|
13 | 13 | "text": [
|
14 |
| - "(1682L, 943L) (1682L, 943L)" |
15 |
| - ] |
16 |
| - }, |
17 |
| - { |
18 |
| - "name": "stdout", |
19 |
| - "output_type": "stream", |
20 |
| - "text": [ |
21 |
| - "\n[[5 4 0 0 4]\n [3 0 0 0 3]\n [4 0 0 0 0]\n [3 0 0 0 0]\n [3 0 0 0 0]]\nAverage rating for movie 1 (Toy Story): 4.520679 / 5\n" |
| 14 | + "(1682L, 943L) (1682L, 943L)\n[[5 4 0 0 4]\n [3 0 0 0 3]\n [4 0 0 0 0]\n [3 0 0 0 0]\n [3 0 0 0 0]]\nAverage rating for movie 1 (Toy Story): 3.878319 / 5\n" |
22 | 15 | ]
|
23 | 16 | }
|
24 | 17 | ],
|
|
37 | 30 | "print Y.shape,R.shape\n",
|
38 | 31 | "print Y[0:5,0:5]\n",
|
39 | 32 | "print 'Average rating for movie 1 (Toy Story): %f / 5'\\\n",
|
40 |
| - " %(np.mean(Y[0,R[0,:]]))\n", |
| 33 | + " %(np.mean(Y[0,R[0,:]==1]))\n", |
41 | 34 | "\n",
|
42 | 35 | "\n",
|
43 | 36 | "\n"
|
44 | 37 | ]
|
45 | 38 | },
|
46 | 39 | {
|
47 | 40 | "cell_type": "code",
|
48 |
| - "execution_count": 51, |
| 41 | + "execution_count": 7, |
49 | 42 | "metadata": {},
|
50 | 43 | "outputs": [
|
51 | 44 | {
|
|
71 | 64 | },
|
72 | 65 | {
|
73 | 66 | "cell_type": "code",
|
74 |
| - "execution_count": 52, |
| 67 | + "execution_count": 8, |
75 | 68 | "metadata": {},
|
76 | 69 | "outputs": [
|
77 | 70 | {
|
|
116 | 109 | },
|
117 | 110 | {
|
118 | 111 | "cell_type": "code",
|
119 |
| - "execution_count": 53, |
| 112 | + "execution_count": 9, |
120 | 113 | "metadata": {},
|
121 | 114 | "outputs": [],
|
122 | 115 | "source": [
|
|
157 | 150 | },
|
158 | 151 | {
|
159 | 152 | "cell_type": "code",
|
160 |
| - "execution_count": 44, |
| 153 | + "execution_count": 10, |
161 | 154 | "metadata": {},
|
162 | 155 | "outputs": [
|
163 | 156 | {
|
164 | 157 | "name": "stdout",
|
165 | 158 | "output_type": "stream",
|
166 | 159 | "text": [
|
167 |
| - "[[ 0.14401176 -6.22590369 10.0516782 0.14401176 -6.22590369\n 10.0516782 ]\n [ 4.45625126 2.03223863 7.35329766 4.45625126 2.03223863\n 7.35329766]\n [ 0.11515446 -9.74467008 -1.91337529 0.11515446 -9.74467008\n -1.91337529]\n [ 0.90141124 -1.28554704 1.55376368 0.90141124 -1.28554704\n 1.55376368]]\n[[ -0.12785057 -0.73651273 -1.21583757 -0.12785057 -0.73651273\n -1.21583757]\n [ -2.72597491 4.66380798 0.23347243 -2.72597491 4.66380798\n 0.23347243]\n [ -0.42229439 -0.40927681 -11.5753487 -0.42229439 -0.40927681\n -11.5753487 ]\n [ 3.61809669 12.26402186 7.18521682 3.61809669 12.26402186\n 7.18521682]\n [ 1.15495755 -1.53477221 -2.08059821 1.15495755 -1.53477221\n -2.08059821]]\nx max relative error: 4.429443e-10\ntheta max relative error: 4.670187e-10\n" |
| 160 | + "[[ 3.2694473 0.82260885 0.34742436 3.2694473 0.82260885 0.34742436]\n [ 0.60548622 -5.29818853 -8.71667995 0.60548622 -5.29818853 -8.71667995]\n [ 0.98872539 -5.60773103 -7.1520766 0.98872539 -5.60773103 -7.1520766 ]\n [ 1.27425229 5.80646836 -0.94849499 1.27425229 5.80646836 -0.94849499]]\n[[ -3.21431313 -0.73938093 -0.17662467 -3.21431313 -0.73938093\n -0.17662467]\n [ 1.62875238 -7.84411116 -10.54935153 1.62875238 -7.84411116\n -10.54935153]\n [ 1.04497469 0.56498113 1.51627789 1.04497469 0.56498113\n 1.51627789]\n [ 0.57523149 -0.36704409 0.23436928 0.57523149 -0.36704409\n 0.23436928]\n [ -1.08518528 -3.44655202 -0.38480487 -1.08518528 -3.44655202\n -0.38480487]]\nx max relative error: 5.289068e-10\ntheta max relative error: 2.211348e-10\n" |
168 | 161 | ]
|
169 | 162 | }
|
170 | 163 | ],
|
|
205 | 198 | },
|
206 | 199 | {
|
207 | 200 | "cell_type": "code",
|
208 |
| - "execution_count": 54, |
| 201 | + "execution_count": 11, |
209 | 202 | "metadata": {},
|
210 | 203 | "outputs": [
|
211 | 204 | {
|
|
237 | 230 | },
|
238 | 231 | {
|
239 | 232 | "cell_type": "code",
|
240 |
| - "execution_count": 55, |
| 233 | + "execution_count": 12, |
241 | 234 | "metadata": {},
|
242 | 235 | "outputs": [
|
243 | 236 | {
|
|
273 | 266 | },
|
274 | 267 | {
|
275 | 268 | "cell_type": "code",
|
276 |
| - "execution_count": 60, |
| 269 | + "execution_count": 13, |
277 | 270 | "metadata": {},
|
278 | 271 | "outputs": [
|
279 | 272 | {
|
|
325 | 318 | },
|
326 | 319 | {
|
327 | 320 | "cell_type": "code",
|
328 |
| - "execution_count": 69, |
| 321 | + "execution_count": 15, |
329 | 322 | "metadata": {},
|
330 | 323 | "outputs": [
|
331 | 324 | {
|
332 | 325 | "name": "stdout",
|
333 | 326 | "output_type": "stream",
|
334 | 327 | "text": [
|
335 |
| - "Warning: Maximum number of iterations has been exceeded.\n Current function value: 27769.265138\n Iterations: 100\n Function evaluations: 152\n Gradient evaluations: 152\n(1682L, 10L) (944L, 10L)\n(1682L, 944L)\n[ 3.89489124 3.56569854 3.25036093 ..., 1.99989186 2.99999858\n 3.00004241]\nfavorite 1 score is 5,for:Return of the Jedi (1983) \nfavorite 2 score is 5,for:Star Wars (1977) \nfavorite 3 score is 5,for:Prefontaine (1997) \n" |
| 328 | + "Warning: Maximum number of iterations has been exceeded.\n Current function value: 27848.964019\n Iterations: 100\n Function evaluations: 149\n Gradient evaluations: 149\n(1682L, 10L) (944L, 10L)\n(1682L, 944L)\n[ 4.13867803 3.96850127 4.31376906 3.68773897 3.73023817]\nfavorite 1 score is 5,for:Princess Bride, The (1987) \nfavorite 2 score is 5,for:Star Wars (1977) \nfavorite 3 score is 5,for:Affair to Remember, An (1957) \nfavorite 4 score is 5,for:Entertaining Angels: The Dorothy Day Story (1996) \nfavorite 5 score is 5,for:Great Day in Harlem, A (1994) \nfavorite 6 score is 5,for:Prefontaine (1997) \nfavorite 7 score is 5,for:They Made Me a Criminal (1939) \nfavorite 8 score is 5,for:Santa with Muscles (1996) \nfavorite 9 score is 5,for:Aiqing wansui (1994) \nfavorite 10 score is 5,for:Saint of Fort Washington, The (1993) \n" |
336 | 329 | ]
|
337 | 330 | }
|
338 | 331 | ],
|
|
387 | 380 | "print score.shape\n",
|
388 | 381 | "print my_score[:5]\n",
|
389 | 382 | "sort_index = my_score.argsort()\n",
|
390 |
| - "favorite = 3\n", |
| 383 | + "favorite = 10\n", |
391 | 384 | "for i in xrange(favorite):\n",
|
392 | 385 | " print \"favorite %d score is %d,for:%s\" \\\n",
|
393 | 386 | " %(i+1,my_score[sort_index[-(i+1)]],movieList[sort_index[-(i+1)]])"
|
394 | 387 | ]
|
395 | 388 | },
|
396 |
| - { |
397 |
| - "cell_type": "code", |
398 |
| - "execution_count": 49, |
399 |
| - "metadata": {}, |
400 |
| - "outputs": [ |
401 |
| - { |
402 |
| - "name": "stdout", |
403 |
| - "output_type": "stream", |
404 |
| - "text": [ |
405 |
| - "Optimization terminated successfully.\n Current function value: 1.617021\n Iterations: 4\n Function evaluations: 8\n Gradient evaluations: 8\n[-1.80851064 -0.25531915]\n" |
406 |
| - ] |
407 |
| - } |
408 |
| - ], |
409 |
| - "source": [ |
410 |
| - "#an example of optimize\n", |
411 |
| - "args = (2, 3, 7, 8, 9, 10) # parameter values\n", |
412 |
| - "def f(x, *args):\n", |
413 |
| - " \n", |
414 |
| - " u, v = x\n", |
415 |
| - " a, b, c, d, e, f = args\n", |
416 |
| - " return a*u**2 + b*u*v + c*v**2 + d*u + e*v + f\n", |
417 |
| - "def gradf(x, *args):\n", |
418 |
| - " u, v = x\n", |
419 |
| - " a, b, c, d, e, f = args\n", |
420 |
| - " gu = 2*a*u + b*v + d # u-component of the gradient\n", |
421 |
| - " gv = b*u + 2*c*v + e # v-component of the gradient\n", |
422 |
| - " return np.asarray((gu, gv))\n", |
423 |
| - "x0 = np.asarray((0, 0)) # Initial guess.\n", |
424 |
| - "from scipy import optimize\n", |
425 |
| - "res1 = optimize.fmin_cg(f, x0, fprime=gradf, args=args)\n", |
426 |
| - "print res1" |
427 |
| - ] |
428 |
| - }, |
429 |
| - { |
430 |
| - "cell_type": "code", |
431 |
| - "execution_count": 70, |
432 |
| - "metadata": {}, |
433 |
| - "outputs": [ |
434 |
| - { |
435 |
| - "name": "stdout", |
436 |
| - "output_type": "stream", |
437 |
| - "text": [ |
438 |
| - "[0 3 2 4 1 5]\nfavorite 1 score is 6,for f\nfavorite 2 score is 5,for b\nfavorite 3 score is 4,for e\n" |
439 |
| - ] |
440 |
| - } |
441 |
| - ], |
442 |
| - "source": [ |
443 |
| - "y = np.array([1,5,3,2,4,6])\n", |
444 |
| - "yy = ['a','b','c','d','e','f']\n", |
445 |
| - "index = np.argsort(y)\n", |
446 |
| - "print index\n", |
447 |
| - "for i in xrange(3):\n", |
448 |
| - " print \"favorite %d score is %d,for %s\" \\\n", |
449 |
| - " %(i+1,y[index[-(i+1)]],yy[index[-(i+1)]])\n" |
450 |
| - ] |
451 |
| - }, |
452 | 389 | {
|
453 | 390 | "cell_type": "code",
|
454 | 391 | "execution_count": null,
|
455 | 392 | "metadata": {},
|
456 | 393 | "outputs": [],
|
457 |
| - "source": [] |
| 394 | + "source": [ |
| 395 | + "\n", |
| 396 | + "\n" |
| 397 | + ] |
458 | 398 | }
|
459 | 399 | ],
|
460 | 400 | "metadata": {
|
|
0 commit comments