From 3D world into a 2D screen by lookat matrix? - python

I would like to get 2D screen coordinates of a 3D coordinates point by LookAt matrix. Is there any simple function to do this?
For example:
I get one matrix by lookAt:
[[ 1. 0. 0. 0.]
[ 0. 1. 0. 0.]
[ 0. 0. 1. -1.]
[ 0. 0. 0. 1.]]
And I have one 3D vector [1,0,1]
What is its "2D screen coordinates"?
Thanks a lot.

Related

Why are 3D numpy arrays printed the way they are (How are they ordered)?

I am trying to wrap my head around 3D arrays (or multi-dimensional arrays in general), but it's blowing my brains a bit. Especially the way in which 3D numpy arrays are printed is counter-intuitive to me. This question is similar but it is more about the differences between programming languages, and I still do not fully get it. Let me try to explain.
Say I want to create a 3D array with 3 rows (length), 5 columns(width) and 2 depth. So a 3x5x2 matrix.
I do the following:
import numpy as np
a = np.zeros(30).reshape(3, 5, 2)
To me, a logical way to print this would be like this:
[[[0. 0. 0. 0. 0.] #We can still see three rows from top to bottom
[0. 0. 0. 0. 0.]] #We can still see five columns from left to right
[[0. 0. 0. 0. 0.] #Depth values are shown underneath each other
[0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]]
However, when I print this array it prints like this:
[[[0. 0.] #We can still see three rows from top to bottom,
[0. 0.] #However columns now also appear from top to bottom instead of from left to right
[0. 0.] #Depth values are now shown from left to right
[0. 0.]
[0. 0.]]
[[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]]
[[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]]]
It is unobvious to me why the array would be printed in this way. Maybe it is just me (Maybe my spatial reasoning is lacking here), or is there a specific reason why NumPy arrays are printed like this?
Synthesizing the comments into a proper answer:
First, take a look at np.zeros(10).reshape(5, 2). That's 5 rows of 2 columns, not 2 rows of 5 columns. Adding 3 at the front means 3 planes of 5 rows and 2 columns. What you're missing is that you new dimension is at the front, not the end. In mathematics, usually the extra dimensions are added at the end (Like extending an (x,y) with a z becomes (x,y,z). However, in computer science array dimensions are typically done this way. It reflects the way arrays are typically stored in row-major order in memory.

Is there a sparse version of tf.multiply?

Does Tensorflow has a sparse element wise multiplication?
I.e. A sparse version of tf.multiply()
I only found tf.sparse_tensor_dense_matmul(), but it's not element wise multiplication.
The function you might be looking for is: __mul__
Additional details from official documentation:
The output locations corresponding to the implicitly zero elements in the sparse tensor will be zero (i.e., will not take up storage space), regardless of the contents of the dense tensor (even if it's +/-INF and that INF*0 == NaN).
Limitation: this Op only broadcasts the dense side to the sparse side, but not the other direction.
Example:
sp_mat = tf.SparseTensor([[0,0],[0,2],[1,2],[2,1]], np.ones(4), [3,3])
const1 = tf.constant([[1,2,3],[4,5,6],[7,8,9]], dtype=tf.float64)
const2 = tf.constant(np.array([1,2,3]),dtype=tf.float64)
elementwise_result = sp_mat.__mul__(const1)
broadcast_result = sp_mat.__mul__(const2)
print("Sparse Matrix:\n",tf.sparse_tensor_to_dense(sp_mat).eval())
print("\n\nElementwise:\n",tf.sparse_tensor_to_dense(elementwise_result).eval())
print("\n\nBroadcast:\n",tf.sparse_tensor_to_dense(broadcast_result).eval())
Output:
Sparse Matrix:
[[ 1. 0. 1.]
[ 0. 0. 1.]
[ 0. 1. 0.]]
Elementwise:
[[ 1. 0. 3.]
[ 0. 0. 6.]
[ 0. 8. 0.]]
Broadcast:
[[ 1. 0. 3.]
[ 0. 0. 3.]
[ 0. 2. 0.]]

Prohibit automatic linebreaks in Pycharm Output when using large Matrices

I'm working in PyCharm on Windows. In the project I'm currently working on I have "large" matrices, but when i output them Pycharm automatically adds linebreaks so that one row occupys two lines instead of just one:
[[ 3. -1.73205081 0. 0. 0. 0. 0.
0. 0. 0. ]
[-1.73205081 1. -1. -2. 0. 0. 0.
0. 0. 0. ]
[ 0. -1. 1. 0. -1.41421356 0. 0.
0. 0. 0. ]
[ 0. -2. 0. 1. -1.41421356 0.
-1.73205081 0. 0. 0. ]
[ 0. 0. -1.41421356 -1.41421356 0. -1.41421356
0. -1.41421356 0. 0. ]
[ 0. 0. 0. 0. -1.41421356 0. 0.
0. -1. 0. ]
[ 0. 0. 0. -1.73205081 0. 0. 3.
-1.73205081 0. 0. ]
[ 0. 0. 0. 0. -1.41421356 0.
-1.73205081 1. -2. 0. ]
[ 0. 0. 0. 0. 0. -1. 0.
-2. 0. -1.73205081]
[ 0. 0. 0. 0. 0. 0. 0.
0. -1.73205081 0. ]]
It make my results very hard to reed and to compare. The window is big enough so that everything should be displayed but it still breaks the rows. Is there any setting to prevent this?
Thanks in advance!
PyCharm default console width is set to 80 characters.
Lines are printed without wrapping unless you set soft wrap in options:
File -> Settings -> Editor -> General -> Console -> Use soft wraps in console.
However both options make reading big matrices hard.
You can fix this in few ways.
With this test code:
import random
m = [[random.random() for a in range(10)] for b in range(10)]
print(m)
You can try one of these:
Pretty print
Use pprint module, and override line width:
import pprint
pprint.pprint(m, width=300)
Numpy
For numpy version 1.13 and lower:
If you use numpy module, configure arrayprint option:
import numpy
numpy.core.arrayprint._line_width = 300
print(numpy.matrix(m))
For numpy version 1.14 and above (thanks to #Alex Johnson):
import numpy
numpy.set_printoptions(linewidth=300)
print(numpy.matrix(m))
Pandas
If you use pandas module, configure display.width option:
import pandas
pandas.set_option('display.width', 300)
print(pandas.DataFrame(m))

3D model filled with cubes of fixed size

I have geometry definition in .obj file /WaveFront format/. I can load meshes of this 3D model and get vertices :
[[ 0. 0. 0.] ,[ 0. 0. 3.25] ,[-2.48 14. 0.] ,[ 0. 0. 0.] ,[0. 3.25
-2.48] ,[9.01 0. 0.] ,...]
QUESTIONS -- what i have accomplish :
How fill the model with the same cubes of the given size /in mathematical sense/ using Python 2.7 ?
In advanced mode I have each squares have random|different color and draw by using pyOpenGL ?

How do I change column type in Python from int to object for sklearn?

I am really new to Python and scikit-learn (sklearn) and I am trying to load this dataset which consists of 7 columns of attributes and 1 column of the data classification (class/data target). But there's this one attribute which consists of data [1,2,3,4,5] which actually marks a stage of something, thus making it a nominal, not numeric. But of course python recognizes it as a numerical data (int64), when in fact I want it to be treated as a nominal data (object). How do I change the column type to nominal?
I have done the following.
print(data.dtypes)
data["col_name"]=data["col_name"].astype(numpy.object)
print(data.dtypes)
In the first print, it still recognizes my data["col_name"] as an int64, but after the astype line, it has changed it object. But it doesn't make any difference to the data, since when I try to use matplotlib and create a histogram, it still recognizes both the X and Y as numbers instead of object.
Also I have read about the One Hot Encoding and Label Encoding on the documentation, but I figured they are not what I need in my case. I wonder if I have misunderstood something or maybe there's another solution.
Thanks
Reading through the documents for sklearn. This package has thorough documentation. In particular the Preprocessing section on encoding categorical features:
In regards to keeping categorical features represented in an array of integers, ie [1,2,3,4,5], we have this:
Such integer representation can not be used directly with scikit-learn
estimators, as these expect continuous input, and would interpret the
categories as being ordered, which is often not desired (i.e. the set
of browsers was ordered arbitrarily). One possibility to convert
categorical features to features that can be used with scikit-learn
estimators is to use a one-of-K or one-hot encoding, which is
implemented in OneHotEncoder. This estimator transforms each
categorical feature with m possible values into m binary features,
with only one active.
So what you can to do is convert your array into 5 new columns (this case, since you have 5 possible values) using one-hot encoding.
Here is some working code. The input is a column of categorical parameters [1,2,3,4,5], the ouput is a matrix, 5 columns, 1 for each of the 5 possible choices:
from sklearn.preprocessing import OneHotEncoder
enc = OneHotEncoder()
enc.fit([[1],[2],[3],[4],[5]])
OneHotEncoder(categorical_features='all', dtype='numpy.float64', handle_unknown='error', n_values='auto', sparse=True)
print enc.transform([[1],[2],[3],[4],[5]]).toarray()
Output:
[[ 1. 0. 0. 0. 0.]
[ 0. 1. 0. 0. 0.]
[ 0. 0. 1. 0. 0.]
[ 0. 0. 0. 1. 0.]
[ 0. 0. 0. 0. 1.]]
Say your categorical parameters were in this order: [1,3,2,5,4,3,2,1,3,4,2]. You would get this output:
[[ 1. 0. 0. 0. 0.]
[ 0. 0. 1. 0. 0.]
[ 0. 1. 0. 0. 0.]
[ 0. 0. 0. 0. 1.]
[ 0. 0. 0. 1. 0.]
[ 0. 0. 1. 0. 0.]
[ 0. 1. 0. 0. 0.]
[ 1. 0. 0. 0. 0.]
[ 0. 0. 1. 0. 0.]
[ 0. 0. 0. 1. 0.]
[ 0. 1. 0. 0. 0.]]
So this 1 column will convert into 5 columns.
print(data.dtypes)
data["col_name"]=data["col_name"].astype(str)
print(data.dtypes)

Categories