免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
返回列表 發帖

python class numpy 練習

  1. class point3:
  2.     x = 0
  3.     y = 0
  4.     z = 0
  5.     def length(self):
  6.         return (self.x**2+self.y**2+self.z**2)**(1/2)
  7.     def normalize(self):
  8.         temp = point3(self.x,self.y,self.z)
  9.         temp.x = temp.x * (1.0/temp.length())
  10.         temp.y = temp.y * (1.0/temp.length())
  11.         temp.z = temp.z * (1.0/temp.length())
  12.         return temp
  13.     def __init__(self,x=0,y=0,z=0):
  14.         self.x=int(x)
  15.         self.y=int(y)
  16.         self.z=int(z)
  17.     def __str__(self):
  18.         return 'x=' + str(self.x) + '\n'\
  19.                +'y=' + str(self.y) + '\n'\
  20.                +'z=' + str(self.z) + '\n'
  21.     def __add__(self,other):
  22.         temp = point3(self.x,self.y,self.z)
  23.         if isinstance(other,point3):
  24.             temp.x = self.x + other.x
  25.             temp.y = self.y + other.y
  26.             temp.z = self.z + other.z
  27.         else:
  28.             temp.x = self.x + other
  29.             temp.y = self.y + other
  30.             temp.z = self.z + other
  31.         return temp
  32.     def __radd__(self,other):
  33.         temp = point3(self.x,self.y,self.z)
  34.         if isinstance(other,point3):
  35.             temp.x = self.x + other.x
  36.             temp.y = self.y + other.y
  37.             temp.z = self.z + other.z
  38.         else:
  39.             temp.x = self.x + other
  40.             temp.y = self.y + other
  41.             temp.z = self.z + other
  42.         return temp
  43.     def __sub__(self,other):
  44.         temp = point3(self.x,self.y,self.z)
  45.         if isinstance(other,point3):
  46.             temp.x = self.x - other.x
  47.             temp.y = self.y - other.y
  48.             temp.z = self.z - other.z
  49.         else:
  50.             temp.x = self.x - other
  51.             temp.y = self.y - other
  52.             temp.z = self.z - other
  53.         return temp
  54.     def __rsub__(self,other):
  55.         temp = point3(self.x,self.y,self.z)
  56.         if isinstance(other,point3):
  57.             temp.x = other.x - self.x
  58.             temp.y = other.y - self.y
  59.             temp.z = other.z - self.z
  60.         else:
  61.             temp.x = other - self.x
  62.             temp.y = other - self.y
  63.             temp.z = other - self.z
  64.         return temp
  65.     def __mul__(self,other):
  66.         temp = point3(self.x,self.y,self.z)
  67.         if isinstance(other,point3):
  68.             temp.x = self.x * other.x
  69.             temp.y = self.y * other.y
  70.             temp.z = self.z * other.z
  71.         else:
  72.             temp.x = self.x * other
  73.             temp.y = self.y * other
  74.             temp.z = self.z * other
  75.         return temp

  76.    
  77. p1 = point3(4,3,0)
  78. p2 = point3(2,2,6)
  79. p3 = point3()


  80. print(p1.length())
  81. print(p1.normalize())

  82. print(p2.length())
  83. print(p2.normalize())

  84. print(p1+p2)
  85. print(p1-p2)
  86. print(p1+5)
  87. print(5+p1)
  88. print(p1-5)
  89. print(5-p1)
  90. print(p1*p2)
  91. print(p1*5)
複製代碼
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

  1. import numpy as np
  2. import matplotlib.pylab as plt

  3. a = [[4,2,1],
  4.      [16,4,1],
  5.      [64,8,1],
  6.      [100,10,1]]
  7. b = [10,50,75,20]
  8. for i in range(4):
  9.     plt.plot(a[i][1],b[i],'ro')
  10.    


  11. b2 = np.matrix(b).transpose()
  12. a2 = np.matrix(a)

  13. c = np.linalg.lstsq(a2,b2,rcond=None)
  14. print("a = ",c[0][0])
  15. print("b = ",c[0][1])
  16. print("c = ",c[0][2])


  17. plt.show()

  18. #----------------------------

  19. p = [[8,0], [1,1], [8,3], [7,4], [2,2], [6,3], [3,4], [0,7], [7,7] ,[1,4]]
  20. n = []

  21. for i in range(len(p)):
  22.     plt.plot(p[i][0],p[i][1],'ro')
  23.    
  24. plt.show()
  25. c=0
  26. d=0
  27. for i in range(len(a)):
  28.     c += np.linalg.norm(p[i][0])
  29.     d += np.linalg.norm(p[i][1])

  30. c = c/10
  31. d = d/10

  32. #print(a)

  33. for i in range(len(p)):
  34.     p[i][0] -= c
  35.     p[i][1] -= d

  36. A = np.matrix(p)
  37. x = A*A.transpose()

  38. U,D,V = np.linalg.svd(A)
  39. print("Q is")
  40. print(V)
  41. n = p * V

  42. for i in range(len(p)):
  43.     plt.plot(n[i,0],n[i,1],'ro')
  44. plt.show()
複製代碼

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

  1. from openpyxl import load_workbook
  2. import numpy as np

  3. wb = load_workbook(filename = 'Scores.xlsx', data_only = True)

  4. ws = wb['Sheet1']

  5. L = []
  6. L2 = []
  7. L3 = []
  8. L4 = []
  9. print("name","min","max",sep="\t")
  10. for iRow in range(2, ws.max_row + 1):
  11.     for iCol in range(3, 6):
  12.         cell = ws.cell(row = iRow, column = iCol)
  13.         L.append(cell.value)
  14.     cellname = ws.cell(row = iRow, column = 2)
  15.     print(cellname.value , np.min(L) , np.max(L) , sep="\t")
  16.     L.clear()
  17.    
  18. print("----------------------------")

  19. print(" ","sum","mean","median","標準差   變異量",sep="\t")

  20. for iCol in range(3, 6):
  21.     for iRow in range(2, ws.max_row + 1):
  22.         cell = ws.cell(row = iRow, column = iCol)
  23.         cellexam = ws.cell(row = iRow, column = 7)
  24.         if (cellexam.value<60):
  25.             L.append(cell.value)
  26.     cellname = ws.cell(row = 1, column = iCol)
  27.     print(cellname.value, np.sum(L) , '%.3f' % np.mean(L) , np.median(L) , '%.3f' % np.std(L) , '%.3f' % np.var(L) , sep="\t")
  28.     L.clear()
  29.    
  30. print("----------------------------")

  31. print(" covariance matrix ","Pearson correlation matrix",sep="\t")
  32. for iRow in range(2, ws.max_row + 1):
  33.     for iCol in range(3, 6):
  34.         cell = ws.cell(row = iRow, column = iCol)
  35.         L.append(cell.value)
  36.     cellexam = ws.cell(row = iRow, column = 7)
  37.     L2.append(cellexam.value)
  38.     L3.append(np.mean(L))
  39.     L.clear

  40. A1 = np.array(L3)
  41. A2 = np.array(L2)
  42. print("\n", np.cov(A1,A2) ,"\n", np.corrcoef(A1,A2))

  43. print("----------------------------")

  44. print("discrete difference")

  45. for iRow in range(2, ws.max_row + 1):
  46.     for iCol in range(3, 6):
  47.         cell = ws.cell(row = iRow, column = iCol)
  48.         L.append(cell.value)
  49.     cell1 = ws.cell(row = iRow, column = 7)
  50.     cell2 = (cell1.value + np.mean(L))/2 #final
  51.     L4.append(cell2)
  52.    
  53. L4.sort()
  54. A3 = np.array(L4)
  55. print(np.diff(A3))


  56.    
複製代碼

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

返回列表