1. # from random import randrange
  2. # num = int(input('摇几次骰子: '))
  3. # sides=int(input('筛子有几个面: '))
  4. # sum=0
  5. # for i in range(num):
  6. # sum+= randrange(sides)+1
  7. # print('最终的点数和是 ',sum,'平均点数是:',sum/num)
  8.  
  9. # from random import shuffle
  10. # from pprint import pprint
  11. # values=list(range(1,11))+'Jack Queen King'.split() #并入列表中
  12. # card_suits='diamonds clubs hearts spades'.split()
  13. # value_suit=['{} of {}'.format(v,c) for v in values for c in card_suits]
  14. # shuffle(value_suit) #打乱顺序
  15. # pprint(value_suit[:12])
  16. # while value_suit:
  17. # input(value_suit.pop())
  18.  
  19. f=open('a123.txt','a')
  20. f.write('hello aaaaaaaaaaaaadddddddddddddddddd')
  21. f.close()
  22.  
  23. f=open('a123.txt','r')
  24. for i in range(10):
  25. print(f.readline(),end='')
  26.  
  27. f = open('a123.txt','a')
  28. f.write('This\nis no\nhaikou')
  29. f.close()
  30.  
  31. def process(string):
  32. print('处理中...',string)
  33.  
  34. # with open('a123.txt','r') as f:
  35. # while True:
  36. # line=f.readline()
  37. # if not line:
  38. # break
  39. # process(line)
  40. with open('a123.txt','r') as f:
  41. for line in f:
  42. process(line)
  43.  
  44. with open('a123.txt','r') as f:
  45. for line in f.readlines():
  46. process(line)
  47.  
  48. def triangles():
  49. row = [1]
  50. while True:
  51. yield(row)
  52. row = [1] + [row[k] + row[k + 1] for k in range(len(row) - 1)] + [1]
  53. n = 0
  54. results = []
  55. for t in triangles():
  56. print(t)
  57. results.append(t)
  58. n = n + 1
  59. if n == 10:
  60. break
  61. if results == [
  62. [1],
  63. [1, 1],
  64. [1, 2, 1],
  65. [1, 3, 3, 1],
  66. [1, 4, 6, 4, 1],
  67. [1, 5, 10, 10, 5, 1],
  68. [1, 6, 15, 20, 15, 6, 1],
  69. [1, 7, 21, 35, 35, 21, 7, 1],
  70. [1, 8, 28, 56, 70, 56, 28, 8, 1],
  71. [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
  72. ]:
  73. print('测试通过!')
  74. else:
  75. print('测试失败!')
  76.  
  77. ' a test module '
  78.  
  79. __author__ = 'Michael Liao'
  80.  
  81. import sys
  82.  
  83. def test():
  84. args = sys.argv
  85. if len(args)==1:
  86. print('Hello, world!')
  87. elif len(args)==2:
  88. print('Hello, %s!' % args[1])
  89. else:
  90. print('Too many arguments!')
  91.  
  92. if __name__=='__main__':
  93. test()
  94.  
  95. class Student(object):
  96. pass
  97. bart = Student()
  98. bart.name='jojo'
  99. bart.name
  100.  
  101. class Student(object):
  102. def __init__(self, name, score):
  103. self.name = name
  104. self.score = score
  105.  
  106. def get_grade(self):
  107. if self.score >= 90:
  108. return 'A'
  109. elif self.score >= 60:
  110. return 'B'
  111. else:
  112. return 'C'
  113.  
  114. gg=Student('aaa',100)
  115. gg.get_grade()
  116.  
  117. for c in "python":
  118. if c=='t':
  119. continue
  120. print(c,end=' ')
  121.  
  122. s='python'
  123. while s !='':
  124. for c in s:
  125. print(c,end='')
  126. s=s[:-1]
  127.  
  128. import random
  129. from pprint import pprint
  130. pprint(random.seed(10))
  131. random.random()
  132.  
  133. from random import random
  134. from time import perf_counter
  135. DARTS=1000*10000
  136. hits=0.0
  137. start=perf_counter()
  138. for i in range(1,DARTS+1):
  139. x,y=random(),random()
  140. dist=pow(x**2+y**2,0.5)
  141. if dist <= 1:
  142. hits=hits+1
  143. pi = 4*(hits/DARTS)
  144. print("圆周率值是:{}".format(pi))
  145. print('运行时间是:{:.20f}s'.format(perf_counter()-start))
  146.  
  147. import requests
  148. r=requests.get('http://www.shipxy.com/')
  149. r.status_code
  150. r.text
  151.  
  152. for i in range(1,5):
  153. for j in range(1,5):
  154. for k in range(1,5):
  155. if (i!=j)and(j!=k)and(k!=i):
  156. print(i,j,k)
  157.  
  158. profit = int(input('输入发放的利润值(万元): '))
  159. if 0 <= profit <10:
  160. print('提成为:',profit*0.1,'万元')
  161. if 10 <= profit < 20:
  162. print('提成为:',(profit-10)*0.075+10*0.1,'万元')
  163. if 20 <= profit < 40:
  164. print('提成为:',(profit-20)*0.05+10*0.075+10*0.1,'万元')
  165. if 40 <= profit < 60:
  166. print('提成为:',(profit-40)*0.03+20*0.05+10*0.075+10*0.1,'万元')
  167. if 60 <= profit < 100:
  168. print('提成为:',(profit-60)*0.015+20*0.03+20*0.05+10*0.075+10*0.1,'万元')
  169. if profit >= 100:
  170. print('提成为:',(profit-100)*0.01+40*0.015+20*0.03+20*0.05+10*0.075+10*0.1,'万元')
  171.  
  172. profit = int(input('输入企业的利润值(万元): '))
  173. gap = [100,60,40,20,10,0]
  174. ratio =[0.01,0.015,0.03,0.05,0.075,0.1]
  175. bonus=0
  176. for idx in range(0,6):
  177. if profit >= gap[idx]:
  178. bonus += (profit-gap[idx])*ratio[idx]
  179. profit=gap[idx]
  180. print('提成为:',bonus,'万元')
  181.  
  182. profit = int(input('输入企业的利润值(万元): '))
  183. def get_bonus(profit):
  184. bonus = 0
  185. if 0 <= profit <= 10:
  186. bonus = 0.1*profit
  187. elif (profit > 10) and (profit <= 20):
  188. bonus = (profit-10)*0.075 + get_bonus(10)
  189. elif (profit > 20) and (profit <= 40):
  190. bonus = (profit-20)*0.05 + get_bonus(20)
  191. elif (profit > 40) and (profit <= 60):
  192. bonus = (profit-40)*0.03 + get_bonus(40)
  193. elif (profit > 60) and (profit <= 100):
  194. bonus = (profit-60)*0.015 + get_bonus(60)
  195. elif (profit >100):
  196. bonus = (profit-100)*0.01 + get_bonus(100)
  197. else:
  198. print("利润输入值不能为负")
  199. return bonus
  200.  
  201. if __name__ == '__main__':
  202. print('提成为:',get_bonus(profit),'万元')
  203.  
  204. '''
  205. 分析:
  206. x + 100 = m^2
  207. x + 100 + 168 = n^2
  208. n^2 - m^2 = 168
  209. (n + m) * (n - m) = 168
  210. n > m >= 0
  211. n - m 最小值为 1
  212. n + m 最大为 168
  213. n 最大值为 168
  214. m 最大值为 167
  215. '''
  216.  
  217. def _test():
  218. for m in range(0, 168):
  219. for n in range(m + 1, 169):
  220. #print('n=%s,m=%s' % (n, m))
  221. if (n + m) * (n - m) == 168:
  222. print("该数为:" + str(n * n - 168 - 100))
  223. print("该数为:" + str(m * m - 100))
  224. print('n为%s,m为%s' % (n, m))
  225. if __name__ == '__main__':
  226. _test()
  227.  
  228. def test1():
  229. for n in range(0,168):
  230. for m in range(n,169):
  231. if (m+n)*(m-n) == 168:
  232. print("这个整数是: ",str(n*n-100))
  233. if __name__ =='__main__':
  234. test1()
  235.  
  236. import pandas as pd
  237. df = pd.read_csv(r'c:\Users\clemente\Desktop\all\train.csv',index_col='Id')
  238. df.head()
  239.  
  240. for i in range(0,7):
  241. for j in range(0,7):
  242. for k in range(0,7):
  243. for g in range(0,7):
  244. for h in range(0,7):
  245. while (i!=j) and(i!=g) and(g!=h)and(h!=k)and(k!=i):
  246. if (i+j+k+g+h)==15:
  247. print (i,j,k,g,h)
  248.  
  249. import random
  250. def gen5num():
  251. alldigit=[0,1,2,3,4,5,6,0]
  252. first=random.randint(0,6) #randint包含两端,0和6
  253. alldigit.remove(first)
  254. second=random.choice(alldigit)
  255. alldigit.remove(second)
  256. third=random.choice(alldigit)
  257. alldigit.remove(third)
  258. forth=random.choice(alldigit)
  259. alldigit.remove(forth)
  260. fiveth=random.choice(alldigit)
  261. alldigit.remove(fiveth)
  262. if (first+second+third+forth+fiveth)==15:
  263. return first,second,third,forth,fiveth
  264. if __name__=='__main__':
  265. for i in range(100):
  266. print(gen5num())
  267.  
  268. #!/usr/bin/env python3
  269. #coding=utf-8
  270.  
  271. from itertools import permutations
  272. t = 0
  273. for i in permutations('',5):
  274. print(''.join(i))
  275. t += 1
  276.  
  277. print("不重复的数量有:%s"%t)
  278.  
  279. def sum_1():
  280. """
  281. aaaddd
  282. """
  283. for i in '':
  284. p += int(i)
  285. print(sum(p))
  286. sum_1()
  287.  
  288. np.*load*?
  289.  
  290. #题目:数组中找出两个元素之和 等于给定的整数
  291.  
  292. # 思路:
  293. # 1、将数组元素排序;
  294. # 2、array[i]与a[j](j的取值:i+1到len_array-1) 相加;
  295. # 3、如两两相加<整数继续,如=整数则输出元素值;
  296. # 4、如>则直接退出,i+1 开始下一轮相加比较
  297.  
  298. def addData(array, sumdata):
  299. """
  300. aaaadddd
  301. """
  302. temp_array = array
  303. temp_sumdata = sumdata
  304. print ("sumdata: {}".format(temp_sumdata))
  305. sorted(temp_array)
  306. len_temp_array = len(temp_array)
  307.  
  308. # 计数符合条件的组数
  309. num = 0
  310.  
  311. for i in range(0, len_temp_array-1):
  312. for j in range(i+1, len_temp_array):
  313. for k in range(j+1,len_temp_array):
  314. if temp_array[i] + temp_array[j] + temp_array[k] < temp_sumdata:
  315. continue
  316. elif temp_array[i] + temp_array[j] + temp_array[k] == temp_sumdata:
  317. num += 1
  318. print("Group {} :".format(num))
  319. print("下标:{}, 元素值: {}".format(i, temp_array[i]))
  320.  
  321. else:
  322. break
  323.  
  324. if __name__=="__main__":
  325. test_array = [0,1,2,3,4,5,6,0]
  326. test_sumdata = 4
  327. addData(test_array, test_sumdata)
  328.  
  329. #题目:数组中找出两个元素之和 等于给定的整数
  330.  
  331. # 思路:
  332. # 1、将数组元素排序;
  333. # 2、array[i]与a[j](j的取值:i+1到len_array-1) 相加;
  334. # 3、如两两相加<整数继续,如=整数则输出元素值;
  335. # 4、如>则直接退出,i+1 开始下一轮相加比较
  336.  
  337. import numpy as np
  338. names=np.array(['Bob','Joe','Will','Bob','Will','Joe','Joe'])
  339. data=np.random.randn(7,4)
  340. names
  341. data
  342. names == 'Bob'
  343. data[names=='Bob']
  344.  
  345. arr[[4,3,0,6]]
  346.  
  347. import matplotlib.pyplot as plt
  348. points = np.arange(-5,5,0.01)
  349. xs,ys=np.meshgrid(points,points)
  350. z=np.sqrt(xs**2+ys**2)
  351.  
  352. plt.imshow(z,cmap=plt.cm.gray)
  353. plt.colorbar()
  354. plt.title("图像 $\sqrt{x^2+y^2}$")
  355.  
  356. import pandas as pd
  357. obj=pd.Series(range(3),index=["a","b","c"])
  358. index=obj.index
  359. index[1]='d'
  360. import numpy as np
  361. import pandas as pd
  362. data=pd.DataFrame(np.arange(16).reshape(4,4),index=[1,2,3,4],columns=["one","two","three","forth"])
  363. data<3
  364.  
  365. df1=pd.DataFrame({"A":[1,2]})
  366. df1
  367.  
  368. obj=pd.Series(["a","a","b","c"]*4)
  369. obj
  370. obj.describe()
  371.  
  372. import json
  373. result = json.loads(obj)
  374. result
  375.  
  376. import pandas as pd
  377. ages=[12,34,23,45,67,30,20,55,98,30,43]
  378. bins=[1,20,30,40,50,100]
  379. cats=pd.cut(ages,bins)
  380. cats
  381. cats.codes
  382. pd.value_counts(cats)
  383.  
  384. DataF=pd.DataFrame(np.arange(5*4).reshape((5,4)))
  385. DataF
  386. sample_1=np.random.permutation(5*4)
  387. sample_1.reshape(5,4)
  388.  
  389. df=pd.DataFrame({'key':['b','b','a','c','a','b'],'data1':range(6)})
  390. df
  391. df[["data1"]]
  392.  
  393. import pandas as pd
  394. left=pd.DataFrame({'key1':['foo','foo','bar'],'key2':['one','two','one'],'lval':[1,2,3]})
  395. right=pd.DataFrame({'key1':['foo','foo','bar','bar'],'key2':['one','one','one','two'],'rval':[4,5,6,7]})
  396. pd.merge(left,right,on=['key1'])
  397.  
  398. import matplotlib.pyplot as plt
  399. import numpy as np
  400. data=np.arange(10000)
  401. plt.plot(data)
  402.  
  403. fig=plt.figure()
  404. ax1=fig.add_subplot(2,2,1)
  405. ax2=fig.add_subplot(2,2,2)
  406. ax3=fig.add_subplot(2,2,3)
  407.  
  408. ax1.hist(np.random.randn(100),bins=20,color='k',alpha=0.5)
  409. ax2.scatter(np.arange(30),np.arange(30)+3*np.random.randn(30))
  410. ax3.plot(np.random.randn(50).cumsum(),drawstyle='steps-post')
  411.  
  412. fig=plt.figure()
  413. ax=fig.add_subplot(1,1,1)
  414. rect=plt.Rectangle((0.5,0.8),0.4,0.4,color='g',alpha=0.4)
  415. ax.add_patch(rect)
  416.  
  417. plt.savefig("真的.svg",bbox_inches='tight')
  418.  
  419. s=pd.Series(np.random.randn(10).cumsum())
  420. s.plot()
  421.  
  422. s=pd.Series(np.random.randn(10).cumsum(),index=np.arange(0,100,10))
  423. s.plot()
  424.  
  425. df=pd.DataFrame(np.random.randn(10,4).cumsum(0),columns=['A','B','C','D'],index=np.arange(0,100,10))
  426. df.plot()
  427.  
  428. fig,axes=plt.subplots(2,1)
  429. data=pd.Series(np.random.rand(16),index=list("abcdefghijklmnop"))
  430. data.plot.bar(ax=axes[0],color='k',alpha=0.7)
  431. data.plot.barh(ax=axes[1],color='g',alpha=0.7)
  432. plt.show()
  433.  
  434. df=pd.DataFrame(np.random.rand(6,4),index=['one','two','three','four','five','six'],columns=pd.Index(['A','B','C','D'],name='Genus'))
  435. df
  436. df.plot.bar()
  437. df.plot.barh(stacked=True,alpha=0.5)
  438.  
  439. tips=pd.read_csv('tips.csv')
  440. party_counts = pd.crosstab(tips['day'],tips['size'])
  441. party_counts
  442. party_counts=party_counts.loc[:,2:5]
  443. party_counts
  444.  
  445. party_counts.sum(1)
  446.  
  447. party_pcts= party_counts.div(party_counts.sum(1),axis=0)
  448. party_pcts.plot.bar()
  449.  
  450. import seaborn as sns
  451. tips=pd.read_csv('tips.csv')
  452. tips['tip_pct']=tips['tip']/(tips['total_bill']-tips['tip'])
  453. tips.head()
  454. sns.barplot(x='tip_pct',y='day',data=tips,orient='h')
  455. sns.barplot(x='tip_pct',y='day',hue='time',data=tips,orient='h')
  456. sns.set(style='whitegrid')
  457.  
  458. tips['tip_pct'].plot.hist(bins=50)
  459. tips['total_bill'].plot.hist(bins=50)
  460.  
  461. tips['tip_pct'].plot.density()
  462. tips['total_bill'].plot.density()
  463.  
  464. comp1=np.random.normal(0,1,size=200)
  465. comp2=np.random.normal(10,2,size=200)
  466. values=pd.Series(np.concatenate([comp1,comp2]))
  467. sns.distplot(values,bins=101,color='k')
  468.  
  469. macro=pd.read_csv('macrodata.csv')
  470. data=macro[['cpi','m1','tbilrate','unemp']]
  471. trans_data=np.log(data).diff().dropna()
  472. trans_data.head()
  473. trans_data[-5:]
  474.  
  475. sns.regplot("m1","unemp",data=trans_data)
  476. plt.title('Changes in log {} versus log {}'.format('m1','unemp'))
  477. sns.set(style="ticks", color_codes=True)
  478. sns.pairplot(trans_data,diag_kind='kde',kind='reg')
  479. sns.pairplot(trans_data,diag_kind='hist',kind='reg')
  480.  
  481. sns.factorplot(x='day',y='tip_pct',row='time',hue='smoker',kind='box',data=tips[tips.tip_pct<0.5])
  482.  
  483. tips.describe()
  484.  
  485. import matplotlib.pyplot as plt
  486. import pandas as pd
  487. import numpy as np
  488. df=pd.DataFrame({'key1':['a','a','b','b','a'],'key2':['one','two','one','two','one'],'data1':np.random.randn(5),'data2':np.random.randn(5)})
  489. df
  490.  
  491. group_1=df['data1'].groupby(df['key1'])
  492. group_1.describe()
  493. group_2=df['data1'].groupby([df['key1'],df['key2']]).mean()
  494. group_2
  495.  
  496. states=np.array(['Ohio','California','California','Ohio','Ohio'])
  497. years=np.array([2005,2005,2006,2005,2006])
  498. df['data1'].groupby([states,years]).mean()
  499.  
  500. dict(list(df.groupby('key1')))
  501.  
  502. try:
  503. year=input("输入年份:")
  504. month=input("输入月份: ")
  505. day=input("输入日期号: ")
  506. finally:
  507. print("正在计算")
  508.  
  509. months2days=[0,31,59,90,120,151,181,212,243,273,304,334]
  510. # 闰年
  511. if int(year) % 4 ==0:
  512. for i in range(2,12,1):
  513. months2days[i] +=1
  514.  
  515. month_index=[]
  516. for j in range(12):
  517. month_index.append(i+1)
  518. dict_md=dict(zip(month_index,months2days))
  519. whichday=dict_md[int(month)]+int(day)
  520. print('结果是: 第{}天'.format(whichday))
  521.  
  522. def unsortedSearch(list, i, u):
  523. found = False
  524. pos = 0
  525. pos2 = 0
  526.  
  527. while pos < len(list) and not found:
  528. if int(list[pos]) < int(u) :
  529. if int(list[pos2]) > int(i):
  530. found = True
  531. pos2 = pos2 + 1
  532. pos = pos + 1
  533. return found
  534.  
  535. unsortedList = ['', '', '', '', '', '', '', '', '', '']
  536. num1 = ''
  537. num2 = ''
  538.  
  539. isItThere = unsortedSearch(unsortedList, num1, num2)
  540.  
  541. if isItThere:
  542. print ("There is a number between those values")
  543. else:
  544. print ("There isn't a number between those values")
  545.  
  546. def get_nums():
  547. nums=[]
  548. n=int(input("一共有几个整数?"))
  549. for i in range(n):
  550. x=int(input('请按次随机输入第{}个整数(剩余{}次输入):'.format(i+1,n-i)))
  551. nums.append(x)
  552. return nums
  553. if __name__=='__main__':
  554. list_nums=get_nums()
  555.  
  556. def BubbleSort(nums): #冒泡法
  557. print('初始整数集合为:{}'.format(nums))
  558. for i in range(len(nums)-1):
  559. for j in range(len(nums)-i-1):
  560. if nums[j]>nums[j+1]:
  561. nums[j],nums[j+1]=nums[j+1],nums[j] #调换位置,相互赋值
  562. print("第{}次迭代排序结果:{}".format((len(nums)-j-1),nums))
  563. return nums
  564. if __name__=='__main__':
  565. print('经过冒泡法排序最终得到:{}'.format(BubbleSort(list_nums)))
  566.  
  567. def get_nums():
  568. nums=[]
  569. n=int(input("一共有几个整数?"))
  570. for i in range(n):
  571. x=int(input('请按次随机输入第{}个整数(剩余{}次输入):'.format(i+1,n-i)))
  572. nums.append(x)
  573. return nums
  574. if __name__=='__main__':
  575. myList=get_nums()
  576.  
  577. def selectedSort(myList):
  578. #获取list的长度
  579. length = len(myList)
  580. #一共进行多少轮比较
  581. for i in range(0,length-1):
  582. #默认设置最小值得index为当前值
  583. smallest = i
  584. #用当先最小index的值分别与后面的值进行比较,以便获取最小index
  585. for j in range(i+1,length):
  586. #如果找到比当前值小的index,则进行两值交换
  587. if myList[j]<myList[smallest]:
  588. tmp = myList[j]
  589. myList[j] = myList[smallest]
  590. myList[smallest]=tmp
  591. #打印每一轮比较好的列表
  592. print("Round ",i,": ",myList) #根据第一个i循环进行打印,而不是选j循环
  593.  
  594. print("选择排序法:迭代过程 ")
  595. selectedSort(myList)
  596.  
  597. def merge_sort(LIST):
  598. start = []
  599. end = []
  600. while len(LIST) > 1:
  601. a = min(LIST)
  602. b = max(LIST)
  603. start.append(a)
  604. end.append(b)
  605. LIST.remove(a)
  606. LIST.remove(b)
  607. if LIST:
  608. start.append(LIST[0])
  609. end.reverse()
  610. return (start + end)
  611.  
  612. if __name__=='__main__':
  613. nums=[]
  614. n=int(input('一共几位数: '))
  615. for i in range(n):
  616. x=int(input("请依次输入整数:"))
  617. nums.append(x)
  618. print(merge_sort(nums))
  619.  
  620. # =============================================================================
  621. #10.1.2
  622. # =============================================================================
  623. import pandas as pd
  624. df=pd.DataFrame({'key1':['a','a','b','b','a'],'key2':['one','two','one','two','one'],'data1':np.random.randn(5),'data2':np.random.randn(5)})
  625. df
  626. df.groupby(['key1','key2'])['data1'].mean()
  627.  
  628. people=pd.DataFrame(np.random.randn(5,5),columns=['a','b','c','d','e'],index=['joe','steve','wes','jim','travis'])
  629. people
  630. mapping={'a':'red','b':'red','c':'blue','d':'blue','e':'red','f':'orange'}
  631. by_column=people.groupby(mapping,axis=1)
  632. by_column.mean()
  633. map_series=pd.Series(mapping)
  634.  
  635. people.groupby(len).sum()
  636.  
  637. # =============================================================================
  638. # 分组加权
  639. # =============================================================================
  640.  
  641. import pandas as pd
  642. df=pd.DataFrame({'目录':['a','a','a','a','b','b','b','b'],'data':np.random.randn(8),'weights':np.random.randn(8)})
  643. df
  644. grouped=df.groupby('目录')
  645. get_weighpoint=lambda x: np.average(x['data'],weights=x['weights'])
  646. grouped.apply(get_weighpoint)
  647.  
  648. # =============================================================================
  649. #
  650. # =============================================================================
  651.  
  652. spx=pd.read_csv('stock_px_2.csv',index_col=0,parse_dates=True)
  653. spx
  654. spx.info()
  655.  
  656. from datetime import datetime
  657.  
  658. datestrs=['7/6/2011','8/6/2011']
  659. [datetime.strptime(x,'%m/%d/%Y')for x in datestrs]
  660.  
  661. dates=pd.date_range('1/1/2018',periods=1000)
  662. dates
  663. long_df=pd.DataFrame(np.random.randn(1000,4),index=dates,columns=['Colorado','Texas','New York','Ohio'])
  664. long_df
  665.  
  666. pd.date_range('2018-10-1',periods=30,freq='1h')
  667.  
  668. # =============================================================================
  669. #
  670. # =============================================================================
  671. close_px_all=pd.read_csv("stock_px_2.csv",parse_dates=True,index_col=0)
  672. close_px=close_px_all[['AAPL','MSFT','XOM']]
  673. close_px=close_px.resample("B").ffill()
  674. close_px.AAPL.plot()
  675. close_px.AAPL.rolling(250).mean().plot()
  676.  
  677. import pandas as pd
  678. import numpy as np
  679. values=pd.Series(['apple','orange','apple','apple']*2)
  680. values
  681. pd.unique(values)
  682. pd.value_counts(values)
  683.  
  684. import pandas as pd
  685. import matplotlib.pyplot as plt
  686. from sklearn.linear_model import RANSACRegressor, LinearRegression, TheilSenRegressor
  687. from sklearn.metrics import explained_variance_score, mean_absolute_error, mean_squared_error, median_absolute_error, r2_score
  688. from sklearn.svm import SVR
  689. from sklearn.linear_model import Ridge,Lasso,ElasticNet,BayesianRidge
  690. from sklearn.ensemble import GradientBoostingRegressor
  691. from sklearn.cross_validation import train_test_split
  692.  
  693. data = pd.read_csv('../cement_data.csv')
  694. # 查看数据记录的长度,共1030行
  695. print(len(data))
  696. # 查看前五行数据
  697. data.head()
  698.  
  699. import pandas
  700. titanic=pandas.read_csv('train.csv')
  701. titanic.head()
  702. titanic.describe()
  703. titanic['Age']=titanic['Age'].fillna(titanic['Age'].median())
  704. print(titanic['Sex'].unique()) #找Sex特征里的分类字符名,只有两种可能性
  705. titanic.loc[titanic['Sex']=='female','Sex']=1#把分类字符名转换成整数1,0形式,进行标记
  706. titanic.loc[titanic['Sex']=='male','Sex']=0
  707. #对embarked 登船地 进行填充(按最多标记)
  708. print(titanic['Embarked'].unique())
  709. titanic['Embarked']=titanic['Embarked'].fillna('S')
  710. titanic.loc[titanic['Embarked']=='S']=0
  711. titanic.loc[titanic['Embarked']=='C']=1
  712. titanic.loc[titanic['Embarked']=='Q']=2
  713.  
  714. # =============================================================================
  715. # 引进模型,线性回归
  716. # =============================================================================
  717. from sklearn.linear_model import LinearRegression
  718. from sklearn.cross_validation import KFold
  719. #cross_validation 交叉验证,进行调参,训练数据集分成三份,三份做交叉验证
  720.  
  721. predictors=['Pclass','Sex','Age','SibSp','Parch','Fare','Embarked'] #需要输入并做预测的特征列
  722. alg=LinearRegression()
  723. kf=KFold(titanic.shape[0],n_folds=3,random_state=1) #shape[0]一共有多少行,random_state=1 随机种子开启,n_fold=3把训练集分为三份
  724.  
  725. predictions=[]
  726. for train,test in kf:
  727. train_predictors=titanic[predictors].iloc[train,:] #交叉验证中,除开训练的部分
  728. train_target=titanic['Survived'].iloc[train]#获取目标训练集
  729. alg.fit(train_predictors,train_target) #依据模型,训练
  730.  
  731. test_predictions=alg.predict(titanic[predictors].iloc[test,:]) #测试集
  732. predictions.append(test_predictions)
  733.  
  734. import numpy as np
  735. predictions=np.concatenate(predictions,axis=0)
  736. # 整理输出值,按照可能性分类到0,1
  737. predictions[predictions>=0.5]=0
  738. predictions[predictions<0.5]=1
  739. accuracy=sum(predictions[predictions==titanic['Survived']])/len(predictions)
  740. print(accuracy)
  741.  
  742. # =============================================================================
  743. # 逻辑回归
  744. # =============================================================================
  745. from sklearn import cross_validation
  746. from sklearn.linear_model import LogisticRegression
  747. alg=LogisticRegression(random_state=1)
  748. scores=cross_validation.cross_val_score(alg,titanic[predictors],titanic['Survived'],cv=3)
  749. print(scores.mean())
  750.  
  751. # =============================================================================
  752. # 随机森林
  753. # =============================================================================
  754. from sklearn import cross_validation
  755. from sklearn.ensemble import RandomForestClassifier
  756. predictors=['Pclass','Sex','Age','SibSp','Parch','Fare','Embarked']
  757. alg=RandomForestClassifier(random_state=1,n_estimators=10,min_samples_split=2,min_samples_leaf=1)
  758. kf=cross_validation.KFold(titanic.shape[0],n_folds=3,random_state=1)
  759. scores=scores=cross_validation.cross_val_score(alg,titanic[predictors],titanic['Survived'],cv=kf)
  760. print(scores.mean())

[Python] 练习代码的更多相关文章

  1. Python一行代码

    1:Python一行代码画出爱心 print]+(y*-)**-(x**(y*<= ,)]),-,-)]) 2:终端路径切换到某文件夹下,键入: python -m SimpleHTTPServ ...

  2. python爬虫代码

    原创python爬虫代码 主要用到urllib2.BeautifulSoup模块 #encoding=utf-8 import re import requests import urllib2 im ...

  3. Python小代码_2_格式化输出

    Python小代码_2_格式化输出 name = input("name:") age = input("age:") job = input("jo ...

  4. Python小代码_1_九九乘法表

    Python小代码_1_九九乘法表 max_num = 9 row = 1 while row <= max_num: col = 1 while col <= row: print(st ...

  5. Python IDLE 代码高亮主题

    Python IDLE 代码高亮主题 使用方法: 打开C盘我的 C:\Documents and Settings\你的用户名.idlerc文件夹 里面会有一个 config-highlight.cf ...

  6. 代码块: 以冒号作为开始,用缩进来划分作用域,这个整体叫做代码块,python的代码块可以提升整体的整齐度,提高开发效率

    # ### 代码块: 以冒号作为开始,用缩进来划分作用域,这个整体叫做代码块 if 5 == 5: print(1) print(2) if True: print(3) print(4) if Fa ...

  7. uiautomatorviewer 优化定位符生成,支持生成Java,Python自动化代码

    项目介绍 二次开发 uiautomatorviewer 优化定位符生成,支持生成Java,Python自动化代码,修复自带工具画面有动态加载时截图失败问题,优化自带工具截图速度 ,实现类似录制脚本功能 ...

  8. Python实现代码统计工具——终极加速篇

    Python实现代码统计工具--终极加速篇 声明 本文对于先前系列文章中实现的C/Python代码统计工具(CPLineCounter),通过C扩展接口重写核心算法加以优化,并与网上常见的统计工具做对 ...

  9. Python静态代码检查工具Flake8

    简介 Flake8 是由Python官方发布的一款辅助检测Python代码是否规范的工具,相对于目前热度比较高的Pylint来说,Flake8检查规则灵活,支持集成额外插件,扩展性强.Flake8是对 ...

  10. 【Python】《大话设计模式》Python版代码实现

    <大话设计模式>Python版代码实现 上一周把<大话设计模式>看完了,对面向对象技术有了新的理解,对于一个在C下写代码比较多.偶尔会用到一些脚本语言写脚本的人来说,很是开阔眼 ...

随机推荐

  1. 面试官:"谈谈分库分表吧?"

    原文链接:面试官:"谈谈分库分表吧?" 面试官:“有并发的经验没?”  应聘者:“有一点.”   面试官:“那你们为了处理并发,做了哪些优化?”   应聘者:“前后端分离啊,限流啊 ...

  2. javascript 点击触发复制功能

    摘要: js调用复制功能使用: document.execCommand("copy", false); document.execCommand()方法功能很强大,了解更多请戳: ...

  3. 1,JavaScript前世今生

    JavaScript历史大概在1992年,一家称作Nombas的公司开始开发一种叫做C–(C-minus-minus,简称Cmm)的嵌入式脚本语言. Cmm背后的理念很简单:一个足够强大可以替代宏操作 ...

  4. margin:0 auto是什么意思

    一.margin设置对象外边距 二.margin后面如果只有两个参数的话,第一个表示top和bottom,第二个表示left和right 因为0 auto

  5. K8S 通过 yaml 文件创建资源

    创建 pod cd ~ vi pod-demo.yaml # 内容如下 apiVersion: v1 kind: Pod metadata: name: pod-demo namespace: def ...

  6. C#基础(203)实例方法和重载方法总结,构造方法与实例方法总结,this关键字

    c#方法的重载:分为实例方法重载和静态方法重载俩种 1.实例方法重载的调用特点 首先写三个Add方法和三个Sub方法 public int Add(int a,int b) { return a + ...

  7. Ubuntu、deepin 支持 yum

    1,首先检测是否安装了build-essential程序包 sudo apt-get install build-essential 2,安装 yum sudo apt-get yum 3,检测是否安 ...

  8. Android getprop 读取的属性哪里来的?

    Android  getprop 和  setprop 可以对系统属性进行读取和设置. 通过串口执行以下 geyprop    打印出来的属性让你一目了然. 属性出来了,但是在哪里设置的呢,这里有两个 ...

  9. Django 登录验证-自动重定向到登录页

    Web项目有些场景需要做用户登录验证,以便访问不同页面. 方法一:login_required装饰器 适用于函数视图. from django.contrib.auth.decorators impo ...

  10. Android 官方DEMO - ActionBarCompat-Basic

    ActionBarCompat-Basic Demo下载地址:https://github.com/googlesamples/android-ActionBarCompat-Basic/#readm ...