Python資料分析之numpy的使用
阿新 • • 發佈:2018-12-02
在完成了自己的一個小目標後,想繼續往資料探勘和資料分析的方向前進,接下來會陸陸續續的完成學習筆記,方便日後的回顧。在之前的部落格裡有一篇關於numpy的使用:https://blog.csdn.net/totoro1745/article/details/79243465,這裡是進行相關的補充~
資料分析
資料分析致力於在資料中提取有效資訊,會使用統計學、機器學習、訊號處理、自然語言處理等領域的知識,對資料進行研究、概括、總結。
python資料分析大家族
1.numpy:資料結構基礎
2.scipy:強大的科學計算方法(矩陣分析、訊號分析、數理分析)
3.matplotlib
4.pandas:基礎資料分析套件
5.scikit-learn:強大的資料分析建模庫
6.keras:人工神經網路
numpy學習程式碼
# coding=utf-8
"""
created on:2018/4/22
author:DilicelSten
target:Learn numpy
"""
import numpy as np
# ------------------------ndarray---------------------------------
lst = [[1, 2, 3], [4, 5, 6]]
print type(lst)
# 列表轉ndarray
np_lst = np.array(lst)
print type(np_lst)
# 定義型別
np_lst = np.array(lst, dtype=np.float)
print np_lst.shape
print np_lst.ndim # 維數
print np_lst.dtype # 型別
print np_lst.itemsize # 每個的大小
print np_lst.size # 大小
# ---------------------------some kinds of array-------------------
# 數值的初始化
print np.zeros([2 , 4]) # 全0
print np.ones([3, 5]) # 全1
# 生成隨機數
print np.random.rand(2, 4)
print np.random.rand()
# 隨機整數
print np.random.randint(1, 10, 3)
# 標準正態分佈
print np.random.randn(2, 4)
# 生成指定值
print np.random.choice([1, 2, 3, 4, 6, 7, 8, 9])
# 生成貝塔分佈1-10 100個
print np.random.beta(1, 10, 100) # 使用random生成各種分佈
# -------------------operations---------------------------------
# 生成等差數列
lst = np.arange(1, 11).reshape([2, 5]) # 2行5列,5可以預設成-1
# 自然指數
print np.exp(lst)
# 指數的平方
print np.exp2(lst)
# 開方
print np.sqrt(lst)
# 三角函式
print np.sin(lst)
# 對數
print np.log(lst)
# 求和
lst = np.array([[1, 2, 3], [4, 5, 6]])
print lst.sum(axis=0) # axis指定維度
# 最大最小
print lst.max(axis=1)
print lst.min(axis=0)
# 拆解/拉直——>多維陣列變一維陣列
print lst.ravel() # 返回的只是陣列的檢視
print lst.flatten() # 返回的是真實的陣列
# 兩個的操作
lst1 = np.array([10, 20, 30, 40])
lst2 = np.array([1, 2, 3, 4])
# +-*/
print lst1+lst2
# 平方
print lst1**2
# 點積
print np.dot(lst1.reshape([2, 2]), lst2.reshape([2, 2]))
# 合成
print np.concatenate((lst1, lst2), axis=1) # 0水平疊加 1垂直疊加
# 堆疊
print np.vstack((lst1, lst2)) # 垂直疊加
print np.hstack((lst1, lst2)) # 水平疊加
# 分開
print np.split(lst1, 2) # 分成兩份
# 拷貝
print np.copy(lst1)
# ------------------------linear algebra-----------------------
# 引入模組
from numpy.linalg import *
# 生成單位矩陣
print np.eye(3)
lst = np.array([[1, 2], [3, 4]])
# 求矩陣的逆
print inv(lst)
# 轉置矩陣T
print lst.transpose()
# 行列式
print det(lst)
# 特徵值和特徵向量
print eig(lst)
# 解多元一次方程
a = np.array([[1, 2, 1], [2, -1, 3], [3, 1, 2]])
b = np.array([7, 7, 18])
x = solve(a, b)
print x
# ------------------------------other-------------------------------
# 求相關係數
print np.corrcoef([1, 0, 1], [0, 2, 1])
# 生成函式
print np.poly1d([2, 1, 3])