Zan's Home

Good Good Study, Day Day Up!


  • 首页

  • 归档

机器学习能解决什么样的问题

发表于 2019-03-08 | 更新于 2021-02-19 | 分类于 ml

问题需要满足以下几个特性

  1. 存在一种模式或者说一种表现可以让我们对它进行改造提高
  2. 规则并不容易定义
  3. 需要有数据

Luhn算法PHP实现

发表于 2019-02-22 | 更新于 2021-02-19 | 分类于 algorithm

关于Luhn算法

参考:

https://en.wikipedia.org/wiki/Luhn_algorithm

https://baike.baidu.com/item/Luhn算法

PHP代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Luhn
{
/**
* 补填校验位
* @param string $rawNo 原始号码
* @return string 补填校验位后的号码
*/
public static function gen($rawNo)
{
return $rawNo . self::calcCheckNum($rawNo);
}

/**
* 校验给定数据
* @param string $no 需要校验的数据
* @return bool true:通过|false:不通过
*/
public static function verify($no)
{
$rawNo = substr($no, 0, -1);
$checkNo = intval(substr($no, -1));
return $checkNo == self::calcCheckNum($rawNo);
}

/**
* 计算校验值
* @param string $rawStr 原始字符串(不含校验位)
* @return int 校验值
*/
private static function calcCheckNum($rawStr)
{
$strrev = strrev($rawStr);
$sum = 0;
for ($i = 0; $i < strlen($strrev); $i++) {
$val = intval($strrev[$i]);
$sum += ($i % 2 == 0) ? ($val > 4 ? (2 * $val - 9) : (2 * $val)) : $val;
}
$ret = 10 - $sum % 10;
return $ret == 10 ? 0 : $ret;
}
}

Vagrant同时管理多台机器

发表于 2019-02-01 | 更新于 2021-02-19 | 分类于 tools

1. Vagrantfile

下面为1个master和3个node的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 启动Node的数量
$workerNodeNumber = 3

# IP前缀
$workerNodeIpBase = "192.168.33."

# Node的IP地址数组(从192.168.33.20开始分配)
$workerNodeIps = $workerNodeNumber.times.collect {|n| $workerNodeIpBase + "#{n+20}"}

Vagrant.configure("2") do |config|

# 定义master
config.vm.define "master" do |master|
master.vm.box = "centos7" # centos7为本地的box(使用`vagrant box list`查看)
master.vm.network "private_network", ip: $workerNodeIpBase + "10" # 192.168.33.10
master.vm.hostname = "master"
master.vm.provider "virtualbox" do |vb|
vb.memory = "1024" # 定义master机器的内存:1024MB
vb.cpus = "1" # 定义master机器的CPU数量:1
vb.name = "master" # 虚拟机名称(在VirtualBox中显示)
end
end

# 定义node
$workerNodeNumber.times do |n|
vmName = "node#{n}"
config.vm.define vmName do |node|
node.vm.box = "centos7"
node.vm.network "private_network", ip: $workerNodeIps[n]
node.vm.hostname = "node#{n}"
node.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
vb.cpus = "1"
vb.name = "node#{n}"
end
end
end
end

2. 启动

1
vagrant up

3. 关闭

1
vagrant halt

Gitlab: refusing to merge unrelated histories

发表于 2019-01-17 | 更新于 2021-02-19 | 分类于 git

1. 错误发生背景

在gitlab中初始化一个项目,并生成了README.md文件;本地已有代码进行合并时报错

2. 处理方法

1
2
# merge命令附加--allow-unrelated-histories
git merge [feature/init] --allow-unrelated-histories # feature/init是已有代码开发分支

在独立项目中使用illuminate/database

发表于 2018-10-20 | 更新于 2021-02-19 | 分类于 php , database

1. Composer引入

1
2
composer require illuminate/database
composer require illuminate/events

2. 创建DB操作类

文件可以创建为/library/DB.php, 内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;

// 注意修改此处的配置
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);

// Set the event dispatcher used by Eloquent models... (optional)
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule->setEventDispatcher(new Dispatcher(new Container));

// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();

// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();

3. 使用DB操作类

1
DB::table('table_name')->select();

4. 更多操作

https://laravel-china.org/docs/laravel/5.7/database/2288

12

xiaozan (i#xiaozan.me)

15 日志
12 分类
19 标签
© 2018 – 2022 xiaozan (i#xiaozan.me)
由 Hexo 强力驱动
|
主题 – NexT.Mist