importnumpy as np
import
matplotlib.pyplot as plt
from sklearn.linear_model import
ridge
from sklearn.metrics import
r2_score, mean_squared_error
from sklearn.neighbors import
kneighborsregressor
from sklearn.preprocessing import
standardscaler
x_train =np.array([
[158, 1],
[170, 1],
[183, 1],
[191, 1],
[155, 0],
[163, 0],
[180, 0],
[158, 0],
[170, 0]
])ss =standardscaler()
x_trainss =ss.fit_transform(x_train)
x_train_log = np.log(x_train + 1)
y_train = [64, 86, 84, 80, 49, 59, 67, 54, 67]
x_test =np.array([
[160, 1],
[196, 1],
[168, 0],
[177, 0]
])x_testss =ss.transform(x_test)
x_test_log = np.log(x_test + 1)
y_test = [66, 87, 68, 74]
k = 5clf = kneighborsregressor(n_neighbors=k)
clf.fit(x_train, y_train)
clf1 = kneighborsregressor(n_neighbors=k)
clf1.fit(x_trainss, y_train)
clf2 =ridge().fit(x_train_log, y_train)
predictions =clf.predict(np.array(x_test))
predictions1 =clf1.predict(x_testss)
predictions2 =clf2.predict(x_test_log)
print('
actual weights: %s
' %y_test)
print('
predicted weights: %s
' %predictions)
print('
predicted weights standardscaler: %s
' %predictions1)
print('
predicted weights log: %s
' %predictions2)
(mean_squared_error(y_test, predictions))
(mean_squared_error(y_test, predictions1))
(mean_squared_error(y_test, predictions2))
(r2_score(y_test, predictions))
(r2_score(y_test, predictions1))
print(r2_score(y_test, predictions2))
結果是:
actual weights: [66, 87, 68, 74]
predicted weights: [62.4 76.8 66. 72.6]
predicted weights by standardscaler: [69.4 76.8 59.2 59.2]
predicted weights by log: [72.98731557 73.88528401 63.37281696 63.60369452]
mean_squared_error: 30.740000000000023
mean_squared_error by standardscaler: 103.02
mean_squared_error by log: 87.57808624078896
0.5424744186046508
-0.5333209302325581
-0.30348779521174274
process finished with exit code 0
我們發現特徵值經過標準化和對數化的轉換後,**均方差偏移反而更大了。本例來自《scikit-learn機器學習》的第三章的最後乙個例子的延展,所以書中的例子感覺還有缺陷,原因是樣本太少了。
ConstraintLayout使用,提公升布局效能
constraintlayout是谷歌在2016年i o大會上發布的全新布局,基本可以實現其他布局的全部功能,重要的是它解決了負責布局過多巢狀的問題,使得效能上有了很大提高。參考分析constraintlayout效能優勢 對android studio的版本要求是2.2以上,現在android s...
使用Service Worker提公升Web應用體驗
service worker能做的事情很多,通過在其生命週期內自定義響應過程可以做到 1.資源快取控制 2.實現離線頁面 3.版本更迭 要注意的幾點是 1.出於安全原因,service worker只能在https下執行。2.需要確保service worker自身的即時更新。最重要的一點是 你需要...
使用tcmalloc提公升mysql效能
網上搜到了tcmalloc,說是這個東西可以讓mysql在高併發下效能也很穩定,同時也說了mysql這個問題是因為malloc記憶體分配函式的bug,這個bug會使高併發的mysql效能急劇下降。使用google的tcmalloc 記憶體分配函式代替libc裡的標準malloc.google的開源效...