摘要: 例题2.42代码 with open('data2_2.txt') as fp: L1=[]; L2=[]; for line in fp: L1.append(len(line)) L2.append(len(line.strip())) #去掉换行符 data = [str(num)+'\t' 阅读全文
posted @ 2024-10-22 22:19 等我刷把宗师 阅读(25) 评论(0) 推荐(0)
摘要: 例题2.41代码 import pandas as pd import numpy as np a = pd.DataFrame(np.random.randint(1,6,(5,3)), index=['a', 'b', 'c', 'd', 'e'], columns=['one', 'two', 阅读全文
posted @ 2024-10-22 22:19 等我刷把宗师 阅读(32) 评论(0) 推荐(0)
摘要: 例题2.40代码 import pandas as pd import numpy as np # 创建一个 DataFrame,其中包含 10 行 4 列的随机整数(1 到 5 之间),并添加一个名为 'A' 的随机列 np.random.seed(0) # 设置随机种子以确保结果可复现 d = 阅读全文
posted @ 2024-10-22 22:16 等我刷把宗师 阅读(21) 评论(0) 推荐(0)
摘要: 例题2.39代码 import pandas as pd 读取CSV文件,指定列范围从第二列到第四列(Python索引从0开始,但usecols的索引从1开始) try: a = pd.read_csv("data2_38_2.csv", usecols=range(1, 5)) print("CS 阅读全文
posted @ 2024-10-22 22:14 等我刷把宗师 阅读(27) 评论(0) 推荐(0)
摘要: 例题2.38_2代码 import pandas as pd import numpy as np dates=pd.date_range(start='20191101', end='20191124', freq='D') a1=pd.DataFrame(np.random.randn(24,4 阅读全文
posted @ 2024-10-22 22:14 等我刷把宗师 阅读(16) 评论(0) 推荐(0)
摘要: 例题2.38代码 import pandas as pd import numpy as np dates=pd.date_range(start='20191101', end='20191124', freq='D') a1=pd.DataFrame(np.random.randn(24,4), 阅读全文
posted @ 2024-10-22 22:10 等我刷把宗师 阅读(22) 评论(0) 推荐(0)
摘要: 例题2.37代码 import pandas as pd import numpy as np dates=pd.date_range(start='20191101',end='20191124',freq='D') a1=pd.DataFrame(np.random.randn(24,4), i 阅读全文
posted @ 2024-10-22 22:08 等我刷把宗师 阅读(24) 评论(0) 推荐(0)
摘要: 例题2.36代码 import numpy as np a = np.eye(4) b = np.rot90(a) c, d = np.linalg.eig(b) print('特征值为:', c) print('特征向量为:\n', d) 阅读全文
posted @ 2024-10-22 22:05 等我刷把宗师 阅读(26) 评论(0) 推荐(0)
摘要: 例题2.35代码 import numpy as np a = np.array([[3, 1], [1, 2], [1, 1]]) b = np.array([9, 8, 6]) x = np.linalg.pinv(a) @ b print(np.round(x, 4)) 阅读全文
posted @ 2024-10-22 22:04 等我刷把宗师 阅读(11) 评论(0) 推荐(0)
摘要: 例题2.34代码 `` import numpy as np a = np.array([[3, 1], [1, 2]]) b = np.array([9, 8]) x1 = np.linalg.inv(a) @ b #第一种解法 上面语句中@表示矩阵乘法 x2 = np.linalg.solve( 阅读全文
posted @ 2024-10-22 22:04 等我刷把宗师 阅读(38) 评论(0) 推荐(0)
摘要: 例题2.33代码 import numpy as np a = np.array([[0, 3, 4], [1, 6, 4]]) b = np.linalg.norm(a, axis=1) #求行向量2范数 c = np.linalg.norm(a, axis=0) #求列向量2范数 d = np. 阅读全文
posted @ 2024-10-22 22:03 等我刷把宗师 阅读(21) 评论(0) 推荐(0)
摘要: 例题2.32代码 import numpy as np a = np.ones(4) b = np.arange(2, 10, 2) c = a @ b #a作为行向量,b作为列向量 d = np.arange(16).reshape(4,4) f = a @ d #a作为行向量 g = d @ a 阅读全文
posted @ 2024-10-22 22:02 等我刷把宗师 阅读(35) 评论(0) 推荐(0)
摘要: 例题2.31代码 import numpy as np a = np.array([[0, 3, 4], [1, 6, 4]]) b = np.array([[1, 2, 3], [2, 1, 4]]) c = a / b #两个矩阵对应元素相除 d = np.array([2, 3, 2]) e 阅读全文
posted @ 2024-10-22 22:00 等我刷把宗师 阅读(27) 评论(0) 推荐(0)
摘要: 例题2.30代码 import numpy as np a = np.array([[0, 3, 4], [1, 6, 4]]) b = a.sum() c1 = sum(a) c2 = np.sum(a, axis = 0) c3 = np.sum(a, axis = 1, keepdims = 阅读全文
posted @ 2024-10-22 21:58 等我刷把宗师 阅读(27) 评论(0) 推荐(0)
摘要: 例题2.29代码 import numpy as np a = np.arange(16).reshape(4, 4) b = np.vsplit(a, 2) print('行分割: \n', b[0], '\n', b[1]) c = np.hsplit(a, 4) print('列分割:: \n 阅读全文
posted @ 2024-10-22 21:56 等我刷把宗师 阅读(29) 评论(0) 推荐(0)
摘要: 例题2.28代码 import numpy as np a = np.arange(16).reshape(4,4) b = np.floor(5 * np.random.random((2, 4))) c = np.ceil(6 * np.random.random((4, 2))) d = np 阅读全文
posted @ 2024-10-22 21:55 等我刷把宗师 阅读(14) 评论(0) 推荐(0)
摘要: 例题2.27代码 import numpy as np a = np.arange(16).reshape(4,4) b = a[1][2] c= a[1, 2] d = a[1:2, 2:3] x = np.array([0, 1, 2, 1]) print(a[x == 1]) 阅读全文
posted @ 2024-10-22 21:54 等我刷把宗师 阅读(16) 评论(0) 推荐(0)
摘要: 例题2.26代码 import numpy as np a = np.ones(4, dtype= int) b = np.ones((4,), dtype= int) c = np.ones((4,1)) d = np.zeros(4) e = np.empty(3) f = np.eye(3) 阅读全文
posted @ 2024-10-22 21:53 等我刷把宗师 阅读(11) 评论(0) 推荐(0)
摘要: 例题2.25代码 import numpy as np a1 = np.array([1,2,3,4]) a2 = a1.astype(float) a3 = np.array([1,2,3,4], dtype = float) print(a1.dtype); print(a2.dtype); p 阅读全文
posted @ 2024-10-22 21:52 等我刷把宗师 阅读(23) 评论(0) 推荐(0)
摘要: 例题2.24代码 s1 = [str(x) for x, y in zip(['v'] * 4, range(1, 5))] s2 = list(zip('abcd', range(4))) print(s1); print(s2) 阅读全文
posted @ 2024-10-22 21:51 等我刷把宗师 阅读(22) 评论(0) 推荐(0)
摘要: 例题2.23代码 def filter_non_unique(L): return [item for item in L if L.count(item) == 1] a = filter_non_unique([1, 2, 3, 4, 5]) print(a) 阅读全文
posted @ 2024-10-22 21:50 等我刷把宗师 阅读(24) 评论(0) 推荐(0)
摘要: 例题2.22代码 a = filter(lambda x: x > 10, [1, 11, 2, 45, 7, 6, 13]) b = filter(lambda x: x.isalnum(), ['abc', 'xy12', '***']) print(list(a)); print(list(b 阅读全文
posted @ 2024-10-22 21:49 等我刷把宗师 阅读(17) 评论(0) 推荐(0)
摘要: 例题2.20代码 x1 = "abcde" x2 = list(enumerate(x1)) for ind, ch in enumerate(x1): print(ch) 阅读全文
posted @ 2024-10-22 21:48 等我刷把宗师 阅读(17) 评论(0) 推荐(0)
摘要: 例题2.21代码 import random x = random.randint(1e5, 1e8) y = list(map(int, str(x))) z = list(map(lambda x, y: x%2 == 1 and y%2 == 0, [1, 3, 2, 4, 1], [3, 2 阅读全文
posted @ 2024-10-22 21:48 等我刷把宗师 阅读(26) 评论(0) 推荐(0)
摘要: 例题2.19代码 import numpy.random as nr x1 = list(range(9,21)) nr.shuffle(x1) x2 = sorted(x1) x3 = sorted(x1, reverse=True) x4 = sorted(x1, key = lambda it 阅读全文
posted @ 2024-10-22 21:47 等我刷把宗师 阅读(32) 评论(0) 推荐(0)
摘要: 例题2.18代码 from ex2_12_2 import * print(factorial(6)) fib(300) 阅读全文
posted @ 2024-10-22 21:46 等我刷把宗师 阅读(30) 评论(0) 推荐(0)
摘要: 例题2.17代码 from math import * a = sin(3) b = pi c = e d = radians(180) print(a); print(b); print(c); print(d) 阅读全文
posted @ 2024-10-22 21:45 等我刷把宗师 阅读(24) 评论(0) 推荐(0)
摘要: 例题2.16代码 from random import sample from numpy.random import randint a = sample(range(10), 5) b = randint(0, 10, 5) print(a); print(b) 阅读全文
posted @ 2024-10-22 21:44 等我刷把宗师 阅读(35) 评论(0) 推荐(0)
摘要: 例题2.15代码 import math import random import numpy.random as nr a = math.gcd(12, 21) b = random.randint(0, 2) c = nr.randint(0, 2, (4, 3)) print(a); prin 阅读全文
posted @ 2024-10-22 21:43 等我刷把宗师 阅读(24) 评论(0) 推荐(0)
摘要: 例题2.14代码 f = lambda x, y, z: x * y * z L = lambda x: [x**2, x**3, x**4] print(f(3, 4, 5)); print(L(2)) 阅读全文
posted @ 2024-10-22 21:42 等我刷把宗师 阅读(42) 评论(0) 推荐(0)
摘要: 例题2.13代码 def bifurcate_by(L, fn): return [[x for x in L if fn(x)], [x for x in L if not fn(x)]] s = bifurcate_by(['beep', 'boop', 'foo', 'bar'], lambd 阅读全文
posted @ 2024-10-22 21:41 等我刷把宗师 阅读(15) 评论(0) 推荐(0)
摘要: 例题2.12_2代码 def factorial(n): r = 1 while n > 1: r *= n n -= 1 return r def fib(n): a, b = 1, 1 while a < n: print(a, end = ' ') a, b = b, a + b 阅读全文
posted @ 2024-10-22 21:40 等我刷把宗师 阅读(15) 评论(0) 推荐(0)
摘要: 例题2.12_3代码 from ex2_12_2 import factorial, fib print('%d! =%d'%(5, factorial(5))) fib(200) 阅读全文
posted @ 2024-10-22 21:39 等我刷把宗师 阅读(19) 评论(0) 推荐(0)
摘要: 例题2.12代码 def factorial(n): r = 1 while n > 1: r *= n n -= 1 return r def fib(n): a, b = 1, 1 while a < n: print(a, end = ' ') a, b = b, a + b print('% 阅读全文
posted @ 2024-10-22 21:38 等我刷把宗师 阅读(28) 评论(0) 推荐(0)
摘要: 例题2.11_2代码 import string, random, collections x = string.ascii_letters + string.digits y = ' '.join([random.choice(x) for i in range(1000)]) count = c 阅读全文
posted @ 2024-10-22 21:36 等我刷把宗师 阅读(15) 评论(0) 推荐(0)
摘要: 例题2.11代码 import string import random x = string.ascii_letters + string.digits y = ' '.join([random.choice(x) for i in range(1000)]) d = dict() for ch 阅读全文
posted @ 2024-10-22 21:33 等我刷把宗师 阅读(30) 评论(0) 推荐(0)
摘要: 例题2.10代码 Dict = {'age': 18, 'score': [98, 97], 'name': 'zhang', 'sex': 'male'} for item in Dict: print(item) print(" ") for item in Dict.items(): prin 阅读全文
posted @ 2024-10-22 21:30 等我刷把宗师 阅读(20) 评论(0) 推荐(0)
摘要: 例题2.9代码 Dict = {'age': 18, 'score': [98, 97], 'name': 'zhang', 'sex': 'male'} try: print(Dict['age']) print(Dict.get('age')) print(Dict.get('address', 阅读全文
posted @ 2024-10-22 21:29 等我刷把宗师 阅读(22) 评论(0) 推荐(0)
摘要: 例题2.8代码 dict1 = {'Alice': '123', 'Beth': '456', 'Cecil': 'abc'} print(dict1['Alice']) dict1['new'] = 'Hello' dict1['Alice'] = '1234' dict2 = {'abc': 1 阅读全文
posted @ 2024-10-22 21:27 等我刷把宗师 阅读(36) 评论(0) 推荐(0)
摘要: 例题2.7代码 student = {'Tom', 'Jim', 'Mary', 'Tom', 'Jack', 'Rose'} print(student) a = set('abcdabc') print(a) 阅读全文
posted @ 2024-10-22 21:26 等我刷把宗师 阅读(9) 评论(0) 推荐(0)
摘要: 例题2.6代码 T = ('abc', 12, 3.45, 'python', 2.789) print(T) print(T[-1]) print(T[1:3]) 阅读全文
posted @ 2024-10-22 19:11 等我刷把宗师 阅读(12) 评论(0) 推荐(0)
摘要: 例题2.5_2代码 from numpy.random import randint import numpy as np a = randint(10, 20, 16) ma = max(a) ind1 = [index for index, value in enumerate(a) if va 阅读全文
posted @ 2024-10-22 19:10 等我刷把宗师 阅读(18) 评论(0) 推荐(0)
摘要: 例题2.5_1代码 import os fn = [filename for filename in os.listdir('F:\PYTHONwork\SpyderPython') if filename.endswith((',exe', '.py'))] print(fn) 阅读全文
posted @ 2024-10-22 19:09 等我刷把宗师 阅读(21) 评论(0) 推荐(0)
摘要: 例题2.4代码 a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] d = [c for b in a for c in b] print(d) 阅读全文
posted @ 2024-10-22 19:06 等我刷把宗师 阅读(21) 评论(0) 推荐(0)
摘要: 例题2.3代码 L = ['abc', 12, 3.45, 'python', 2.789] print(L) print(L[0]) L[0] = 'a' L[1:3] = ['b', 'Hello'] print(L) L[2:4] = [] print(L) 阅读全文
posted @ 2024-10-22 19:06 等我刷把宗师 阅读(17) 评论(0) 推荐(0)
摘要: 例题2.1代码 import numpy as np a = [] with open('F:\python数学建模与算法\第二章例题\data2_2.txt') as f: for (i, s) in enumerate(f): a.append([s.count('a'), s.count('c 阅读全文
posted @ 2024-10-22 19:04 等我刷把宗师 阅读(15) 评论(0) 推荐(0)
摘要: 习题5.5代码 把max放进约束条件,不妨假定小于等于10000 linprog from scipy.optimize import linprog c=[0,0,3,2] A=[[-1,-1,0,0],[0,0,-1,-1],[3,0,2,0],[0,3,0,2],[100,90,80,70]] 阅读全文
posted @ 2024-10-22 19:02 等我刷把宗师 阅读(21) 评论(0) 推荐(0)
摘要: 习题5.4代码 import numpy as np import math from scipy.optimize import minimize, Bounds def func(x): return sum(math.sqrt(x[i]) for i in range(100)) def co 阅读全文
posted @ 2024-10-22 19:01 等我刷把宗师 阅读(33) 评论(0) 推荐(0)
摘要: 习题2.5代码 import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # 参数 a = 2 b = np.sqrt(10) c = np.sqrt(8) # 创建u和v的网 阅读全文
posted @ 2024-10-22 18:58 等我刷把宗师 阅读(26) 评论(0) 推荐(0)
摘要: 习题2.1代码 import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_unicode=True) import matplotlib.pyplot as plt plt.rcParams['fon 阅读全文
posted @ 2024-10-22 13:35 等我刷把宗师 阅读(27) 评论(0) 推荐(0)
摘要: 习题2.2代码 import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_unicode=True) import matplotlib.pyplot as plt plt.rcParams['fon 阅读全文
posted @ 2024-10-22 13:34 等我刷把宗师 阅读(21) 评论(0) 推荐(0)
摘要: 习题2.3代码 import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_unicode=True) import matplotlib.pyplot as plt plt.rcParams['fon 阅读全文
posted @ 2024-10-22 13:33 等我刷把宗师 阅读(21) 评论(0) 推荐(0)
摘要: 习题2.4代码 import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_unicode=True) import matplotlib.pyplot as plt plt.rcParams['fon 阅读全文
posted @ 2024-10-22 13:32 等我刷把宗师 阅读(10) 评论(0) 推荐(0)
摘要: 习题2.6代码 import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_unicode=True) import matplotlib.pyplot as plt plt.rcParams['fon 阅读全文
posted @ 2024-10-22 13:24 等我刷把宗师 阅读(29) 评论(0) 推荐(0)
摘要: 习题2.7代码 import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_unicode=True) import matplotlib.pyplot as plt plt.rcParams['fon 阅读全文
posted @ 2024-10-22 13:20 等我刷把宗师 阅读(24) 评论(0) 推荐(0)
摘要: 习题2.8代码 import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_unicode=True) import matplotlib.pyplot as plt plt.rcParams['fon 阅读全文
posted @ 2024-10-22 13:19 等我刷把宗师 阅读(33) 评论(0) 推荐(0)
摘要: 习题2.9代码 import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_unicode=True) import matplotlib.pyplot as plt plt.rcParams['fon 阅读全文
posted @ 2024-10-22 13:11 等我刷把宗师 阅读(17) 评论(0) 推荐(0)
摘要: 习题2.10代码 import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_unicode=True) import matplotlib.pyplot as plt plt.rcParams['fo 阅读全文
posted @ 2024-10-22 13:10 等我刷把宗师 阅读(19) 评论(0) 推荐(0)
摘要: 习题2.11代码 import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_unicode=True) import matplotlib.pyplot as plt plt.rcParams['fo 阅读全文
posted @ 2024-10-22 13:09 等我刷把宗师 阅读(19) 评论(0) 推荐(0)
摘要: 习题2.12代码 import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_unicode=True) import matplotlib.pyplot as plt plt.rcParams['fo 阅读全文
posted @ 2024-10-22 13:07 等我刷把宗师 阅读(24) 评论(0) 推荐(0)
摘要: 习题2.13代码 import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_unicode=True) import matplotlib.pyplot as plt plt.rcParams['fo 阅读全文
posted @ 2024-10-22 13:05 等我刷把宗师 阅读(23) 评论(0) 推荐(0)
摘要: 习题3.3代码 import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_unicode=True) import matplotlib.pyplot as plt plt.rcParams['fon 阅读全文
posted @ 2024-10-22 13:03 等我刷把宗师 阅读(20) 评论(0) 推荐(0)
摘要: 习题3.2代码 import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_unicode=True) import matplotlib.pyplot as plt plt.rcParams['fon 阅读全文
posted @ 2024-10-22 13:02 等我刷把宗师 阅读(15) 评论(0) 推荐(0)
摘要: 习题4.4代码 import cvxpy as cp import numpy as np import pandas as pd import sympy as sp sp.init_printing(use_unicode=True) import matplotlib.pyplot as pl 阅读全文
posted @ 2024-10-22 12:59 等我刷把宗师 阅读(18) 评论(0) 推荐(0)
摘要: 习题4.3代码 import cvxpy as cp import numpy as np import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC 阅读全文
posted @ 2024-10-22 12:57 等我刷把宗师 阅读(19) 评论(0) 推荐(0)
摘要: 习题5.7代码 import cvxpy import cvxpy as cp import numpy as np import pandas as pd from scipy.optimize import minimize import sympy as sp sp.init_printing 阅读全文
posted @ 2024-10-22 12:41 等我刷把宗师 阅读(35) 评论(0) 推荐(0)
摘要: 例题6.11代码 import cvxpy as cp import networkx as nx import numpy as np Adjt = [(1,2,18),(1,5,15),(2,3,20),(2,4,60),(2,5,12), (3,4,30),(3,5,18),(4,6,10), 阅读全文
posted @ 2024-10-22 12:34 等我刷把宗师 阅读(23) 评论(0) 推荐(0)
摘要: 例题6.10代码 import networkx as nx import numpy as np Adjt = [(1,2,20), (1,5,15), (2,3,20), (2,4,60), (2,5,25), (3,4,30), (3,5,18), (5,6,15)] G = nx.Graph 阅读全文
posted @ 2024-10-22 12:32 等我刷把宗师 阅读(15) 评论(0) 推荐(0)
摘要: 习题6.1代码 import numpy as np import pandas as pd import cvxpy as cp import networkx as nx import matplotlib.pyplot as plt plt.rcParams['font.sans-serif' 阅读全文
posted @ 2024-10-22 12:29 等我刷把宗师 阅读(20) 评论(0) 推荐(0)
摘要: 习题6.4代码 \import numpy as np import pandas as pd import cvxpy as cp import networkx as nx import matplotlib.pyplot as plt plt.rcParams['font.sans-serif 阅读全文
posted @ 2024-10-22 12:28 等我刷把宗师 阅读(5) 评论(0) 推荐(0)
摘要: 习题6.3代码 import numpy as np import pandas as pd import cvxpy as cp import networkx as nx import matplotlib.pyplot as plt plt.rcParams['font.sans-serif' 阅读全文
posted @ 2024-10-22 12:26 等我刷把宗师 阅读(29) 评论(0) 推荐(0)