This is a text!
print "Hello World!"
Hello World!
%pylab inline
Populating the interactive namespace from numpy and matplotlib
%quickref
help("numpy")
x = 24
help(x)
Help on int object: class int(object) | int(x=0) -> int or long | int(x, base=10) -> int or long | | Convert a number or string to an integer, or return 0 if no arguments | are given. If x is floating point, the conversion truncates towards zero. | If x is outside the integer range, the function returns a long instead. | | If x is not a number or if base is given, then x must be a string or | Unicode object representing an integer literal in the given base. The | literal can be preceded by '+' or '-' and be surrounded by whitespace. | The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to | interpret the base from the string as an integer literal. | >>> int('0b100', base=0) | 4 | | Methods defined here: | | __abs__(...) | x.__abs__() <==> abs(x) | | __add__(...) | x.__add__(y) <==> x+y | | __and__(...) | x.__and__(y) <==> x&y | | __cmp__(...) | x.__cmp__(y) <==> cmp(x,y) | | __coerce__(...) | x.__coerce__(y) <==> coerce(x, y) | | __div__(...) | x.__div__(y) <==> x/y | | __divmod__(...) | x.__divmod__(y) <==> divmod(x, y) | | __float__(...) | x.__float__() <==> float(x) | | __floordiv__(...) | x.__floordiv__(y) <==> x//y | | __format__(...) | | __getattribute__(...) | x.__getattribute__('name') <==> x.name | | __getnewargs__(...) | | __hash__(...) | x.__hash__() <==> hash(x) | | __hex__(...) | x.__hex__() <==> hex(x) | | __index__(...) | x[y:z] <==> x[y.__index__():z.__index__()] | | __int__(...) | x.__int__() <==> int(x) | | __invert__(...) | x.__invert__() <==> ~x | | __long__(...) | x.__long__() <==> long(x) | | __lshift__(...) | x.__lshift__(y) <==> x<<y | | __mod__(...) | x.__mod__(y) <==> x%y | | __mul__(...) | x.__mul__(y) <==> x*y | | __neg__(...) | x.__neg__() <==> -x | | __nonzero__(...) | x.__nonzero__() <==> x != 0 | | __oct__(...) | x.__oct__() <==> oct(x) | | __or__(...) | x.__or__(y) <==> x|y | | __pos__(...) | x.__pos__() <==> +x | | __pow__(...) | x.__pow__(y[, z]) <==> pow(x, y[, z]) | | __radd__(...) | x.__radd__(y) <==> y+x | | __rand__(...) | x.__rand__(y) <==> y&x | | __rdiv__(...) | x.__rdiv__(y) <==> y/x | | __rdivmod__(...) | x.__rdivmod__(y) <==> divmod(y, x) | | __repr__(...) | x.__repr__() <==> repr(x) | | __rfloordiv__(...) | x.__rfloordiv__(y) <==> y//x | | __rlshift__(...) | x.__rlshift__(y) <==> y<<x | | __rmod__(...) | x.__rmod__(y) <==> y%x | | __rmul__(...) | x.__rmul__(y) <==> y*x | | __ror__(...) | x.__ror__(y) <==> y|x | | __rpow__(...) | y.__rpow__(x[, z]) <==> pow(x, y[, z]) | | __rrshift__(...) | x.__rrshift__(y) <==> y>>x | | __rshift__(...) | x.__rshift__(y) <==> x>>y | | __rsub__(...) | x.__rsub__(y) <==> y-x | | __rtruediv__(...) | x.__rtruediv__(y) <==> y/x | | __rxor__(...) | x.__rxor__(y) <==> y^x | | __str__(...) | x.__str__() <==> str(x) | | __sub__(...) | x.__sub__(y) <==> x-y | | __truediv__(...) | x.__truediv__(y) <==> x/y | | __trunc__(...) | Truncating an Integral returns itself. | | __xor__(...) | x.__xor__(y) <==> x^y | | bit_length(...) | int.bit_length() -> int | | Number of bits necessary to represent self in binary. | >>> bin(37) | '0b100101' | >>> (37).bit_length() | 6 | | conjugate(...) | Returns self, the complex conjugate of any int. | | ---------------------------------------------------------------------- | Data descriptors defined here: | | denominator | the denominator of a rational number in lowest terms | | imag | the imaginary part of a complex number | | numerator | the numerator of a rational number in lowest terms | | real | the real part of a complex number | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __new__ = <built-in method __new__ of type object> | T.__new__(S, ...) -> a new object with type S, a subtype of T
%%bash
echo "Hello World!"
Hello World!
%%bash
awk '{print;}' data.txt
row1col1 row1col2 row2col2 row2col2
%%bash
awk '{print $1;}' data.txt
row1col1 row2col2
x = 24
x = 3.140372
x = "Hello World!"
print x
type(x)
Hello World!
str
3 + 4
7
3 - 4
-1
4 / 4
1
4 * 4
16
x = 3
x += 3
print x
6
x / 3
2
x = 1
y = 3
res = x / y
print res
type(res)
0
int
x / float(y)
0.3333333333333333
x = 1.
y = float(3)
res = x / y
print res
type(res)
0.333333333333
float
a = "13"
float(a)
13.0
text = "Hello world!"
len(text)
12
text[0:3]
'Hel'
text[0]
'H'
text.upper()
'HELLO WORLD!'
text.lower()
'hello world!'
text.find('l')
2
text.split(' ')
['Hello', 'world!']
text[0] = 'A'
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-33-32d57f349336> in <module>() ----> 1 text[0] = 'A' TypeError: 'str' object does not support item assignment
'A' + text[1:]
'Aello world!'
True
True
False
False
x = None
print x
None
x = [3, 4, 5]
print x
[3, 4, 5]
x[0]
3
x.append(7)
print x
[3, 4, 5, 7]
x[0:2]
[3, 4]
x[-1]
7
x[-3:]
[4, 5, 7]
a = x + [3,1]
a
[3, 4, 5, 7, 3, 1]
len(a)
6
a.append("string")
a
[3, 4, 5, 7, 3, 1, 'string']
a.sort()
print a
[1, 3, 3, 4, 5, 7, 'string']
for i in a:
print i
1 3 3 4 5 7 string
x = [1,2,3]
a = [i*2 for i in x]
print a
[2, 4, 6]
for x in a:
print x
2 4 6
for i,x in enumerate(a):
print i, x, i, x
0 2 0 2 1 4 1 4 2 6 2 6
range(4)
[0, 1, 2, 3]
a = set()
b = set()
a.add(1)
a.add(2)
b.add(1)
b.add(4)
print a, b
set([1, 2]) set([1, 4])
a.add(1)
print a
set([1, 2])
a[0] = 0
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-60-968b294291f9> in <module>() ----> 1 a[0] = 0 TypeError: 'set' object does not support item assignment
a & b #intersection
{1}
a | b #union
{1, 2, 4}
a - b #difference
{2}
a ^ b #symmetric difference
{2, 4}
a = [1,1,3,5]
b = set(a)
b
a = list(b)
print a
[1, 3, 5]
for x in b:
print x
1 3 5
a = (1,2)
a
(1, 2)
b = (1,2,3)
b
(1, 2, 3)
a[0] = 0
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-71-968b294291f9> in <module>() ----> 1 a[0] = 0 TypeError: 'tuple' object does not support item assignment
a[0]
1
a[2]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-73-016a87a854bc> in <module>() ----> 1 a[2] IndexError: tuple index out of range
zip(a,b)
a = (3,1,2)
b = (1,1,1)
zip(a,b)
[(3, 1), (1, 1), (2, 1)]
a = [3,1,2]
b = [1,1,1]
zip(a,b)
[(3, 1), (1, 1), (2, 1)]
a = dict()
a[0] = "string"
print a
{0: 'string'}
print a[0]
string
print a[1]
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-79-e9d376687425> in <module>() ----> 1 print a[1] KeyError: 1
a["string"] = 0
print a
{0: 'string', 'string': 0}
a.keys()
[0, 'string']
a.values()
['string', 0]
for key, value in a.iteritems():
print key,value
0 string string 0
for key in a.keys():
print key, a[key]
0 string string 0
from collections import Counter
a = ["c", "a", "b", "a", "c", "c"]
Counter(a)
Counter({'c': 3, 'a': 2, 'b': 1})
from collections import OrderedDict
a = OrderedDict()
a[1] = 3
a[2] = 5
a["string"] = 6
print a
a[1]
OrderedDict([(1, 3), (2, 5), ('string', 6)])
3
from collections import defaultdict
#initialized with a function
a = defaultdict(int)
a[1] += 1
print a[1]
a[1] += 1
print a[1]
1 2
a = defaultdict(int)
if a[1] == 0:
print "wow"
wow
print a
defaultdict(<type 'int'>, {1: 0})
print a.keys()
a = dict()
a[1] = 0
a[1] += 1
print a[2]
a.keys()
a = defaultdict(lambda: defaultdict(int))
a[1][2] = 3
print a
print a[1]
print a[1][2]
defaultdict(<function <lambda> at 0x10477e758>, {1: defaultdict(<type 'int'>, {2: 3})}) defaultdict(<type 'int'>, {2: 3}) 3
a = [3,4,5]
b = a
b[0] = 10
print a
[10, 4, 5]
import collections
a = collections.defaultdict()
from collections import defaultdict
a = defaultdict()
from collections import *
a = Counter()
x = True
print "Hello World!"
if x == True:
print "wohooo"
Hello World! wohooo
if x:
print "wohooo"
wohooo
x = 3
if x == 2:
print "2"
elif x > 2:
print "larger than 2"
else:
print "smaller than 2"
larger than 2
x = 0
while x < 5:
print x
x += 1
0 1 2 3 4
x = 3
x >= 3
True
3 in [3,4,5]
True
3 in {3:4, 4:4}
True
5 in {3:4, 4:4}
False
x = 0
while True:
x += 1
print x
if x == 5:
break
1 2 3 4 5
x = 0
while True:
x += 1
print x
if x == 5:
pass
if x == 6:
break
1 2 3 4 5 6
x = [1,2,3]
x[99]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-110-9be32c536b46> in <module>() 1 x = [1,2,3] ----> 2 x[99] IndexError: list index out of range
x = [1,2,3]
try:
x[99]
print "all fine"
except:
print "cought an exception"
cought an exception
a = [1,2,3]
if 4 in a:
print "something"
f = open("data.txt")
for line in f:
print line
row1col1 row1col2 row2col2 row2col2
f = open("data.txt")
data1 = []
data2 = []
dict_data = {}
for line in f:
columns = line.strip().split(" ")
data1.append(columns[0])
data2.append(columns[1])
dict_data[columns[0]] = columns[1]
print data1
print data2
print dict_data
['row1col1', 'row2col2'] ['row1col2', 'row2col2'] {'row2col2': 'row2col2', 'row1col1': 'row1col2'}
f = open("data.txt", 'a')
f.write("row3col3 row2col3\n")
f.write("row4col4 row4col4\n")
f = open("data.txt")
for line in f:
print line
row1col1 row1col2 row2col2 row2col2 row3col3 row2col3 row4col4 row4col4
def name(para):
para += 1
return para
name(1)
2
def factorial(n):
if n<=0:
return 1
return n*factorial(n-1)
factorial(10)
3628800
def func(a):
a[0] = 3
x = [4,5,6]
func(x)
print x
[3, 5, 6]
%pylab inline
Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: ['f', 'text'] `%matplotlib` prevents importing * from pylab and numpy
#pip install matplotlib
import matplotlib.pyplot as plt
x = range(10)
y = range(10)
fig = plt.figure()
plt.plot(x,y, label="linear")
[<matplotlib.lines.Line2D at 0x104c6a4d0>]
x = range(10)
y = range(10)
fig = plt.figure(figsize(12,10))
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
ax1.plot(x,y, label="linear")
y = [factorial(i) for i in x]
ax2.plot(x,y,label="factorial")
[<matplotlib.lines.Line2D at 0x1102b7e90>]
x = range(10)
y = range(10)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y, label="linear")
y = [factorial(i) for i in x]
ax.plot(x,y,label="factorial")
[<matplotlib.lines.Line2D at 0x11056b8d0>]
ax.set_yscale('log')
fig
import numpy as np
a = np.array([1,2,3])
print type(a)
print a
<type 'numpy.ndarray'> [1 2 3]
a[0]
1
a.shape
(3,)
a = np.array([[1,2,3],[4,5,6]])
print a
[[1 2 3] [4 5 6]]
a[1,2]
6
a[0,:]
array([1, 2, 3])
a.shape
(2, 3)
np.zeros((3,3))
array([[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]])
np.ones((3,3))
array([[ 1., 1., 1.], [ 1., 1., 1.], [ 1., 1., 1.]])
a
array([[1, 2, 3], [4, 5, 6]])
a * 3
array([[ 3, 6, 9], [12, 15, 18]])
a * a
array([[ 1, 4, 9], [16, 25, 36]])
a
array([[1, 2, 3], [4, 5, 6]])
a.dot(a.T)
array([[14, 32], [32, 77]])
a = np.random.random((3,3))
a
array([[ 0.4949619 , 0.12587845, 0.58050931], [ 0.02695513, 0.99974167, 0.17466186], [ 0.21897059, 0.6037107 , 0.23323345]])
from numpy.linalg import eig
eig(a)
(array([-0.0689059 , 0.62300922, 1.1738337 ]), array([[ 0.70327176, -0.9507704 , -0.57164664], [ 0.09736052, 0.18372854, -0.6235509 ], [-0.70422281, -0.24955855, -0.53329579]]))
a = np.array([1,2,3], dtype=np.float32)
a
array([ 1., 2., 3.], dtype=float32)
a = np.array([1,2,3], dtype=np.int32)
a
array([1, 2, 3], dtype=int32)
from scipy.sparse import csr_matrix
a = csr_matrix(np.zeros((3,3)))
print a
a.toarray()
array([[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]])
a[1,1] = 4.
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/sparse/compressed.py:730: SparseEfficiencyWarning: Changing the sparsity structure of a csr_matrix is expensive. lil_matrix is more efficient. SparseEfficiencyWarning)
print a
(1, 1) 4.0
import networkx as nx
G = nx.Graph()
G.add_node(1)
G.nodes()
[1]
G.add_edge(1,2)
G.nodes()
[1, 2]
G.edges()
[(1, 2)]
G.add_edge(2,3)
G.add_edge(1,3)
G.add_edge(3,4)
plt.figure()
nx.draw(G, with_labels=True)
nx.draw_circular(G)
nx.draw_spectral(G)
G.neighbors(1)
[2, 3]
nx.degree(G)
{1: 2, 2: 2, 3: 3, 4: 1}
nx.clustering(G)
{1: 1.0, 2: 1.0, 3: 0.3333333333333333, 4: 0.0}
G.degree(3)
3
Machine learning library http://scikit-learn.org/
The diabetes dataset consists of 10 physiological variables (age, sex, weight, blood pressure) measure on 442 patients, and an indication of disease progression after one year
#http://scikit-learn.org/stable/auto_examples/linear_model/plot_ols.html
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model
# Load the diabetes dataset
diabetes = datasets.load_diabetes()
# Use only one feature
diabetes_X = diabetes.data[:, np.newaxis]
diabetes_X_temp = diabetes_X[:, :, 2]
# Split the data into training/testing sets
diabetes_X_train = diabetes_X_temp[:-20]
diabetes_X_test = diabetes_X_temp[-20:]
# Split the targets into training/testing sets
diabetes_y_train = diabetes.target[:-20]
diabetes_y_test = diabetes.target[-20:]
# Create linear regression object
regr = linear_model.LinearRegression()
# Train the model using the training sets
regr.fit(diabetes_X_train, diabetes_y_train)
# The coefficients
print('Coefficients: \n', regr.coef_)
# The mean square error
print("Residual sum of squares: %.2f"
% np.mean((regr.predict(diabetes_X_test) - diabetes_y_test) ** 2))
# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % regr.score(diabetes_X_test, diabetes_y_test))
# Plot outputs
plt.scatter(diabetes_X_test, diabetes_y_test, color='black')
plt.plot(diabetes_X_test, regr.predict(diabetes_X_test), color='blue',
linewidth=3)
plt.xticks(())
plt.yticks(())
plt.show()
diabetes.target