k = 3 rnd = 0 ROUND_LIMIT = 10 THRESHOLD = 1e-10 melons = [] clusters = [] f = open('melons.txt', 'r') for line in f: melons.append(np.array(line.split(' '), dtype = np.string_).astype(np.float64)) mean_vectors = random.sample(melons, k) while True: rnd += 1 change = 0 clusters = [] for i in range(k): clusters.append([]) for melon in melons: c = np.argmin( list(map( lambda vec: np.linalg.norm(melon - vec, ord = 2), mean_vectors)) ) clusters[c].append(melon) for i in range(k): new_vector = np.zeros((1,2)) for melon in clusters[i]: new_vector += melon new_vector /= len(clusters[i]) change += np.linalg.norm(mean_vectors[i] - new_vector, ord = 2) mean_vectors[i] = new_vector if rnd > ROUND_LIMIT or change < THRESHOLD: break print('最终迭代%d轮'%rnd) colors = ['red', 'green', 'black'] for i, col in zip(range(k), colors): for melon in clusters[i]: plt.scatter(melon[0], melon[1], color = col) plt.show()
时间: 2024-04-28 22:22:17 浏览: 172
这段代码是 K-means 聚类算法的实现,它的作用是将 melons.txt 文件中的数据分成 k 个簇。其中,k=3 表示聚成三个簇,ROUND_LIMIT=10 表示最多迭代十次,THRESHOLD=1e-10 表示当簇中心向量的变化小于 1e-10 时停止迭代。
首先,代码读取文件 melons.txt 中的数据,将其存储在名为 melons 的列表中。然后,从 melons 中随机选择 k 个数据作为初始簇中心向量 mean_vectors。接着,进入循环,将数据分配到离其最近的簇中去,计算每个簇的新中心向量,如果中心向量的变化小于 THRESHOLD,则停止迭代。最后,将聚类结果用散点图显示出来。
需要注意的是,本段代码中并没有对聚类的效果进行评估和调参,因此其聚类效果可能不是最优的。
相关问题
vm.admin_reserve_kbytes = 8192 vm.block_dump = 0 vm.compact_unevictable_allowed = 1 vm.dirty_background_bytes = 0 vm.dirty_background_ratio = 10 vm.dirty_bytes = 0 vm.dirty_expire_centisecs = 3000 vm.dirty_ratio = 20 vm.dirty_writeback_centisecs = 500 vm.dirtytime_expire_seconds = 43200 vm.drop_caches = 3 vm.extfrag_threshold = 500 vm.hugepages_treat_as_movable = 0 vm.hugetlb_shm_group = 0 vm.laptop_mode = 0 vm.legacy_va_layout = 0 vm.lowmem_reserve_ratio = 256 256 32 1 vm.max_map_count = 65530 vm.memory_failure_early_kill = 0 vm.memory_failure_recovery = 1 vm.min_free_kbytes = 67584 vm.min_slab_ratio = 5 vm.min_unmapped_ratio = 1 vm.mmap_min_addr = 65536 vm.mmap_rnd_bits = 28 vm.mmap_rnd_compat_bits = 8 vm.nr_hugepages = 0 vm.nr_hugepages_mempolicy = 0 vm.nr_overcommit_hugepages = 0 vm.numa_stat = 1 vm.numa_zonelist_order = Node vm.oom_dump_tasks = 1 vm.oom_kill_allocating_task = 0 vm.overcommit_kbytes = 0 vm.overcommit_memory = 0 vm.overcommit_ratio = 50 vm.page-cluster = 3 vm.panic_on_oom = 0 vm.percpu_pagelist_fraction = 0 vm.stat_interval = 1 vm.swappiness = 60 vm.user_reserve_kbytes = 131072 vm.vfs_cache_pressure = 100 vm.watermark_scale_factor = 10 vm.zone_reclaim_mode = 0
这是一个包含多个与虚拟内存相关的系统参数的列表。以下是其中一些参数的解释:
- `vm.max_map_count`:最大映射数量,限制了内存中可以同时映射的页面数量,从而影响了缓存的大小。
- `vm.dirty_background_bytes` 和 `vm.dirty_background_ratio`:脏页的阈值,当脏页的数量超过该阈值时,系统会开始写回脏页。
- `vm.dirty_bytes` 和 `vm.dirty_ratio`:系统允许脏页占用的内存大小的上限。
- `vm.min_free_kbytes`:系统保留的空闲内存的大小,当空闲内存低于该值时,系统会开始清理缓存。
- `vm.swappiness`:控制系统在内存不足时使用交换空间的程度,该参数的值越高,系统越可能使用交换空间。
你可以使用 `sysctl` 命令来修改这些参数的值,具体的命令如下:
```
sysctl -w <parameter>=<value>
```
其中 `<parameter>` 表示参数名,`<value>` 表示你希望设置的值。请注意,该命令的修改是临时的,如果你想要永久生效,需要将修改写入到 `/etc/sysctl.conf` 配置文件中。
port = 3306 socket = /tmp/mysql.sock datadir = /www/server/data default_storage_engine = InnoDB performance_schema_max_table_instances = 400 table_definition_cache = 400 skip-external-locking key_buffer_size = 256M max_allowed_packet = 100G table_open_cache = 1024 sort_buffer_size = 4M net_buffer_length = 4K read_buffer_size = 4M read_rnd_buffer_size = 256K myisam_sort_buffer_size = 64M thread_cache_size = 128 query_cache_size = 128M tmp_table_size = 128M sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES explicit_defaults_for_timestamp = true #skip-name-resolve max_connections = 500 max_connect_errors = 100 open_files_limit = 65535 log-bin=mysql-bin log_error=/var/log/mysql/error.log binlog_format=mixed server-id = 1 expire_logs_days = 10 slow_query_log=1 slow-query-log-file=/www/server/data/mysql-slow.log long_query_time=3 #log_queries_not_using_indexes=on early-plugin-load = "" innodb_data_home_dir = /www/server/data innodb_data_file_path = ibdata1:10M:autoextend innodb_log_group_home_dir = /www/server/data innodb_buffer_pool_size = 1024M innodb_log_file_size = 512M innodb_log_buffer_size = 128M innodb_flush_log_at_trx_commit = 1 innodb_lock_wait_timeout = 50 innodb_max_dirty_pages_pct = 90 innodb_read_io_threads = 4 innodb_write_io_threads = 4 #skip-grant-tables bind-address = 0.0.0.0 为什么把bind-address改成101.201.118.49就不能用了?
将 的 `bind-address` 配置项更改为特定的 IP 地址会导致 MySQL 仅监听该 IP 地址上的连接请求。如果您将 `bind-address` 修改为 `101.201.118.49` 并且无法连接到 MySQL,可能是由于以下原因之一:
1. 防火墙配置:请确保防火墙允许来自 `101.201.118.49` 的连接请求到达 MySQL 服务器的 `3306` 端口。您可以检查防火墙规则并相应地调整配置。
2. IP 地址绑定的网络接口:如果 `101.201.118.49` 不是该服务器上的有效网络接口地址,MySQL 无法侦听该地址上的连接请求。请确保在服务器上存在具有 `101.201.118.49` IP 地址的网络接口,并且该接口处于活动状态。
3. MySQL 用户权限:检查 MySQL 用户的权限配置,确保允许来自 `101.201.118.49` 的连接请求。您可以使用以下命令检查和修改用户权限:
```
mysql -u root -p
GRANT ALL PRIVILEGES ON *.* TO 'your_username'@'101.201.118.49' IDENTIFIED BY 'your_password' WITH GRANT OPTION;
FLUSH PRIVILEGES;
```
请注意,将 `bind-address` 配置项更改为特定的 IP 地址可能会限制仅允许来自该 IP 地址的连接请求。如果您需要允许其他 IP 地址连接到 MySQL,请相应地调整配置。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)