繁體
|
簡體
Sclub交友聊天~加入聊天室當版主
(檢舉)
分享
新浪微博
QQ空间
人人网
腾讯微博
Facebook
Google+
Plurk
Twitter
Line
標題:
python class numpy 練習
[打印本頁]
作者:
Smallcat
時間:
2018-11-23 16:17
標題:
python class numpy 練習
class point3:
x = 0
y = 0
z = 0
def length(self):
return (self.x**2+self.y**2+self.z**2)**(1/2)
def normalize(self):
temp = point3(self.x,self.y,self.z)
temp.x = temp.x * (1.0/temp.length())
temp.y = temp.y * (1.0/temp.length())
temp.z = temp.z * (1.0/temp.length())
return temp
def __init__(self,x=0,y=0,z=0):
self.x=int(x)
self.y=int(y)
self.z=int(z)
def __str__(self):
return 'x=' + str(self.x) + '\n'\
+'y=' + str(self.y) + '\n'\
+'z=' + str(self.z) + '\n'
def __add__(self,other):
temp = point3(self.x,self.y,self.z)
if isinstance(other,point3):
temp.x = self.x + other.x
temp.y = self.y + other.y
temp.z = self.z + other.z
else:
temp.x = self.x + other
temp.y = self.y + other
temp.z = self.z + other
return temp
def __radd__(self,other):
temp = point3(self.x,self.y,self.z)
if isinstance(other,point3):
temp.x = self.x + other.x
temp.y = self.y + other.y
temp.z = self.z + other.z
else:
temp.x = self.x + other
temp.y = self.y + other
temp.z = self.z + other
return temp
def __sub__(self,other):
temp = point3(self.x,self.y,self.z)
if isinstance(other,point3):
temp.x = self.x - other.x
temp.y = self.y - other.y
temp.z = self.z - other.z
else:
temp.x = self.x - other
temp.y = self.y - other
temp.z = self.z - other
return temp
def __rsub__(self,other):
temp = point3(self.x,self.y,self.z)
if isinstance(other,point3):
temp.x = other.x - self.x
temp.y = other.y - self.y
temp.z = other.z - self.z
else:
temp.x = other - self.x
temp.y = other - self.y
temp.z = other - self.z
return temp
def __mul__(self,other):
temp = point3(self.x,self.y,self.z)
if isinstance(other,point3):
temp.x = self.x * other.x
temp.y = self.y * other.y
temp.z = self.z * other.z
else:
temp.x = self.x * other
temp.y = self.y * other
temp.z = self.z * other
return temp
p1 = point3(4,3,0)
p2 = point3(2,2,6)
p3 = point3()
print(p1.length())
print(p1.normalize())
print(p2.length())
print(p2.normalize())
print(p1+p2)
print(p1-p2)
print(p1+5)
print(5+p1)
print(p1-5)
print(5-p1)
print(p1*p2)
print(p1*5)
複製代碼
作者:
Smallcat
時間:
2018-11-30 16:02
from openpyxl import load_workbook
import numpy as np
wb = load_workbook(filename = 'Scores.xlsx', data_only = True)
ws = wb['Sheet1']
L = []
L2 = []
L3 = []
L4 = []
print("name","min","max",sep="\t")
for iRow in range(2, ws.max_row + 1):
for iCol in range(3, 6):
cell = ws.cell(row = iRow, column = iCol)
L.append(cell.value)
cellname = ws.cell(row = iRow, column = 2)
print(cellname.value , np.min(L) , np.max(L) , sep="\t")
L.clear()
print("----------------------------")
print(" ","sum","mean","median","標準差 變異量",sep="\t")
for iCol in range(3, 6):
for iRow in range(2, ws.max_row + 1):
cell = ws.cell(row = iRow, column = iCol)
cellexam = ws.cell(row = iRow, column = 7)
if (cellexam.value<60):
L.append(cell.value)
cellname = ws.cell(row = 1, column = iCol)
print(cellname.value, np.sum(L) , '%.3f' % np.mean(L) , np.median(L) , '%.3f' % np.std(L) , '%.3f' % np.var(L) , sep="\t")
L.clear()
print("----------------------------")
print(" covariance matrix ","Pearson correlation matrix",sep="\t")
for iRow in range(2, ws.max_row + 1):
for iCol in range(3, 6):
cell = ws.cell(row = iRow, column = iCol)
L.append(cell.value)
cellexam = ws.cell(row = iRow, column = 7)
L2.append(cellexam.value)
L3.append(np.mean(L))
L.clear
A1 = np.array(L3)
A2 = np.array(L2)
print("\n", np.cov(A1,A2) ,"\n", np.corrcoef(A1,A2))
print("----------------------------")
print("discrete difference")
for iRow in range(2, ws.max_row + 1):
for iCol in range(3, 6):
cell = ws.cell(row = iRow, column = iCol)
L.append(cell.value)
cell1 = ws.cell(row = iRow, column = 7)
cell2 = (cell1.value + np.mean(L))/2 #final
L4.append(cell2)
L4.sort()
A3 = np.array(L4)
print(np.diff(A3))
複製代碼
作者:
Smallcat
時間:
2018-12-14 16:58
import numpy as np
import matplotlib.pylab as plt
a = [[4,2,1],
[16,4,1],
[64,8,1],
[100,10,1]]
b = [10,50,75,20]
for i in range(4):
plt.plot(a[i][1],b[i],'ro')
b2 = np.matrix(b).transpose()
a2 = np.matrix(a)
c = np.linalg.lstsq(a2,b2,rcond=None)
print("a = ",c[0][0])
print("b = ",c[0][1])
print("c = ",c[0][2])
plt.show()
#----------------------------
p = [[8,0], [1,1], [8,3], [7,4], [2,2], [6,3], [3,4], [0,7], [7,7] ,[1,4]]
n = []
for i in range(len(p)):
plt.plot(p[i][0],p[i][1],'ro')
plt.show()
c=0
d=0
for i in range(len(a)):
c += np.linalg.norm(p[i][0])
d += np.linalg.norm(p[i][1])
c = c/10
d = d/10
#print(a)
for i in range(len(p)):
p[i][0] -= c
p[i][1] -= d
A = np.matrix(p)
x = A*A.transpose()
U,D,V = np.linalg.svd(A)
print("Q is")
print(V)
n = p * V
for i in range(len(p)):
plt.plot(n[i,0],n[i,1],'ro')
plt.show()
複製代碼
歡迎光臨 小貓貓大聯盟!幻與想的境界\(0w0)/ (http://smallcat.utmall.com/)
Powered by Discuz! 7.2