In [ ]:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import statsmodels.api as sm
from scipy.stats import norm
from scipy.stats.stats import pearsonr

str(3)
int('5')
In [2]:
float('7.1')
range(10)
range(1, 10)
Out[2]:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
In [3]:
dir(str)[:5]
Out[3]:
['__add__', '__class__', '__contains__', '__delattr__', '__doc__']
In [4]:
dir(str)[0:5]
Out[4]:
['__add__', '__class__', '__contains__', '__delattr__', '__doc__']
In [5]:
dir(str)[-5:]
Out[5]:
['swapcase', 'title', 'translate', 'upper', 'zfill']
In [6]:
x = ' Hello WorlD  '
dir(x)[-10:]
Out[6]:
['rstrip',
 'split',
 'splitlines',
 'startswith',
 'strip',
 'swapcase',
 'title',
 'translate',
 'upper',
 'zfill']
In [7]:
x.lower()
Out[7]:
' hello world  '
In [8]:
x.upper()
x.rstrip()
x.strip()
x.replace('lo', '')
Out[8]:
' Hel WorlD  '
In [9]:
x.split('lo')
','.join(['a', 'b'])
x = 'hello world'
type(x)
Out[9]:
str
In [10]:
l = [1,2,3,3] 
t = (1, 2, 3, 3) 
s = set([1,2,3,3])
d = {'a':1,'b':2,'c':3} 
a = np.array(l) 
print l, t, s, d, a
[1, 2, 3, 3] (1, 2, 3, 3) set([1, 2, 3]) {'a': 1, 'c': 3, 'b': 2} [1 2 3 3]
In [11]:
l = [1,2,3,3] 
l.append(4)
l
d = {'a':1,'b':2,'c':3} 
d.keys()
Out[11]:
['a', 'c', 'b']
In [12]:
d = {'a':1,'b':2,'c':3} # dict
d.values()
Out[12]:
[1, 3, 2]
In [13]:
d = {'a':1,'b':2,'c':3} # dict
d['b']
Out[13]:
2
In [14]:
d = {'a':1,'b':2,'c':3} # dict
d.items()
Out[14]:
[('a', 1), ('c', 3), ('b', 2)]
In [16]:
def devidePlus(m, n): 
    y = float(m)/n+ 1 
    return y
In [17]:
range(10)
Out[17]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [18]:
range(1, 10)  
Out[18]:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
In [19]:
for i in range(10):
    print i, i*10, i**2
0 0 0
1 10 1
2 20 4
3 30 9
4 40 16
5 50 25
6 60 36
7 70 49
8 80 64
9 90 81
In [20]:
for i in range(10):
    print devidePlus(i, 2)
1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
5.0
5.5
In [21]:
r = [devidePlus(i, 2)  for i in range(10)]
r
map(devidePlus, [4,3,2], [2, 1, 5])
Out[21]:
[3.0, 4.0, 1.4]
In [22]:
map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
Out[22]:
[3, 7, 11, 15, 19]
In [23]:
map(lambda x, y, z: x + y - z, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10], [3, 3, 2, 2, 5])
Out[23]:
[0, 4, 9, 13, 14]
In [24]:
j = 3
if j%2 == 1:
    print
elif j%2 ==2:
    print 
else:
    print 

In [25]:
j = 3
if j%2 == 1:
    print "余数是1"
elif j%2 ==2:
    print "余数是2"
else:
    print "余数不是1也不是2"
余数是1
In [26]:
x = 5
if x < 5:
    y = -1
    z = 5
elif x > 5:
    y = 1
    z = 11
else:
    y = 0
    z = 10
print(x, y, z)
(5, 0, 10)
In [27]:
j=0
while j<10:
    print j
    j+=1
0
1
2
3
4
5
6
7
8
9
In [28]:
j = 0
while j <50:
    if j == 30:
        break
    if j%2 != 0: 
        print j**2
    j+=1
1
9
25
49
81
121
169
225
289
361
441
529
625
729
841
In [29]:
a = 4
while a:
    print a
    a -= 1
    if a < 0:
        break
4
3
2
1
In [30]:
a = 4
while a:
    print a
    a -= 1
    if a < 0:
        a=None
4
3
2
1
In [31]:
for i in [2, 0, 5]:
    try:
        print devidePlus(4, i)
    except Exception, e:
        print e
        pass
3.0
float division by zero
1.8
In [32]:
alist = [[1,1], [0, 0, 1]]
for aa in alist:
    try:
        for a in aa:
            print 10 / a
    except Exception, e:
        print e
        pass
10
10
integer division or modulo by zero
In [33]:
alist = [[1,1], [0, 0, 1]]
for aa in alist:
    for a in aa:
        try:
            print 10 / a
        except Exception, e:
            print e
            pass
10
10
integer division or modulo by zero
integer division or modulo by zero
10
In [34]:
data =[[i, i**2, i**3] for i in range(10)] 
data
Out[34]:
[[0, 0, 0],
 [1, 1, 1],
 [2, 4, 8],
 [3, 9, 27],
 [4, 16, 64],
 [5, 25, 125],
 [6, 36, 216],
 [7, 49, 343],
 [8, 64, 512],
 [9, 81, 729]]
In [35]:
for i in data:
    print '\t'.join(map(str, i))
0	0	0
1	1	1
2	4	8
3	9	27
4	16	64
5	25	125
6	36	216
7	49	343
8	64	512
9	81	729
In [36]:
type(data)
Out[36]:
list
In [37]:
len(data)
Out[37]:
10
In [38]:
data[0:2]
Out[38]:
[[0, 0, 0], [1, 1, 1]]
In [39]:
data =[[i, i**2, i**3] for i in range(10000)] 

f = open("/Users/chengjun/github/cjc/data/data_write_to_file.txt", "wb")
for i in data:
    f.write('\t'.join(map(str,i)) + '\n')
f.close()
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-39-179f67da9013> in <module>()
      1 data =[[i, i**2, i**3] for i in range(10000)]
      2 
----> 3 f = open("/Users/chengjun/github/cjc/data/data_write_to_file.txt", "wb")
      4 for i in data:
      5     f.write('\t'.join(map(str,i)) + '\n')

IOError: [Errno 2] No such file or directory: '/Users/chengjun/github/cjc/data/data_write_to_file.txt'
In [40]:
f = [1, 2, 3, 4, 5]
for k, i in enumerate(f):
    print k, i
0 1
1 2
2 3
3 4
4 5
In [41]:
with open('/Users/chengjun/github/cjc/data/data_write_to_file.txt','r') as f:
    for i in f:
        print i
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-41-90757e1d81bb> in <module>()
----> 1 with open('/Users/chengjun/github/cjc/data/data_write_to_file.txt','r') as f:
      2     for i in f:
      3         print i

IOError: [Errno 2] No such file or directory: '/Users/chengjun/github/cjc/data/data_write_to_file.txt'
In [42]:
data =[[i, i**2, i**3] for i in range(100)] 

f = open("/Users/jamiezhu/data_write_to_file.txt", "wb")
for i in data:
    f.write('\t'.join(map(str,i)) + '\n')
f.close()
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-42-1104cef30b8f> in <module>()
      1 data =[[i, i**2, i**3] for i in range(100)]
      2 
----> 3 f = open("/Users/jamiezhu/data_write_to_file.txt", "wb")
      4 for i in data:
      5     f.write('\t'.join(map(str,i)) + '\n')

IOError: [Errno 2] No such file or directory: '/Users/jamiezhu/data_write_to_file.txt'
In [43]:
data =[[i, i**2, i**3] for i in range(100)] 

f = open("/jamiezhu/data_write_to_file.txt", "wb")
for i in data:
    f.write('\t'.join(map(str,i)) + '\n')
f.close()
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-43-e2f3ee35a9fc> in <module>()
      1 data =[[i, i**2, i**3] for i in range(100)]
      2 
----> 3 f = open("/jamiezhu/data_write_to_file.txt", "wb")
      4 for i in data:
      5     f.write('\t'.join(map(str,i)) + '\n')

IOError: [Errno 2] No such file or directory: '/jamiezhu/data_write_to_file.txt'
In [44]:
data =[[i, i**2, i**3] for i in range(100)] 

f = open("/Users/conghuizhu/downloads/data_write_to_file.txt", "wb")
for i in data:
    f.write('\t'.join(map(str,i)) + '\n')
f.close()
In [45]:
with open('/Users/conghuizhu/downloads/data_write_to_file.txt','r') as f:
    data = f.readlines()
data[:5]
Out[45]:
['0\t0\t0\n', '1\t1\t1\n', '2\t4\t8\n', '3\t9\t27\n', '4\t16\t64\n']
In [46]:
with open('/Users/conghuizhu/downloads/data_write_to_file.txt','r') as f:
    data = f.readlines(100)
len(data)
Out[46]:
100
In [47]:
with open('/Users/conghuizhu/downloads/data_write_to_file.txt','r') as f:
    print f.readline()
0	0	0

In [48]:
f = [1, 2, 3, 4, 5]
for k, i in enumerate(f):
    print k, i
0 1
1 2
2 3
3 4
4 5
In [49]:
with open('/Users/conghuizhu/downloads/data_write_to_file.txt','r') as f:
    for i in f:
        print i
0	0	0

1	1	1

2	4	8

3	9	27

4	16	64

5	25	125

6	36	216

7	49	343

8	64	512

9	81	729

10	100	1000

11	121	1331

12	144	1728

13	169	2197

14	196	2744

15	225	3375

16	256	4096

17	289	4913

18	324	5832

19	361	6859

20	400	8000

21	441	9261

22	484	10648

23	529	12167

24	576	13824

25	625	15625

26	676	17576

27	729	19683

28	784	21952

29	841	24389

30	900	27000

31	961	29791

32	1024	32768

33	1089	35937

34	1156	39304

35	1225	42875

36	1296	46656

37	1369	50653

38	1444	54872

39	1521	59319

40	1600	64000

41	1681	68921

42	1764	74088

43	1849	79507

44	1936	85184

45	2025	91125

46	2116	97336

47	2209	103823

48	2304	110592

49	2401	117649

50	2500	125000

51	2601	132651

52	2704	140608

53	2809	148877

54	2916	157464

55	3025	166375

56	3136	175616

57	3249	185193

58	3364	195112

59	3481	205379

60	3600	216000

61	3721	226981

62	3844	238328

63	3969	250047

64	4096	262144

65	4225	274625

66	4356	287496

67	4489	300763

68	4624	314432

69	4761	328509

70	4900	343000

71	5041	357911

72	5184	373248

73	5329	389017

74	5476	405224

75	5625	421875

76	5776	438976

77	5929	456533

78	6084	474552

79	6241	493039

80	6400	512000

81	6561	531441

82	6724	551368

83	6889	571787

84	7056	592704

85	7225	614125

86	7396	636056

87	7569	658503

88	7744	681472

89	7921	704969

90	8100	729000

91	8281	753571

92	8464	778688

93	8649	804357

94	8836	830584

95	9025	857375

96	9216	884736

97	9409	912673

98	9604	941192

99	9801	970299

In [50]:
with open('/Users/conghuizhu/downloads/data_write_to_file.txt','r') as f:
    for k, i in enumerate(f):
        if k%2000 ==0:
            print i
0	0	0

In [51]:
data = []
line = '0\t0\t0\n'
line = line.replace('\n', '')
line = line.split('\t')
line = [int(i) for i in line] # convert str to int
data.append(line)
data
Out[51]:
[[0, 0, 0]]
In [52]:
data = []
with open('/Users/chengjun/github/cjc/data/data_write_to_file.txt','r') as f:
    for line in f:
        #line = line.replace('\n', '').split('\t')
        #line = [int(i) for i in line]
        data.append(line)
data
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-52-092aa774b80b> in <module>()
      1 data = []
----> 2 with open('/Users/chengjun/github/cjc/data/data_write_to_file.txt','r') as f:
      3     for line in f:
      4         #line = line.replace('\n', '').split('\t')
      5         #line = [int(i) for i in line]

IOError: [Errno 2] No such file or directory: '/Users/chengjun/github/cjc/data/data_write_to_file.txt'
In [53]:
data = []
with open('/Users/conghuizhu/downloads/data_write_to_file.txt','r') as f:
    for line in f:
        #line = line.replace('\n', '').split('\t')
        #line = [int(i) for i in line]
        data.append(line)
