1. zip()
a = [x for x in range(10)]
b = [x for x in range(10,20)]
for a_, b_ in zip(a,b)
print(a_+b_)
2.eval()
3.csv數據讀取技巧
在python3使用read_csv的時候,如果傳入的參數不是文件名而是文件的路徑,就會報這個錯。
原因應該是這個庫的問題,解決方法是,先切換到這個目錄,然後傳文件名作爲參數。
出錯代碼:
import pandas as pd
import os
trainFile = "F:/Projects/Python/coursera/intro-to-data-science/kaggle/data/train.csv"
pwd = os.getcwd()
os.chdir(os.path.dirname(trainFile))
trainData = pd.read_csv(os.path.basename(trainFile))
os.chdir(pwd)
4.numpy矩陣操作
一維矩陣np.array[]矩陣轉置等於其本身,
一維矩陣np.array[]矩陣可以正常轉置 .T即可
注意np.ones(1,2) = array[[1, 2]],np.ones(2) = array[1, 2]
5.list拼接:
1> L1 + L2
2>L1.extend(L2)
3> L1[len(L1):len(L1)]=L2
1、切片操作:b = a[:] 或者 b = [each for each in a]
2、工廠函數:b = list(a)
3、copy函數:b = copy.copy(a)
7.python調用list的時候需要指示第幾個list,使用enumerate
for i,j in enumerate([x for x in range(5)]):
print(i*j):::0 1 4 9 16
8.三維矩陣展開二維矩陣
a = np.arange(24).reshape(2,3,4)
a
Out[43]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
a.reshape(2, -1) -1的意思是不管多少,展開完爲止
Out[44]:
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]])
9.urllib.request.urlretrieve下載文件
首先定義一個回顯函數
defcallbackfunc(blocknum, blocksize, totalsize):
pass
urllib.request.urlretrieve(url, localname, callbackfunc)
10.sorted中的key
dict = {'the':10, 'abc':10}
sorted(dict.items, key = lambda x : (x[1], x[0]) 首先按照 數字排序,數字一樣的再按照字母排序
11. 獲取當前路徑,和當前路徑的所有文件
print ("os.getcwd()=%s" % os.getcwd())
print ("sys.path[0]=%s" % sys.path[0])
print ("sys.argv[0]=%s" % sys.argv[0])
---》"G:\work" "G:\work" "G:\work\xxxx.py"
第二個第三個在console中不適中。
獲取路徑中的文件已經文件夾
for root, dirs, files in os.walk(file_dir): 每次迭代對應的是一個tuple(root,dirs files)
獲取文件夾 os.listdir()
12.使用setup.py install 安裝的文件卸載問題
To record a list of installed files, you can use:
python setup.py install --record files.txt
Once you want to uninstall you can use xargs to do the removal:
cat files.txt | xargs rm -rf
Or if you're running Windows, use Powershell:
Get-Content files.txt | ForEach-Object {Remove-Item $_ -Recurse -Force}
非常有效!!!13.有關conda
命令行輸入
conda install -c anaconda anaconda-navigator=1.6.2
conda update conda
conda update anaconda
conda update --all
conda update
用來更新……
14. tensorflow變量重用問題
通過共享 變量作用域(variable_scope) 來實現 共享變量 。在重複使用(即 非第一次使用)時,設置 reuse=True 來 再次調用 該共享變量作用域(variable_scope)。主要有兩種方法進行reuse。
注意!:定義變量時使用tf.Variable() 定義的變量均不能共享,必須使用tf.get_variable()纔可進行共享!使用高級api(不用自己定義變量自動生成)的時候默認使用的後者,能夠進行共享!
方法一:
使用 tf.Variable_scope(..., reuse=tf.AUTO_REUSE)
。
def net(...):
with tf.variable_scope('this scope', reuse=tf.AUTO_REUSE): ### 改動部分 ###
w1 = tf.get_variable(name='w1', initilizer=0......)
x = tf.placeholder(...)
x1 = tf.placeholder(...)
D_logits = net(x)
G_logits = net(x1)#這時net裏面的變量都是共享的,
variables = tf.trainable_variables()
print([var.name for var in variables])
#只有一個w1變量
方法二:
使用 if reuse:
。
tf.get_variable_scope().reuse_variables()scope(..., reuse=tf.AUTO_REUSE)
def net(...):
with tf.variable_scope('this scope', reuse=tf.AUTO_REUSE): ### 改動部分 ###
if reuse:
tf.get_variable_scope().reuse_variables()
w1 = tf.get_variable(name='w1', initilizer=0......)
x = tf.placeholder(...)
x1 = tf.placeholder(...)
D_logits = net(x)
G_logits = net(x1)
variables = tf.trainable_variables()
print([var.name for var in variables])
#只有一個w1變量
15.dlib庫編譯安裝問題
使用vs2015,記得安裝vs裏面的vc++ sdk8.1(這倆是在一起的)其他的不用過多勾選,安裝好以後編譯即可。