numpy和sympy模块函数与方法练习。

1.   (2分)请利用sympy模块中的函数/方法求极限:

2.   (3分)从正态分布N(5,5)中采样1000个样本,构建为一维数组。

①   计算数组的最大值、最小值、均值、标准差、三个四分位数(提示:numpy中有提供了相应函数)并保留3位小数格式化输出;

②   打印数组中的数据类型,并计算数组所占据的内存大小;

③   将数组的数据类型转换为int32后再次计算内存大小。

3.   (3分)从均匀分布U[-10,10)中采样,创建一个5*4的二维数组。

①   将数组中较大的2个与较小的2个数重新赋值为0(若存在多个最大和最小值任取即可,共修改4个数)

②   统计数组中绝对值在 (4,6) 之间的数据个数。

4.  (2分)定义一个普通的标量函数,其功能为计算数字字符串参数各位数字和,将此标量函数向量化,并创建一个元素为1~1000所有整数的ndarray数组进行测试。

题1:

from sympy import *

x = symbols('x')

y = limit(x * ln(sqrt(2) + 1 / x), x, +oo)

print(y)

 题2:

import numpy as np
samples = np.random.normal(loc=5, scale=5, size=1000)
max_val = np.max(samples)
min_val = np.min(samples)
mean_val = np.mean(samples)
std_val = np.std(samples)
quartile_1 = np.percentile(samples, 25)
quartile_2 = np.percentile(samples, 50)
quartile_3 = np.percentile(samples, 75)
print("max value: {:.3f}".format(max_val))
print("min value: {:.3f}".format(min_val))
print("mean value: {:.3f}".format(mean_val))
print("standard deviation: {:.3f}".format(std_val))
print("1st quartile: {:.3f}".format(quartile_1))
print("2st quartile: {:.3f}".format(quartile_2))
print("3st quartile: {:.3f}".format(quartile_3))
print("Array data type: {}".format(samples.dtype))
print("Array size: {} bytes".format(samples.nbytes))
samples = samples.astype(np.int32)
print("New array size: {} bytes".format(samples.nbytes))

题3:

import numpy as np

array = np.random.uniform(-10, 10, (5, 4))

largest_indices = np.argpartition(array.flatten(), -2)[-2:]

smallest_indices = np.argpartition(array.flatten(), 2)[:2]

array[np.unravel_index(largest_indices, array.shape)] = 0

array[np.unravel_index(smallest_indices, array.shape)] = 0

count = np.sum((4 < np.abs(array)) & (np.abs(array) < 6))

print("Modified array:")

print(array)

print("Count of elements with absolute value between 4 and 6:", count)

题4:

import numpy as np

def sum_of_digits(s: str) -> int:

    return sum(int(digit) for digit in s)

vectorized_sum_of_digits = np.vectorize(sum_of_digits)

array = np.arange(1, 1001)

result = vectorized_sum_of_digits(array.astype(str))

print("Resulting array:")

print(result)

每一次认真输出都值得记录!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cytingle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值