![](https://img-blog.csdnimg.cn/20201014180756757.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Python
掌握 Python 编程基础
smallTutou
这个作者很懒,什么都没留下…
展开
-
排序算法桶排序
排序算法,桶排序原创 2022-06-10 18:12:20 · 143 阅读 · 1 评论 -
word2vec模型训练
import numpy as npfrom collections import defaultdictclass word2vec(): def __init__(self): self.n = settings['n'] self.lr = settings['learning_rate'] self.epochs = settings['epochs'] self.window = settings['window_s原创 2022-04-15 16:00:33 · 859 阅读 · 0 评论 -
读取只有一行数据的大文件
"""读取大文件,一行数据"""def myreadlines(f,newline): buf = "" while True: while newline in buf: pos = buf.index(newline) print(pos) print(pos + len(newline)) yield buf[:pos] buf = buf[pos+原创 2022-04-08 16:08:49 · 125 阅读 · 0 评论 -
斐波那契数列
斐波那契数列##递归方式"""斐波那契函数:1,1,2,3,5,8.."""def fib(index): if index <= 2: return 1 else: return fib(index-1)+fib(index-2)##普通方式def fib(index): re_list = [] n,a,b = 0,0,1 while n < index: re_list.append(b)原创 2022-04-08 16:04:54 · 91 阅读 · 0 评论 -
利用tf-idf提取关键词
##读取.txt并保存为.txtfrom jieba import analysef = open("result.txt", "w", encoding="utf-8")for line in open("text.txt",encoding="utf-8"): tfidf = analyse.extract_tags text = line keywords = tfidf(text, topK=4, withWeight=False, allowPOS=())原创 2022-04-08 15:27:13 · 1709 阅读 · 0 评论 -
单词拼写纠错
from nltk.corpus import reutersfrom docx import Documentfrom nltk import sent_tokenize, word_tokenizeimport reimport numpy as np#词典库vocab = []for line in open("data/spell-testset1.txt"): items = line.split(":") item = items[0].strip() v原创 2022-04-08 15:20:18 · 105 阅读 · 0 评论 -
词性标注pos_tagging
import numpy as np#1.构造词典、词性库tag2id,id2tag = {},{}word2id ,id2word = {},{}for line in open("traindata.txt"): items = line.split("/") tag,word = items[1].rstrip(),items[0] if tag not in tag2id: tag2id[tag] = len(tag2id) i原创 2022-04-08 15:14:36 · 292 阅读 · 0 评论 -
二叉树创建
#定义树节点class Node(object): def __init__(self,data): #data为树节点存储数据,left左子树,right右子树 self.elem = data self.lchild = None self.rchild = None#创建二叉树class BinTree(object): def __init__(self): self.root = None .原创 2022-04-08 15:09:48 · 98 阅读 · 0 评论 -
读取、处理Yelp数据集
##读取、处理Yelp数据集import pandas as pdimport jsonreviews_path =r"../yelp_dataset/yelp_academic_dataset_review.json"'''读取原始数据'''file = open(reviews_path, 'r', encoding="utf-8")users_id = []items_id = []ratings = []reviews = []dates = []i =0for原创 2022-04-08 11:14:49 · 3047 阅读 · 0 评论 -
基础的协同过滤算法
##ml_1m数据集下载地址:http://files.grouplens.org/datasets/movielens/##基于用户的协同过滤import numpy as npimport pandas as pdfile = "ml-1m/ratings.dat"header = ["uid","iid","rating"]dtype = {"uid" :np.int32,"iid":np.int32,"rating":np.float32}data = pd.read_csv(fi原创 2022-04-08 10:38:56 · 1455 阅读 · 0 评论 -
DataFrame 数据集切分
DataFrame 数据集切分#方式1from sklearn.model_selection import train_test_split # split train set and test setdata_train, data_test = train_test_split(data, test_size=0.2, random_state=1234)# split validate set and test setdata_test, data_val = train_test_s原创 2021-11-08 11:19:47 · 2809 阅读 · 0 评论 -
网易算法笔试
import numpy as npdef func1(a):n, m = a.shapeif n == 2:a = a.reshape(-1)a = a.tolist()a.sort()return a[1]l = []for i in range(0, n, 2):for j in range(0, m, 2):li = [a[i][j], a[i + 1][j], a[i][j + 1], a[i + 1][j + 1]]li.sort()l.append(li[1])ma原创 2021-09-23 17:19:15 · 135 阅读 · 0 评论 -
子序列相关题目 python
‘’‘最长连续子序列’’’def findmax(nums):maxnum = 1curr = 1for i in range(len(nums)-1):if nums[i] < nums[i+1]:curr += 1else:maxnum = max(maxnum,curr)curr = 1return max(maxnum,curr)‘’‘最长连续递增子序列个数’’’def findmaxnum(nums):maxnum = 1curr = 1num = []for原创 2021-09-23 17:18:01 · 166 阅读 · 0 评论 -
列表切片操作
列表切片操作alist = [0,1,2,3,4,5]#1. 列表取值print(alist[::]) #返回列表中所有元素print(alist[::-1]) #返回列表中所有元素的逆序列表print(alist[::2]) #返回列表中偶数位置的元素print(alist[1::2]) #返回列表中奇数位置的元素print(alist[2:4]) #指定列表的开始和结束位置print(alist[0:10]) #若切片结束位置大于列表长度时,从列表尾部截断print(alist[10:原创 2021-04-04 14:53:28 · 1681 阅读 · 1 评论 -
序列相加
序列相加#1.直接用“+”,序列形式必须相同,即均为list、tuple等eg:a = [1,2,3]b = [4,5]c = a+bprint©#结果:[1,2,3,4,5]#2.就地加,可以任意序列形式相加eg:a = [1,2,3]a += (4,5print(a#结果:[1,2,3,4,5]原创 2021-04-04 13:55:46 · 1096 阅读 · 0 评论 -
上下文管理协议器
上下文管理协议器#上下文管理器协议#法1import contextlib@contextlib.contextmanagerdef file_open(file_name):print(“file open”)yield{}print(“file end”)with file_open(“body.txt”) as f_opened:print(“file processing”)#法2class Sample:def enter(self):print(“enter”)re原创 2021-04-04 13:25:55 · 51 阅读 · 0 评论