程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python series - numpy details

編輯:Python
  • numpy
from numpy import *
import numpy as np
# numpy Simple application examples
print(eye(4))
# Create simple ndarray object
a = np.array([1, 2, 3])
print(a)
# Create greater than 1 An array of dimensions Use ndmin Parameters ,ndmin The default value of the parameter is 0
b = np.array([1, 2, 3], ndmin=2)
print(b)
b1 = np.array([2, 3, 4],ndmin=-1)
print(b1)
# Create an array of composite types
c = np.array([1, 2, 3],dtype=complex)
print(c)
# see dtype Use examples
# Create an array , View the data type of the array
da = np.array([1, 2, 3])
print(da.dtype)
# Create a
dt = np.dtype('i4')
print(dt)
# The use of structured data types
# First create structured data types
da = np.dtype(np.int64)
print(da)
dt = np.dtype([('age',np.int8)])
print(dt)
# Apply structured data types to ndarray object
dt = np.dtype([('age',np.int8)])
a = np.array([(10,),(20,),(30,)], dtype = dt)
print(a)
# Use the type object to access the actual column
dt = np.dtype([('age',np.int8)])
a = np.array([(10,),(20,),(30,)], dtype = dt)
print(a['age'])
# Define a structured data type student, Contains string fields name, Integer fields age, And floating point fields marks, And will the dtype Applied to the ndarray object
# Create array
student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])
# Use array for ndarray object
a = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student)
print(a)
# NumPy Data type conversion instance
da = np.array([1.2,1.1,1.0])
# Output da Data type of
print(da.dtype)
# transformation da Data type of
print(da.astype(np.int32))
# Revisit the data type , Found that the data type has not changed
print(da.dtype)
# Re assign the value
da = da.astype(np.int32)
print(da.dtype)
print(da)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved