Skip to content

Instantly share code, notes, and snippets.

@EvanZ
Created February 21, 2015 02:09
Show Gist options
  • Save EvanZ/d16c11d6f85138b2fe6f to your computer and use it in GitHub Desktop.
Save EvanZ/d16c11d6f85138b2fe6f to your computer and use it in GitHub Desktop.
rapm tutorial
{
"metadata": {
"name": "",
"signature": "sha256:d9ade76355975088b3690cbbe0e77efd9ab2b9a11213aa8621b901be21346921"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"units = [['Stephen Curry','Klay Thompson','Harrison Barnes','Draymond Green','Andrew Bogut'],\n",
" ['Stephen Curry','Klay Thompson','Harrison Barnes','Draymond Green','Andrew Bogut'],\n",
" ['Shaun Livingston','Klay Thompson','Harrison Barnes','Draymond Green','Andrew Bogut'],\n",
" ['Shaun Livingston','Klay Thompson','Andre Iguodala','Draymond Green','Andrew Bogut'],\n",
" ['Leandro Barbosa','Shaun Livingston','Andre Iguodala','Harrison Barnes','Draymond Green']]\n",
"\n",
"from sklearn.feature_extraction import DictVectorizer\n",
"v = DictVectorizer(sparse=False)\n",
"list_dicts = []\n",
"for unit in units:\n",
" list_dicts.append({name: 1 for name in unit})\n",
"print(list_dicts)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[{'Andrew Bogut': 1, 'Klay Thompson': 1, 'Draymond Green': 1, 'Harrison Barnes': 1, 'Stephen Curry': 1}, {'Andrew Bogut': 1, 'Klay Thompson': 1, 'Draymond Green': 1, 'Harrison Barnes': 1, 'Stephen Curry': 1}, {'Klay Thompson': 1, 'Andrew Bogut': 1, 'Shaun Livingston': 1, 'Draymond Green': 1, 'Harrison Barnes': 1}, {'Klay Thompson': 1, 'Andrew Bogut': 1, 'Shaun Livingston': 1, 'Draymond Green': 1, 'Andre Iguodala': 1}, {'Shaun Livingston': 1, 'Leandro Barbosa': 1, 'Harrison Barnes': 1, 'Andre Iguodala': 1, 'Draymond Green': 1}]\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"X = v.fit_transform(list_dicts)\n",
"print(X)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[[ 0. 1. 1. 1. 1. 0. 0. 1.]\n",
" [ 0. 1. 1. 1. 1. 0. 0. 1.]\n",
" [ 0. 1. 1. 1. 1. 0. 1. 0.]\n",
" [ 1. 1. 1. 0. 1. 0. 1. 0.]\n",
" [ 1. 0. 1. 1. 0. 1. 1. 0.]]\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = v.inverse_transform(X)\n",
"print(x)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[{'Klay Thompson': 1.0, 'Andrew Bogut': 1.0, 'Draymond Green': 1.0, 'Harrison Barnes': 1.0, 'Stephen Curry': 1.0}, {'Klay Thompson': 1.0, 'Andrew Bogut': 1.0, 'Draymond Green': 1.0, 'Harrison Barnes': 1.0, 'Stephen Curry': 1.0}, {'Klay Thompson': 1.0, 'Shaun Livingston': 1.0, 'Andrew Bogut': 1.0, 'Draymond Green': 1.0, 'Harrison Barnes': 1.0}, {'Klay Thompson': 1.0, 'Shaun Livingston': 1.0, 'Andrew Bogut': 1.0, 'Draymond Green': 1.0, 'Andre Iguodala': 1.0}, {'Shaun Livingston': 1.0, 'Harrison Barnes': 1.0, 'Leandro Barbosa': 1.0, 'Draymond Green': 1.0, 'Andre Iguodala': 1.0}]\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(v.get_feature_names())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"['Andre Iguodala', 'Andrew Bogut', 'Draymond Green', 'Harrison Barnes', 'Klay Thompson', 'Leandro Barbosa', 'Shaun Livingston', 'Stephen Curry']\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import json\n",
"data = []\n",
"with open('/Users/evanzamir/PycharmProjects/sklearn_tutorial/matchups2014.json') as units_file:\n",
" for j in units_file:\n",
" data.append(json.loads(j))\n",
" \n",
"pprint(data[0])\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"{u'Bobcats': {u'entered': [u'Ben Gordon'],\n",
" u'exited': [u'Michael Kidd-Gilchrist'],\n",
" u'on': [u'Kemba Walker',\n",
" u'Ben Gordon',\n",
" u'Gerald Henderson',\n",
" u'Josh McRoberts',\n",
" u'Al Jefferson'],\n",
" u'stats': {u'dreb': 0,\n",
" u'drebx': 0,\n",
" u'fg2m': 1,\n",
" u'fg3m': 0,\n",
" u'fgm': 1,\n",
" u'fgx': 0,\n",
" u'foul': 1,\n",
" u'fta': 2,\n",
" u'ftm': 2,\n",
" u'non_steal_tov': 0,\n",
" u'oreb': 0,\n",
" u'orebx': 0,\n",
" u'poss': 1,\n",
" u'pts': 4,\n",
" u'team_tov': 0,\n",
" u'time': 0,\n",
" u'tov': 0}},\n",
" u'Heat': {u'entered': [],\n",
" u'exited': [],\n",
" u'on': [u'Norris Cole',\n",
" u'Mario Chalmers',\n",
" u'Dwyane Wade',\n",
" u'LeBron James',\n",
" u'Chris Bosh'],\n",
" u'stats': {u'dreb': 0,\n",
" u'drebx': 0,\n",
" u'fg2m': 0,\n",
" u'fg3m': 0,\n",
" u'fgm': 0,\n",
" u'fgx': 0,\n",
" u'foul': 1,\n",
" u'fta': 2,\n",
" u'ftm': 2,\n",
" u'non_steal_tov': 0,\n",
" u'oreb': 0,\n",
" u'orebx': 0,\n",
" u'poss': 1,\n",
" u'pts': 2,\n",
" u'team_tov': 0,\n",
" u'time': 0,\n",
" u'tov': 0}},\n",
" u'_id': {u'$oid': u'5353245a5bca6d5652001dd1'},\n",
" u'as': 97,\n",
" u'away': u'Bobcats',\n",
" u'date': u'2013-12-01',\n",
" u'end': u'0:01',\n",
" u'espn_id': u'400489125',\n",
" u'home': u'Heat',\n",
" u'hs': 99,\n",
" u'mid': 5,\n",
" u'plays': [{u'id': {u'$oid': u'5353189f5bca6d54dd01c27b'}},\n",
" {u'id': {u'$oid': u'5353189f5bca6d54dd01c27c'}},\n",
" {u'id': {u'$oid': u'5353189f5bca6d54dd01c27d'}},\n",
" {u'id': {u'$oid': u'5353189f5bca6d54dd01c27e'}},\n",
" {u'id': {u'$oid': u'5353189f5bca6d54dd01c27f'}},\n",
" {u'id': {u'$oid': u'5353189f5bca6d54dd01c280'}},\n",
" {u'id': {u'$oid': u'5353189f5bca6d54dd01c281'}},\n",
" {u'id': {u'$oid': u'5353189f5bca6d54dd01c282'}},\n",
" {u'id': {u'$oid': u'5353189f5bca6d54dd01c283'}}],\n",
" u'q': 4,\n",
" u'season': u'2014',\n",
" u'start': u'0:12',\n",
" u'time': 11,\n",
" u'url': u'http://scores.nbcsports.msnbc.com/nba/pbp.asp?gamecode=2013120114'}\n"
]
}
],
"prompt_number": 37
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from pprint import pprint\n",
"units = []\n",
"points = []\n",
"weights = []\n",
"for d in data:\n",
" home = d['home']\n",
" away = d['away']\n",
" home_unit = {name:1 for name in d[home]['on']}\n",
" away_unit = {name:-1 for name in d[away]['on']}\n",
" stint = home_unit.copy()\n",
" stint.update(away_unit)\n",
" home_poss = d[home]['stats']['poss']\n",
" away_poss = d[away]['stats']['poss']\n",
" point_diff = 100*(d[home]['stats']['pts']-d[away]['stats']['pts'])/((home_poss+away_poss+1)/2.)\n",
" units.append(stint)\n",
" points.append(point_diff)\n",
" weights.append((home_poss+away_poss+1)/2.)\n",
"#pprint(d)\n",
"print(len(units),len(points),len(weights))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"(39265, 39265, 39265)\n"
]
}
],
"prompt_number": 92
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"u = DictVectorizer(sparse=False)\n",
"u_mat = u.fit_transform(units)\n",
"print(u_mat)\n",
"print(points[:25])\n",
"print(weights[:100])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[[ 0. 0. 0. ..., 0. 0. 0.]\n",
" [ 0. 0. 0. ..., 0. 0. 0.]\n",
" [ 0. 0. 0. ..., 0. 0. 0.]\n",
" ..., \n",
" [ 0. 0. 0. ..., 0. 0. 0.]\n",
" [ 0. 0. 0. ..., 0. 0. 0.]\n",
" [ 0. 0. 0. ..., 0. 0. 0.]]\n",
"[-133.33333333333334, -40.0, -85.71428571428571, -66.66666666666667, 0.0, 40.0, 0.0, 40.0, 100.0, -71.42857142857143, 0.0, 60.0, 200.0, -200.0, 66.66666666666667, 57.142857142857146, -50.0, -23.529411764705884, 66.66666666666667, 0.0, -15.384615384615385, 200.0, 0.0, -20.0, -100.0]\n",
"[1.5, 7.5, 7.0, 1.5, 1.0, 5.0, 0.5, 5.0, 1.0, 7.0, 1.0, 5.0, 3.0, 0.5, 6.0, 3.5, 2.0, 8.5, 1.5, 1.0, 6.5, 0.5, 1.0, 5.0, 2.0, 4.5, 2.0, 1.5, 4.5, 1.5, 2.5, 3.0, 1.5, 2.0, 6.0, 5.0, 1.5, 0.5, 5.0, 8.5, 4.5, 1.0, 2.5, 0.5, 1.5, 2.0, 1.0, 3.5, 1.0, 6.5, 5.5, 3.0, 3.0, 1.0, 6.5, 9.5, 1.5, 0.5, 4.5, 4.5, 7.0, 5.5, 1.0, 3.5, 6.0, 2.0, 2.5, 8.5, 2.0, 2.5, 2.0, 0.5, 1.5, 1.5, 2.5, 1.5, 4.0, 4.5, 1.5, 1.0, 6.5, 1.0, 3.0, 2.5, 2.5, 2.0, 4.5, 1.0, 3.5, 1.0, 12.5, 6.5, 3.0, 0.5, 2.5, 2.0, 8.5, 3.0, 11.5, 12.0]\n"
]
}
],
"prompt_number": 93
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"pprint(u.get_feature_names()[:25])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[u'A.J. Price',\n",
" u'Aaron Brooks',\n",
" u'Aaron Gray',\n",
" u'Adonis Thomas',\n",
" u'Al Harrington',\n",
" u'Al Horford',\n",
" u'Al Jefferson',\n",
" u'Al-Farouq Aminu',\n",
" u'Alan Anderson',\n",
" u'Alec Burks',\n",
" u'Alex Len',\n",
" u'Alexey Shved',\n",
" u'Alexis Ajinca',\n",
" u'Allen Crabbe',\n",
" u'Alonzo Gee',\n",
" u\"Amar'e Stoudemire\",\n",
" u'Amir Johnson',\n",
" u'Anderson Varejao',\n",
" u'Andray Blatche',\n",
" u'Andre Drummond',\n",
" u'Andre Iguodala',\n",
" u'Andre Miller',\n",
" u'Andre Roberson',\n",
" u'Andrea Bargnani',\n",
" u'Andrei Kirilenko']\n"
]
}
],
"prompt_number": 40
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"pprint(u.inverse_transform(u_mat)[:25])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[{u'Al Jefferson': -1.0,\n",
" u'Ben Gordon': -1.0,\n",
" u'Chris Bosh': 1.0,\n",
" u'Dwyane Wade': 1.0,\n",
" u'Gerald Henderson': -1.0,\n",
" u'Josh McRoberts': -1.0,\n",
" u'Kemba Walker': -1.0,\n",
" u'LeBron James': 1.0,\n",
" u'Mario Chalmers': 1.0,\n",
" u'Norris Cole': 1.0},\n",
" {u'Corey Brewer': -1.0,\n",
" u'Kendrick Perkins': 1.0,\n",
" u'Kevin Durant': 1.0,\n",
" u'Kevin Love': -1.0,\n",
" u'Kevin Martin': -1.0,\n",
" u'Nikola Pekovic': -1.0,\n",
" u'Ricky Rubio': -1.0,\n",
" u'Russell Westbrook': 1.0,\n",
" u'Serge Ibaka': 1.0,\n",
" u'Thabo Sefolosha': 1.0},\n",
" {u'Corey Brewer': -1.0,\n",
" u'Kevin Durant': 1.0,\n",
" u'Kevin Love': -1.0,\n",
" u'Kevin Martin': -1.0,\n",
" u'Nikola Pekovic': -1.0,\n",
" u'Ricky Rubio': -1.0,\n",
" u'Russell Westbrook': 1.0,\n",
" u'Serge Ibaka': 1.0,\n",
" u'Steven Adams': 1.0,\n",
" u'Thabo Sefolosha': 1.0},\n",
" {u'Corey Brewer': -1.0,\n",
" u'J.J. Barea': -1.0,\n",
" u'Jeremy Lamb': 1.0,\n",
" u'Kevin Durant': 1.0,\n",
" u'Kevin Love': -1.0,\n",
" u'Kevin Martin': -1.0,\n",
" u'Nikola Pekovic': -1.0,\n",
" u'Russell Westbrook': 1.0,\n",
" u'Serge Ibaka': 1.0,\n",
" u'Steven Adams': 1.0},\n",
" {u'Andrew Nicholson': -1.0,\n",
" u'Arron Afflalo': -1.0,\n",
" u'Evan Turner': 1.0,\n",
" u'Glen Davis': -1.0,\n",
" u'Hollis Thompson': 1.0,\n",
" u'Michael Carter-Williams': 1.0,\n",
" u'Ronnie Price': -1.0,\n",
" u'Spencer Hawes': 1.0,\n",
" u'Thaddeus Young': 1.0,\n",
" u'Victor Oladipo': -1.0},\n",
" {u'Corey Brewer': -1.0,\n",
" u'J.J. Barea': -1.0,\n",
" u'Kevin Durant': 1.0,\n",
" u'Kevin Love': -1.0,\n",
" u'Kevin Martin': -1.0,\n",
" u'Nikola Pekovic': -1.0,\n",
" u'Russell Westbrook': 1.0,\n",
" u'Serge Ibaka': 1.0,\n",
" u'Steven Adams': 1.0,\n",
" u'Thabo Sefolosha': 1.0},\n",
" {u'Al-Farouq Aminu': -1.0,\n",
" u'Eric Gordon': -1.0,\n",
" u'Jason Smith': -1.0,\n",
" u'Joakim Noah': 1.0,\n",
" u'Jrue Holiday': -1.0,\n",
" u'Luol Deng': 1.0,\n",
" u'Mike Dunleavy': 1.0,\n",
" u'Ryan Anderson': -1.0,\n",
" u'Taj Gibson': 1.0},\n",
" {u'Elliot Williams': -1.0,\n",
" u'James Anderson': -1.0,\n",
" u'Jonas Jerebko': 1.0,\n",
" u'Josh Harrellson': 1.0,\n",
" u'Kyle Singler': 1.0,\n",
" u'Lavoy Allen': -1.0,\n",
" u'Peyton Siva': 1.0,\n",
" u'Rodney Stuckey': 1.0,\n",
" u'Thaddeus Young': -1.0,\n",
" u'Tony Wroten': -1.0},\n",
" {u'Elliot Williams': -1.0,\n",
" u'James Anderson': -1.0,\n",
" u'Jonas Jerebko': 1.0,\n",
" u'Josh Harrellson': 1.0,\n",
" u'Kyle Singler': 1.0,\n",
" u'Peyton Siva': 1.0,\n",
" u'Rodney Stuckey': 1.0,\n",
" u'Spencer Hawes': -1.0,\n",
" u'Thaddeus Young': -1.0,\n",
" u'Tony Wroten': -1.0},\n",
" {u'Brandon Davies': -1.0,\n",
" u'Brandon Jennings': 1.0,\n",
" u'Evan Turner': -1.0,\n",
" u'Greg Monroe': 1.0,\n",
" u'James Anderson': -1.0,\n",
" u'Jonas Jerebko': 1.0,\n",
" u'Kentavious Caldwell-Pope': 1.0,\n",
" u'Michael Carter-Williams': -1.0,\n",
" u'Rodney Stuckey': 1.0,\n",
" u'Spencer Hawes': -1.0},\n",
" {u'Brandon Jennings': 1.0,\n",
" u'Evan Turner': -1.0,\n",
" u'James Anderson': -1.0,\n",
" u'Josh Smith': 1.0,\n",
" u'Kentavious Caldwell-Pope': 1.0,\n",
" u'Kyle Singler': 1.0,\n",
" u'Michael Carter-Williams': -1.0,\n",
" u'Rodney Stuckey': 1.0,\n",
" u'Spencer Hawes': -1.0,\n",
" u'Thaddeus Young': -1.0},\n",
" {u'Andrew Bogut': -1.0,\n",
" u'Ben McLemore': 1.0,\n",
" u'David Lee': -1.0,\n",
" u'DeMarcus Cousins': 1.0,\n",
" u'Derrick Williams': 1.0,\n",
" u'Harrison Barnes': -1.0,\n",
" u'Isaiah Thomas': 1.0,\n",
" u'Jason Thompson': 1.0,\n",
" u'Klay Thompson': -1.0,\n",
" u'Stephen Curry': -1.0},\n",
" {u'Andre Drummond': 1.0,\n",
" u'Brandon Davies': -1.0,\n",
" u'Brandon Jennings': 1.0,\n",
" u'Evan Turner': -1.0,\n",
" u'Greg Monroe': 1.0,\n",
" u'Hollis Thompson': -1.0,\n",
" u'Josh Smith': 1.0,\n",
" u'Michael Carter-Williams': -1.0,\n",
" u'Rodney Stuckey': 1.0,\n",
" u'Spencer Hawes': -1.0},\n",
" {u'Al Jefferson': -1.0,\n",
" u'Ben Gordon': -1.0,\n",
" u'Chris Andersen': 1.0,\n",
" u'Cody Zeller': -1.0,\n",
" u'Dwyane Wade': 1.0,\n",
" u'Jeffery Taylor': -1.0,\n",
" u'Norris Cole': 1.0,\n",
" u'Ramon Sessions': -1.0,\n",
" u'Rashard Lewis': 1.0,\n",
" u'Ray Allen': 1.0},\n",
" {u'Andre Drummond': 1.0,\n",
" u'Brandon Jennings': 1.0,\n",
" u'Daniel Orton': -1.0,\n",
" u'Greg Monroe': 1.0,\n",
" u'Hollis Thompson': -1.0,\n",
" u'Josh Smith': 1.0,\n",
" u'Michael Carter-Williams': -1.0,\n",
" u'Rodney Stuckey': 1.0,\n",
" u'Spencer Hawes': -1.0,\n",
" u'Tony Wroten': -1.0},\n",
" {u'Andre Drummond': 1.0,\n",
" u'Brandon Jennings': 1.0,\n",
" u'Daniel Orton': -1.0,\n",
" u'Greg Monroe': 1.0,\n",
" u'Hollis Thompson': -1.0,\n",
" u'Josh Smith': 1.0,\n",
" u'Lavoy Allen': -1.0,\n",
" u'Michael Carter-Williams': -1.0,\n",
" u'Rodney Stuckey': 1.0,\n",
" u'Tony Wroten': -1.0},\n",
" {u'Andre Drummond': 1.0,\n",
" u'Brandon Jennings': 1.0,\n",
" u'Daniel Orton': -1.0,\n",
" u'Hollis Thompson': -1.0,\n",
" u'Jonas Jerebko': 1.0,\n",
" u'Kyle Singler': 1.0,\n",
" u'Lavoy Allen': -1.0,\n",
" u'Michael Carter-Williams': -1.0,\n",
" u'Rodney Stuckey': 1.0,\n",
" u'Tony Wroten': -1.0},\n",
" {u'Andre Drummond': 1.0,\n",
" u'Brandon Jennings': 1.0,\n",
" u'Evan Turner': -1.0,\n",
" u'Greg Monroe': 1.0,\n",
" u'Hollis Thompson': -1.0,\n",
" u'Josh Smith': 1.0,\n",
" u'Kentavious Caldwell-Pope': 1.0,\n",
" u'Michael Carter-Williams': -1.0,\n",
" u'Spencer Hawes': -1.0,\n",
" u'Thaddeus Young': -1.0},\n",
" {u'Andre Drummond': 1.0,\n",
" u'Brandon Davies': -1.0,\n",
" u'Brandon Jennings': 1.0,\n",
" u'Hollis Thompson': -1.0,\n",
" u'Jonas Jerebko': 1.0,\n",
" u'Kyle Singler': 1.0,\n",
" u'Lavoy Allen': -1.0,\n",
" u'Michael Carter-Williams': -1.0,\n",
" u'Rodney Stuckey': 1.0,\n",
" u'Tony Wroten': -1.0},\n",
" {u\"Amar'e Stoudemire\": -1.0,\n",
" u'Andrea Bargnani': -1.0,\n",
" u'Danny Green': 1.0,\n",
" u'J.R. Smith': -1.0,\n",
" u'Kawhi Leonard': 1.0,\n",
" u'Matt Bonner': 1.0,\n",
" u'Patrick Mills': 1.0,\n",
" u'Tiago Splitter': 1.0,\n",
" u'Tim Hardaway Jr.': -1.0,\n",
" u\"Toure' Murry\": -1.0},\n",
" {u'Andre Drummond': 1.0,\n",
" u'Brandon Jennings': 1.0,\n",
" u'Evan Turner': -1.0,\n",
" u'Greg Monroe': 1.0,\n",
" u'Hollis Thompson': -1.0,\n",
" u'Jonas Jerebko': 1.0,\n",
" u'Josh Smith': 1.0,\n",
" u'Kentavious Caldwell-Pope': 1.0,\n",
" u'Michael Carter-Williams': -1.0,\n",
" u'Spencer Hawes': -1.0,\n",
" u'Thaddeus Young': -1.0},\n",
" {u'Andre Drummond': 1.0,\n",
" u'Brandon Jennings': 1.0,\n",
" u'Evan Turner': -1.0,\n",
" u'Greg Monroe': 1.0,\n",
" u'James Anderson': -1.0,\n",
" u'Jonas Jerebko': 1.0,\n",
" u'Josh Smith': 1.0,\n",
" u'Michael Carter-Williams': -1.0,\n",
" u'Rodney Stuckey': 1.0,\n",
" u'Spencer Hawes': -1.0,\n",
" u'Thaddeus Young': -1.0},\n",
" {u'Chris Bosh': -1.0,\n",
" u'J.J. Hickson': 1.0,\n",
" u'Mario Chalmers': -1.0,\n",
" u'Michael Beasley': -1.0,\n",
" u'Randy Foye': 1.0,\n",
" u'Ray Allen': -1.0,\n",
" u'Roger Mason Jr.': -1.0,\n",
" u'Timofey Mozgov': 1.0,\n",
" u'Ty Lawson': 1.0,\n",
" u'Wilson Chandler': 1.0},\n",
" {u'Andre Drummond': 1.0,\n",
" u'Brandon Jennings': 1.0,\n",
" u'Daniel Orton': -1.0,\n",
" u'Evan Turner': -1.0,\n",
" u'Greg Monroe': 1.0,\n",
" u'James Anderson': -1.0,\n",
" u'Jonas Jerebko': 1.0,\n",
" u'Kyle Singler': 1.0,\n",
" u'Michael Carter-Williams': -1.0,\n",
" u'Rodney Stuckey': 1.0,\n",
" u'Thaddeus Young': -1.0},\n",
" {u'Brandon Jennings': 1.0,\n",
" u'Daniel Orton': -1.0,\n",
" u'Evan Turner': -1.0,\n",
" u'Greg Monroe': 1.0,\n",
" u'James Anderson': -1.0,\n",
" u'Jonas Jerebko': 1.0,\n",
" u'Kyle Singler': 1.0,\n",
" u'Michael Carter-Williams': -1.0,\n",
" u'Rodney Stuckey': 1.0,\n",
" u'Thaddeus Young': -1.0}]\n"
]
}
],
"prompt_number": 41
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from sklearn import linear_model\n",
"clf = linear_model.RidgeCV(alphas=(100,200,500,1000,2000),cv=10)\n",
"clf.fit(u_mat,points,sample_weight=weights)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 117,
"text": [
"RidgeCV(alphas=(100, 200, 500, 1000, 2000), cv=10, fit_intercept=True,\n",
" gcv_mode=None, loss_func=None, normalize=False, score_func=None,\n",
" scoring=None, store_cv_values=False)"
]
}
],
"prompt_number": 117
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"clf.score(u_mat,points)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 106,
"text": [
"0.01390846579314553"
]
}
],
"prompt_number": 106
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(clf.alpha_)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1000\n"
]
}
],
"prompt_number": 107
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"players = u.get_feature_names()\n",
"print('Stephen Curry',players.index('Stephen Curry'))\n",
"print('James Harden', players.index('James Harden'))\n",
"print('LeBron James', players.index('LeBron James'))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"('Stephen Curry', 426)\n",
"('James Harden', 196)\n",
"('LeBron James', 286)\n"
]
}
],
"prompt_number": 46
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy\n",
"eye = numpy.identity(len(u.get_feature_names()))\n",
"print(eye[players.index('Stephen Curry')])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n",
" 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n"
]
}
],
"prompt_number": 47
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print('Stephen Curry',clf.predict(X=eye[players.index('Stephen Curry')]))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"('Stephen Curry', 7.4779501572076779)\n"
]
}
],
"prompt_number": 108
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print('LeBron James',clf.predict(X=eye[players.index('LeBron James')]))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"('LeBron James', 6.9868245200265058)\n"
]
}
],
"prompt_number": 109
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print('Klay Thompson',clf.predict(X=eye[players.index('Klay Thompson')]))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"('Klay Thompson', 4.9435644797712355)\n"
]
}
],
"prompt_number": 110
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print('Draymond Green',clf.predict(X=eye[players.index('Draymond Green')]))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"('Draymond Green', 2.8594848692118706)\n"
]
}
],
"prompt_number": 111
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print('Harrison Barnes',clf.predict(X=eye[players.index('Harrison Barnes')]))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"('Harrison Barnes', 1.2527181597021388)\n"
]
}
],
"prompt_number": 112
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print('Andre Iguodala',clf.predict(X=eye[players.index('Andre Iguodala')]))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"('Andre Iguodala', 5.3341398078777758)\n"
]
}
],
"prompt_number": 113
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print('James Harden',clf.predict(X=eye[players.index('James Harden')]))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"('James Harden', 6.4989707608336023)\n"
]
}
],
"prompt_number": 114
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print('David Lee',clf.predict(X=eye[players.index('David Lee')]))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"('David Lee', 4.9915379339601564)\n"
]
}
],
"prompt_number": 115
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print('John Henson',clf.predict(X=eye[players.index('John Henson')]))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"('John Henson', -2.449916756615377)\n"
]
}
],
"prompt_number": 116
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment