使用CPU多核进行并行运算 import multiprocessingdef function(x): return(x * x) arg_list = [1, 3, 5, 7]cores = multiprocessing.cpu_count()pool = multiprocessing.Pool(processes=cores)for result in pool.imap(functi...
Machine learning series: Handling Missing Values The method cann’t deal with ‘object’ type dataExplore missing valuesmissing_val_count_by_column = (data.isnull().sum())print(missing_val_count_by_column[missing_val_count_by_column > 0])A simp...
Machine learning series:How to handle categorical variable One hot encodingone hot encoding creates new (binary) columns, indicating the presence of each possible value from the original data.## explore the data typeprint(train_data.dtypes)## one hot enco...
机器学习——入门 import pandas as pdfile_path = ''name_data = pd.read_csv(file_path)name_data.describe() ## describe function can tell you the information of each column in data, such as count(how manty rows have n...
Pat乙级1031题——查验身份证(Python) 注意使用多层循环,在打断的时候要注意确保不再执行之后不需要循环的地方代码如下def idCard(): weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] mList = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'] ...
Pat乙级1030题—— 完美数列(Python) 注意完美数列不一定要从给出的数列最小的一项开始,而是找尽可能的包含最多数的完美数列。这个代码用python2实现会出现一个测试点超时,用python3没有问题。代码如下# -*- coding: UTF-8 -*-def perfectSequence(): N, q= map(int, input().split()) numList = sorted(...
Pat乙级1029题——旧键盘(Python) 代码如下def oldKeyboard(): standStr = raw_input() inputStr = raw_input() result = [] i, j = 0, 0 while i < len(standStr): if standStr[i] == inputStr[j]: if j...
Pat乙级1028题——人口普查(Python)一个运行超时 注意考虑排除不合理年龄后,人数为0的情况代码如下def popQuery(): count = int(raw_input()) info = [] for i in range(count): personInfo = raw_input().split(' ') personInfo[1] = map(int, perso...
Pat乙级1027题——打印沙漏(Python) 注意一行打印空格和符号时,不能用逗号连接(会多出一个空格)代码如下# -*- coding:UTF-8 -*-def hourglassPrint(): content = raw_input().split(' ') if content[0] == 0: print 0 return content[0] = in...
Pat乙级1026题——程序运行时间(Python) 注意注意不足1秒的时间要四舍五入到秒,我使用的方法是round()时间输出注意高位补零,即%02d代码如下def time(): content = map(int, raw_input().split(' ')) CLK_TCK = 100 if content[0] >= content[1]: return allSec...
Pat乙级1025题——反转链表(Python)一个非零返回,一个运行超时 注意如果上一次有反转,那么要将上一次最后一个数据的next改成本次反转后的开始的地址测试结果为21分#!/usr/bin/python# -*- coding: UTF-8 -*-def reverseList(): input = raw_input().split(' ') input[1] = int(input[1]) input[2] = int...
Pat乙级1024题——科学计数法(Python) 测试结果为满分def sciCount(): input = raw_input() inputList = [] for i in range(len(input)): inputList.append(input[i]) eIndex = inputList.index('E') sciNum = int(input[eIndex+2...
Pat乙级1023题——组个最小数(Python) 代码测试结果为满分def minNum(): input = map(int, raw_input().split(' ')) result = '' for i in range(1, len(input)): if input[i] == 0: continue else: for j ...
Pat乙级1022题——D进制的A+B(Python) 代码测试结果为满分def dHex(): input = map(int, raw_input().split(' ')) number = input[0] + input [1] d = input[2] result = [] while number >= d: result.append(number % d) ...
Pat乙级1021题——个位数统计(Python) 代码测试结果为满分def singleDigit(): number = raw_input() numList = [] numDict = {} for i in range(len(number)): numList.append(int(number[i])) for i in range(10): count =...
Pat乙级1020题——月饼(Python)有一个结果是非零返回 注意考虑需求量大于所有月饼供应量之和的情况。代码如下 测试结果是有一个非零返回,说明有一种情况没有考虑到。def mooncake(): input = map(int, raw_input().split(' ')) if input[0] == 0: print '0.00' return stock = map...