python-数组-1

# -*- coding: utf-8 -*-
"""
Created on Sat Oct 20 10:04:22 2018

@author: fengjuan
"""

import numpy as np
from numpy.random import randn
'''arr=np.array([1,2,3,4,5])
#print(arr.dtype)
float_arr=arr.astype(np.float64)
#print(float_arr.dtype)
arr1=np.array([3.7,-1.2,-2.6,0.5,12.9,10.2])
print(arr1.dtype)
arr2=arr1.astype(np.int32)#要重新赋一个数组,要不然还会输出arr1的数据类型
print(arr2.dtype)
print(arr2)
#将数字字符转换为数值
numeric_string=np.array(['1.23','5.6','43'])
numeric_string=numeric_string.astype(float)#要重新赋一个数组
print(numeric_string)
int_array=np.arange(10)
calibers=np.array([.22,.270,.357,.380,.44,.50],dtype=np.float64)
print(int_array.astype(calibers.dtype))
empty_unit32=np.empty(8,dtype='u4')
print(empty_unit32)
arr=np.array([[1,2,3],[4,5,6]])
print(arr*arr)
print(arr+arr)
print(arr-arr)
print(1/arr)
print(arr*0.5)
''''''
arr=np.arange(10)
print(arr)
print(arr[5])
print(arr[5:8])
arr[5:8]=12
print(arr)'''
arr3d=np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
#print(arr3d)
'''[[[ 1  2  3]
  [ 4  5  6]]

 [[ 7  8  9]
  [10 11 12]]]'''
#print(arr3d[0])
'''[[1 2 3]
 [4 5 6]]'''
arr3d[0]=42
#print(arr3d)
old_values=arr3d[0].copy()
arr3d[0]=42
#print(arr3d)
'''[[[42 42 42]
  [42 42 42]]

 [[ 7  8  9]
  [10 11 12]]]'''
arr3d[0]=old_values
print(arr3d)
'''[[[42 42 42]
  [42 42 42]]

 [[ 7  8  9]
  [10 11 12]]]'''
arr=np.array([1,2,3,4,5,7,9,8,0])
#print(arr[1:6])
'''[2 3 4 5 7]'''
arr2d=np.array([[1,2,3],[4,5,6],[7,8,9]])
#print(arr2d[:2])
'''
print('this is 1*2',arr2d[:1,:2])
print('this is 2*1',arr2d[:2,:1])
print('this is 2*2',arr2d[:2,:2])'''
'''this is 1*2 [[1 2]]
this is 2*1 [[1]
 [4]]
this is 2*2 [[1 2]'''
data=randn(7,4)
#print(data)
'''[[-1.24734546 -0.4148296  -1.80000535 -2.5189253 ]
 [ 0.5630976  -0.58740314  0.31742987  0.04278619]
 [ 1.13932646 -0.52458922  0.71888754 -0.48672394]
 [ 0.46501    -0.27293732  0.26287021 -1.39207027]
 [ 1.14054827  1.46194662 -1.76638175  1.31461211]
 [-1.80507049  1.75645465 -0.27707486 -0.93877164]
 [ 1.48919028 -1.15454357  1.00450808  1.3627017 ]]
'''

你可能感兴趣的:(python-数组-1)