Nice clear post on grouping with linq in C# (and VB.NET too) by Christian Nagel:
http://weblogs.thinktecture.com/cnagel/2010/10/linq-grouping-with-c-and-visual-basic.html
Lambda syntax:
var q = Formula1.GetChampions()
.GroupBy(r => r.Country)
.Select(g => new
{
Count = g.Count(),
Group = g
})
.OrderByDescending(x => x.Count)
.ThenBy(x => x.Group.Key)
.Select(x => new
{
Country = x.Group.Key,
Count = x.Count,
Racers = x.Group.Select(r => r.FirstName + " " + r.LastName)
})
.Take(5);
Similarly in comprehension syntax:
var q = (from r in Formula1.GetChampions()
group r by r.Country into g
let count = g.Count()
orderby count descending, g.Key
select new
{
Country = g.Key,
Count = count,
Racers = from r1 in g
select r1.FirstName + " " + r1.LastName
}).Take(5);
Sunday, November 20, 2011
Wednesday, September 28, 2011
Python 2.7 Setup Steps
UPDATE 2013:
Now follow these instructions: https://python-guide.readthedocs.org/en/latest/starting/install/win.html
Use distribute, not setuptools as described here
/UPDATE
I've set up Python 2.7 on several wind0ze machines recently, these are the "best practice" steps I now use:
Installing matplotlib
Now follow these instructions: https://python-guide.readthedocs.org/en/latest/starting/install/win.html
Use distribute, not setuptools as described here
/UPDATE
I've set up Python 2.7 on several wind0ze machines recently, these are the "best practice" steps I now use:
- Download and install Python 2.7 to C:\Python27
- Download and install setuptools to C:\Python27\Lib\site-packages\
(setuptools includes easy_install, so we can install pip! Read instructions for 32 vs 64bit versions) - Add env variable PYTHON_HOME=C:\Python27
- Append env variable PATH with ;%PYTHON_HOME%;%PYTHON_HOME%\Scripts;
- Run C:\Python27\Scripts>easy_install-2.7 pip to install pip
- Install virtualenv: easy_install-2.7 virtualenv (now all pip install commands I run from a new virtualenv)
That takes care of the base environment.
Next is to install packages as required, remembering that matplotlib is best installed using an installer after executing "pip install numpy".
References:
pip Installation instructions (recommends installing pip using virtualenv)
Public service announcement (tool transition infographic)
Tools of the Modern Python Hacker: Virtualenv, Fabric and Pip (covers the new cool tools)
How to install pip on Windows (installing pip globally using setuptools)Installing matplotlib
Wednesday, September 14, 2011
python to solve linear system
Solving good ol' Ax=b:
>>> import numpy as np
>>> A = np.matrix('[3 -1 1; -1 1 -1; 1 -1 3]')
>>> A
matrix([[ 3, -1, 1],
[-1, 1, -1],
[ 1, -1, 3]])
>>> b = np.matrix('[1;1;1]')
>>> from scipy import linalg
>>> x = linalg.solve(A,b)
>>> x
array([[ 1.],
[ 3.],
[ 1.]])
See docs:
numpy.linalg.solve
routines.linalg
numpy solve examples (prob better than above!)
>>> import numpy as np
>>> A = np.matrix('[3 -1 1; -1 1 -1; 1 -1 3]')
>>> A
matrix([[ 3, -1, 1],
[-1, 1, -1],
[ 1, -1, 3]])
>>> b = np.matrix('[1;1;1]')
>>> from scipy import linalg
>>> x = linalg.solve(A,b)
>>> x
array([[ 1.],
[ 3.],
[ 1.]])
See docs:
numpy.linalg.solve
routines.linalg
numpy solve examples (prob better than above!)
Tuesday, September 6, 2011
python to visualise vectors
I've been working on a simple example SVM problem and wanted to be able to visualise the vectors in 3D. Python with Matplotlib works nicely.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
ax.plot3D([0,-1], [0,1], [0,1], zdir='z', label='x1 +1')
ax.plot3D([0,0], [0,1], [0,0], zdir='z', label='x2 -1')
ax.plot3D([0,1], [0,1], [0,1], zdir='z', label='x3 +1')
ax.plot3D([0,0], [0,0], [0,0], zdir='z', label='x4 +1')
ax.scatter([0,-1], [0,1], [0,1], zdir='z', label='x1')
ax.scatter([0,0], [0,1], [0,0], zdir='z', label='x2')
ax.scatter([0,1], [0,1], [0,1], zdir='z', label='x3')
ax.scatter([0,0], [0,0], [0,0], zdir='z', label='x4')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.legend()
plt.show()
To add an equation to the graph:
import numpy as np
x = np.linspace(-1, 1, 50)
y = 2*x**2
ax.plot3D(x, y, 0);
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
ax.plot3D([0,-1], [0,1], [0,1], zdir='z', label='x1 +1')
ax.plot3D([0,0], [0,1], [0,0], zdir='z', label='x2 -1')
ax.plot3D([0,1], [0,1], [0,1], zdir='z', label='x3 +1')
ax.plot3D([0,0], [0,0], [0,0], zdir='z', label='x4 +1')
ax.scatter([0,-1], [0,1], [0,1], zdir='z', label='x1')
ax.scatter([0,0], [0,1], [0,0], zdir='z', label='x2')
ax.scatter([0,1], [0,1], [0,1], zdir='z', label='x3')
ax.scatter([0,0], [0,0], [0,0], zdir='z', label='x4')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.legend()
plt.show()
To add an equation to the graph:
import numpy as np
x = np.linspace(-1, 1, 50)
y = 2*x**2
ax.plot3D(x, y, 0);
Sunday, August 21, 2011
LibSVM v3.1 python example
LibSVM v3.1 python example: (many older examples break in v3.1)
from svmutil import *
svm_model.predict = lambda self, x: svm_predict([0], [x], self)[0][0]
prob = svm_problem([1,-1], [[1,0,1], [-1,0,-1]])
param = svm_parameter()
param.kernel_type = LINEAR
param.C = 10
m=svm_train(prob, param)
m.predict([1,1,1])
From: http://stackoverflow.com/questions/4214868/an-example-using-libsvm-in-python/4215056
from svmutil import *
svm_model.predict = lambda self, x: svm_predict([0], [x], self)[0][0]
prob = svm_problem([1,-1], [[1,0,1], [-1,0,-1]])
param = svm_parameter()
param.kernel_type = LINEAR
param.C = 10
m=svm_train(prob, param)
m.predict([1,1,1])
From: http://stackoverflow.com/questions/4214868/an-example-using-libsvm-in-python/4215056
Sunday, August 14, 2011
Windows run commands
Original post: http://mypchell.com/guides/34-guides/69-156-useful-run-commands (thanks mypchell.com!)
| Accessibility Controls | access.cpl |
| Accessibility Wizard | accwiz |
| Add Hardware Wizard | hdwwiz.cpl |
| Add/Remove Programs | appwiz.cpl |
| Administrative Tools | control admintools |
| Adobe Acrobat (if installed) | acrobat |
| Adobe Designer (if installed) | formdesigner |
| Adobe Distiller (if installed) | acrodist |
| Adobe ImageReady (if installed) | imageready |
| Adobe Photoshop (if installed) | photoshop |
| Automatic Updates | wuaucpl.cpl |
| Bluetooth Transfer Wizard | fsquirt |
| Calculator | calc |
| Certificate Manager | certmgr.msc |
| Character Map | charmap |
| Check Disk Utility | chkdsk |
| Clipboard Viewer | clipbrd |
| Command Prompt | cmd |
| Component Services | dcomcnfg |
| Computer Management | compmgmt.msc |
| Control Panel | control |
| Date and Time Properties | timedate.cpl |
| DDE Shares | ddeshare |
| Device Manager | devmgmt.msc |
| Direct X Control Panel (if installed)* | directx.cpl |
| Direct X Troubleshooter | dxdiag |
| Disk Cleanup Utility | cleanmgr |
| Disk Defragment | dfrg.msc |
| Disk Management | diskmgmt.msc |
| Disk Partition Manager | diskpart |
| Display Properties | control desktop |
| Display Properties | desk.cpl |
| Display Properties (w/Appearance Tab Preselected) | control color |
| Dr. Watson System Troubleshooting Utility | drwtsn32 |
| Driver Verifier Utility | verifier |
| Event Viewer | eventvwr.msc |
| Files and Settings Transfer Tool | migwiz |
| File Signature Verification Tool | sigverif |
| Findfast | findfast.cpl |
| Firefox (if installed) | firefox |
| Folders Properties | folders |
| Fonts | control fonts |
| Fonts Folder | fonts |
| Free Cell Card Game | freecell |
| Game Controllers | joy.cpl |
| Group Policy Editor (XP Prof) | gpedit.msc |
| Hearts Card Game | mshearts |
| Help and Support | helpctr |
| HyperTerminal | hypertrm |
| Iexpress Wizard | iexpress |
| Indexing Service | ciadv.msc |
| Internet Connection Wizard | icwconn1 |
| Internet Explorer | iexplore |
| Internet Properties | inetcpl.cpl |
| Internet Setup Wizard | inetwiz |
| IP Configuration (Display Connection Configuration) | ipconfig /all |
| IP Configuration (Display DNS Cache Contents) | ipconfig /displaydns |
| IP Configuration (Delete DNS Cache Contents) | ipconfig /flushdns |
| IP Configuration (Release All Connections) | ipconfig /release |
| IP Configuration (Renew All Connections) | ipconfig /renew |
| IP Configuration (Refreshes DHCP & Re-Registers DNS) | ipconfig /registerdns |
| IP Configuration (Display DHCP Class ID) | ipconfig /showclassid |
| IP Configuration (Modifies DHCP Class ID) | ipconfig /setclassid |
| Java Control Panel (if installed) | jpicpl32.cpl |
| Java Control Panel (if installed) | javaws |
| Keyboard Properties | control keyboard |
| Local Security Settings | secpol.msc |
| Local Users and Groups | lusrmgr.msc |
| Logs You Out Of Windows | logoff |
| Malicious Software Removal Tool | mrt |
| Microsoft Access (if installed) | msaccess |
| Microsoft Chat | winchat |
| Microsoft Excel (if installed) | excel |
| Microsoft Frontpage (if installed) | frontpg |
| Microsoft Movie Maker | moviemk |
| Microsoft Paint | mspaint |
| Microsoft Powerpoint (if installed) | powerpnt |
| Microsoft Word (if installed) | winword |
| Microsoft Syncronization Tool | mobsync |
| Minesweeper Game | winmine |
| Mouse Properties | control mouse |
| Mouse Properties | main.cpl |
| Nero (if installed) | nero |
| Netmeeting | conf |
| Network Connections | control netconnections |
| Network Connections | ncpa.cpl |
| Network Setup Wizard | netsetup.cpl |
| Notepad | notepad |
| Nview Desktop Manager (if installed) | nvtuicpl.cpl |
| Object Packager | packager |
| ODBC Data Source Administrator | odbccp32.cpl |
| On Screen Keyboard | osk |
| Opens AC3 Filter (if installed) | ac3filter.cpl |
| Outlook Express | msimn |
| Paint | pbrush |
| Password Properties | password.cpl |
| Performance Monitor | perfmon.msc |
| Performance Monitor | perfmon |
| Phone and Modem Options | telephon.cpl |
| Phone Dialer | dialer |
| Pinball Game | pinball |
| Power Configuration | powercfg.cpl |
| Printers and Faxes | control printers |
| Printers Folder | printers |
| Private Character Editor | eudcedit |
| Quicktime (If Installed) | QuickTime.cpl |
| Quicktime Player (if installed) | quicktimeplayer |
| Real Player (if installed) | realplay |
| Regional Settings | intl.cpl |
| Registry Editor | regedit |
| Registry Editor | regedit32 |
| Remote Access Phonebook | rasphone |
| Remote Desktop | mstsc |
| Removable Storage | ntmsmgr.msc |
| Removable Storage Operator Requests | ntmsoprq.msc |
| Resultant Set of Policy (XP Prof) | rsop.msc |
| Scanners and Cameras | sticpl.cpl |
| Scheduled Tasks | control schedtasks |
| Security Center | wscui.cpl |
| Services | services.msc |
| Shared Folders | fsmgmt.msc |
| Shuts Down Windows | shutdown |
| Sounds and Audio | mmsys.cpl |
| Spider Solitare Card Game | spider |
| SQL Client Configuration | cliconfg |
| System Configuration Editor | sysedit |
| System Configuration Utility | msconfig |
| System File Checker Utility (Scan Immediately) | sfc /scannow |
| System File Checker Utility (Scan Once At The Next Boot) | sfc /scanonce |
| System File Checker Utility (Scan On Every Boot) | sfc /scanboot |
| System File Checker Utility (Return Scan Setting To Default) | sfc /revert |
| System File Checker Utility (Purge File Cache) | sfc /purgecache |
| System File Checker Utility (Sets Cache Size to size x) | sfc /cachesize=x |
| System Information | msinfo32 |
| System Properties | sysdm.cpl |
| Task Manager | taskmgr |
| TCP Tester | tcptest |
| Telnet Client | telnet |
| Tweak UI (if installed) | tweakui |
| User Account Management | nusrmgr.cpl |
| Utility Manager | utilman |
| Windows Address Book | wab |
| Windows Address Book Import Utility | wabmig |
| Windows Backup Utility (if installed) | ntbackup |
| Windows Explorer | explorer |
| Windows Firewall | firewall.cpl |
| Windows Magnifier | magnify |
| Windows Management Infrastructure | wmimgmt.msc |
| Windows Media Player | wmplayer |
| Windows Messenger | msmsgs |
| Windows Picture Import Wizard (need camera connected) | wiaacmgr |
| Windows System Security Tool | syskey |
| Windows Update Launches | wupdmgr |
| Windows Version (to show which version of windows) | winver |
| Windows XP Tour Wizard | tourstart |
| Wordpad | write |
Subscribe to:
Comments (Atom)