data
Out[53]:
['0\t0\t0\n',
 '1\t1\t1\n',
 '2\t4\t8\n',
 '3\t9\t27\n',
 '4\t16\t64\n',
 '5\t25\t125\n',
 '6\t36\t216\n',
 '7\t49\t343\n',
 '8\t64\t512\n',
 '9\t81\t729\n',
 '10\t100\t1000\n',
 '11\t121\t1331\n',
 '12\t144\t1728\n',
 '13\t169\t2197\n',
 '14\t196\t2744\n',
 '15\t225\t3375\n',
 '16\t256\t4096\n',
 '17\t289\t4913\n',
 '18\t324\t5832\n',
 '19\t361\t6859\n',
 '20\t400\t8000\n',
 '21\t441\t9261\n',
 '22\t484\t10648\n',
 '23\t529\t12167\n',
 '24\t576\t13824\n',
 '25\t625\t15625\n',
 '26\t676\t17576\n',
 '27\t729\t19683\n',
 '28\t784\t21952\n',
 '29\t841\t24389\n',
 '30\t900\t27000\n',
 '31\t961\t29791\n',
 '32\t1024\t32768\n',
 '33\t1089\t35937\n',
 '34\t1156\t39304\n',
 '35\t1225\t42875\n',
 '36\t1296\t46656\n',
 '37\t1369\t50653\n',
 '38\t1444\t54872\n',
 '39\t1521\t59319\n',
 '40\t1600\t64000\n',
 '41\t1681\t68921\n',
 '42\t1764\t74088\n',
 '43\t1849\t79507\n',
 '44\t1936\t85184\n',
 '45\t2025\t91125\n',
 '46\t2116\t97336\n',
 '47\t2209\t103823\n',
 '48\t2304\t110592\n',
 '49\t2401\t117649\n',
 '50\t2500\t125000\n',
 '51\t2601\t132651\n',
 '52\t2704\t140608\n',
 '53\t2809\t148877\n',
 '54\t2916\t157464\n',
 '55\t3025\t166375\n',
 '56\t3136\t175616\n',
 '57\t3249\t185193\n',
 '58\t3364\t195112\n',
 '59\t3481\t205379\n',
 '60\t3600\t216000\n',
 '61\t3721\t226981\n',
 '62\t3844\t238328\n',
 '63\t3969\t250047\n',
 '64\t4096\t262144\n',
 '65\t4225\t274625\n',
 '66\t4356\t287496\n',
 '67\t4489\t300763\n',
 '68\t4624\t314432\n',
 '69\t4761\t328509\n',
 '70\t4900\t343000\n',
 '71\t5041\t357911\n',
 '72\t5184\t373248\n',
 '73\t5329\t389017\n',
 '74\t5476\t405224\n',
 '75\t5625\t421875\n',
 '76\t5776\t438976\n',
 '77\t5929\t456533\n',
 '78\t6084\t474552\n',
 '79\t6241\t493039\n',
 '80\t6400\t512000\n',
 '81\t6561\t531441\n',
 '82\t6724\t551368\n',
 '83\t6889\t571787\n',
 '84\t7056\t592704\n',
 '85\t7225\t614125\n',
 '86\t7396\t636056\n',
 '87\t7569\t658503\n',
 '88\t7744\t681472\n',
 '89\t7921\t704969\n',
 '90\t8100\t729000\n',
 '91\t8281\t753571\n',
 '92\t8464\t778688\n',
 '93\t8649\t804357\n',
 '94\t8836\t830584\n',
 '95\t9025\t857375\n',
 '96\t9216\t884736\n',
 '97\t9409\t912673\n',
 '98\t9604\t941192\n',
 '99\t9801\t970299\n']
In [54]:
import pandas as pd
In [55]:
df = pd.read_csv('/Users/chengjun/github/cjc/data/data_write_to_file.txt', sep = '\t', names = ['a', 'b', 'c'])
df[:5]
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-55-4010384032f9> in <module>()
----> 1 df = pd.read_csv('/Users/chengjun/github/cjc/data/data_write_to_file.txt', sep = '\t', names = ['a', 'b', 'c'])
      2 df[:5]

/Users/conghuizhu/anaconda/lib/python2.7/site-packages/pandas/io/parsers.pyc in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, skip_footer, doublequote, delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, buffer_lines, memory_map, float_precision)
    644                     skip_blank_lines=skip_blank_lines)
    645 
--> 646         return _read(filepath_or_buffer, kwds)
    647 
    648     parser_f.__name__ = name

/Users/conghuizhu/anaconda/lib/python2.7/site-packages/pandas/io/parsers.pyc in _read(filepath_or_buffer, kwds)
    387 
    388     # Create the parser.
