=Logisticregressionwithlbfgs.train (parseddata)#evaluating the model on training data evaluates the error on the training setLabelsandpreds = Parseddata.map (LambdaP: (P.label, Model.predict (p.features))) Trainerr= Labelsandpreds.filter (LambdaLP:LP[0]! = lp[1]). COUNT ()/Float (parseddata.count ())Print("Training Error ="+ str (TRAINERR))#Training Error = 0.366459627329#Save and load model saving models and loading modelsModel.save (SC,"Pythonlogisticregressionwithlbfgsmodel") Samemodel= Logi
The Python introductory tutorial has been completed, following is the completion script: (part of the source code in the book, part of the exercise of their own)
#写一个文本统计的脚本: Calculates and prints statistical data about a text file, include the number of characters, lines, and words in the file, and the first 10 occurrences of the most occurrences of the words in order import time keep=[' A ', ' B ', ' C '
Recently in Python data statistics, here summarizes some recent use to find and summarize some of the tips, hoping to help in doing this when some of the children's shoes. Some techniques are very common usage and we don't notice them at ordinary times, but in certain scenarios, these small methods can be very helpful.
1. Map keys to multiple values in the dictionary
{' B ': [4, 5, 6], ' A ': [1, 2, 3]}
S
This example shows the number of occurrences of duplicates in the Python statistics list, and is a useful feature for beginners in Python to learn from. Here's how:
For a list, such as [1,2,2,2,2,3,3,3,4,4,4,4], now we need to count the duplicates in this table and repeat them several times.
Method 1:
MyList = [1,2,2,2,2,3,3,3,4,4,4,4]myset = set (mylist) #myse
[list_all.index (S[x]) at the corresponding position in List_statistic= List_to_statistic[list_all.index (s[x])]+1#print List_all.index (s[x]),#PrintList_all=Tran_s_to_list (s)#copy one and list equal length array list_statistic, and assign all values to 0List_to_statistic =list_all[:] forXinchxrange (0, Len (list_all)): List_to_statistic[x]=0statistic (S, List_all, list_to_statistic);#PrintListlength =Len (list_all) forXinchxrange (0, listlength):PrintSTR (list_all[x]) +""+"---appers---"+str (L
"debugger." A debugger is like doing a full-body scan on a sick person. YouDon't get any specific useful information, and you find a whole lot of information thatDoesn ' t help and is just confusing.2. The best-of-the-to-debug a program is-use print-to-print out the values of variables atPoints in the program to see where they go wrong.3. Make sure parts of the your programs work as your work on them. Do not write massive filesof code before you try to run them. Code a little, run a little, fix
The principle and the previous chapter to get the same, just a change of the content of the analysis.Code:#-*-coding:utf-8-*-import urllib2import redef getpagecontent (page_url,heads): Try:req = Urllib2. Request (page_url,headers=heads) resp = Urllib2.urlopen (req) return Resp.read (). Decode (' UTF8 ') except Exce Ption, E:print "Request [%s] error. "% (Page_url), E return" "Def gettopnotes (cont): Strre = '. *?Output:The secret 4820=== of C:\Python27\python.exe f:/srccode/
A = "Aasmr3idd4bgs7dlsf9eaf"Please take the number of a string out and output it as a new string.Please count the occurrences of each letter of a string (ignoring case, a and a are the same letter) and outputting it as a dictionary. Example {' A ': 3, ' B ': 1}Remove the letters that appear multiple times in a string, leaving only the first occurrence of the letter, case insensitive. Example ' Aasmr3idd4bgs7dlsf9eaf ', after removal, output ' asmr3id4bg7lf9e 'A ="Aasmr3idd4bgs7dlsf9eaf"defFun1_2
,'R') as F: +Lines =F.readlines () A forSinchlines: at " "Do not count into blank lines" " - ifs = ='\ n': - Continue -n + = 1 - exceptFilenotfounderror as E: - Print('filename or file path error, no file! ') inOs.system ('Pause') -Os._exit (1) to exceptOSError as E: + Print('file name is not valid') -Os.system ('Pause') theOs._exit (1) * Print(Time.strftime ('%y-%m-%d%h:%m:%s', Time.localtime (Time.time ())
Import Jiebatxt = open (' C:/users/eternal/desktop/threekingdoms.txt ', ' R ', encoding= ' UTF-8 '). Read () #提前修改txt文件编码格式utf-8excludes = {' General ', ' but said ', ' Jingzhou ', ' two ', ' not ', ' can't ', ' so '} #错误的名字Words = jieba.lcut (TXT)print (words)Counts = {}For word in words: If Len (word) = = 1: Continue elif Word = = ' Zhuge Liang ' or word = = ' Hung Ming Yue ': RWord = ' Hung Ming ' elif Word = = ' Guan Gong ' or word = = ' cloud length ': RWord =
#!/usr/bin/env python#coding=utf-8# Phone Book management, you can add, delete, modify, query user information import Cpickle as P class Telephone:def __init__ (self): "Construction Method" Def addpeople (Self,name,email,telephone): "" "Add User" "" Teledict = Self.getdictda Ta () if teledict:infolist = [Name,email,telephone] teledict[name] = infolist Self.writefile (teledict) else:teledict
memory - :p Aram S_strfile: - : return: - """ - #com = re.compile (' in #'. *? - #two rows and one line of effect to #com = re.compile ( + #' - #Re. S) the #above is to build the regular through method compile into an object * $ #try to get it all out by findall, but it takes up a lot of memory, so find the result store, plan to build a generator, take one at a timePanax Notoginseng #page = Com.findall (s_strfile) - #Print (page) the +ret = Patter
Jingdong Book review has a very rich information, which contains the date of purchase, the title, author, Praise, in the evaluation, the difference between the evaluation and so on. Take the purchase date as an example, using Python + MySQL with the implementation of the program is not large, only 100 lines. I have raised the relevant explanations in the program:
From selenium import WebdriverFrom BS4 impo
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.