python庫numpy的使用

2021-07-31 06:18:52 字數 567 閱讀 1971

python在構造機器學習應用程式時,numpy作為乙個重要的函式庫會被經常使用,裡面有便捷的向量和矩陣的計算函式

from numpy import *

## 構造4*4的隨機矩陣

matrix = mat(random.rand(4,4))

## 矩陣逆矩陣

invmat = matrix.i

## 單位矩陣

matrix * matrix.i = eye(4)

## 矩陣行,列數

matrix.shape

## 矩陣拉伸

vector = array([3, 9])

tile(vector,2)        ## 橫向拉伸2倍

tile(vector, (3, 1))   ## 縱向拉伸3倍, 橫向拉伸1倍

## 矩陣每個元素平方

matrix ** 2

## 矩陣行列向量相加 axis=0列向量相加, axis=1行向量相加

matrix.sum(axis=0)

matrix.sum(axis=1)

python的numpy庫結構 Numpy庫簡介

今天給大家分享乙個資料分析處理資料的常見的庫 numpy。這個庫是 python 資料分析的基礎,它提供的資料結構比 python 自身的更高效。我們知道 python 有自帶的列表資料結構。numpy 庫和 list 列表有什麼區別呢?python list 列表儲存的是物件的指標,比如 0,1,...

使用python的numpy庫實現PCA演算法

usr bin env python coding utf 8 from numpy import 引數1 特徵值向量,引數2 比率 返回值 k 符合指定比率的topk k值 def setk eigvals,rate 0.9 eigvalind argsort eigvals 對特徵值進行排序 f...

Python入門筆記(numpy庫的使用)

import numpy 讀取資料都為矩陣格式 numpy讀取檔案 test numpy.genfromtxt text2.txt delimiter dtype str,skip header 1 讀取並跳過第一行 print type test print test print help num...