Skip to content

Commit eec692c

Browse files
committed
2018-05-05
1 parent ff18105 commit eec692c

29 files changed

Lines changed: 2489 additions & 0 deletions

.DS_Store

14 KB
Binary file not shown.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Discovering structure in heatmap data
3+
=====================================
4+
5+
_thumb: .4, .25
6+
"""
7+
import pandas as pd
8+
import seaborn as sns
9+
sns.set()
10+
11+
# Load the brain networks example dataset
12+
df = sns.load_dataset("brain_networks", header=[0, 1, 2], index_col=0)
13+
14+
# Select a subset of the networks
15+
used_networks = [1, 5, 6, 7, 8, 12, 13, 17]
16+
used_columns = (df.columns.get_level_values("network")
17+
.astype(int)
18+
.isin(used_networks))
19+
df = df.loc[:, used_columns]
20+
21+
# Create a categorical palette to identify the networks
22+
network_pal = sns.husl_palette(8, s=.45)
23+
network_lut = dict(zip(map(str, used_networks), network_pal))
24+
25+
# Convert the palette to vectors that will be drawn on the side of the matrix
26+
networks = df.columns.get_level_values("network")
27+
network_colors = pd.Series(networks, index=df.columns).map(network_lut)
28+
29+
# Draw the full plot
30+
sns.clustermap(df.corr(), center=0, cmap="vlag",
31+
row_colors=network_colors, col_colors=network_colors,
32+
linewidths=.75, figsize=(13, 13))

InteractionMySQL/.DS_Store

6 KB
Binary file not shown.
106 KB
Binary file not shown.

InteractionMySQL/pythonMysql.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Wed Feb 28 21:57:26 2018
4+
5+
@author: Administrator
6+
"""
7+
8+
#6. Python与mysql交互(python script)
9+
10+
#1) mysql数据导入到python
11+
import pandas
12+
from sqlalchemy import creat_engine
13+
engine = create_engine('mysql+mysqlconnector://root:@127.0.0.1:3306/cp') #"mysql+mysqlconnector://用户名:密码@IP地址:端口号/数据库名"
14+
data = pandas.read_sql("select * from news;",con=engine)
15+
16+
17+
#2)python数据导入到mysql
18+
from pandas import DataFrame;
19+
from sqlalchemy import create_engine
20+
engine = create_engine('mysql+mysqlconnector://root:@127.0.0.1:3306/cp')
21+
data = DataFrame({'age':[21,22,23],'name':['KEN','大数据分析实战','小蚊子']})
22+
data.to_sql("testTable",index=False,con=engine,if_exists='append')
23+
24+
25+
26+
#%% MySQLdb
27+
#http://www.runoob.com/python/python-mysql.html
28+
import MySQLdb
29+
30+
#install
31+
$ gunzip MySQL-python-1.2.2.tar.gz
32+
$ tar -xvf MySQL-python-1.2.2.tar
33+
$ cd MySQL-python-1.2.2
34+
$ python setup.py build
35+
$ python setup.py install
36+
37+
#已经创建了数据库cp中的score表,字段为sid,student_id,corse_id,number
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+

NormalFunc/BasicGrammar.py

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
# -*- coding: UTF-8 -*-
2+
3+
#%%数据结构
4+
##字符串
5+
##加号(+)是字符串连接运算符,星号(*)是重复操作
6+
str='Hello World!'
7+
print(str) # 输出完整字符串
8+
print(str[0]) # 输出字符串中的第一个字符
9+
print(str[2:5]) # 输出字符串中第三个至第五个之间的字符串
10+
print(str[2:]) # 输出从第三个字符开始的字符串
11+
print(str * 2) # 输出字符串两次
12+
print(str + "TEST")# 输出连接的字符串
13+
14+
##列表
15+
##加号(+)是列表连接运算符,星号(*)是重复操作
16+
##列表中的值得分割也可以用到变量[头下标:尾下标],就可以截取相应的列表,
17+
##从左到右索引默认0开始的,从右到左索引默认-1开始,下标可以为空表示取到头或尾。
18+
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
19+
tinylist = [123, 'john']
20+
21+
print(list) # 输出完整列表
22+
print(list[0]) # 输出列表的第一个元素
23+
print(list[1:3]) # 输出第二个至第三个的元素
24+
print(list[2:]) # 输出从第三个开始至列表末尾的所有元素
25+
print(tinylist * 2) # 输出列表两次
26+
print(list + tinylist) # 打印组合的列表
27+
28+
##元组
29+
##元组是不允许更新的。而列表是允许更新
30+
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
31+
tinytuple = (123, 'john')
32+
33+
print(tuple) # 输出完整元组
34+
print(tuple[0]) # 输出元组的第一个元素
35+
print(tuple[1:3])# 输出第二个至第三个的元素
36+
print(tuple[2:])# 输出从第三个开始至列表末尾的所有元素
37+
print(tinytuple * 2) # 输出元组两次
38+
print(tuple + tinytuple) # 打印组合的元组
39+
40+
##元字典
41+
##字典用"{ }"标识。字典由索引(key)和它对应的值value组成。
42+
dict = {}
43+
dict['one'] = "This is one"
44+
dict[2] = "This is two"
45+
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
46+
47+
print(dict['one']) # 输出键为'one' 的值
48+
print(dict[2])# 输出键为 2 的值
49+
print(tinydict)# 输出完整的字典
50+
print(tinydict.keys()) # 输出所有键
51+
print(tinydict.values())# 输出所有值
52+
53+
#%%条件语句
54+
# 例1:if 基本用法
55+
flag = False
56+
name = 'luren'
57+
if name == 'python': # 判断变量否为'python'
58+
flag = True # 条件成立时设置标志为真
59+
print('welcome boss') # 并输出欢迎信息
60+
else:
61+
print(name) # 条件不成立时输出变量名称
62+
63+
# 例2:elif用法
64+
num = 5
65+
if num == 3: # 判断num的值
66+
print('boss')
67+
elif num == 2:
68+
print('user')
69+
elif num == 1:
70+
print('worker')
71+
elif num < 0: # 值小于零时输出
72+
print('error')
73+
else:
74+
print('roadman') # 条件均不成立时输出
75+
76+
77+
# 例3:if语句多个条件
78+
num = 9
79+
if num >= 0 and num <= 10: # 判断值是否在0~10之间
80+
print('hello')
81+
82+
num = 10
83+
if num < 0 or num > 10: # 判断值是否在小于0或大于10
84+
print('hello')
85+
else:
86+
print('undefine')
87+
88+
num = 8
89+
# 判断值是否在0~5或者10~15之间
90+
if (num >= 0 and num <= 5) or (num >= 10 and num <= 15):
91+
print('hello')
92+
else:
93+
print('undefine')
94+
# 输出结果
95+
96+
#%%循环语句
97+
count = 0
98+
while (count < 9):
99+
print('The count is:', count)
100+
count = count + 1
101+
102+
# continue 和 break 用法
103+
i = 1
104+
while i < 10:
105+
i += 1
106+
if i%2 > 0: # 非双数时跳过输出
107+
continue
108+
print(i) # 输出双数2、4、6、8、10
109+
110+
i = 1
111+
while 1: # 循环条件为1必定成立
112+
print(i) # 输出1~10
113+
i += 1
114+
if i > 10: # 当i大于10时跳出循环
115+
break
116+
##在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,
117+
##else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,
118+
##while … else 也是一样。
119+
count = 0
120+
while count < 5:
121+
print(count, " is less than 5")
122+
count = count + 1
123+
else:
124+
print(count, " is not less than 5")
125+
126+
for letter in 'Python': # 第一个实例
127+
print('当前字母 :', letter)
128+
129+
fruits = ['banana', 'apple', 'mango']
130+
for fruit in fruits: # 第二个实例
131+
print('当前字母 :', fruit)
132+
133+
##函数 len() 返回列表的长度,即元素的个数。 range返回一个序列的数
134+
fruits = ['banana', 'apple', 'mango']
135+
for index in range(len(fruits)):
136+
print('当前水果 :', fruits[index])
137+
138+
for num in range(10,20): # 迭代 10 到 20 之间的数字
139+
for i in range(2,num): # 根据因子迭代
140+
if num%i == 0: # 确定第一个因子
141+
j=num/i # 计算第二个因子
142+
print('%d 等于 %d * %d' % (num,i,j))
143+
break # 跳出当前循环
144+
else: # 循环的 else 部分
145+
print(num, '是一个质数')
146+
147+
148+
149+
#%% 其他常用函数
150+
celldata = str(celldata)
151+
strnew = celldata.replace('miR', 'MIR') #将miR替换成MIR
152+
strnew = celldata.split('|')
153+
celldata.startswith('MIR') #检查字符串是否是以指定子字符串开头,返回True or False
154+
celldata.strip('\n') #移除字符串头尾指定的字符(默认为空格)
155+
156+
classmates = ['Michael', 'Bob', 'Tracy']
157+
classmates.append('Adam') #For list,末尾添加,没有add()方法
158+
s={1,2,3}
159+
s.add(8) #For set对象,末尾添加,没有append()方法
160+
'strssss'.count('s',2) #从索引为2开始记录匹配字符串的数目
161+
162+
data.insert(0, 'Ones', 1) #在第1列的位置,插入列名为Ones、值为1的列
163+
164+
zip() #对应向量中各取一个元素
165+
predictions = [0,1,1,1,0,0,1,0,1,0,0,1]
166+
y = [1,1,1,0,1,0,0,0,1,1,0,1]
167+
a = zip(predictions,y)
168+
correct = [1 if ((a == 1 and b == 1) or (a == 0 and b == 0)) else 0 for (a, b) in zip(predictions, y)]
169+
[m,n]=zip(*zip(predictions,y))
170+
171+
map()
172+
list(map(lambda x: x**2, [1,3,5,7])) #对每个元素运算function中内容
173+
accuracy = (sum(map(int, correct)) / len(correct)) #%与/的区别?
174+
175+
#*args和**args适用于函数的参数不确定的时候
176+
#*args可以看做是多个变量组成的list,**args可以看做是个字典
177+
def prepare_poly_data(*args, power):
178+
return [x for x in args]
179+
180+
#print()
181+
print('test cost(l={}) = {}'.format(l, J))
182+
print('test cost(l = %s) = %s' % (l, J)) #同上
183+
184+
185+
186+
187+
188+
189+
190+
191+
192+
193+
194+
195+
196+
197+

0 commit comments

Comments
 (0)