--> 389     parser = TextFileReader(filepath_or_buffer, **kwds)
    390 
    391     if (nrows is not None) and (chunksize is not None):

/Users/conghuizhu/anaconda/lib/python2.7/site-packages/pandas/io/parsers.pyc in __init__(self, f, engine, **kwds)
    728             self.options['has_index_names'] = kwds['has_index_names']
    729 
--> 730         self._make_engine(self.engine)
    731 
    732     def close(self):

/Users/conghuizhu/anaconda/lib/python2.7/site-packages/pandas/io/parsers.pyc in _make_engine(self, engine)
    921     def _make_engine(self, engine='c'):
    922         if engine == 'c':
--> 923             self._engine = CParserWrapper(self.f, **self.options)
    924         else:
    925             if engine == 'python':

/Users/conghuizhu/anaconda/lib/python2.7/site-packages/pandas/io/parsers.pyc in __init__(self, src, **kwds)
   1388         kwds['allow_leading_cols'] = self.index_col is not False
   1389 
-> 1390         self._reader = _parser.TextReader(src, **kwds)
   1391 
   1392         # XXX

pandas/parser.pyx in pandas.parser.TextReader.__cinit__ (pandas/parser.c:4184)()

pandas/parser.pyx in pandas.parser.TextReader._setup_parser_source (pandas/parser.c:8449)()

IOError: File /Users/chengjun/github/cjc/data/data_write_to_file.txt does not exist
In [56]:
df = pd.read_csv('/Users/conghuizhu/downloads/data_write_to_file.txt', sep = '\t', names = ['a', 'b', 'c'])
df[:5]
Out[56]:
a b c
0 0 0 0
1 1 1 1
2 2 4 8
3 3 9 27
4 4 16 64
In [57]:
import json
data_dict = {'a':1, 'b':2, 'c':3}
with open('/Users/chengjun/github/cjc/save_dict.json', 'w') as f:
    json.dump(data_dict, f)
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-57-77b7919599d1> in <module>()
      1 import json
      2 data_dict = {'a':1, 'b':2, 'c':3}
----> 3 with open('/Users/chengjun/github/cjc/save_dict.json', 'w') as f:
      4     json.dump(data_dict, f)

IOError: [Errno 2] No such file or directory: '/Users/chengjun/github/cjc/save_dict.json'
In [58]:
import json
data_dict = {'a':1, 'b':2, 'c':3}
with open('/Users/conghuizhu/downloads/save_dict.json', 'w') as f:
    json.dump(data_dict, f)
In [59]:
dd = json.load(open("/Users/conghuizhu/downloads/save_dict.json"))
dd
Out[59]:
{u'a': 1, u'b': 2, u'c': 3}
In [60]:
data_list = range(10)
with open('/Users/chengjun/github/cjc/save_list.json', 'w') as f:
    json.dump(data_list, f)
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-60-f9fba1dd602d> in <module>()
      1 data_list = range(10)
----> 2 with open('/Users/chengjun/github/cjc/save_list.json', 'w') as f:
      3     json.dump(data_list, f)

IOError: [Errno 2] No such file or directory: '/Users/chengjun/github/cjc/save_list.json'
In [61]:
data_list = range(10)
with open('/Users/conghuizhu/downloads/save_list.json', 'w') as f:
    json.dump(data_list, f)
In [62]:
 dl = json.load(open("/Users/chengjun/github/cjc/save_list.json"))
dl
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-62-0bb1e05d320d> in <module>()
----> 1 dl = json.load(open("/Users/chengjun/github/cjc/save_list.json"))
      2 dl

IOError: [Errno 2] No such file or directory: '/Users/chengjun/github/cjc/save_list.json'
In [63]:
dl = json.load(open("/Users/conghuizhu/downloads/save_list.json"))
dl
Out[63]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [64]:
import dill # pip insstall dill 
# http://trac.mystic.cacr.caltech.edu/project/pathos/wiki/dill
def myFunction(num):
    return num,num

with open('/Users/conghuizhu/downloads/data.pkl', 'wb') as f:
    dill.dump(myFunction, f)
In [65]:
with open('/Users/conghuizhu/downloads/data.pkl', 'r') as f:
    newFunction = dill.load(f)#, strictio=strictio))
newFunction('hello')
Out[65]:
('hello', 'hello')
In [ ]: