星球
找回 Ubuntu 11.10 的傳統介面 (Gnome Classic)
![]() |
登入時預設沒有 Gnome Classic 可選。 |
jonny@oneiric:~$ sudo aptitude install gnome-session-fallback Enter
[sudo] password for jonny: PASSWORD
The following NEW packages will be installed: alacarte{a} gir1.2-panelapplet-4.0{a}
gnome-applets{a} gnome-applets-data{a}
gnome-panel{a} gnome-panel-data{a}
gnome-session-fallback libpanel-applet-4-0{a}
0 packages upgraded, 8 newly installed, 0 to remove and 11 not upgraded.
Need to get 9,459 kB of archives. After unpacking 40.0 MB will be used.
Do you want to continue? [Y/n/?] y
![]() |
裝好就會多個「Gnome Classic」可以選。 |
![]() |
「Gnome 傳統介面」回來了。 |
資料來源:
★Installing / Using Classic GNOME Desktop In Ubuntu 11.10 Oneiric Ocelot ~ Web Upd8: Ubuntu / Linux blog
請改用國網中心的鏡像站(mirror)
可以看的出來下方有幾個網域名稱(Domain name)是指到同一個 IP,凍仁雖不能肯定是指到同一台 Server,但可以肯定的是機器不夠力了!
OS | Mirror | IP |
Debian | opensource.nchc.org.tw | 211.73.64.9 |
Ubuntu | free.nchc.org.tw | 211.73.64.9 |
Ubuntu | ftp.twaren.net | 140.110.123.9 2001:e10:5c00:5::9 |
OS | Mirror | IP |
Debian | ftp.tw.debian.org | 140.138.145.242 |
Ubuntu | tw.archive.ubuntu.com | 140.138.145.242 |
Ubuntu | shadow.ind.ntou.edu.tw | 140.121.80.201 |
1. 使用更新管理員更換 mirror
![]() |
1.1. 開啟「更新管理員」。 |
![]() |
1.2. 點選「設定」按鈕。 |
![]() |
1.3. 點選「下載自:」 一欄,並點選「其他...」。 |
![]() |
1.4. 找到國網中心的 mirror 後點選「選擇伺服器」。 |
2. 使用 sed 取代 mirror
2.1. 在取代前得先找出目前使用的 mirror。
jonny@oneiric:~$ cat /etc/apt/sources.list | grep main | awk '{ print $2}' | cut -d'/' -f3 | sed -n '3P' [Enter]tw.archive.ubuntu.com
2.2. 將 tw.archive.ubuntu.com 替換成 free.nchc.org.tw。
jonny@oneiric:~$ sudo sed -i 's/tw.archive.ubuntu.com/free.nchc.org.tw/g' /etc/apt/sources.list [Enter]
使用完以上任ㄧ方法都得再次更新套件來源,
# 註1:國網中心的全名為國家實驗研究院高速網路與計算中心(NCHC)。
# 註2:「切忌」指的是千万不可,而「切記」則是要牢記的意思。
延伸閱讀:
★NCHC | Main / HomePage
相關連結:
★大澤木小鐵 好奇 ftp://os.nchc.org.tw 滿載了,請問大家都是在哪裡更新 Ubuntu ? [已解] 國研院國網中心自由軟體
【Network】網路設定 - Wireless 篇
1. 右下角為 某 D 大廠的 Wireless Access Point。
2. 盾牌圖示為凍仁的 IBM Thinkpad A31。
3. PC 皆外接某 D 大廠的 USB 外接網卡。
在沒有電腦又得開發系統的情況下,跟友人借了台淘汰下來的 A31,
嘗試灌過 Ubuntu 9.10、openSUSE 11.2 及 Debian lenny,可 Ubuntu 怎樣都無法驅動 PCMicia Card,或許是 A31 真的有點年紀,新版的 firmware 不支援,
啟動 eth1 網卡(或 wlan0)。
jonny@linux:~$ sudo ifconfig eth1 up
搜尋無線基地台。
jonny@linux:~$ sudo iwlist eth1 scan
設定基地台 ssid 與密碼。
# ASCII
jonny@linux:~$ sudo iwconfig eth1 essid <ESSID> key s:<PASSWORD>
# hex WEP
jonny@linux:~$ sudo iwconfig eth1 essid <ESSID> key <PASSWORD>
與基地台取得動態 IP。
jonny@linux:~$ sudo dhclient eth1
測試連線是否正常。
jonny@linux:~$ ping www.google.com.tw -c 1
若以上還無法正常連接網路,可以先檢查:
1. Application → Administrator → Additional Drives # 看有無支援的驅動。
2. 手動開啟 Wireless 電源:一般可分為實體開關與 Fn + WiFi 的組合鍵開關。
資料來源:
★Gentoo Linux 文件 -- 設定網路
相關連結:
★wicd
在Ubuntu Linux上使用外接硬碟
我在台北光華原價屋買的,體驗到了傳說中店員的傲慢,類似傲驕系的女僕店的體驗。
買來以後第一次接上電腦,發現他已經格式化成ntfs,當然在Linux下效能很不好,所以重新格式化成ext4。然後這一定會導致這硬碟automount以後的owner是root,所以我在上面做了一個目錄home,把home的owner改成我自己的帳號。
新買了硬碟於是馬上用磁碟公用程式來檢查,來看看磁碟是不是健康的。
不過SMART的檢測的功能是無法按的
查了資料
找到這一篇https://www.smartmontools.org/wiki/Supported_USB-Devices
找到我的硬碟WD My Passport Ultra (USB 3.0) 2TB
所以我要下
sudo
利用UNetbootin製作ubuntu開機隨身碟—節省光碟做環保
ubuntu固定每半年會推出新的版本,一般可直接以更新的方式進行升級,但有時基於穩定性的考量,會以光碟重新安裝的方式進行,這時就會需要下載iso檔並燒入光碟來安裝。幾次下來,開始意識到這樣每半年就得耗費一張光碟,雖然現在光碟成本不高,但如果能避免不必要的資源浪費當然更好,後來就改以複寫片來燒寫。
前兩天在ubuntu論壇閒逛時,看到網友介紹的一個好用的工具—UNetbootin,透過UNetbootin可以輕易的將各Linux發行版做進隨身碟,甚至連下載的動作都可透過它一併完成。
到官網下載UNetbootin後,請先將程式權限裡的「允許檔案作為程式執行」打勾。
如果要透過UNetbootin下載,請先選好Linux發行版。
接下來便會自動幫你下載並連同開機設定(Bootloader)做進隨機碟。
如果你已經抓好映像檔,請選擇「映像檔」。
接下來就會進行寫入隨身碟的動作。
製作的時間不用太久,等程式跑完即完成一支ubuntu安裝碟。
之後用此隨身碟開機即可看到Bootloader開機選單
【相關連結】
更新套件庫或安裝套件遇到「無法將 /var/lib/dpkg/lock 鎖定」 解法
如果忘記是那個程式沒關的話,可使用 lsof (list open files)...
請按標題讀更多:)
Clonezilla live 小硬碟複製到大硬碟筆記
參考
http://clonezilla.nchc.org.tw/clonezilla-live/
http://note.drx.tw/2013/10/working-on-gnu-linux-26-clonezilla.html
現在最新的Clonezilla實在是很厲害!
首先同一台筆電作硬碟複製時,來源硬碟跟目標硬碟都必須是unmount的,所以不可能用原來開機的Linux系統來作,所以我燒出一片Clonezilla-live的光碟來處理。
過程中我是用專業模式,因為就是用MBR開機磁區+ Linux的分割區,所以一些非MBR跟Windows磁區的猜測都不用了,此外分割區表的建立,我選擇按照來源磁碟,等比例擴大的選項,我的分割區本來就規劃 / /boot
擁有漂亮圖形介面的解壓縮程式—PeaZip
雖然ubuntu有內建的的壓縮程式,且搭配rar及p7zip等套件即可無所不解,但有時碰到一些特定加密形式的壓縮檔,有時卻會怪怪的,且沒個漂亮的gui介面也讓使用上的爽度大打折扣。
先前不知在哪個開源網站看到介紹PeaZip的主題,當時抓下來試用一會兒後變沒機會使用。某次在解壓縮一加密檔時,頻頻時出現密碼錯誤的情形,後來想說試試PeaZip,沒想到一解就成功,之後我便常以此程式作為解壓縮工具。PeaZip是個跨平台程式,因此不只在ubuntu,Windows平台也有對應版本,且包含中文語系,推薦給大家。
【相關連結】
Linux 上處理壞軌硬碟的兩三事
有養動物園或是管理伺服器的人,難免都會碰到一些硬碟故障的問題。這篇文章略為整理一些在 Linux 上處理壞軌硬體的一些軟體知識與技巧。針對讀者為中等程度以上的使用者,文中僅僅提供參考資料,不說明實際操作或指令明細,請自行參考相關文件。作者以最大善意盡力提供參考資訊,但文中如有疏漏或錯誤造成資料遺失,作者不負任何責任。
文中說明均以 Linux href="http://en.wikipedia.org/wiki/Extended_file_system">Extended file system 為主,操作平臺是 href="http://www.debian.org/">Debian GNU/Linux. 所提及工具授權均為 href="http://en.wikipedia.org/wiki/Free_and_open_source_software">FLOSS 。假設錯誤硬碟為 /dev/sdb,所有指令需要以 root 執行。
章節結構
- 錯誤類型。
- 壞軌測試。
- 預先偵測。
- 備份與資料回復。
- 排除壞軌磁區。
- 送修與資料清除。
錯誤類型
這裡討論的硬碟資料損毀,不外乎物理性傷害,摔落、靜電、停電、過熱、原件老化等等。這些傷害會造成邏輯性 (logical error) 上或物理 (physical error) 性的錯誤。
所謂的邏輯性錯誤,就是硬碟離線時,暫存資料來不及寫入,導致資料錯誤異常。所幸大部分的檔案系統都會有一個檔案系統狀態 (Filesystem state) 的值,若是最後使用未順利完成卸載 (umount),下次掛載時就會提示要求作 href="http://wiki.debian.org.hk/w/Configure_regular_filesystem_check#.E6.AA.94.E6.A1.88.E7.B3.BB.E7.B5.B1.E7.8B.80.E6.85.8B_.28Filesystem_state.29">fsck 檢查。如果是href="http://en.wikipedia.org/wiki/Journaling_file_system">日誌式檔案系統如 href="http://en.wikipedia.org/wiki/Ext3">ext3/href="http://en.wikipedia.org/wiki/Ext4">ext4,則會試著回復未完成的操作。這種錯誤偶爾會破壞檔案,fsck/e2fsck 可能會將錯誤檔案移到 lost+found 目錄下,造成困擾,但問題不大。
物理性錯誤指的是硬體磁盤受到損壞,該段href="http://en.wikipedia.org/wiki/Disk_sector">磁區無法讀取,也就是href="http://en.wikipedia.org/wiki/Bad_sector">壞軌。肯定導致資料遺失,最常見的是系統吐出 I/O Error,無法讀取該檔案。實際的症狀是href="http://en.wikipedia.org/wiki/Access_time">讀取變慢、聽到 href="http://en.wikipedia.org/wiki/Spin-up">Spin-up 時的卡卡聲(href="http://en.wikipedia.org/wiki/Click_of_death#Hard_disk">Clicking sound),甚至是系統當機等問題。
當壞軌開始產生時,通常代表硬碟壽命將至,得趕快開始更換健康新硬碟。這篇文章主要討論的是壞軌這種物理性錯誤的處理辦法。
壞軌測試
最基本的測試方法是利用 href="http://en.wikipedia.org/wiki/Badblocks">badblocks (8) 來檢測硬碟是否存在壞軌,這個指令不分檔案系統。若是含有重要資料的硬碟,應該先作預設的非破壞性唯讀測試 (non-destructive read-only test),以避免寫入測試時異常,造成磁碟重置離線。
badblocks -vs /dev/sdb
上述只要一發現壞軌,請第一時間拔下備份吧。若是可承受資料損失,可以使用 “-n” 的非破壞性寫入式測試。這基本上會嘗試寫入資料,再三確認硬碟運作正常。
badblocks -nvs /dev/sdb
預先偵測
不過 badblocks 需要在離線時才能執行。很多時候,伺服器是無法忍受長時間的離線檢查的,必須要能夠線上檢測硬碟健康狀況才行。目前市面所有 href="http://en.wikipedia.org/wiki/Advanced_Technology_Attachment">ATA 硬碟都已經支援 href="http://en.wikipedia.org/wiki/Self-Monitoring,_Analysis,_and_Reporting_Technology">S.M.A.R.T. (Self-Monitoring, Analysis, and Reporting Technology),S.M.A.R.T. 基本上提供一個界面讓管理者可以查詢幾個重要的href="http://en.wikipedia.org/wiki/Self-Monitoring,_Analysis,_and_Reporting_Technology#Known_ATA_S.M.A.R.T._attributes">參數。
透過讀取這些參數,管理者能夠預先警覺href="http://en.wikipedia.org/wiki/Hard_disk_failure">硬碟錯誤。根據 Google 的調查十萬顆硬碟後的href="http://antbsd.twbbs.org/~ant/wordpress/?p=592">研究報告 (href="http://research.google.com/pubs/pub32774.html">Failure Trends in a Large Disk Drive Population),S.M.A.R.T. 的數值的確反應硬碟錯誤率 – 六十天內,出現 S.M.A.R.T. 掃描錯誤的硬碟是其他硬碟的 39 倍高。
在 Linux 上,可以使用 href="http://sourceforge.net/apps/trac/smartmontools/wiki">smartmontools 中的 smartctl (8) 來查詢硬碟的 S.M.A.R.T. 狀態。指令如下
smartctl --attributes /dev/sdb
根據 Google 的研究以及 href="http://smartmontools.sourceforge.net/badblockhowto.html">Bad block HOWTO for smartmontools 指出,與硬碟失效關係最高的數值是 Reallocated Sectors Count/Reallocations event count,/Current Pending Sector Count/Uncorrectable Sector Count。這幾組數據代表硬碟發現該磁區已經損毀,它會重新從備用磁區分配一位置取代該損壞磁區,或磁區已發現問題尚未替換或無法替換。這通常可以視為磁區損耗 (surface wear) 的狀況。當你發現這些值不為 0 時,就該開始準備備份資料並作壞軌檢查了。
除了使用 smartctl 手動檢測外,也可以設定 smarted,讓系統自動觀測 S.M.A.R.T. 狀態,有錯誤會顯示於桌面訊息或發送郵件。smartd 的設定可以參考 title="Permanent Link to 檢測硬碟狀態 – smartmontools" rel="bookmark" href="http://blog.ssh.tw/?p=778">檢測硬碟狀態 – smartmontools 一文。S.M.A.R.T. 也支援 Self-Test,可以讓硬碟作效能與功能檢查,詳情請見 smartctl (8).
/>
備份與資料回復
在發現壞軌之際,應該第一優先備份資料。如果還能夠掛起檔案系統,則趕緊利用 href="http://en.wikipedia.org/wiki/Rsync">rsync, href="http://www.gnu.org/software/tar/">tar 等工具備份檔案。或者利用 href="http://en.wikipedia.org/wiki/Dd_%28Unix%29">dd 將整顆硬碟資料或分割區存檔,未來置換到新硬碟上
dd if=/dev/sdb of=sdb.img
若要確保資料正確複製,可使用 href="http://dcfldd.sourceforge.net/">dcfldd 來複製,它會邊複製邊作 href="http://en.wikipedia.org/wiki/Hash_function">hash 確保資料複製正確。不過由於壞軌處常常無法讀取,這會造成 dd 執行失敗。此時可以改用 href="http://www.gnu.org/software/ddrescue/ddrescue.html">dd_rescue, 它可以略過無法讀取的部分,儘可能複製其他健康的資料。
不過天有不測風雲,人有旦夕禍福。萬一資料損壞的區域不是資料區域,而是存放重要的系統資訊,那就需要額外的處置。建議無論任何時候,都不要在壞硬碟上直接操作,永遠先備份,再於備份碟上進行處理。以下略談三種情境的可用工具
- 若不幸你的 href="http://en.wikipedia.org/wiki/Partition_table_%28master_boot_record%29">MBR 爛掉以至於 partition table 消失,可以嘗試使用href="http://en.wikipedia.org/wiki/Partition_table_%28master_boot_record%29"> href="http://www.cgsecurity.org/wiki/TestDisk">testdisk 來猜測各 partition table 的位址,或許可以救回 partition table 資料。
- 若是檔案系統的 superblock 損壞,會看到 “can’t read superblock” 訊息,ext? 系列檔案系統通常有多個 superblock 備份,你可以從備份的 superblock 進行復原工作 (href="http://www.cyberciti.biz/faq/recover-bad-superblock-from-corrupted-partition/">Recover Corrupted Partition From A Bad Superblock,style="font-family: 宋体;"> href="http://blogold.chinaunix.net/u1/48373/showart_1331657.html">硬盘href="http://blogold.chinaunix.net/u1/48373/showart_1331657.html">ext2/3style="font-family: 宋体;">文件系统superblockstyle="font-family: 宋体;">损坏修复试验)。
- 若你手忙腳亂已經把硬碟重新格式化,那麼只能透過 href="http://foremost.sourceforge.net/">foremost 或 href="http://www.sleuthkit.org/sleuthkit/">Sleuth Kit 慢慢的揀出硬碟中仍可辨識的檔案。祝你幸運。
排除壞軌區域
通常如果你的硬碟還在保固範圍,一般原廠均會更換健康的硬碟給你。但如果硬碟已經超過保固,壞軌硬碟仍堪用,可以用來存一些低度重要的資料。
對於這些壞軌,可以用 href="http://en.wikipedia.org/wiki/Sector_slipping">sector slipping 或叫做 href="http://www.pcguide.com/ref/hdd/geom/errorMapping-c.html">defect mapping 的機制來跳過不用。基本上分成兩種形式,一是透過href="http://en.wikipedia.org/wiki/Disk_controller">硬碟控制器或是透過檔案系統的 bad block table 來記錄。
傳統的 (1990 年代中期以前的硬碟)是可以進行href="http://en.wikipedia.org/wiki/Disk_formatting#Low-level_formatting_.28LLF.29_of_hard_disks">低階格式化 (href="http://www.pcguide.com/ref/hdd/geom/formatLow-c.html">Low-Level Formatting) 的,透過原廠的指令來重新定義物理儲存格式、重新定義 href="http://en.wikipedia.org/wiki/Cylinder-head-sector">CHS、若是硬碟中有壞軌,可以在低階格式化時避開不用。
由於低階格式化常因為操作錯誤而href="http://en.wikipedia.org/wiki/Brick_%28electronics%29">損壞 (bricked) 硬碟,新款硬碟已經不提供使用者透過href="http://en.wikipedia.org/wiki/SCSI_Format_Unit_Command">指令作低階格式化的工具。取而代之的是透過 href="http://www.dataclinic.co.uk/hard-drive-defects-table.htm">hard-drive defect management 功能,以 href="http://www.dataclinic.co.uk/hard-drive-defects-table.htm">Hard drive defects table – P & G Lists 來記錄 href="http://www.mjm.co.uk/sectorremapping.html">Bad Sector Mapping。所謂 Primary defect list 是代表出廠時已經產生缺陷的磁區列表,而 Grown defect list 則是使用中產生缺陷的磁區列表。
當硬碟發現壞軌時,便會自動將該磁區列入 G-List 中,並從備用的磁區中替換。因此現在已經沒有所謂低階格式化,目前所謂低階格式化大多指的是 Zero-filled,對磁區填入空值,強迫硬碟重新分配損壞磁區,這也是前述 S.M.A.R.T. 所謂 Reallocated Sectors。
所以你可以做的事情是對壞軌區域寫入空值,迫使它重置。你可以直接透過一開始的 badblocks 所產生之數值直接覆寫錯誤區塊,範例請見 href="http://smartmontools.sourceforge.net/badblockhowto.html">Bad block HOWTO for smartmontools。
上述 HOWTO 中詳述了,怎麼計算 e2fsck 吐出來數值,人工計算有點麻煩。以下介紹如何處理利用工具自動處理。
不保留資料
如果確認整顆硬碟資料都不需要保留,可以直接下達一下 dd 指令,重寫整顆硬碟。
dd if=/dev/zero of=/dev/sdb
或者在格式化時下達 -c -c 參數,讓 mkfs.ext3 順便檢查壞軌。
mkfs.ext3 -c -c /dev/sdb1
保留資料
如果硬碟已有資料,並想保留此資料,可以用 badblocks 檢查,並生成 badblocks list,再將 badblocks 吐出的列表,餵給 e2fsck -l.。但是必須確認執行 badblocks 時,指派了正確的 block size,可用 dumpe2fs -h 確認 block size.然後指示給 badblocks。
上述操作有點複雜,比較保險的做法是直接讓 e2fsck 用 badblocks 去作壞軌檢查,指令如下
e2fsck -k -c -c /dev/sdb1
如此 e2fsck 會將壞軌寫入檔案系統列表中,可以用下述指令查詢目前壞軌列表
dumpe2fs -b /dev/sdb1
其他做法
本節前述可用低階格式化重置硬碟磁區分配,但是新款硬碟已少提供工具程式給終端使用者。有些工具透過反組譯硬碟韌體的方式取得 vendor specific ATA commands。另外,像是 Seagate 等品牌,則在硬碟上留有 href="http://en.wikipedia.org/wiki/Differential_TTL">TTLhref="http://en.wikipedia.org/wiki/Serial_communication">serial communication 的href="http://sites.google.com/site/seagatefix/">界面,於是你可以透過終端機連上硬碟韌體,以 href="http://www.scribd.com/doc/39042992/Seagate-Diagnostic-Command">Diagnostic Commands 進行一些偵錯以及重新格式化的動作。
送修與資料清除
爲了避免href="http://zh.wikipedia.org/wiki/%E9%99%B3%E5%86%A0%E5%B8%8C%E8%A3%B8%E7%85%A7%E4%BA%8B%E4%BB%B6">陳冠希裸照事件發生,送修前最好確保資料已經完全刪除。
就如前述所說,若只是進行快速格式化,硬碟資料也可能被href="http://en.wikipedia.org/wiki/Data_remanence">回復。事實上,根據 href="http://en.wikipedia.org/wiki/Peter_Gutmann_%28computer_scientist%29">Peter Gutmann 的在 90 年代的href="http://en.wikipedia.org/wiki/Data_remanence#Feasibility_of_recovering_overwritten_data">研究,他可以用href="http://en.wikipedia.org/wiki/Magnetic_force_microscopy">磁力顯微鏡取得複寫過三十五次以上的磁區資料。而美國 href="http://en.wikipedia.org/wiki/National_Institute_of_Standards_and_Technology">National Institute of Standards and Technology 的研究 rel="nofollow" href="http://csrc.nist.gov/publications/nistpubs/800-88/NISTSP800-88_rev1.pdf">“Guidelines for Media Sanitization” 或title="View content where Author is Craig Wright" href="http://www.springerlink.com/content/?Author=Craig+Wright"> Craig Wright, Dave Kleiman, Shyaam Sundhar R.S. 的論文 href="http://www.springerlink.com/content/408263ql11460147/">Overwriting Hard Drive Data: The Great Wiping Controversy 指出,新型硬碟只需要複寫一次即可。
爲了達到資料保密措施,各國軍方也因此設定有不同的資料清除href="http://en.wikipedia.org/wiki/Data_remanence#Standards">標 href="http://en.wikipedia.org/wiki/Data_erasure#Standards">準。總之,無論你的資料是否重要,在 Linux 上,建議使用這兩種 href="http://lambda-diode.com/software/wipe/">wipe, href="http://code.google.com/p/diskscrub/">scrub 指令之一來抹除資料,這兩種工具支援上述的各國標準。你也可下載使用 href="http://en.wikipedia.org/wiki/Darik%27s_Boot_and_Nuke">DBAN 製作開機片來清除硬碟資料。
References
- href="http://smartmontools.sourceforge.net/badblockhowto.html">Bad block HOWTO for smartmontools
- href="http://www.pcguide.com/ref/hdd/geom/format.htm">The PC Guide: Hard Disk Formatting and Capacity
CMClass: 簡述 libsvm(Support Vector Machine library) 使用方法
詳細數學就不介紹了,怕大家睡著(但其實是因為還沒看懂),各位可以看一下下面那段這個影片,大略了解 SVM 分類器如何區別不同資料。
[!IFRAME FILTERED!]
本文章主要介紹的是用 Python 語言去操作 libsvm 函式庫。
先解壓縮 libsvm.tgz 檔,可以看到 python 及 windows 資料夾,如果要在 Linux 中使用的話,請在主目錄中作
$ make lib
這樣會得到 libsvm.so.2 檔,這是 libsvm 的主函式庫,而在 windows 中使用的話,它則是先幫你編譯好這個檔了,可在 windows/ 找到這個 libsvm.dll 檔。
在 Linux 中,請把 python/*py 放到 /usr/local/lib/python2.6/site-packages 中,而 libsvm.so.2 放到 /usr/local/lib/python2.6/ 。
在 windows 中,請把 python/*py 放到 C:\Python26\Lib\site-packages 中,而 libsvm.dll 請放到 C:\Python26\Lib\windows 資料夾中(因為 svmutil.py 寫死了它的相對路徑,所以務必依它的相對位置置放)。
請在 Python shell 中,鍵入下列指令,測試是否安裝成功。
>>> from svmutil import *
>>>
沒錯誤訊息,那就是安裝對了。
使用 svm ,主要就是兩個動作: 訓練及預測。
訓練:
svmutil.svm_train 函式的引數有「類別標籤」、「觀察值」、「參數」。
你的原始資料若是如下:
1. 3, 4, 5, 6 => 第二類
2. 3, 4, 5, 5 => 第一類
3. ....
前面的 #. 表第幾個觀察值,後面逗號分隔的數據為各維度的值,行末則是放置該觀察值為第幾類的說明。請把它轉成
>>> Y = [2, 1, ...]
>>> x = [(3, 4, 5, 6), (3, 4, 5, 5), ...]
類別標籤請獨立放置到一個 list 中,而觀察值維度則依序放置到另一個 list 中。接下來,就能使用 svm_train:
>>> from svmutil import *
>>> model = svm_train(Y, x, '-c 4')
所得到的 model 就是一個經過訓練的分類器。
預測
接下來,我們要拿訓練好的分類器去預測新的觀察值:
>>> p_label, p_acc, p_val = svm_predict([0]*len(new_x), new_x, model)
而 p_label 就是依 new_x 順序所對應的類別標籤 list 。
下圖是我隨機生成的 300 點,圓點為原始的觀察值,而以線相連的連續點則是預測點。

詳細程式碼請參照如下:
1 #! /usr/bin/python
2 # -*- coding: utf8 -*-
3
4 __author__="hoamon"
5 __date__ =u"$2011/4/12 下午 05:52:31$"
6
7 from math import pi, sin, cos
8 from random import random
9 from matplotlib import pyplot as plt
10 from svmutil import *
11
12 def circleData(centre, radius, down_limit_percent=0, lens=100, range=[0, 100]):
13 points = []
14 while len(points) < lens:
15 _angle = 2 * pi * random()
16 radius_percent = random()
17 if radius_percent < down_limit_percent: continue
18 _radius = radius * radius_percent
19 x = centre[0] + cos(_angle) * _radius
20 y = centre[1] + sin(_angle) * _radius
21 if range[0] <= x <= range[1] and range[0] <= y <= range[1]:
22 points.append((x, y))
23 return points
24
25
26 def test():
27 u""" 製作三群的隨機資料,每群皆 100 個點,點位置的 x, y 限制在 0 ~ 100 之間
28
29 最後利用 matplotlib 繪製出來的圖,"單點"表原始資料,而連續點畫線的部份,
30 該點位的類別則是利用 svm_predict 計算出來的。
31
32 Y = [1, 1, 1, ..., 2, 2, 2, ..., 3, 3, 3, ...]
33 x = [(x1, y1), (x2, y2), ...]
34 """
35 Y = [1] * 100 + [2] * 100 + [3] * 100
36 x1, x2, x3 = (circleData((35, 40), 12),
37 circleData((35, 40), 48, down_limit_percent=0.25),
38 circleData((80, 80), 20)
39 )
40 x = x1 + x2 + x3
41
42 m = svm_train(Y, x, '-c 4')
43
44 #INFO 在 100x100 的畫布上,打出 40000 個點,拿這 4 萬個點去給 m 作預測,算出這 4 萬個點的類別
45 points = [(i*0.5, j*0.5) for j in xrange(0, 200) for i in xrange(0, 200)]
46 p_label, p_acc, p_val = svm_predict([0]*40000, points, m)
47
48 line_1, line_2, line_3, pre_label = [], [], [], p_label[0]
49 for i in xrange(0, 200):
50 for j in xrange(0, 200):
51 index = i * 200 + j
52 now_label = p_label[index]
53 if now_label == 1 :
54 line_1.append(points[index])
55 elif now_label == 2 :
56 line_2.append(points[index])
57 elif now_label == 3 :
58 line_3.append(points[index])
59
60 fig = plt.figure()
61 ax = fig.add_subplot(111)
62 ax.plot([p[0] for p in x1], [p[1] for p in x1], 'ro')
63 ax.plot([p[0] for p in x2], [p[1] for p in x2], 'go')
64 ax.plot([p[0] for p in x3], [p[1] for p in x3], 'bo')
65 ax.plot([p[0] for p in line_1], [p[1] for p in line_1], 'r-', alpha=0.5)
66 ax.plot([p[0] for p in line_3], [p[1] for p in line_3], 'b-', alpha=0.5)
67 ax.set_title('Points of three classes')
68 ax.set_xlabel('x')
69 ax.set_ylabel('y')
70 ax.set_xlim(0, 100)
71 ax.set_ylim(0, 100)
72 plt.show()
73 return m, p_label, p_acc, p_val
74
75
76 if __name__ == "__main__":
77 test()