import numpy as np
t = np.arange(12).reshape(2, 6)
t1 = np.arange(2).reshape(2, 1)
np.aranger(12) #相当于range(12),生成[ 0 1 2 3 4 5 6 7 8 9 10 11]
np.reshape( 块,行 ,列 ) # 分割成多维数组这个是三维数组,二维:np.reshape( 行 ,列 )
数组相加减乘除:
t1 = np.arange(12).reshape((2,6)) #(2,6)
t22 = np.arange(6) #(6,)
t2 = np.arange(4,6).reshape((2,1)) #(2,1)
print(t1)
print(t2)
print(t22)
t1 >> [[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]]
t2>>
[[4]
[5]]
t22>>
[0 1 2 3 4 5]
print(t1*t2)>> [[ 0 4 8 12 16 20]
[30 35 40 45 50 55]]
print(t1*t22)>> [[ 0 1 4 9 16 25]
[ 0 7 16 27 40 55]]
计算平均值:
t10 = np.arange(0,10).reshape((10))
>> [0 1 2 3 4 5 6 7 8 9]
#计算平均数
t_mean = t10.mean(axis=0)
print t_mean
print t10-t_mean
>> 4.5
>> [-4.5 -3.5 -2.5 -1.5 -0.5 0.5 1.5 2.5 3.5 4.5]
#读取csv文件(csv文件数据的格式是以逗号隔开)
us_file_path= "./youtube_video_data/US_video_data_numbers.csv"
uk_file_path= "./youtube_video_data/GB_video_data_numbers.csv"
#np.loadtxt()读取文件
#参数说明(us_file_path :文件路径; delimiter=",":读取的数据以逗号分隔;dtype=int 读取的数据指定类型)
us_data = np.loadtxt(us_file_path,delimiter=",",dtype=int)
#unpack=1转置,对角线旋转不是很理解
uk_data = np.loadtxt(uk_file_path,delimiter=",",dtype=int,unpack=1)
print(uk_data.shape)
uk_data = np.loadtxt(uk_file_path,delimiter=",",dtype=int)
print(uk_data.shape)
numpy取值操作:
t = np.arange(24).reshape((4,6))
>> [[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
#t(,)逗号隔开,前面是对行进行操作,后面是对列进行操作
#选择某几行的某几个列
print t[0:2, 3:6] # 0~2 )行的第3~6 列
>> [[ 3 4 5]
[ 9 10 11]]
#选择两个列表中对应位置的值
print(t[[1,2],[2,3]]) #相当于坐标(1,2)和(2,3)
>> [ 8 15]
#选择某一列(结构也是一维数组)
print(t[:,2])
>> [ 2 8 14 20]
#选择一个值
print(t[2,3])
#修改值:
t1 = np.arange(24,dtype=float).reshape((6,4))
t1[[1,2],[3,1]] = np.nan
print t1
>> [[ 0. 1. 2. 3.]
[ 4. 5. 6. nan]
[ 8. nan 10. 11.]
[12. 13. 14. 15.]
[16. 17. 18. 19.]
[20. 21. 22. 23.]]
numpy
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 文章作者:Tyan博客:noahsnail.com | CSDN | 简书 本文主要是关于numpy的一些基本运算...
- 文章作者:Tyan博客:noahsnail.com | CSDN | 简书 本文主要是关于numpy的一些基本运算...