python科学计算基础库
# coding=utf-8
import numpy as np
import random
# 使用numpy生成数组,得到ndarray的类型
t1 = np.array([1, 2, 3])
print("t1:", t1)
print(type(t1))
print()
t2 = np.array(range(10))
print("t2:", t2)
print(type(t2))
print()
t3 = np.arange(10)
print("t3:", t3)
print(type(t3))
print(t3.dtype)
print()
t4 = np.array(range(1, 4), dtype="i1")
print("t4:", t4)
print(t4.dtype)
print()
# numpy中的bool类型
t5 = np.array([1,1,0,1,0,0],dtype=bool)
print("t5:", t5)
print(t5.dtype)
print()
# 调整数据类型
t6 = t5.astype("int8")
print("t6:", t6)
print(t6.dtype)
print()
# numpy中的小数
t7 = np.array([random.random() for i in range(10)])
print("t7:", t7)
print(t7.dtype)
print()
t8 = np.round(t7, 2)
print("t8:", t8)
如果两个数组的后院维度(trailing dimension,即从末尾开始算起的维度)的轴长度相符或其中一方的长度为1,则认为它们是广播兼容的。广播会在确实和(或)长度为1 的维度上进行。
a.tranpose()
a.t()
a.swapaxis()
np.loadtxt(frame.dtype=np.float,delimiter=None,skoprows=0,usecols=None,unpack=False)
numpy三元运算符
numpy中的clip
t.clip(a,b)将小于a的值替换为a,将大于b的值替换为b
# coding=utf-8
import numpy as np
# print(t1)
def fill_ndarray(t1):
for i in range(t1.shape[1]):
temp_col = t1[:, i] # 当前这一列
nan_num = np.count_nonzero(temp_col != temp_col)
if nan_num != 0:
# 当前一列部位nan的array
temp_not_nan_col = temp_col[temp_col == temp_col]
# 选中当前为nan 的位置,将其赋值为非nan的均值
temp_col[np.isnan(temp_col)] = temp_not_nan_col.mean()
return t1
if __name__ == "__main__":
t1 = np.arange(12).reshape((3,4)).astype("float")
t1[1, 2:] = np.nan
print(t1)
t1 = fill_ndarray(t1)
print("*"*20)
print(t1)
# coding=utf-8
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
us_file_path = "./US_video_data_numbers.csv"
uk_file_path = "./GB_video_data_numbers.csv"
t_uk = np.loadtxt(uk_file_path, delimiter=",", dtype="int")
# 选择喜欢数比五十万小数据
t_uk = t_uk[t_uk[:,1]<=500000]
t_uk_comment = t_uk[:,-1]
t_uk_like = t_uk[:,1]
plt.figure(figsize=(20, 8), dpi=80)
plt.scatter(t_uk_like, t_uk_comment)
plt.show()