2021年03月30日信息消化
每天学点机器学习
利用flask与docker部署sciki-learn模型
原文:How to Easily Deploy Machine Learning Models Using Flask
repository:abhinavsagar/machine-learning-deployment
# 模型训练
linear = LinearRegression() # 初始化线性回归模型
linear.fit(x_train,y_train) # 利用测试数据进行拟合
# 模型导出
pickle.dump(linear, open('model.pkl','wb'))
# 预测测试
model = pickle.load(open('model.pkl','rb'))
print(model.predict([[12,2,2,1,2,2]]))
Docker部署
FROM python:3.9.0
RUN mkdir /app
## We copy everything in the root directory into our /app directory
ADD . /app
WORKDIR /app
#Install the dependencies (pip3 freeze > requirements.txt)
RUN pip install -r requirements.txt
EXPOSE 7778 # public
#Run the container
CMD [ "python", "./app.py" ]
构建镜像并运行容器
# build image
docker build -t <project> .
# start container
docker run -p 5000:5000 <project>
其他值得阅读
我的个人Mac高效配置
原文:My Personal Mac Productivity Setup
Karabiner-Elements + Hammerspoon ⚡
通过hyperkey快速切换应用与窗口layout
Aren’t you ever tired of pressing
⌘ + tabevery time you want to switch between apps? And how many times have you missed the apps that you want to switch on so you have to go through the cycle again? I felt your pain. Fortunately, I found something that can mitigate this problem,Karabiner-Elements + Hammerspoon
brew cask install hammerspoon
brew cask install karabiner-elements
# Error: Unknown command: --cask
brew install --cask hammerspoon
brew install --cask karabiner-elements
在我的设置上,因为我几乎不用capslock,所以我把它作为我的超键,翻译成 ⌘ + control + option + shift.
On my setup, because I barely used capslock, I made it as my hyperkey and translate it as ⌘ + control + option + shift.
打开Karabiner-Elements,在Complex modifications tab下添加如下规则
mkdir ~/.hammerspoon/
# init.lua
# sizeup.lua
配置脚本参考附录。
※因为开karabiner-elements发现所有键盘的映射都错位了...就放弃了 capslock = ⌘ + control + option + shift这个组合,直接用control + option + shift来实现。
Documents
-
Keycodes: www.hammerspoon.org/docs/hs.key…
-
Window: www.hammerspoon.org/docs/hs.win…
其他参考设置:
Alfred workflowy
需要买Powerpack
npm install --global alfred-currency-conversion
9个可以从今天开始使用的终端命令
原文:9 Terminal Commands You Can Start Using Today
# Cut Command
cat sample.txt | cut -c 5-10
# Paste Command to Create Columns
seq 1 10 | paste - - -
# outputs the file content without empty lines.
cat sample.txt | grep -v '^$'
# bc (Basic Calculator)
echo '10*2/5' | bc
# calculate 100
echo {1..100} | tr ' ' '*' | bc
# to copy and delete the nth line and paste it to the mth line
sed '<n>h;<n>d;<m>G'
# rename
ls
>> file-1 file-2 file-3 file-4
rename -A "my-" *
>> my-file-1 my-file-2 my-file-3 my-file-4
附录:snippets
- init.lua
require "sizeup"
-- https://medium.com/macoclock/my-personal-mac-productivity-setup-b99a388e6163
-- Specify your combination (your hyperkey)
-- local hyper = { "cmd", "alt", "ctrl", "shift" } -- with karabiner-elements
local hyper = {"alt", "ctrl", "shift" }
-- We use 0 to reload the configuration
hs.hotkey.bind(hyper, "0", function()
hs.reload()
end)
-- Notify about the config reload
hs.notify.new({title="Hammerspoon", informativeText="Config loaded"}):send()
-- this one for lock the screen
hs.hotkey.bind(hyper, "l", function()
hs.caffeinate.lockScreen()
end)
-- these are only my favourite apps, you can replace it with yours
local applicationHotkeys = {
c = 'Google Chrome',
d = 'Chromium',
w = 'Wechat',
t = 'iTerm',
s = 'Slack',
v = 'Visual Studio Code',
p = 'Typora',
}
for key, app in pairs(applicationHotkeys) do
hs.hotkey.bind(hyper, key, function()
hs.application.launchOrFocus(app)
end)
end
-- change window to other monitor
hs.hotkey.bind(hyper, 'n', function()
local win = hs.window.focusedWindow()
local screen = win:screen()
win:move(win:frame():toUnitRect(screen:frame()), screen:next(), true, 0)
end)
- sizeup.lua
-- https://gist.github.com/josephholsten/1e17c7418d9d8ec0e783
-- === sizeup ===
--
-- SizeUp emulation for hammerspoon
--
-- To use, you can tweak the key bindings and the margins
local sizeup = { }
--------------
-- Bindings --
--------------
local hyper = {"alt", "ctrl", "shift" }
--- Split Screen Actions ---
-- Send Window Left
hs.hotkey.bind(hyper, "Left", function()
sizeup.send_window_left()
end)
-- Send Window Right
hs.hotkey.bind(hyper, "Right", function()
sizeup.send_window_right()
end)
-- Send Window Up
hs.hotkey.bind(hyper, "Up", function()
sizeup.send_window_up()
end)
-- Send Window Down
hs.hotkey.bind(hyper, "Down", function()
sizeup.send_window_down()
end)
--- Quarter Screen Actions ---
-- Send Window Upper Left
hs.hotkey.bind({"ctrl", "alt", "cmd"}, "Left", function()
sizeup.send_window_upper_left()
end)
-- Send Window Upper Right
hs.hotkey.bind({"ctrl","alt", "cmd"}, "Up", function()
sizeup.send_window_upper_right()
end)
-- Send Window Lower Left
hs.hotkey.bind({"ctrl", "alt", "cmd"}, "Down", function()
sizeup.send_window_lower_left()
end)
-- Send Window Lower Right
hs.hotkey.bind({"ctrl", "alt", "cmd"}, "Right", function()
sizeup.send_window_lower_right()
end)
--- Multiple Monitor Actions ---
-- Send Window Prev Monitor
hs.hotkey.bind({ "ctrl", "alt" }, "Left", function()
sizeup.send_window_prev_monitor()
end)
-- Send Window Next Monitor
hs.hotkey.bind({ "ctrl", "alt" }, "Right", function()
sizeup.send_window_next_monitor()
end)
--- Spaces Actions ---
-- Apple no longer provides any reliable API access to spaces.
-- As such, this feature no longer works in SizeUp on Yosemite and
-- Hammerspoon currently has no solution that isn't a complete hack.
-- If you have any ideas, please visit the ticket
--- Snapback Action ---
hs.hotkey.bind(hyper, "Z", function()
sizeup.snapback()
end)
--- Other Actions ---
-- Make Window Full Screen
hs.hotkey.bind(hyper, "M", function()
sizeup.maximize()
end)
-- Send Window Center
hs.hotkey.bind(hyper, "J", function()
sizeup.move_to_center_absolute({w=800, h=600})
-- sizeup.move_to_center_relative({w=0.75, h=0.75})
end)
-------------------
-- Configuration --
-------------------
-- Margins --
sizeup.screen_edge_margins = {
top = 0, -- px
left = 0,
right = 0,
bottom = 0
}
sizeup.partition_margins = {
x = 0, -- px
y = 0
}
-- Partitions --
sizeup.split_screen_partitions = {
x = 0.5, -- %
y = 0.5
}
sizeup.quarter_screen_partitions = {
x = 0.5, -- %
y = 0.5
}
----------------
-- Public API --
----------------
function sizeup.send_window_left()
local s = sizeup.screen()
local ssp = sizeup.split_screen_partitions
local g = sizeup.gutter()
sizeup.set_frame("Left", {
x = s.x,
y = s.y,
w = (s.w * ssp.x) - sizeup.gutter().x,
h = s.h
})
end
function sizeup.send_window_right()
local s = sizeup.screen()
local ssp = sizeup.split_screen_partitions
local g = sizeup.gutter()
sizeup.set_frame("Right", {
x = s.x + (s.w * ssp.x) + g.x,
y = s.y,
w = (s.w * (1 - ssp.x)) - g.x,
h = s.h
})
end
function sizeup.send_window_up()
local s = sizeup.screen()
local ssp = sizeup.split_screen_partitions
local g = sizeup.gutter()
sizeup.set_frame("Up", {
x = s.x,
y = s.y,
w = s.w,
h = (s.h * ssp.y) - g.y
})
end
function sizeup.send_window_down()
local s = sizeup.screen()
local ssp = sizeup.split_screen_partitions
local g = sizeup.gutter()
sizeup.set_frame("Down", {
x = s.x,
y = s.y + (s.h * ssp.y) + g.y,
w = s.w,
h = (s.h * (1 - ssp.y)) - g.y
})
end
function sizeup.send_window_upper_left()
local s = sizeup.screen()
local qsp = sizeup.quarter_screen_partitions
local g = sizeup.gutter()
sizeup.set_frame("Upper Left", {
x = s.x,
y = s.y,
w = (s.w * qsp.x) - g.x,
h = (s.h * qsp.y) - g.y
})
end
function sizeup.send_window_upper_right()
local s = sizeup.screen()
local qsp = sizeup.quarter_screen_partitions
local g = sizeup.gutter()
sizeup.set_frame("Upper Right", {
x = s.x + (s.w * qsp.x) + g.x,
y = s.y,
w = (s.w * (1 - qsp.x)) - g.x,
h = (s.h * (qsp.y)) - g.y
})
end
function sizeup.send_window_lower_left()
local s = sizeup.screen()
local qsp = sizeup.quarter_screen_partitions
local g = sizeup.gutter()
sizeup.set_frame("Lower Left", {
x = s.x,
y = s.y + (s.h * qsp.y) + g.y,
w = (s.w * qsp.x) - g.x,
h = (s.h * (1 - qsp.y)) - g.y
})
end
function sizeup.send_window_lower_right()
local s = sizeup.screen()
local qsp = sizeup.quarter_screen_partitions
local g = sizeup.gutter()
sizeup.set_frame("Lower Right", {
x = s.x + (s.w * qsp.x) + g.x,
y = s.y + (s.h * qsp.y) + g.y,
w = (s.w * (1 - qsp.x)) - g.x,
h = (s.h * (1 - qsp.y)) - g.y
})
end
function sizeup.send_window_prev_monitor()
hs.alert.show("Prev Monitor")
local win = hs.window.focusedWindow()
local nextScreen = win:screen():previous()
win:moveToScreen(nextScreen)
end
function sizeup.send_window_next_monitor()
hs.alert.show("Next Monitor")
local win = hs.window.focusedWindow()
local nextScreen = win:screen():next()
win:moveToScreen(nextScreen)
end
-- snapback return the window to its last position. calling snapback twice returns the window to its original position.
-- snapback holds state for each window, and will remember previous state even when focus is changed to another window.
function sizeup.snapback()
local win = sizeup.win()
local id = win:id()
local state = win:frame()
local prev_state = sizeup.snapback_window_state[id]
if prev_state then
win:setFrame(prev_state)
end
sizeup.snapback_window_state[id] = state
end
function sizeup.maximize()
sizeup.set_frame("Full Screen", sizeup.screen())
end
--- move_to_center_relative(size)
--- Method
--- Centers and resizes the window to the the fit on the given portion of the screen.
--- The argument is a size with each key being between 0.0 and 1.0.
--- Example: win:move_to_center_relative(w=0.5, h=0.5) -- window is now centered and is half the width and half the height of screen
function sizeup.move_to_center_relative(unit)
local s = sizeup.screen()
sizeup.set_frame("Center", {
x = s.x + (s.w * ((1 - unit.w) / 2)),
y = s.y + (s.h * ((1 - unit.h) / 2)),
w = s.w * unit.w,
h = s.h * unit.h
})
end
--- move_to_center_absolute(size)
--- Method
--- Centers and resizes the window to the the fit on the given portion of the screen given in pixels.
--- Example: win:move_to_center_relative(w=800, h=600) -- window is now centered and is 800px wide and 600px high
function sizeup.move_to_center_absolute(unit)
local s = sizeup.screen()
sizeup.set_frame("Center", {
x = (s.w - unit.w) / 2,
y = (s.h - unit.h) / 2,
w = unit.w,
h = unit.h
})
end
------------------
-- Internal API --
------------------
-- SizeUp uses no animations
hs.window.animation_duration = 0.0
-- Initialize Snapback state
sizeup.snapback_window_state = { }
-- return currently focused window
function sizeup.win()
return hs.window.focusedWindow()
end
-- display title, save state and move win to unit
function sizeup.set_frame(title, unit)
hs.alert.show(title)
local win = sizeup.win()
sizeup.snapback_window_state[win:id()] = win:frame()
return win:setFrame(unit)
end
-- screen is the available rect inside the screen edge margins
function sizeup.screen()
local screen = sizeup.win():screen():frame()
local sem = sizeup.screen_edge_margins
return {
x = screen.x + sem.left,
y = screen.y + sem.top,
w = screen.w - (sem.left + sem.right),
h = screen.h - (sem.top + sem.bottom)
}
end
-- gutter is the adjustment required to accomidate partition
-- margins between windows
function sizeup.gutter()
local pm = sizeup.partition_margins
return {
x = pm.x / 2,
y = pm.y / 2
}
end
--- hs.window:moveToScreen(screen)
--- Method
--- move window to the the given screen, keeping the relative proportion and position window to the original screen.
--- Example: win:moveToScreen(win:screen():next()) -- move window to next screen
function hs.window:moveToScreen(nextScreen)
local currentFrame = self:frame()
local screenFrame = self:screen():frame()
local nextScreenFrame = nextScreen:frame()
self:setFrame({
x = ((((currentFrame.x - screenFrame.x) / screenFrame.w) * nextScreenFrame.w) + nextScreenFrame.x),
y = ((((currentFrame.y - screenFrame.y) / screenFrame.h) * nextScreenFrame.h) + nextScreenFrame.y),
h = ((currentFrame.h / screenFrame.h) * nextScreenFrame.h),
w = ((currentFrame.w / screenFrame.w) * nextScreenFrame.w)
})
end