- 博客(20)
- 收藏
- 关注
原创 6.13作业(sklearn练习)
题目Step 1:sklearn.datasets.make_classification 函数能随机产生 n_classes 类数据集,返回一组样本(sample)和一组标记(label)。from sklearn import datasetsfrom sklearn import model_selectionfrom sklearn import naive_bayesfrom sk...
2018-06-18 17:48:50
377
原创 6.6作业(pandas练习)
题目来源题目给了一个 csv 文件,读入文件得到一个 DataFrame 对象,该对象中有4个数据集,要对这4个数据集做一些操作。如果不熟悉 python pandas,可以参考 pandas 入门教程Exercise 1题意题解用布尔索引的方式划分数据集,对每个数据集调用相应的函数求解。求线性回归的API是OLS类。endog 参数是因变量, exog 参数是自变量。要先用 add_consta...
2018-06-10 14:47:58
838
原创 5.30作业(scipy练习)
Exercise 1: Least squares题意题解要求出最小二乘解 x 和残差的范数,用 scipy.linalg.lstsq 函数。from numpy import randomfrom scipy import linalgm = 30n = 20A = random.randn(m, n)b = random.randn(m, 1)x, residual, rank...
2018-06-03 13:53:13
506
原创 5.23作业(matplotlib练习)
Exercise 1: Plotting a function题意题解numpy.linspace 生成一段区间内均匀分布的点。用这个函数生成横坐标。numpy 提供了很多数学函数,这里能用到的有 numpy.sin, numpy.power, numpy.exp, numpy.negative。matplotlib.pyplot 提供了绘制图像的函数 plot,也提供了设置横坐标名、纵坐标名、标...
2018-05-29 00:13:16
513
原创 5.16作业(numpy练习)
Exercise 0题意题解randn(d0, d1, ..., dn) 返回一个n维数组,数组是标准正态分布的样本。用randn函数得到二维数组A。import numpyfrom numpy import randomn = 200m = 500A = random.randn(n, m)numpy库中没有toeplitz函数,scipy.linalg库有。其实可以自己写一个函数生成...
2018-05-21 01:15:35
436
原创 [LeetCode 37] Sudoku Solver
题意 解一个大小为 9*9 的数独。思路 回溯法。 存储每一行、每一列、每个块分别有哪些数字已经使用过,每次填写新的数字之前都要检查,如果当前的数字是可行的,则递归地检查、填写下一个数字。递归函数的返回值可以用来判断是否有解,在第 i 行第 j 列时,如果已经找到了解,就直接返回,否则往当前位置填另外一个数字。 递归的结束条件是:位置越界。这表明已经填完了整个数独。pyth...
2018-05-06 20:12:05
201
原创 [LeetCode 32] Longest Valid Parentheses
题意 给一个只包含 '(' 和 ')' 的字符串,求最长有效括号子序列的长度。 如 "(()" 的最长有效括号子序列的长度为2。思路 动态规划。 dp[i] 表示从下标 i 开始向左数,有效括号子序列的长度。 1° 若 s[i] == '(' ,则 dp[i] = 0 2° 若 s[i] == ')' ,上一个可能与 s[i] 匹配的位置 j = i-1-dp[i-1...
2018-04-29 22:23:08
197
原创 [LeetCode 123] Best Time to Buy and Sell Stock III
题意 有一个数组price,price[ i ] 是第 i 天的股票价格。你的手上最多持有一支股票,且最多进行两次交易,求最大利润。思路 求出从第一天到第 i 天之内,买卖一次获得的最大收益,再求出从第 i+1 天到最后一天之内,买卖一次获得的最大收益。然后 i 从 0 到 n-1 扫一遍就行了。时间复杂度是O(n)。 可能是坑的地方:你可以不买股票,因此利润的最小值是0。如果数...
2018-04-29 19:14:51
297
原创 [LeetCode 45] Jump Game II
题意 给一个数组A, A[i] 表示在 i 这个位置最多能移动多远,i.e.在位置 i 最多移动到 A[i] + i 这个位置。问从数组第一个元素开始,最少移动多少次,能够到达数组的最后一个元素?我们假设始终能够到达数组的最后一个元素。思路 最直接的想法是在位置 i 处,更新 [ i + 1, i + A[i] ] 范围内的最少移动次数。但如果数组A中的元素都很大,这种方法的效率很低,...
2018-04-25 20:32:40
181
原创 4.9作业
11-1 城市和国家# city.pydef get_formatted_city(city, country): message = city.title() message += ", " message += country.title() return message# test_city.pyimport unittestfrom city import ...
2018-04-10 12:03:55
250
原创 4.4作业
10-4 访客名单file_name = "test.out.txt"with open(file_name, 'w') as file: while True: user_name = input("Please enter your name(enter \'q\' to quit): ") if user_name == 'q': br...
2018-04-05 16:22:43
216
原创 4.2作业
9-1 餐馆class Restaurant(): def __init__(self, restaurant_name, cuisine_type): self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type def describe_restauran...
2018-04-04 12:21:21
334
原创 3.28作业
8-4 大号T恤def make_shirt(size = "large", words = "I love Python"): print("A " + size + " T-shirt with \"" + words + "\"")make_shirt()make_shirt(size = "medium")make_shirt(words = "
2018-03-29 11:50:18
360
原创 3.26作业
7-3 10的整数倍number = int(input("Please enter a number: "))if number % 10 == 0: print(str(number) + " is a multiple of 10.")else: print(str(number) + " is not a multiple of 10.")7-4 披萨配料print("Plea...
2018-03-26 22:11:52
273
原创 3.21作业
6-6 调查favorite_language = { 'jen' : 'Python', 'sarah' : 'c', 'edward' : 'ruby', 'phil' : 'python', 'bill' : 'cpp'}people_to_be_polled = ('edward', 'phil', 'steve', 'harry')for person in...
2018-03-22 00:01:28
262
原创 3.19作业
5-5 外星人颜色#3def print_score(alien_color): pre = "You've got " post = " scores!" if alien_color == "green": print(pre + "5" + post) elif alien_color == "yellow": print(pre + "1.
2018-03-19 23:54:27
252
原创 3.14作业
4-2 动物animals = {"pig", "chicken", "beef cattle"}for animal in animals: print("A " + animal + " can be cooked into a great dish.")print("Any of these animals would make up of a great dish.")4-5
2018-03-15 14:37:10
219
原创 3.12作业
3-4 嘉宾名单people = ["first person", "second person", "third person"]for person in people: print(person.title() + ", would you like to have dinner with me?")3-5 修改嘉宾名单def invite(people): for person...
2018-03-12 13:18:32
265
原创 3.7作业
2-1 简单消息message = "this is a message"print(message)2-2 多条简单消息message = "this is the first message"print(message)message = "this is the second message"print(message)2-3 个性化消息name = "Eric Matthes"print(...
2018-03-11 14:08:49
249
原创 3.5作业
一、浏览python主页,写下发现和收获 python主页:https://www.python.org 主页是这样形容python的: Python is powerful... and fast; plays well with others; runs everywhere; is friendly & easy to learn; ...
2018-03-11 12:11:59
269
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人