#安裝nvm
brew install nvm
#設定路徑
echo "source $(brew --prefix nvm)/nvm.sh" >> ~/.bash_profile
. ~/.bash_profile
#查看版本
nvm ls-remote
#安裝8.x
nvm install v8.15.1
#顯示已安裝版本
nvm ls
#切換版本
nvm use v8.15.1
#設定PATH
echo "PATH=$PATH:$(dirname $(nvm which v8.15.1))" >> ~/.bash_profile
. ~/.bash_profile
分類彙整: programming
[python練習] numpy
引用
import numpy as np;
1維陣列
a = np.array([1, 2, 3]);
2維陣列
a = np.array([ [1, 2, 3], [4, 5, 6] ]);
3維陣列
a = np.array([ [ [1, 2, 3], [4, 5, 6] ], [ [7, 8, 9], [10, 11, 12] ] ]);
內積 (vector dot)
v1 = np.array([1, 2, 3]);
v2 = np.array([4, 5, 6]);
v3 = np.dot(v1,v2); #v3 = 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32
print(v3);
矩陣相乘 (matrix multiplication)
m1 = np.array([ [1, 2, 3], [4, 5, 6] ]); #size: 2*3
m2 = np.array([ [1, 2 ], [3, 4], [5, 6] ]); #size: 3*2
m3 = np.matmul(m1,m2); # m3= [ [ 1*1+2*3+3*5 1*2+2*4+3*6] [ 4*1+5*3+6*5 4*2+5*4+6*6 ] ] = [ [ 22 28 ] [ 49 64 ] ]
print(m3);
元素相乘 (multiply)
- 不常用
m1 = np.arange(1,5).reshape(2,2); # m1 = [ [ 1 2 ] [ 3 4 ] ]
m2 = np.arange(0,4).reshape(2,2); # m2 = [ [ 0 1 ] [ 2 3 ] ]
m3 = np.multiply(m1,m2); # m3 = [ [ 1*0 2*1 ] [ 3*2 4*3 ] ] = [ [ 0 2 ] [ 6 12 ] ]
print(m3);
[python練習] pyplot
import matplotlib.pyplot as plt
import numpy as np
#產生隨機資料
x_data = np.linspace(-100, 100, 300)[:, np.newaxis];
#加入雜訊
noise = np.random.normal(0, 300, x_data.shape);
y_data = np.square(x_data) - 0.5 + noise;
plt.plot(x_data, y_data, 'g.');
#動態繪製sin曲線
x = np.arange(-100,100)
for i in range(1000):
#嘗試移除畫過的sin曲線,第一次會發生錯誤,因此用try/except
try:
lines.pop(0).remove()
except Exception:
pass
#重新計算y值
y = 1000*np.sin((x+i) * np.pi/ 9.0)
#繪製sin曲線
lines=plt.plot(x,y,'r-')
#延遲0.1秒
plt.pause(0.1)
在centos 7上安裝nvidia docker
#安裝 docker-ce
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce-18.06.0.ce -y
systemctl enable docker && systemctl start docker
# 移除舊版nvidia-docker
docker volume ls -q -f driver=nvidia-docker | xargs -r -I{} -n1 docker ps -q -a -f volume={} | xargs -r docker rm -f
sudo yum remove nvidia-docker
# 加入repositories
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.repo | sudo tee /etc/yum.repos.d/nvidia-docker.repo
# 安裝 nvidia-docker2
sudo yum install -y nvidia-docker2
sudo pkill -SIGHUP dockerd
# 測試
docker run --runtime=nvidia --rm nvidia/cuda:9.0-base nvidia-smi
windows下git clone enter轉換的問題
在cmd下,關掉git自動轉換的功能
git config --global core.autocrlf false
install cuda 10.0 on centos 7
如下:
#安裝必要套件
yum –y update
yum install kernel-devel kernel-headers gcc dkms acpid libglvnd-glx libglvnd-opengl libglvnd-devel pkgconfig pciutils gcc-c++ wget
yum groupinstall "Development Tools" -y
#Nvidia 硬體檢查
lspci | grep -E "VGA|3D"
#Blacklist nouveau
echo "blacklist nouveau" >> /etc/modprobe.d/nvidia-installer-disable-nouveau.conf && echo "options nouveau modeset=0" >> /etc/modprobe.d/nvidia-installer-disable-nouveau.conf
#regenerate the initramfs
dracut /boot/initramfs-$(uname -r).img $(uname -r) --force
reboot
wget http://tw.download.nvidia.com/XFree86/Linux-x86_64/410.78/NVIDIA-Linux-x86_64-410.78.run
bash NVIDIA-Linux-x86_64-410.78.run
#下載cuda
wget https://developer.nvidia.com/compute/cuda/10.0/Prod/local_installers/cuda_10.0.130_410.48_linux
mv cuda_10.0.130_410.48_linux cuda_10.0.130_410.48_linux.run
#安裝cuda
bash cuda_10.0.130_410.48_linux.run
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-10.0/lib64
export CUDA_HOME=/usr/local/cuda-10.0
export PATH=$PATH:/usr/local/cuda-10.0/bin
cd ~/NVIDIA_CUDA-10.0_Samples/1_Utilities/deviceQuery
make
./deviceQuery
cd ~/NVIDIA_CUDA-10.0_Samples/0_Simple/vectorAdd
make
./vectorAdd
Latent Semantic Analysis (LSA) 測試
參考下列幾個網址寫出來的code (for Matlab)
http://lsa.colorado.edu/papers/dp1.LSAintro.pdf
https://blog.csdn.net/pipisorry/article/details/42560331
https://read01.com/zh-tw/0zeKRM.html#.W7C8PhMzbKZ
C=[ [ 0,0,1,1,0,0,0,0,0 ];
[ 0,0,0,0,0,1,0,0,1 ];
[ 0,1,0,0,0,0,0,1,0 ];
[ 0,0,0,0,0,0,1,0,1 ];
[ 1,0,0,0,0,1,0,0,0 ];
[ 1,1,1,1,1,1,1,1,1 ];
[ 1,0,1,0,0,0,0,0,0 ];
[ 0,0,0,0,0,0,1,0,1 ];
[ 0,0,0,0,0,2,0,0,1 ];
[ 1,0,1,0,0,0,0,1,0 ];
[ 0,0,0,1,1,0,0,0,0 ]; ];
[w,s,p]=svd(C);
plot(-p(:,2),-p(:,3),'.')
hold on
plot(-w(:,2),-w(:,3),'.')
Label_1={'book';'dads';'dummies';'estate';'guide';'investing';'market';'real';'rich';'stock';'value'};
text(-w(:,2),-w(:,3),Label_1,'Color','red','FontSize',14);
Label_2={'T1';'T2';'T3';'T4';'T5';'T6';'T7';'T8';'T9';};
text(-p(:,2),-p(:,3),Label_2,'Color','blue','FontSize',14);
hold off
結果呈現如下:
MongoDB Replica Set高可用性的建置
- 建立三個MongoDB
echo "[info] 建立測試環境";
docker network create mongo-cluster >/dev/null 2>&1
docker rm -f mongo1 mongo2 mongo3 >/dev/null 2>&1
for i in 1 2 3
do
echo " mongo${i} 建立"
docker run \
-d \
-p 3000$i:27017 \
--name mongo$i \
--net mongo-cluster \
mongo mongod --replSet my-mongo-set >/dev/null 2>&1
done
- 接著設定三者關係,並強制設定electionTimeoutMillis為500ms.
echo " 設定mongo cluster"
docker exec -it mongo3 mongo --eval "config={\"_id\":\"my-mongo-set\",\"members\":[{\"_id\":0,\"host\":\"mongo1:27017\"},{\"_id\":1,\"host\":\"mongo2:27017\"},{\"_id\":2,\"host\":\"mongo3:27017\"}],\"settings\": { \"electionTimeoutMillis\": 500 }}; rs.initiate(config)"
- PHP測試
#https://github.com/mongodb/mongo-php-library
require_once __DIR__ . "/vendor/autoload.php";
try{
$client = new MongoDB\Client("mongodb://mongo1:27017,mongo2:27017,mongo3:27017",[],[
'typeMap' => [
'array' => 'array',
'document' => 'array',
'root' => 'array',
],
]);
$db = $client->selectDatabase('test');
$cursor = $db->command(['isMaster' => 1]);
echo "[info] 成功連線至: {$cursor->toArray()[0]['primary']}\n";
} catch(Exception $e) {
echo "[error] 無法線連\n";
exit;
}
{ //若Collection:testA不存在則建立.
if((iterator_count($db->listCollections([
'filter'=>[
'name'=>'testA'
]
])))==0) //當數量為0時,建立testA
$db->createCollection('testA');
}
//連線至Collection:testA
$collection=$db->selectCollection('testA');
//若沒有任何資料,則建立100則隨機資料
if($collection->count()==0){
for($i=0;$i<100;$i++){
$insertOneResult = $collection->insertOne([
'idx' => $i,
'value' => "{$i}, rand: ".rand(0,9999),
]);
}
}
//取得最後三筆資料
foreach($collection->find([],[
'limit' => 3,
'projection' => ['_id'=>0],
'sort' => [
'idx'=>-1
]
]) as $rs){
echo "\tidx:{$rs['idx']}\tvalue:{$rs['value']}\n";
}
使用google map geolocation api對raspberry pi進行地理定位
下面範例使用了Google Map GeoLocation Api對raspberry pi進行地理定位,
其中key可在https://console.developers.google.com申請,每日免費額度是2500(每100秒限制10000次)
/*
npm i pi-wifi google-geolocation
node main.js
*/
function geolocation(callback){
var geolocation = require ('google-geolocation') ({
key: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
});
var piWifi = require('pi-wifi');
piWifi.scan(function(err, networks) {
if (err) {
return console.error(err.message);
}
//console.log(networks);
var items=[];
for(i in networks){
items.push({
'macAddress': networks[i].bssid,
'signalStrength': networks[i].signalLevel,
});
}
var params={
wifiAccessPoints: items,
};
geolocation (params, (err, data) => {
if (err) {
return;
}
var result={
'lat': data.location.lat,
'lng': data.location.lng,
'accuracy': data.accuracy,
'type': 'wifi',
};
if(callback && typeof(callback) == "function")
callback(result);
});
});
}
geolocation(function(location){
console.log(location);
var url ="https://www.google.com.tw/maps/search/"+location.lat+","+location.lng;
console.log(url);
});
install nodejs 6 on raspberry pi
armv6的raspberry pi似乎無法直接用apt-get install方式安裝.
所以改用下列語法安裝
wget http://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-armv6l.tar.gz && \
tar -xzf node-v6.9.5-linux-armv6l.tar.gz && \
mv node-v6.9.5-linux-armv6l nodejs && \
rm node-v6.9.5-linux-armv6l.tar.gz && \
cp -R * /usr/local/ && \
rm -r -f nodejs && \
ln -s /usr/local/nodejs/bin/node /usr/bin/node && \
ln -s /usr/local/nodejs/bin/npm /usr/bin/npm