In [3]:
import networkx as nx
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pylab
G = nx.DiGraph()
In [4]:
Scores = pd.read_csv("scores.csv")
Scores[:10]
Out[4]:
In [5]:
G = nx.DiGraph()
for i in range(len(Scores)):
for x in Scores.columns.values:
if Scores.ix[i][x] > 0:
for y in Scores.columns.values:
if Scores.ix[i][y] > 0:
if Scores.ix[i][x] > Scores.ix[i][y]:
if (x,y) not in G.edges():
G.add_edges_from([(x,y)])
In [6]:
pos=nx.spring_layout(G)
nx.draw(G,pos,node_color = 'blue', node_size=2000)
node_labels = {node:node for node in G.nodes()}
nx.draw_networkx_labels(G, pos, labels=node_labels)
pylab.show()
In [7]:
nx.pagerank(G)
Out[7]:
In [8]:
sorted(list(nx.pagerank(G)), key=lambda tup: tup[1])
Out[8]:
In [9]:
W = nx.DiGraph()
for i in range(len(Scores)):
for x in Scores.columns.values:
if Scores.ix[i][x] > 0:
for y in Scores.columns.values:
if Scores.ix[i][y] > 0:
if Scores.ix[i][x] > Scores.ix[i][y]:
W.add_edges_from([(x,y)])
In [10]:
nx.pagerank(W)
Out[10]:
In [11]:
sales = {
"A6": 20,
"A8": 12,
"3series": 220,
"5series": 60,
"7series": 14,
"XJ": 6.6,
"ES": 135,
"LS": 30,
"RX": 120,
"Sclass": 25
}
In [13]:
test = pd.DataFrame({"Sales": sales, "Weighted": nx.pagerank(W), "Unweighted": nx.pagerank(G)})
In [17]:
from sklearn.linear_model import LinearRegression
In [29]:
model_uw = LinearRegression()
model_uw.fit(test[["Unweighted"]], test.Sales)
print model_uw.coef_
print model_uw.score(test[["Unweighted"]], test.Sales)
In [26]:
model_w = LinearRegression()
model_w.fit(test[["Weighted"]], test.Sales)
Out[26]:
In [27]:
model_w.score(test[["Weighted"]], test.Sales)
Out[27]: