Custom Vector and Matrix classes in python for machine learning












0














I am creating a machine learning tool set from scratch in python. I have never done something of this kind and I don't usually use python but I thought it would be good to expand my horizons. I am really looking for aspects of the code that would really hinder performance and things to consider since this will be used for a neural network implementation.



class vector:
def __init__(self, size):
self.elems = [0] * size
self.size = size
def __repr__(self):
return repr(self.elems)
def __mul__(self, other):
if(self.size != other.size):
raise ArithmeticError("vectors of two different lengths")
a = 0
for i in range(self.size):
a += self.elems[i] * other.elems[i]
return a
def set(self, array):
for i in range(self.size):
self.elems[i] = array[i]
self.mag = sum([i**2 for i in self.elems])**.5
def normalize(self):
a = vector(self.size)
a.set([i/self.mag for i in self.elems])
return(a)

class matrix:
def __init__(self, r, c):
self.coloums = [vector(c) for i in range(r)]
self.r = r
self.c = c
def __repr__(self):
return repr(self.coloums)
def __mul__(self, other):
if(type(other) != vector):
raise TypeError("matrices can only be multiplied by vectors")
if(self.c != other.size):
raise ArithmeticError("rows and lengths do not match")
a = vector(self.r)
a.set([(other*self.coloums[i]) for i in range(self.r)])
return a
def set(self, multiarray):
for i in range(self.c):
self.coloums[i].set(multiarray[i])


I am aware this has no way of multiply by a scalar but I have no need for that just yet and it would be pretty trivial to implement.










share|improve this question







New contributor




robert gibson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0














    I am creating a machine learning tool set from scratch in python. I have never done something of this kind and I don't usually use python but I thought it would be good to expand my horizons. I am really looking for aspects of the code that would really hinder performance and things to consider since this will be used for a neural network implementation.



    class vector:
    def __init__(self, size):
    self.elems = [0] * size
    self.size = size
    def __repr__(self):
    return repr(self.elems)
    def __mul__(self, other):
    if(self.size != other.size):
    raise ArithmeticError("vectors of two different lengths")
    a = 0
    for i in range(self.size):
    a += self.elems[i] * other.elems[i]
    return a
    def set(self, array):
    for i in range(self.size):
    self.elems[i] = array[i]
    self.mag = sum([i**2 for i in self.elems])**.5
    def normalize(self):
    a = vector(self.size)
    a.set([i/self.mag for i in self.elems])
    return(a)

    class matrix:
    def __init__(self, r, c):
    self.coloums = [vector(c) for i in range(r)]
    self.r = r
    self.c = c
    def __repr__(self):
    return repr(self.coloums)
    def __mul__(self, other):
    if(type(other) != vector):
    raise TypeError("matrices can only be multiplied by vectors")
    if(self.c != other.size):
    raise ArithmeticError("rows and lengths do not match")
    a = vector(self.r)
    a.set([(other*self.coloums[i]) for i in range(self.r)])
    return a
    def set(self, multiarray):
    for i in range(self.c):
    self.coloums[i].set(multiarray[i])


    I am aware this has no way of multiply by a scalar but I have no need for that just yet and it would be pretty trivial to implement.










    share|improve this question







    New contributor




    robert gibson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0







      I am creating a machine learning tool set from scratch in python. I have never done something of this kind and I don't usually use python but I thought it would be good to expand my horizons. I am really looking for aspects of the code that would really hinder performance and things to consider since this will be used for a neural network implementation.



      class vector:
      def __init__(self, size):
      self.elems = [0] * size
      self.size = size
      def __repr__(self):
      return repr(self.elems)
      def __mul__(self, other):
      if(self.size != other.size):
      raise ArithmeticError("vectors of two different lengths")
      a = 0
      for i in range(self.size):
      a += self.elems[i] * other.elems[i]
      return a
      def set(self, array):
      for i in range(self.size):
      self.elems[i] = array[i]
      self.mag = sum([i**2 for i in self.elems])**.5
      def normalize(self):
      a = vector(self.size)
      a.set([i/self.mag for i in self.elems])
      return(a)

      class matrix:
      def __init__(self, r, c):
      self.coloums = [vector(c) for i in range(r)]
      self.r = r
      self.c = c
      def __repr__(self):
      return repr(self.coloums)
      def __mul__(self, other):
      if(type(other) != vector):
      raise TypeError("matrices can only be multiplied by vectors")
      if(self.c != other.size):
      raise ArithmeticError("rows and lengths do not match")
      a = vector(self.r)
      a.set([(other*self.coloums[i]) for i in range(self.r)])
      return a
      def set(self, multiarray):
      for i in range(self.c):
      self.coloums[i].set(multiarray[i])


      I am aware this has no way of multiply by a scalar but I have no need for that just yet and it would be pretty trivial to implement.










      share|improve this question







      New contributor




      robert gibson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I am creating a machine learning tool set from scratch in python. I have never done something of this kind and I don't usually use python but I thought it would be good to expand my horizons. I am really looking for aspects of the code that would really hinder performance and things to consider since this will be used for a neural network implementation.



      class vector:
      def __init__(self, size):
      self.elems = [0] * size
      self.size = size
      def __repr__(self):
      return repr(self.elems)
      def __mul__(self, other):
      if(self.size != other.size):
      raise ArithmeticError("vectors of two different lengths")
      a = 0
      for i in range(self.size):
      a += self.elems[i] * other.elems[i]
      return a
      def set(self, array):
      for i in range(self.size):
      self.elems[i] = array[i]
      self.mag = sum([i**2 for i in self.elems])**.5
      def normalize(self):
      a = vector(self.size)
      a.set([i/self.mag for i in self.elems])
      return(a)

      class matrix:
      def __init__(self, r, c):
      self.coloums = [vector(c) for i in range(r)]
      self.r = r
      self.c = c
      def __repr__(self):
      return repr(self.coloums)
      def __mul__(self, other):
      if(type(other) != vector):
      raise TypeError("matrices can only be multiplied by vectors")
      if(self.c != other.size):
      raise ArithmeticError("rows and lengths do not match")
      a = vector(self.r)
      a.set([(other*self.coloums[i]) for i in range(self.r)])
      return a
      def set(self, multiarray):
      for i in range(self.c):
      self.coloums[i].set(multiarray[i])


      I am aware this has no way of multiply by a scalar but I have no need for that just yet and it would be pretty trivial to implement.







      python performance machine-learning neural-network






      share|improve this question







      New contributor




      robert gibson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      robert gibson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      robert gibson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 17 mins ago









      robert gibson

      1




      1




      New contributor




      robert gibson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      robert gibson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      robert gibson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          robert gibson is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210673%2fcustom-vector-and-matrix-classes-in-python-for-machine-learning%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          robert gibson is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          robert gibson is a new contributor. Be nice, and check out our Code of Conduct.













          robert gibson is a new contributor. Be nice, and check out our Code of Conduct.












          robert gibson is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210673%2fcustom-vector-and-matrix-classes-in-python-for-machine-learning%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python