A function that calculates the mean in Python:
def mean(x): sum = 0 for i in x: sum += i mean = sum / len(x) return mean
or the same function in R:
mean <- function(x) { sum = 0 for (i in x) { sum = sum + i } mean = sum / length(x) print(mean) }
A function that calculates the mean in Python:
def mean(x): sum = 0 for i in x: sum += i mean = sum / len(x) return mean
or the same function in R:
mean <- function(x) { sum = 0 for (i in x) { sum = sum + i } mean = sum / length(x) print(mean) }
from random import shuffle def fancysort(thelist): while thelist != sorted(thelist): print thelist shuffle(thelist) print 'The list is sorted!', thelist
I’ve been learning Numpy lately, and one thing that it has in common with Matlab is that the shape of an array is specified according to the m x n convention in matrix algebra. For example,
import numpy as np a = np.arange(1, 11).reshape(2, 5) print a [[ 1 2 3 4 5] [ 6 7 8 9 10]]
As you can see, the reshape() method does what it says, and takes the first argument as m (the number of rows) and the second argument as n (the number of columns). Since the m x n convention is always rows x columns, with the rows specified first, you would expect this to be consistent elsewhere. This works for indexing, so a[1, 3] will correctly grab the value that is in the second row and fourth column (because Python starts indexing at 0). Now, when summing rows and columns, you specify the axis with 0 and 1. Obviously, 0 comes before 1. So if rows come first in reshape() and indexing, they should come first in sum().
print a.sum(axis=0) [ 7 9 11 13 15] print a.sum(axis=1) [15 40]
Of course, it can’t be that easy, and I’m forced to memorize a special case.
Aside from this little annoyance, Numpy is pretty nice, though. You don’t need any special syntax for array operations like you do in Matlab, and it automatically handles ZeroDivisionErrors like Matlab (printing 0 instead of Inf, but whatever).
I’d really like to replace Matlab entirely with Numpy and related tools, which is why I’m going through a tutorial to evaluate it.
Update: And just to add to the confusion, the insert() function specifies the axis with 0 for row and 1 for column, the exact opposite of sum()!!
a = np.arange(1, 16).reshape(3, 5) print a [[ 1 2 3 4 5] [ 6 7 8 9 10] [11 12 13 14 15]] print np.insert(a, 1, 777, axis=0) [[ 1 2 3 4 5] [777 777 777 777 777] [ 6 7 8 9 10] [ 11 12 13 14 15]]
So basically, any sort of indexing feature follows 0 = row, 1 = column, while any descriptive feature (sum, mean, std) follows 0 = column, 1 = row.
I posted about Gitosis before, but I’ve come to the realization that it’s not necessary for my needs. Here’s how I set up a central Git repository with Gitweb on Ubuntu Server. With this configuration, specific people can be allowed to push to the repo, while everyone else can browse through Gitweb.
On the remote machine, create a new user:
sudo adduser git
Now we need to configure the ssh account. If password logins are enabled, you can just do this on your local machine:
ssh-copy-id git@remoteserver.com
Otherwise, if you’ve already copied your ssh key over, then on the remote machine:
sudo mkdir /home/git/.ssh sudo touch /home/git/.ssh/authorized_keys sudo cat ~/.ssh/authorized_keys >> /home/git/.ssh/authorized_keys sudo chown -R git:git /home/git/.ssh
If you want to give others access to the repo, add their keys the same way you did in line 3 above.
Next, limit the git user to git-shell, so others can only execute git commands on your server:
sudo vim /etc/passwd
And change the git line from /bin/bash to /usr/bin/git-shell
Now you can create or upload a repository to the git account. Since the git user is limited to git-shell, you can scp it to your own account and copy it over. If the repository is in a directory under the root of the git account, like /home/git/myproject, then anyone with access can clone it with:
git clone git@remoteserver.com:myproject
That’s it!
Now, to configure Gitweb, install it and edit /etc/apache2/conf.d/gitweb.conf and change $projectroot to /home/git. Then edit your vhost file and add:
RewriteEngine on RewriteRule ^/gitweb/([a-zA-Z0-9_-]+.git)/?(?.*)?$ /cgi-bin/gitweb.cgi/$1 [L,PT] Alias /gitweb /home/git <Directory /home/git> Options Indexes FollowSymlinks ExecCGI DirectoryIndex /cgi-bin/gitweb.cgi AllowOverride None </Directory>
Finally, symlink the icons and stylesheet for the Gitweb front end, and restart Apache:
sudo ln -s /usr/share/gitweb/*.png /home/git/. sudo ln -s /usr/share/gitweb/*.css /home/git/. sudo service apache2 restart
Then you can browser your repositories at remotemachine.com/gitweb.
I’m writing these posts to help myself remember. After all, you don’t really learn something until you teach someone else, right?
So I’ve been version tracking some Matlab scripts with git, and I decided to make a public repository. Here’s how I did it.
First, if you haven’t started tracking changes yet, install git and and initialize a repository:
sudo apt-get install git
cd /path/to/files
git init
git add .
git commit -am “I wish I was as commited as these files!”
Now, on the remote machine, install and setup gitosis:
sudo apt-get install gitosis
sudo -H -u gitosis gitosis-init < ~/.ssh/id_rsa.pub
That last part is my public SSH key. I created the public (gitosis) repo on the same machine as my development (git) repo. If you’re creating the gitosis repo on a remote machine, you’ll have to copy your public key over first. Just remember to put it in a file that ends in .pub.
Ok, back on the development machine, clone the admin repo:
git clone gitosis@mybox.net:gitosis-admin.git
Open up the gitosis config file:
nano [or vim or whatever] /path/to/gitosis-admin/gitosis.conf
And add two sections:
[repo MyProject]
description = “My witty description”
owner = me[group MyProject]
writable = MyProject
members = me bro dannyboy blue gaga
Notice how repo, group and writable are all the same. Also, the members will be based on SSH keys that you add to /path/to/gitosis-admin/keyfiles, and they should all end in .pub.
We need to commit and push these changes:
git commit -am “added MyProject”
git push origin master
Now go to your personal project directory and set that up:
git remote add origin gitosis@mybox.net:myproject.git
git push origin master
That’s it. The other members of your team can clone the project with:
git clone gitosis@mybox.net:myproject.git
And start pushing and pulling to their heart’s desire.
BTW, did you know that Linus Torvalds, the founder of the Linux kernel, created git, and many people consider it to be his greatest software contribution?