python numpy 笔记(四)

Statistics

1. Write a NumPy program to compute the weighted of a given array.

import numpy as np
x = np.arange(5)
print("\nOriginal array:")
print(x)
weights = np.arange(1, 6)
r1 = np.average(x, weights=weights)
r2 = (x*(weights/weights.sum())).sum()
assert np.allclose(r1, r2)
print("\nWeighted average of the said array:")
print(r1)

2.Write a NumPy program to compute pearson product-moment correlation coefficients of two given arrays.

np.corrcoef(x, y)

3. Write a NumPy program to compute the histogram of nums against the bins.

import numpy as np
import matplotlib.pyplot as plt
nums = np.array([0.5, 0.7, 1.0, 1.2, 1.3, 2.1])
bins = np.array([0, 1, 2, 3])
print("nums: ",nums)
print("bins: ",bins)
print("Result:", np.histogram(nums, bins))
plt.hist(nums, bins=bins)
plt.show()

nums:  [0.5 0.7 1.  1.2 1.3 2.1]
bins:  [0 1 2 3]
Result: (array([2, 3, 1], dtype=int64), array([0, 1, 2, 3]))

DateTime

numpy datetime64 基础
详细

1. Write a NumPy program to get the dates of yesterday, today and tomorrow.

import numpy as np
yesterday = np.datetime64('today', 'D') - np.timedelta64(1, 'D')
print("Yestraday: ",yesterday)
today     = np.datetime64('today', 'D')
print("Today: ",today)
tomorrow  = np.datetime64('today', 'D') + np.timedelta64(1, 'D')
print("Tomorrow: ",tomorrow)

2. Write a NumPy program to find the first Monday in May 2017.

import numpy as np
print("First Monday in May 2017:")
print(np.busday_offset('2017-05', 0, roll='forward', weekmask='Mon'))

String

字符基础操作

1. Write a NumPy program to concatenate element-wise two arrays of string.

import numpy as np
x1 = np.array(['Python', 'PHP'], dtype=np.str)
x2 = np.array([' Java', ' C++'], dtype=np.str)
print("Array1:")
print(x1)
print("Array2:")
print(x2)
new_array = np.char.add(x1, x2)
print("new array:")
print(new_array)

Array1:
['Python' 'PHP']
Array2:
[' Java' ' C++']
new array:
['Python Java' 'PHP C++']

2. Write a NumPy program to remove the leading whitespaces of all the elements of a given array.

import numpy as np
x = np.array([' python exercises ', ' PHP  ', ' java  ', '  C++'], dtype=np.str)
print("Original Array:")
print(x)
lstripped_char = np.char.lstrip(x)
print("\nRemove the leading whitespaces : ", lstripped_char)

Original Array:
[' python exercises ' ' PHP  ' ' java  ' '  C++']
Remove the leading whitespaces :  ['python exercises ' 'PHP  ' 'java  ' 'C++']

3. Write a NumPy program to make all the elements of a given string to a numeric string of 5 digits with zeros on its left.

import numpy as np
x = np.array(['2', '11', '234', '1234', '12345'], dtype=np.str)
print("\nOriginal Array:")
print(x)
r = np.char.zfill(x, 5)
print("\nNumeric string of 5 digits with zeros:")
print(r) 

Original Array:
['2' '11' '234' '1234' '12345']
Numeric string of 5 digits with zeros:
['00002' '00011' '00234' '01234' '12345']

4. Write a NumPy program to count the number of “P” in a given array, element-wise.

import numpy as np
x1 = np.array(['Python', 'PHP', 'JS', 'examples', 'html'], dtype=np.str)
print("\nOriginal Array:")
print(x1)
print("Number of ‘P’:")
r = np.char.count(x1, "P")
print(r)

Original Array:
['Python' 'PHP' 'JS' 'examples' 'html']
Number of ‘P’:
[1 2 0 0 0]

5. Write a NumPy program to count the lowest index of “P” in a given array, element-wise.

import numpy as np
x1 = np.array(['Python', 'PHP', 'JS', 'EXAMPLES', 'HTML'], dtype=np.str)
print("\nOriginal Array:")
print(x1)
print("count the lowest index of ‘P’:")
r = np.char.find(x1, "P")
print(r)

Original Array:
['Python' 'PHP' 'JS' 'EXAMPLES' 'HTML']
count the lowest index of ‘P’:
[ 0  0 -1  4 -1]

6. Write a NumPy program to check whether each element of a given array is composed of digits only, lower case letters only and upper case letters only.

import numpy as np
x = np.array(['Python', 'PHP', 'JS', 'Examples', 'html5', '5'], dtype=np.str)
print("\nOriginal Array:")
print(x)
r1 = np.char.isdigit(x)
r2 = np.char.islower(x)
r3 = np.char.isupper(x)
print("Digits only =", r1)
print("Lower cases only =", r2)
print("Upper cases only =", r3)

Original Array:
['Python' 'PHP' 'JS' 'Examples' 'html5' '5']
Digits only = [False False False False False  True]
Lower cases only = [False False False False  True False]
Upper cases only = [False  True  True False False False]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值