Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Python Matrix?
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
oth23




PostPosted: Tue Mar 06, 2012 11:36 pm   Post subject: Python Matrix?

Hi,
I have a piece of code, however, the output is not a nested list....does anyone know what I can do to get a nested list as an output and then create a matrix from the data?
The matrix needs to look like:

[1, 0.138, 0.2171, 0.1284]
[0.138, 1, 0.1827, 0.1613]
[0.2171, 0.1827, 1, 0.125]
[0.1284, 0.1613, 0.125, 1]

Here is the code I have:


Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> def read_user_movie_matrix(file_name):
file_contents = open(file_name,'r')
matrix = []
for line in file_contents:
remove_trailing_white_space = line.strip()
break_up_line = remove_trailing_white_space.split()
if len(line)>2:
get_first_input = int(break_up_line[0])
get_second_input = int(break_up_line [1])
get_third_input = int(break_up_line[2])
get_fourth_input = int(break_up_line[3])
get_fifth_input = int(break_up_line[4])
matrix.append([get_first_input, get_second_input, get_third_input, get_fourth_input, get_fifth_input])
for user1 in range(len(matrix)):
for user2 in range(len(matrix)):
euclidean_distance = 0
import math
for movie_index in range(len(matrix[0])):
euclidean_distance = euclidean_distance + ((matrix[user1][movie_index] - matrix[user2][movie_index]) **2)
similarity_score = (1.0/(math.sqrt(euclidean_distance)+ 1))
print similarity_score


>>> read_user_movie_matrix('C:/Users/Rimi/Desktop/assign2.txt')
1.0
0.138026263116
0.217129272955
0.128496221847
0.138026263116
1.0
0.182743997632
0.161390477796
0.217129272955
0.182743997632
1.0
0.125
0.128496221847
0.161390477796
0.125
1.0
>>>


Thank you!! Very Happy
Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: Wed Mar 07, 2012 5:52 pm   Post subject: Re: Python Matrix?

I'm not sure if this is what you're looking for but I'll try something
code:
a = [1,0,0]
b = [0,1,0]
c = [0,0,1]
I = [a,b,c] # I is a nested list (I believe this is like your "matrix")

for i in range(len(x)):
    print(I[i])

# This should print
[1, 0, 0]
[0, 1, 0]
[0, 0, 1]

If you're printing individual numbers in a for loop you can force python to continue output on the same line by putting a comma after the print statement (python 2) or specifying an end value (python 3). Note that I'm using python 3.2, so some syntax might be different if you're using python 2.x
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: